/
output.test.js
75 lines (66 loc) · 2.44 KB
/
output.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict'
const fs = require('fs')
const os = require('os')
const fse = require('../..')
const path = require('path')
const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
describe('output', () => {
let TEST_DIR
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'output')
fse.emptyDir(TEST_DIR, done)
})
afterEach(done => fse.remove(TEST_DIR, done))
describe('+ outputFile', () => {
describe('> when the file and directory does not exist', () => {
it('should create the file', done => {
const file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt')
assert(!fs.existsSync(file))
fse.outputFile(file, 'hi jp', err => {
assert.ifError(err)
assert(fs.existsSync(file))
assert.strictEqual(fs.readFileSync(file, 'utf8'), 'hi jp')
done()
})
})
it('should support promises', () => {
const file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt')
assert(!fs.existsSync(file))
return fse.outputFile(file, 'hi jp')
})
})
describe('> when the file does exist', () => {
it('should still modify the file', done => {
const file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt')
fse.mkdirsSync(path.dirname(file))
fs.writeFileSync(file, 'hello world')
fse.outputFile(file, 'hello jp', err => {
if (err) return done(err)
assert.strictEqual(fs.readFileSync(file, 'utf8'), 'hello jp')
done()
})
})
})
})
describe('+ outputFileSync', () => {
describe('> when the file and directory does not exist', () => {
it('should create the file', () => {
const file = path.join(TEST_DIR, Math.random() + 'ts-ne', Math.random() + '.txt')
assert(!fs.existsSync(file))
fse.outputFileSync(file, 'hello man')
assert(fs.existsSync(file))
assert.strictEqual(fs.readFileSync(file, 'utf8'), 'hello man')
})
})
describe('> when the file does exist', () => {
it('should still modify the file', () => {
const file = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt')
fse.mkdirsSync(path.dirname(file))
fs.writeFileSync(file, 'hello world')
fse.outputFileSync(file, 'hello man')
assert.strictEqual(fs.readFileSync(file, 'utf8'), 'hello man')
})
})
})
})