-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
126 lines (105 loc) · 3 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
const {dirname, join} = require('path');
const {hostname} = require('os');
const {isMap} = require('util').types;
const {pathToFileURL} = require('url');
const {symlink, unlink} = require('fs').promises;
const lstatDir = require('.');
const test = require('tape');
const nonEssentialFilesRe = /(?:\.DS_Store|\.git|\.nyc_output|coverage)$/u;
const expected = [
'.editorconfig',
'.gitattributes',
'.gitignore',
'.travis.yml',
'index.js',
'LICENSE',
'node_modules',
'package-lock.json',
'package.json',
'README.md',
'test.js'
];
test('lstatDir()', async t => {
const result = await lstatDir(__dirname);
t.ok(isMap(result), 'should be fulfilled with a Map instance.');
t.deepEqual(
[...result.keys()].filter(path => !nonEssentialFilesRe.test(path)),
expected.map(path => join(__dirname, path)),
'should list all contents in a directory.'
);
const dir = dirname(require.resolve('readdir-sorted'));
const tmp = join(__dirname, 'test-tmp-dir');
await symlink(dir, tmp, 'dir');
t.ok(
(await lstatDir(pathToFileURL(tmp))).values().next().value.isFile(),
'should get fs.Stats of each file.'
);
await unlink(tmp);
try {
await lstatDir('/__This_directory_doesn/t_exist__');
t.fail('Unexpectedly succeeded.');
} catch (err) {
t.equal(err.code, 'ENOENT', 'should fail when it cannot find the directory.');
}
try {
await lstatDir(__filename);
t.fail('Unexpectedly succeeded.');
} catch (err) {
t.equal(err.code, 'ENOTDIR', 'should fail when the target path is not a directory.');
}
t.end();
});
(process.platform === 'win32' ? test : test.skip)('lstatDir() on Windows', async t => {
const uncDir = `\\\\${hostname()}\\${__dirname.replace(/(?<=^[a-z]):/ui, '$')}\\`;
const url = new URL(`file:${uncDir.replace(/\\/ug, '/')}`);
t.deepEqual(
[...(await lstatDir(url)).keys()].filter(path => !nonEssentialFilesRe.test(path)),
expected.map(path => `${uncDir}${path}`),
'should support file URL of a UNC path.'
);
t.end();
});
test('Argument validation', async t => {
try {
await lstatDir([1, 2]);
t.fail('Unexpectedly succeeded.');
} catch ({code}) {
t.equal(
code,
'ERR_INVALID_ARG_TYPE',
'should fail when it takes a non-path argument.'
);
}
try {
await lstatDir('');
t.fail('Unexpectedly succeeded.');
} catch (err) {
t.equal(
err.toString(),
'Error: Expected a valid directory path, but got \'\' (empty string).',
'should fail when it takes an empty string.'
);
}
try {
await lstatDir();
t.fail('Unexpectedly succeeded.');
} catch (err) {
t.equal(
err.toString(),
'TypeError: Expected 1 or 2 arguments (path: <string|Buffer|URL>[, options: <Object>]), but got no arguments.',
'should fail when it takes no arguments.'
);
}
try {
await lstatDir('!', {}, '?');
t.fail('Unexpectedly succeeded.');
} catch (err) {
t.equal(
err.toString(),
'TypeError: Expected 1 or 2 arguments (path: <string|Buffer|URL>[, options: <Object>]), but got 3 arguments.',
'should fail when it takes too many arguments.'
);
}
t.end();
});