-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
102 lines (89 loc) · 2.98 KB
/
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
95
96
97
98
99
100
101
102
const tap = require('tap')
const { asyncFolderWalker, allFiles } = require('.')
const path = require('path')
// @ts-ignore
const tmp = require('p-temporary-directory')
const fixtures = path.join(__dirname, 'fixtures')
tap.test('for of multiple folders', async t => {
for await (const file of asyncFolderWalker([
path.join(fixtures, 'sub-folder'),
path.join(fixtures, 'another-folder')
])) {
t.ok(file, file)
}
})
tap.test('Array from async iterator', async t => {
const files = await allFiles([
path.join(fixtures, 'sub-folder'),
path.join(fixtures, 'another-folder')
])
t.equal(files.length, 4, 'expected number of files are found')
})
tap.test('No args', async t => {
// @ts-ignore
for await (const file of asyncFolderWalker()) {
t.fail('no files should be found!', file)
}
t.pass('for of executed')
})
tap.test('No folders', async t => {
const [dir, cleanup] = await tmp()
try {
for await (const file of asyncFolderWalker(dir)) {
t.fail('no files should be found!', file)
}
t.pass('for of executed')
} finally {
await cleanup()
}
})
tap.test('When you just pass a file', async t => {
const [dir, cleanup] = await tmp()
try {
const theFile = path.join(fixtures, 'test.json')
const files = await allFiles([theFile, dir])
t.equal(files.length, 1, 'only one file is found')
t.equal(theFile, files[0], 'only one file is found')
} finally {
await cleanup()
}
})
tap.test('pathFilter works', async t => {
const filterStrig = 'sub-folder'
const files = await allFiles(fixtures, {
pathFilter: p => !p.includes(filterStrig)
})
t.notOk(files.some(f => f.includes(filterStrig)), 'No paths include the excluded string')
})
tap.test('statFilter works', async t => {
const stats = await allFiles(fixtures, {
statFilter: st => !st.isDirectory(), // Exclude files
shaper: ({ stat /*, root, filepath, relname, basename */ }) => stat // Lets get the stats instead of paths
})
for (const st of stats) {
t.notOk(st.isDirectory(), 'none of the files are directories')
}
})
tap.test('dont include root directory in response', async (t) => {
const root = fixtures
for await (const file of asyncFolderWalker(root)) {
if (file === root) t.fail('root directory should not be in results')
}
t.pass('The root was not included in results.')
})
tap.test('dont walk past the maxDepth', async t => {
const maxDepth = 3
const walker = asyncFolderWalker(['.git', 'node_modules'], { maxDepth })
for await (const file of walker) {
const correctLength = file.split(path.sep).length - process.cwd().split(path.sep).length <= maxDepth
if (!correctLength) t.fail('walker walked past the depth it was supposed to')
}
t.pass('Walker was depth limited')
})
tap.test('ignore-folder should be ignored', async t => {
for await (const file of asyncFolderWalker([fixtures], {
ignore: ['ignore-folder', '.*']
})) {
t.notOk(file.includes('ignore-folder', 'does not include ignored files and folders'))
}
})