/
create.test.js
94 lines (81 loc) · 2.99 KB
/
create.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'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('fs-extra', () => {
let TEST_DIR
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'create')
fse.emptyDir(TEST_DIR, done)
})
afterEach(done => fse.remove(TEST_DIR, done))
describe('+ createFile', () => {
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.createFile(file, err => {
assert.ifError(err)
assert(fs.existsSync(file))
done()
})
})
})
describe('> when the file does exist', () => {
it('should not 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.createFile(file, err => {
assert.ifError(err)
assert.strictEqual(fs.readFileSync(file, 'utf8'), 'hello world')
done()
})
})
it('should give clear error if node in directory tree is a file', done => {
const existingFile = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt')
fse.mkdirsSync(path.dirname(existingFile))
fs.writeFileSync(existingFile, '')
const file = path.join(existingFile, Math.random() + '.txt')
fse.createFile(file, err => {
assert.strictEqual(err.code, 'ENOTDIR')
done()
})
})
})
})
describe('+ createFileSync', () => {
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.createFileSync(file)
assert(fs.existsSync(file))
})
})
describe('> when the file does exist', () => {
it('should not 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.createFileSync(file)
assert.strictEqual(fs.readFileSync(file, 'utf8'), 'hello world')
})
it('should give clear error if node in directory tree is a file', () => {
const existingFile = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt')
fse.mkdirsSync(path.dirname(existingFile))
fs.writeFileSync(existingFile, '')
const file = path.join(existingFile, Math.random() + '.txt')
try {
fse.createFileSync(file)
assert.fail()
} catch (err) {
assert.strictEqual(err.code, 'ENOTDIR')
}
})
})
})
})