-
Notifications
You must be signed in to change notification settings - Fork 774
/
create.test.js
68 lines (59 loc) · 2.03 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
'use strict'
const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
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.equal(fs.readFileSync(file, 'utf8'), 'hello world')
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.equal(fs.readFileSync(file, 'utf8'), 'hello world')
})
})
})
})