Debian Codegraph @

lib/ensure/__tests__/create.test.js

raw symbols SCIP
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 /* global afterEach, beforeEach, describe, it */
10
11 describe('fs-extra', () => {
12 let TEST_DIR
13
14 beforeEach(done => {
15 TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'create')
16 fse.emptyDir(TEST_DIR, done)
17 })
18
19 afterEach(done => fse.remove(TEST_DIR, done))
20
21 describe('+ createFile', () => {
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.createFile(file, err => {
27 assert.ifError(err)
28 assert(fs.existsSync(file))
29 done()
30 })
31 })
32 })
33
34 describe('> when the file does exist', () => {
35 it('should not modify the file', done => {
36 const file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt')
37 fse.mkdirsSync(path.dirname(file))
38 fs.writeFileSync(file, 'hello world')
39 fse.createFile(file, err => {
40 assert.ifError(err)
41 assert.strictEqual(fs.readFileSync(file, 'utf8'), 'hello world')
42 done()
43 })
44 })
45
46 it('should give clear error if node in directory tree is a file', done => {
47 const existingFile = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt')
48 fse.mkdirsSync(path.dirname(existingFile))
49 fs.writeFileSync(existingFile, '')
50
51 const file = path.join(existingFile, Math.random() + '.txt')
52 fse.createFile(file, err => {
53 assert.strictEqual(err.code, 'ENOTDIR')
54 done()
55 })
56 })
57 })
58 })
59
60 describe('+ createFileSync', () => {
61 describe('> when the file and directory does not exist', () => {
62 it('should create the file', () => {
63 const file = path.join(TEST_DIR, Math.random() + 'ts-ne', Math.random() + '.txt')
64 assert(!fs.existsSync(file))
65 fse.createFileSync(file)
66 assert(fs.existsSync(file))
67 })
68 })
69
70 describe('> when the file does exist', () => {
71 it('should not modify the file', () => {
72 const file = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt')
73 fse.mkdirsSync(path.dirname(file))
74 fs.writeFileSync(file, 'hello world')
75 fse.createFileSync(file)
76 assert.strictEqual(fs.readFileSync(file, 'utf8'), 'hello world')
77 })
78
79 it('should give clear error if node in directory tree is a file', () => {
80 const existingFile = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt')
81 fse.mkdirsSync(path.dirname(existingFile))
82 fs.writeFileSync(existingFile, '')
83
84 const file = path.join(existingFile, Math.random() + '.txt')
85 try {
86 fse.createFileSync(file)
87 assert.fail()
88 } catch (err) {
89 assert.strictEqual(err.code, 'ENOTDIR')
90 }
91 })
92 })
93 })
94 })