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