This repository was archived by the owner on Jun 29, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilesystem.spec.ts
More file actions
112 lines (82 loc) · 3.05 KB
/
filesystem.spec.ts
File metadata and controls
112 lines (82 loc) · 3.05 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* @poppinss/dev-utils
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import test from 'japa'
import { join } from 'path'
import { pathExists, remove } from 'fs-extra'
import { Filesystem } from '../src/Filesystem'
test.group('Filesystem', () => {
test('add new file to the disk', async (assert) => {
const fs = new Filesystem(join(__dirname, 'app'))
await fs.add('hello.txt', 'Hello world')
const exists = await pathExists(join(fs.basePath, 'hello.txt'))
assert.isTrue(exists)
await remove(fs.basePath)
})
test('adding js file should populate modules set', async (assert) => {
const fs = new Filesystem(join(__dirname, 'app'))
await fs.add('hello.js', "module.exports = 'Hello world'")
const exists = await pathExists(join(fs.basePath, 'hello.js'))
assert.isTrue(exists)
assert.deepEqual(Array.from(fs['modules'].values()), [join(fs.basePath, 'hello.js')])
await remove(fs.basePath)
})
test('removing js file should clear it from node require cache', async (assert) => {
const fs = new Filesystem(join(__dirname, 'app'))
await fs.add('config/foo.js', "module.exports = 'Hello world'")
assert.equal(require('./app/config/foo.js'), 'Hello world')
/**
* Should clean it from cache too
*/
await fs.remove('config/foo.js')
await fs.add('config/foo.js', "module.exports = 'Hi world'")
assert.equal(require('./app/config/foo.js'), 'Hi world')
await fs.cleanup()
})
test('clean module from require cache, when required without extension', async (assert) => {
const fs = new Filesystem(join(__dirname, 'app'))
await fs.add('foo.js', "module.exports = 'Hello world'")
assert.equal(require('./app/foo'), 'Hello world')
/**
* Should clean it from cache too
*/
await fs.remove('foo.js')
await fs.add('foo.js', "module.exports = 'Hi world'")
assert.equal(require('./app/foo'), 'Hi world')
await fs.cleanup()
})
test('cleanup all files from system basePath', async (assert) => {
assert.plan(3)
const fs = new Filesystem(join(__dirname, 'app'))
await fs.add('hello.js', "module.exports = 'Hello world'")
await fs.cleanup()
const exists = await pathExists(fs.basePath)
assert.isFalse(exists)
assert.isUndefined(process.env.PORT, '3333')
try {
require(join(fs.basePath, 'hello.js'))
} catch (error) {
assert.equal(error.code, 'MODULE_NOT_FOUND')
}
await fs.cleanup()
})
test('create base path directory if missing', async (assert) => {
const fs = new Filesystem(join(__dirname, 'app'))
await fs.ensureRoot()
const exists = await pathExists(fs.basePath)
assert.isTrue(exists)
await fs.cleanup()
})
test('find if file exists', async (assert) => {
const fs = new Filesystem(join(__dirname, 'app'))
await fs.add('hello.txt', 'Hello world')
const exists = await fs.exists('hello.txt')
assert.isTrue(exists)
await fs.cleanup()
})
})