cno-bedrock 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/package.json +116 -116
- package/scripts/generate-docs.js +0 -0
- package/src/cno-assert.js +222 -15
- package/src/cno-assert.test.js +331 -0
- package/src/cno-base64.js +230 -0
- package/src/cno-base64.test.js +262 -0
- package/src/cno-bytes.js +387 -0
- package/src/cno-bytes.test.js +222 -0
- package/src/cno-constants.js +121 -0
- package/src/cno-files.js +2135 -0
- package/src/cno-files.test.js +939 -0
- package/src/cno-math.js +407 -0
- package/src/cno-math.test.js +249 -0
- package/src/cno-noop.js +6 -2
- package/src/cno-options.js +73 -4
- package/src/cno-options.test.js +65 -0
- package/src/cno-sha2-256.js +831 -0
- package/src/cno-sha2-256.test.js +697 -0
- package/src/cno-test-matrix.js +709 -0
- package/src/cno-test-matrix.test.js +199 -0
- package/src/cno-timed-cache-map.js +103 -0
- package/src/cno-timed-cache-map.test.js +113 -0
- package/src/cno-tree.js +365 -0
- package/src/cno-tree.test.js +147 -0
- package/src/cno-utility.js +545 -2
- package/src/cno-utility.test.js +276 -0
- package/src/cno-vector.js +1053 -0
- package/src/cno-vector.test.js +523 -0
- package/src/lib.js +5 -138
- package/src/lib.test.js +19 -7
- package/src/stream-test.js +116 -0
- package/test/big-lorem.txt +29 -0
|
@@ -0,0 +1,939 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
# [cno-files.test.js](src/cno-files.test.js)
|
|
4
|
+
> Tests for [`cno-files`](src/cno-files.js).
|
|
5
|
+
|
|
6
|
+
Author: Anadian
|
|
7
|
+
|
|
8
|
+
Code license: MIT
|
|
9
|
+
```
|
|
10
|
+
Copyright 2026 Anadian
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
12
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
13
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
14
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
16
|
+
conditions:
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
18
|
+
or substantial portions of the Software.
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
20
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
21
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
24
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
```
|
|
26
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
27
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
28
|
+
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
//# Dependencies
|
|
32
|
+
//## Internal
|
|
33
|
+
import defaultExport from './cno-files.js';
|
|
34
|
+
import * as namespaceExport from './cno-files.js';
|
|
35
|
+
import TestNS from './cno-test-matrix.js';
|
|
36
|
+
import AssertNS from './cno-assert.js';
|
|
37
|
+
import { getSHA256FromString, getBase16FromBuffer, getBase64FromBuffer } from './cno-bytes.js';
|
|
38
|
+
//## Standard
|
|
39
|
+
import FSNS from 'node:fs/promises';
|
|
40
|
+
import PathNS from 'node:path';
|
|
41
|
+
import OSNS from 'node:os';
|
|
42
|
+
//## External
|
|
43
|
+
|
|
44
|
+
//# Constants
|
|
45
|
+
const FILENAME = 'cno-files.test.js';
|
|
46
|
+
/**## Fixture
|
|
47
|
+
> A freshly-created temporary directory tree with known contents, used to exercise the tree-processing functionality deterministically. The sizes are the lengths of the strings written to each file, all of which are well under any filesystem's minimum allocation unit, so the *apparent* sizes reported by `stat` match them exactly.
|
|
48
|
+
```
|
|
49
|
+
<root>/
|
|
50
|
+
├── a.txt 4 bytes
|
|
51
|
+
├── b.txt 8 bytes
|
|
52
|
+
├── link.txt symlink to a.txt
|
|
53
|
+
├── empty/ empty directory
|
|
54
|
+
└── sub/
|
|
55
|
+
├── c.bin 12 bytes
|
|
56
|
+
└── nested/
|
|
57
|
+
└── d.txt 1 byte
|
|
58
|
+
```
|
|
59
|
+
*/
|
|
60
|
+
const FIXTURE = {
|
|
61
|
+
/** The absolute path of the fixture root; set by `createFixture`. */
|
|
62
|
+
path: null,
|
|
63
|
+
/** The `Dirent`-independent expected apparent sizes, in bytes, of each leaf file. */
|
|
64
|
+
sizes: {
|
|
65
|
+
'a.txt': 4,
|
|
66
|
+
'b.txt': 8,
|
|
67
|
+
'c.bin': 12,
|
|
68
|
+
'd.txt': 1
|
|
69
|
+
},
|
|
70
|
+
/** The upper bound, in bytes, used for directory inode sizes across platforms. */
|
|
71
|
+
directorySizeUpperBound: 4096
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
### createFixture
|
|
75
|
+
> Creates the temporary fixture tree described by [FIXTURE](#fixture) and returns its absolute path.
|
|
76
|
+
|
|
77
|
+
#### Returns
|
|
78
|
+
| type | description |
|
|
79
|
+
| --- | --- |
|
|
80
|
+
| Promise<string> | A promise which resolves to the absolute path of the created fixture root. |
|
|
81
|
+
|
|
82
|
+
#### History
|
|
83
|
+
| version | change |
|
|
84
|
+
| --- | --- |
|
|
85
|
+
| 0.1.1 | Introduced. |
|
|
86
|
+
*/
|
|
87
|
+
async function createFixture(){
|
|
88
|
+
var root_path = await FSNS.mkdtemp( PathNS.join( OSNS.tmpdir(), 'cno-files-test-' ) );
|
|
89
|
+
await FSNS.mkdir( PathNS.join( root_path, 'sub' ) );
|
|
90
|
+
await FSNS.mkdir( PathNS.join( root_path, 'empty' ) );
|
|
91
|
+
await FSNS.mkdir( PathNS.join( root_path, 'sub', 'nested' ) );
|
|
92
|
+
await FSNS.writeFile( PathNS.join( root_path, 'a.txt' ), 'AAAA' );
|
|
93
|
+
await FSNS.writeFile( PathNS.join( root_path, 'b.txt' ), 'BBBBBBBB' );
|
|
94
|
+
await FSNS.writeFile( PathNS.join( root_path, 'sub', 'c.bin' ), 'CCCCCCCCCCCC' );
|
|
95
|
+
await FSNS.writeFile( PathNS.join( root_path, 'sub', 'nested', 'd.txt' ), 'D' );
|
|
96
|
+
await FSNS.symlink( PathNS.join( root_path, 'a.txt' ), PathNS.join( root_path, 'link.txt' ) );
|
|
97
|
+
FIXTURE.path = root_path;
|
|
98
|
+
return root_path;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
### removeFixture
|
|
102
|
+
> Recursively removes the temporary fixture tree created by [createFixture](#createfixture), if it exists.
|
|
103
|
+
|
|
104
|
+
#### Returns
|
|
105
|
+
| type | description |
|
|
106
|
+
| --- | --- |
|
|
107
|
+
| Promise<void> | A promise which resolves once the fixture has been removed. |
|
|
108
|
+
|
|
109
|
+
#### History
|
|
110
|
+
| version | change |
|
|
111
|
+
| --- | --- |
|
|
112
|
+
| 0.1.1 | Introduced. |
|
|
113
|
+
*/
|
|
114
|
+
async function removeFixture(){
|
|
115
|
+
if( FIXTURE.path != null ){
|
|
116
|
+
await FSNS.rm( FIXTURE.path, { recursive: true, force: true } );
|
|
117
|
+
FIXTURE.path = null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
### findChildByName
|
|
122
|
+
> Returns the first child of the given `TreeItem` whose `name` matches the given name, or `undefined` if there is none.
|
|
123
|
+
|
|
124
|
+
#### Parametres
|
|
125
|
+
| name | type | description |
|
|
126
|
+
| --- | --- | --- |
|
|
127
|
+
| item | object | The `TreeItem` to search. |
|
|
128
|
+
| name | string | The name of the child to find. |
|
|
129
|
+
|
|
130
|
+
#### Returns
|
|
131
|
+
| type | description |
|
|
132
|
+
| --- | --- |
|
|
133
|
+
| object\|undefined | The matching child, or `undefined`. |
|
|
134
|
+
|
|
135
|
+
#### History
|
|
136
|
+
| version | change |
|
|
137
|
+
| --- | --- |
|
|
138
|
+
| 0.1.1 | Introduced. |
|
|
139
|
+
*/
|
|
140
|
+
function findChildByName( item, name ){
|
|
141
|
+
return item.children.find( ( candidate ) => candidate.name === name );
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
### containsOnlyNames
|
|
145
|
+
> Returns `true` if the given `TreeItem`'s children consist of exactly the given names, in any order.
|
|
146
|
+
|
|
147
|
+
#### Parametres
|
|
148
|
+
| name | type | description |
|
|
149
|
+
| --- | --- | --- |
|
|
150
|
+
| item | object | The `TreeItem` whose children to check. |
|
|
151
|
+
| names | Array | The expected child names. |
|
|
152
|
+
|
|
153
|
+
#### Returns
|
|
154
|
+
| type | description |
|
|
155
|
+
| --- | --- |
|
|
156
|
+
| boolean | `true` if the sets of names are equal. |
|
|
157
|
+
|
|
158
|
+
#### History
|
|
159
|
+
| version | change |
|
|
160
|
+
| --- | --- |
|
|
161
|
+
| 0.1.1 | Introduced. |
|
|
162
|
+
*/
|
|
163
|
+
function containsOnlyNames( item, names ){
|
|
164
|
+
var actual_names = item.children.map( ( child ) => child.name ).sort();
|
|
165
|
+
return AssertNS.deepEqual( actual_names, [ ...names ].sort() ) ?? true;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
### everyNodeSatisfies
|
|
169
|
+
> Returns `true` if `predicate` returns `true` for the given `TreeItem` and, recursively, every one of its descendants.
|
|
170
|
+
|
|
171
|
+
#### Parametres
|
|
172
|
+
| name | type | description |
|
|
173
|
+
| --- | --- | --- |
|
|
174
|
+
| item | object | The `TreeItem` to start from. |
|
|
175
|
+
| predicate | function | A function receiving a node, returning `true` if it satisfies the invariant. |
|
|
176
|
+
|
|
177
|
+
#### Returns
|
|
178
|
+
| type | description |
|
|
179
|
+
| --- | --- |
|
|
180
|
+
| boolean | `true` if every node in the subtree satisfies `predicate`. |
|
|
181
|
+
|
|
182
|
+
#### History
|
|
183
|
+
| version | change |
|
|
184
|
+
| --- | --- |
|
|
185
|
+
| 0.1.1 | Introduced. |
|
|
186
|
+
*/
|
|
187
|
+
function everyNodeSatisfies( item, predicate ){
|
|
188
|
+
if( predicate( item ) !== true ){
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
return item.children.every( ( child ) => everyNodeSatisfies( child, predicate ) );
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
//# Global Variables
|
|
195
|
+
/**## Functions*/
|
|
196
|
+
TestNS.test( 'getHJSONFromPath', async function( t ){
|
|
197
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
198
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getHJSONFromPath } );
|
|
199
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getHJSONFromPath } );
|
|
200
|
+
tm.addCondition( { name: 'invalid-parametre', success: false, args: [ false ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
201
|
+
tm.addCondition( { name: 'read-error', success: false, args: [ 'fake-file.tmp' ], expected: { constructor: Error } } );
|
|
202
|
+
tm.addCondition( { name: 'parse-error', success: false, args: [ 'CHANGELOG.md' ], expected: { constructor: Error } } );
|
|
203
|
+
tm.addCondition( {
|
|
204
|
+
name: 'package.json',
|
|
205
|
+
success: true,
|
|
206
|
+
promise: true,
|
|
207
|
+
args: [ 'package.json' ],
|
|
208
|
+
expected: async ( value ) => {
|
|
209
|
+
var resolved = await value;
|
|
210
|
+
return ( resolved != null && typeof( resolved ) === 'object' && typeof( resolved.name ) === 'string' );
|
|
211
|
+
}
|
|
212
|
+
} );
|
|
213
|
+
await tm.run();
|
|
214
|
+
} );
|
|
215
|
+
TestNS.test( 'getHJSONFromObject', async function( t ){
|
|
216
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
217
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getHJSONFromObject } );
|
|
218
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getHJSONFromObject } );
|
|
219
|
+
tm.addCondition( {
|
|
220
|
+
name: 'success',
|
|
221
|
+
success: true,
|
|
222
|
+
args: [ { a: true } ],
|
|
223
|
+
expected: '{\n\t"a": true\n}'
|
|
224
|
+
} );
|
|
225
|
+
tm.addCondition( {
|
|
226
|
+
name: 'success-custom-options',
|
|
227
|
+
success: true,
|
|
228
|
+
args: [ { a: 1 }, { bracesSameLine: true, quotes: 'all', separator: true, space: '\t' } ],
|
|
229
|
+
expected: '{\n\t"a": 1\n}'
|
|
230
|
+
} );
|
|
231
|
+
await tm.run();
|
|
232
|
+
} );
|
|
233
|
+
TestNS.test( 'writeHJSONToPath', async function( t ){
|
|
234
|
+
var temp_path = PathNS.join( OSNS.tmpdir(), `cno-files-test-write-${process.pid}.hjson` );
|
|
235
|
+
var write_hjson_to_path = async function( input_object, path ){
|
|
236
|
+
// `writeHJSONToPath` is not exported from the namespace, so exercise it through the default export.
|
|
237
|
+
return defaultExport.writeHJSONToPath( input_object, path );
|
|
238
|
+
};
|
|
239
|
+
await write_hjson_to_path( { hello: 'world' }, temp_path );
|
|
240
|
+
var read_back = await defaultExport.getHJSONFromPath( temp_path );
|
|
241
|
+
AssertNS.deepStrictEqual( read_back, { hello: 'world' } );
|
|
242
|
+
await FSNS.rm( temp_path, { force: true } );
|
|
243
|
+
// A read of the now-missing file rejects with a wrapped error.
|
|
244
|
+
await AssertNS.rejects( defaultExport.getHJSONFromPath( temp_path ), ( error ) => error.constructor === Error );
|
|
245
|
+
// Writing to an unwritable path rejects with a wrapped error.
|
|
246
|
+
await AssertNS.rejects( defaultExport.writeHJSONToPath( { a: 1 }, PathNS.join( OSNS.tmpdir(), 'no-such-directory-xyz', 'file.hjson' ) ), ( error ) => error.constructor === Error );
|
|
247
|
+
} );
|
|
248
|
+
TestNS.test( 'getDirentArrayFromDirectoryPath', async function( t ){
|
|
249
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
250
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getDirentArrayFromDirectoryPath } );
|
|
251
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getDirentArrayFromDirectoryPath } );
|
|
252
|
+
tm.addCondition( { name: 'invalid-parametre', success: false, args: [ false ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
253
|
+
tm.addCondition( { name: 'read-error', success: false, args: [ 'fake-directory' ], expected: { constructor: Error } } );
|
|
254
|
+
tm.addCondition( {
|
|
255
|
+
name: 'success',
|
|
256
|
+
success: true,
|
|
257
|
+
promise: true,
|
|
258
|
+
args: [ 'src' ],
|
|
259
|
+
expected: async ( value ) => {
|
|
260
|
+
var resolved = await value;
|
|
261
|
+
return ( Array.isArray( resolved ) === true && resolved.length > 0 && typeof( resolved[0].isFile ) === 'function' );
|
|
262
|
+
}
|
|
263
|
+
} );
|
|
264
|
+
tm.addCondition( {
|
|
265
|
+
name: 'withFileTypes-false',
|
|
266
|
+
success: false,
|
|
267
|
+
args: [ 'src', { withFileTypes: false } ],
|
|
268
|
+
expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_VALUE' }
|
|
269
|
+
} );
|
|
270
|
+
await tm.run();
|
|
271
|
+
} );
|
|
272
|
+
TestNS.test( 'getUnixTypeFromStatObject', async function( t ){
|
|
273
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
274
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getUnixTypeFromStatObject } );
|
|
275
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getUnixTypeFromStatObject } );
|
|
276
|
+
tm.addCondition( { name: 'null', success: true, args: [ null ], expected: 'unknown' } );
|
|
277
|
+
tm.addCondition( { name: 'undefined', success: true, args: [ undefined ], expected: 'unknown' } );
|
|
278
|
+
tm.addCondition( { name: 'no-mode', success: true, args: [ {} ], expected: 'unknown' } );
|
|
279
|
+
// Each `S_IF*` type bitflag from `fs.constants` should be classified correctly.
|
|
280
|
+
for( const [ flag_name, unix_type ] of [
|
|
281
|
+
[ 'S_IFIFO', 'fifo' ],
|
|
282
|
+
[ 'S_IFCHR', 'character-device' ],
|
|
283
|
+
[ 'S_IFDIR', 'directory' ],
|
|
284
|
+
[ 'S_IFBLK', 'block-device' ],
|
|
285
|
+
[ 'S_IFREG', 'file' ],
|
|
286
|
+
[ 'S_IFLNK', 'symbolic-link' ],
|
|
287
|
+
[ 'S_IFSOCK', 'socket' ]
|
|
288
|
+
] ){
|
|
289
|
+
tm.addCondition( {
|
|
290
|
+
name: flag_name,
|
|
291
|
+
success: true,
|
|
292
|
+
args: [ { mode: FSNS.constants[flag_name] } ],
|
|
293
|
+
expected: unix_type
|
|
294
|
+
} );
|
|
295
|
+
// The same, but with a `BigInt` mode as produced by `stat`ing with `bigint: true`.
|
|
296
|
+
tm.addCondition( {
|
|
297
|
+
name: `${flag_name}-bigint`,
|
|
298
|
+
success: true,
|
|
299
|
+
args: [ { mode: BigInt( FSNS.constants[flag_name] ) } ],
|
|
300
|
+
expected: unix_type
|
|
301
|
+
} );
|
|
302
|
+
}
|
|
303
|
+
await tm.run();
|
|
304
|
+
} );
|
|
305
|
+
TestNS.test( 'getUnixTypeFromDirentObject', async function( t ){
|
|
306
|
+
var make_dirent = function( name ){
|
|
307
|
+
return { name, isFile: () => name === 'file', isDirectory: () => name === 'directory', isSymbolicLink: () => name === 'symbolic-link', isFIFO: () => name === 'fifo', isCharacterDevice: () => name === 'character-device', isBlockDevice: () => name === 'block-device', isSocket: () => name === 'socket' };
|
|
308
|
+
};
|
|
309
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
310
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getUnixTypeFromDirentObject } );
|
|
311
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getUnixTypeFromDirentObject } );
|
|
312
|
+
tm.addCondition( { name: 'null', success: true, args: [ null ], expected: 'unknown' } );
|
|
313
|
+
tm.addCondition( { name: 'plain-object', success: true, args: [ {} ], expected: 'unknown' } );
|
|
314
|
+
tm.addCondition( {
|
|
315
|
+
name: 'unrecognised-type',
|
|
316
|
+
success: true,
|
|
317
|
+
args: [ { isFile: () => false, isDirectory: () => false, isSymbolicLink: () => false, isFIFO: () => false, isCharacterDevice: () => false, isBlockDevice: () => false, isSocket: () => false } ],
|
|
318
|
+
expected: 'unknown'
|
|
319
|
+
} );
|
|
320
|
+
for( const name of [ 'file', 'directory', 'symbolic-link', 'fifo', 'character-device', 'block-device', 'socket' ] ){
|
|
321
|
+
tm.addCondition( { name, success: true, args: [ make_dirent( name ) ], expected: name } );
|
|
322
|
+
}
|
|
323
|
+
await tm.run();
|
|
324
|
+
} );
|
|
325
|
+
TestNS.test( 'getPathFromDirentObject', async function( t ){
|
|
326
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
327
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getPathFromDirentObject } );
|
|
328
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getPathFromDirentObject } );
|
|
329
|
+
tm.addCondition( { name: 'null', success: false, args: [ null ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
330
|
+
tm.addCondition( { name: 'invalid-parametre', success: false, args: [ 'not-an-object' ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
331
|
+
tm.addCondition( { name: 'parentPath', success: true, args: [ { parentPath: '/tmp/x', name: 'y' } ], expected: PathNS.join( '/tmp/x', 'y' ) } );
|
|
332
|
+
tm.addCondition( { name: 'legacy-path', success: true, args: [ { path: '/tmp/x', name: 'y' } ], expected: PathNS.join( '/tmp/x', 'y' ) } );
|
|
333
|
+
await tm.run();
|
|
334
|
+
} );
|
|
335
|
+
TestNS.test( 'isExcludedFromPath', async function( t ){
|
|
336
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
337
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.isExcludedFromPath } );
|
|
338
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.isExcludedFromPath } );
|
|
339
|
+
tm.addCondition( { name: 'no-regexps', success: true, args: [ 'a.js', 'src/a.js', [] ], expected: false } );
|
|
340
|
+
tm.addCondition( { name: 'name-match', success: true, args: [ 'node_modules', 'src/node_modules', [ /node_modules/ ] ], expected: true } );
|
|
341
|
+
tm.addCondition( { name: 'path-match', success: true, args: [ 'index.js', 'src/generated/index.js', [ /generated/ ] ], expected: true } );
|
|
342
|
+
tm.addCondition( { name: 'no-match', success: true, args: [ 'index.js', 'src/index.js', [ /\.tmp$/ ] ], expected: false } );
|
|
343
|
+
await tm.run();
|
|
344
|
+
} );
|
|
345
|
+
TestNS.test( 'addSizes', async function( t ){
|
|
346
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
347
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.addSizes } );
|
|
348
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.addSizes } );
|
|
349
|
+
tm.addCondition( { name: 'numbers', success: true, args: [ 1, 2 ], expected: 3 } );
|
|
350
|
+
tm.addCondition( { name: 'bigints', success: true, args: [ 1n, 2n ], expected: 3n } );
|
|
351
|
+
tm.addCondition( { name: 'mixed', success: true, args: [ 1, 2n ], expected: 3n } );
|
|
352
|
+
tm.addCondition( { name: 'mixed-reversed', success: true, args: [ 1n, 2 ], expected: 3n } );
|
|
353
|
+
tm.addCondition( { name: 'null-as-zero', success: true, args: [ null, 5 ], expected: 5 } );
|
|
354
|
+
tm.addCondition( { name: 'undefined-as-zero', success: true, args: [ undefined, undefined ], expected: 0 } );
|
|
355
|
+
tm.addCondition( { name: 'null-with-bigint', success: true, args: [ undefined, 5n ], expected: 5n } );
|
|
356
|
+
await tm.run();
|
|
357
|
+
} );
|
|
358
|
+
TestNS.test( 'getNameFromPath', async function( t ){
|
|
359
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
360
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getNameFromPath } );
|
|
361
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getNameFromPath } );
|
|
362
|
+
tm.addCondition( { name: 'null', success: true, args: [], expected: PathNS.basename( PathNS.normalize( '.' ) ) } );
|
|
363
|
+
tm.addCondition( { name: 'simple', success: true, args: [ '/tmp/foo/bar.txt' ], expected: 'bar.txt' } );
|
|
364
|
+
tm.addCondition( { name: 'unnormalised', success: true, args: [ '/tmp/foo/../bar.txt' ], expected: 'bar.txt' } );
|
|
365
|
+
tm.addCondition( { name: 'trailing-separator', success: true, args: [ '/tmp/foo/' ], expected: 'foo' } );
|
|
366
|
+
await tm.run();
|
|
367
|
+
} );
|
|
368
|
+
TestNS.test( 'getDirectoryFromPath', async function( t ){
|
|
369
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
370
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getDirectoryFromPath } );
|
|
371
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getDirectoryFromPath } );
|
|
372
|
+
tm.addCondition( { name: 'null', success: true, args: [], expected: PathNS.dirname( PathNS.normalize( '..' ) ) } );
|
|
373
|
+
tm.addCondition( { name: 'simple', success: true, args: [ '/tmp/foo/bar.txt' ], expected: '/tmp/foo' } );
|
|
374
|
+
tm.addCondition( { name: 'unnormalised', success: true, args: [ '/tmp/foo/../bar.txt' ], expected: '/tmp' } );
|
|
375
|
+
await tm.run();
|
|
376
|
+
} );
|
|
377
|
+
TestNS.test( 'getPathFromRoot', async function( t ){
|
|
378
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
379
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getPathFromRoot } );
|
|
380
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getPathFromRoot } );
|
|
381
|
+
tm.addCondition( { name: 'single', success: true, args: [ 'etc' ], expected: PathNS.join( '/', 'etc' ) } );
|
|
382
|
+
tm.addCondition( { name: 'multiple', success: true, args: [ 'etc', 'cron.d' ], expected: PathNS.join( '/', 'etc', 'cron.d' ) } );
|
|
383
|
+
tm.addCondition( { name: 'absolute-segment', success: true, args: [ '/etc', 'cron.d' ], expected: PathNS.join( '/', '/etc', 'cron.d' ) } );
|
|
384
|
+
await tm.run();
|
|
385
|
+
} );
|
|
386
|
+
TestNS.test( 'getPathFromCWD', async function( t ){
|
|
387
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
388
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getPathFromCWD } );
|
|
389
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getPathFromCWD } );
|
|
390
|
+
tm.addCondition( { name: 'no-segments', success: true, args: [], expected: PathNS.join( process.cwd() ) } );
|
|
391
|
+
tm.addCondition( { name: 'segment', success: true, args: [ 'src' ], expected: PathNS.join( process.cwd(), 'src' ) } );
|
|
392
|
+
await tm.run();
|
|
393
|
+
} );
|
|
394
|
+
TestNS.test( 'getPathFromHome', async function( t ){
|
|
395
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
396
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getPathFromHome } );
|
|
397
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getPathFromHome } );
|
|
398
|
+
tm.addCondition( { name: 'no-segments', success: true, args: [], expected: PathNS.join( OSNS.homedir() ) } );
|
|
399
|
+
tm.addCondition( { name: 'segment', success: true, args: [ '.config' ], expected: PathNS.join( OSNS.homedir(), '.config' ) } );
|
|
400
|
+
await tm.run();
|
|
401
|
+
} );
|
|
402
|
+
TestNS.test( 'addUnixType', async function( t ){
|
|
403
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
404
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.addUnixType } );
|
|
405
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.addUnixType } );
|
|
406
|
+
tm.addCondition( {
|
|
407
|
+
name: 'file',
|
|
408
|
+
success: true,
|
|
409
|
+
args: [ { mode: FSNS.constants.S_IFREG } ],
|
|
410
|
+
expected: { mode: FSNS.constants.S_IFREG, unixType: 'file' }
|
|
411
|
+
} );
|
|
412
|
+
tm.addCondition( {
|
|
413
|
+
name: 'directory-bigint',
|
|
414
|
+
success: true,
|
|
415
|
+
args: [ { mode: BigInt( FSNS.constants.S_IFDIR ) } ],
|
|
416
|
+
expected: { mode: BigInt( FSNS.constants.S_IFDIR ), unixType: 'directory' }
|
|
417
|
+
} );
|
|
418
|
+
tm.addCondition( { name: 'no-mode', success: true, args: [ {} ], expected: { unixType: 'unknown' } } );
|
|
419
|
+
tm.addCondition( { name: 'null-mode', success: true, args: [ { mode: null } ], expected: { mode: null, unixType: 'unknown' } } );
|
|
420
|
+
await tm.run();
|
|
421
|
+
} );
|
|
422
|
+
TestNS.test( 'getFormattedStat', async function( t ){
|
|
423
|
+
var fake_stat = { dev: 1, ino: 2, mode: FSNS.constants.S_IFREG, nlink: 3, uid: 4, gid: 5, rdev: 6, size: 7, blksize: 8, blocks: 9, atimeMs: 10, ctimeMs: 11, mtimeMs: 12, birthtimeMs: 13 };
|
|
424
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
425
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.getFormattedStat } );
|
|
426
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.getFormattedStat } );
|
|
427
|
+
tm.addCondition( { name: 'invalid-parametre', success: false, args: [ false ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
428
|
+
tm.addCondition( {
|
|
429
|
+
name: 'default',
|
|
430
|
+
success: true,
|
|
431
|
+
args: [ { statObject: fake_stat } ],
|
|
432
|
+
expected: { deviceID: 1, inode: 2, mode: FSNS.constants.S_IFREG, numberOfLinks: 3, userID: 4, groupID: 5, representsDevice: 6, fileSize: 7, blockSize: 8, blocks: 9, atimeMs: 10, ctimeMs: 11, mtimeMs: 12, birthtimeMs: 13, unixType: 'file' }
|
|
433
|
+
} );
|
|
434
|
+
tm.addCondition( {
|
|
435
|
+
name: 'custom-property-names',
|
|
436
|
+
success: true,
|
|
437
|
+
args: [ { statObject: fake_stat, propertyNames: { dev: 'device', ino: false } } ],
|
|
438
|
+
expected: { device: 1, mode: FSNS.constants.S_IFREG, nlink: 3, uid: 4, gid: 5, rdev: 6, size: 7, blksize: 8, blocks: 9, atimeMs: 10, ctimeMs: 11, mtimeMs: 12, birthtimeMs: 13, unixType: 'file' }
|
|
439
|
+
} );
|
|
440
|
+
tm.addCondition( {
|
|
441
|
+
name: 'no-unix-type',
|
|
442
|
+
success: true,
|
|
443
|
+
args: [ { statObject: fake_stat, unixType: false } ],
|
|
444
|
+
expected: ( value ) => ( 'unixType' in value ) === false
|
|
445
|
+
} );
|
|
446
|
+
tm.addCondition( {
|
|
447
|
+
name: 'no-property-names-no-unix-type',
|
|
448
|
+
success: true,
|
|
449
|
+
args: [ { statObject: fake_stat, propertyNames: null, unixType: false } ],
|
|
450
|
+
expected: ( value ) => ( value === fake_stat )
|
|
451
|
+
} );
|
|
452
|
+
tm.addCondition( {
|
|
453
|
+
name: 'no-property-names-with-unix-type',
|
|
454
|
+
success: true,
|
|
455
|
+
args: [ { statObject: { ...fake_stat }, propertyNames: null, unixType: true } ],
|
|
456
|
+
expected: ( value ) => ( value.unixType === 'file' && value.size === fake_stat.size )
|
|
457
|
+
} );
|
|
458
|
+
tm.addCondition( {
|
|
459
|
+
name: 'noop',
|
|
460
|
+
success: true,
|
|
461
|
+
args: [ { statObject: fake_stat, noop: true } ],
|
|
462
|
+
expected: null
|
|
463
|
+
} );
|
|
464
|
+
await tm.run();
|
|
465
|
+
} );
|
|
466
|
+
TestNS.test( 'statPath', async function( t ){
|
|
467
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
468
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.statPath } );
|
|
469
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.statPath } );
|
|
470
|
+
tm.addCondition( { name: 'invalid-path', success: false, args: [ false ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
471
|
+
tm.addCondition( { name: 'invalid-follow', success: false, args: [ 'package.json', 'yes' ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
472
|
+
tm.addCondition( { name: 'invalid-bigint', success: false, args: [ 'package.json', true, 'yes' ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
473
|
+
tm.addCondition( { name: 'missing-path', success: false, promise: true, args: [ 'no-such-path-xyz' ], expected: { constructor: Error } } );
|
|
474
|
+
tm.addCondition( {
|
|
475
|
+
name: 'number-mode',
|
|
476
|
+
success: true,
|
|
477
|
+
promise: true,
|
|
478
|
+
args: [ 'package.json' ],
|
|
479
|
+
expected: async ( value ) => {
|
|
480
|
+
var resolved = await value;
|
|
481
|
+
return ( typeof( resolved.size ) === 'number' && resolved.isFile() === true );
|
|
482
|
+
}
|
|
483
|
+
} );
|
|
484
|
+
tm.addCondition( {
|
|
485
|
+
name: 'bigint-mode',
|
|
486
|
+
success: true,
|
|
487
|
+
promise: true,
|
|
488
|
+
args: [ 'package.json', true, true ],
|
|
489
|
+
expected: async ( value ) => {
|
|
490
|
+
var resolved = await value;
|
|
491
|
+
return ( typeof( resolved.size ) === 'bigint' && resolved.isFile() === true );
|
|
492
|
+
}
|
|
493
|
+
} );
|
|
494
|
+
await tm.run();
|
|
495
|
+
} );
|
|
496
|
+
TestNS.test( 'statOptions', async function( t ){
|
|
497
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
498
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.statOptions } );
|
|
499
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.statOptions } );
|
|
500
|
+
tm.addCondition( { name: 'invalid-parametre', success: false, args: [ false ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
501
|
+
tm.addCondition( {
|
|
502
|
+
name: 'default',
|
|
503
|
+
success: true,
|
|
504
|
+
promise: true,
|
|
505
|
+
args: [ { path: 'package.json' } ],
|
|
506
|
+
expected: async ( value ) => {
|
|
507
|
+
var resolved = await value;
|
|
508
|
+
return ( resolved.unixType === 'file' && resolved.isFile() === true && typeof( resolved.size ) === 'number' );
|
|
509
|
+
}
|
|
510
|
+
} );
|
|
511
|
+
tm.addCondition( {
|
|
512
|
+
name: 'bigint',
|
|
513
|
+
success: true,
|
|
514
|
+
promise: true,
|
|
515
|
+
args: [ { path: 'package.json', bigint: true } ],
|
|
516
|
+
expected: async ( value ) => ( typeof( ( await value ).size ) === 'bigint' )
|
|
517
|
+
} );
|
|
518
|
+
tm.addCondition( {
|
|
519
|
+
name: 'no-unix-type',
|
|
520
|
+
success: true,
|
|
521
|
+
promise: true,
|
|
522
|
+
args: [ { path: 'package.json', unixType: false } ],
|
|
523
|
+
expected: async ( value ) => ( ( 'unixType' in ( await value ) ) === false )
|
|
524
|
+
} );
|
|
525
|
+
tm.addCondition( {
|
|
526
|
+
name: 'verbose-property-names',
|
|
527
|
+
success: true,
|
|
528
|
+
promise: true,
|
|
529
|
+
args: [ { path: 'package.json', verbosePropertyNames: true } ],
|
|
530
|
+
expected: async ( value ) => {
|
|
531
|
+
var resolved = await value;
|
|
532
|
+
return ( typeof( resolved.fileSize ) === 'number' && resolved.unixType === 'file' && ( 'size' in resolved ) === false );
|
|
533
|
+
}
|
|
534
|
+
} );
|
|
535
|
+
tm.addCondition( {
|
|
536
|
+
name: 'verbose-no-unix-type',
|
|
537
|
+
success: true,
|
|
538
|
+
promise: true,
|
|
539
|
+
args: [ { path: 'package.json', verbosePropertyNames: true, unixType: false } ],
|
|
540
|
+
expected: async ( value ) => {
|
|
541
|
+
var resolved = await value;
|
|
542
|
+
return ( ( 'unixType' in resolved ) === false && typeof( resolved.fileSize ) === 'number' );
|
|
543
|
+
}
|
|
544
|
+
} );
|
|
545
|
+
tm.addCondition( { name: 'missing-path', success: false, promise: true, args: [ { path: 'no-such-path-xyz' } ], expected: { constructor: Error } } );
|
|
546
|
+
tm.addCondition( { name: 'noop', success: true, args: [ { path: 'package.json', noop: true } ], expected: null } );
|
|
547
|
+
await tm.run();
|
|
548
|
+
} );
|
|
549
|
+
TestNS.test( 'TreeRoot', async function( t ){
|
|
550
|
+
var root = defaultExport.TreeRoot();
|
|
551
|
+
AssertNS.deepStrictEqual( root.name, '[root]' );
|
|
552
|
+
AssertNS.deepStrictEqual( root.children, [] );
|
|
553
|
+
var named = namespaceExport.TreeRoot( { name: 'custom' } );
|
|
554
|
+
AssertNS.deepStrictEqual( named.name, 'custom' );
|
|
555
|
+
AssertNS.assertInstanceof( new defaultExport.TreeRoot(), defaultExport.TreeRoot );
|
|
556
|
+
var provided_children = [ 1, 2 ];
|
|
557
|
+
AssertNS.deepStrictEqual( defaultExport.TreeRoot( { children: provided_children } ).children, provided_children );
|
|
558
|
+
} );
|
|
559
|
+
TestNS.test( 'TreeItem', async function( t ){
|
|
560
|
+
// Construction without `new` behaves like construction with it.
|
|
561
|
+
var item = defaultExport.TreeItem( { name: 'thing', path: '/tmp/thing' } );
|
|
562
|
+
AssertNS.assertInstanceof( item, defaultExport.TreeItem );
|
|
563
|
+
AssertNS.deepStrictEqual( item.name, 'thing' );
|
|
564
|
+
AssertNS.deepStrictEqual( item.relativePath, '/tmp/thing' );
|
|
565
|
+
AssertNS.deepStrictEqual( item.absolutePath, PathNS.resolve( '/tmp/thing' ) );
|
|
566
|
+
AssertNS.deepStrictEqual( item.url.href, new URL( `file://${PathNS.resolve( '/tmp/thing' )}` ).href );
|
|
567
|
+
AssertNS.deepStrictEqual( item.children, [] );
|
|
568
|
+
// A `path` alone derives the name from the path's basename.
|
|
569
|
+
AssertNS.deepStrictEqual( defaultExport.TreeItem( { path: '/tmp/thing.txt' } ).name, 'thing.txt' );
|
|
570
|
+
// No path at all means the item means the working directory.
|
|
571
|
+
var cwd_item = defaultExport.TreeItem();
|
|
572
|
+
AssertNS.deepStrictEqual( cwd_item.name, null );
|
|
573
|
+
AssertNS.deepStrictEqual( cwd_item.relativePath, '' );
|
|
574
|
+
AssertNS.deepStrictEqual( cwd_item.absolutePath, PathNS.resolve( '' ) );
|
|
575
|
+
// Explicit properties win over derived ones.
|
|
576
|
+
var explicit = defaultExport.TreeItem( { path: '/a/b', name: 'n', relativePath: 'r', absolutePath: '/abs', url: 'file:///u', unixType: 'file', size: 5 } );
|
|
577
|
+
AssertNS.deepStrictEqual( explicit.name, 'n' );
|
|
578
|
+
AssertNS.deepStrictEqual( explicit.relativePath, 'r' );
|
|
579
|
+
AssertNS.deepStrictEqual( explicit.absolutePath, '/abs' );
|
|
580
|
+
AssertNS.deepStrictEqual( explicit.url, 'file:///u' );
|
|
581
|
+
AssertNS.deepStrictEqual( explicit.unixType, 'file' );
|
|
582
|
+
AssertNS.deepStrictEqual( explicit.size, 5 );
|
|
583
|
+
// Setting `absolutePath` invalidates the cached `url`, forcing it to be re-derived.
|
|
584
|
+
explicit.absolutePath = '/other';
|
|
585
|
+
AssertNS.deepStrictEqual( explicit.url.href, new URL( 'file:///other' ).href );
|
|
586
|
+
// `size` falls back to the stat object's `size`.
|
|
587
|
+
var with_stat = defaultExport.TreeItem( { statObject: { mode: FSNS.constants.S_IFREG, size: 42 } } );
|
|
588
|
+
AssertNS.deepStrictEqual( with_stat.unixType, 'file' );
|
|
589
|
+
AssertNS.deepStrictEqual( with_stat.size, 42 );
|
|
590
|
+
AssertNS.deepStrictEqual( defaultExport.TreeItem( {} ).size, null );
|
|
591
|
+
// Children can be provided directly.
|
|
592
|
+
AssertNS.deepStrictEqual( defaultExport.TreeItem( { children: [ 1 ] } ).children, [ 1 ] );
|
|
593
|
+
// `direntObject` sets `name`, `relativePath` and `unixType`.
|
|
594
|
+
var dirent_item = defaultExport.TreeItem( { direntObject: { parentPath: '/tmp', name: 'e.txt', isFile: () => true, isDirectory: () => false, isSymbolicLink: () => false, isFIFO: () => false, isCharacterDevice: () => false, isBlockDevice: () => false, isSocket: () => false } } );
|
|
595
|
+
AssertNS.deepStrictEqual( dirent_item.name, 'e.txt' );
|
|
596
|
+
AssertNS.deepStrictEqual( dirent_item.relativePath, PathNS.join( '/tmp', 'e.txt' ) );
|
|
597
|
+
AssertNS.deepStrictEqual( dirent_item.unixType, 'file' );
|
|
598
|
+
// Setting `direntObject` after construction has the same effect.
|
|
599
|
+
var reassigned = defaultExport.TreeItem();
|
|
600
|
+
reassigned.direntObject = { parentPath: '/tmp', name: 'd', isFile: () => false, isDirectory: () => true, isSymbolicLink: () => false, isFIFO: () => false, isCharacterDevice: () => false, isBlockDevice: () => false, isSocket: () => false };
|
|
601
|
+
AssertNS.deepStrictEqual( reassigned.name, 'd' );
|
|
602
|
+
AssertNS.deepStrictEqual( reassigned.unixType, 'directory' );
|
|
603
|
+
AssertNS.deepStrictEqual( reassigned.direntObject.name, 'd' );
|
|
604
|
+
// Invalid `direntObject` throws.
|
|
605
|
+
AssertNS.throws( () => defaultExport.TreeItem( { direntObject: 'nope' } ), ( error ) => error.code === 'ERR_INVALID_ARG_TYPE' );
|
|
606
|
+
// `createChildItem`/`addChild` link children to their parent and return the new index.
|
|
607
|
+
var parent = defaultExport.TreeItem( { path: '/tmp/parent' } );
|
|
608
|
+
AssertNS.deepStrictEqual( parent.addChild( { name: 'kid' } ), 0 );
|
|
609
|
+
AssertNS.deepStrictEqual( parent.children[0].name, 'kid' );
|
|
610
|
+
AssertNS.deepStrictEqual( parent.children[0].parentItem, parent );
|
|
611
|
+
AssertNS.deepStrictEqual( parent.children[0].relativePath, PathNS.join( '/tmp/parent', 'kid' ) );
|
|
612
|
+
AssertNS.deepStrictEqual( parent.addChild( { name: 'kid2' } ), 1 );
|
|
613
|
+
AssertNS.deepStrictEqual( parent.createChildItem( { name: 'kid3' } ).parentItem, parent );
|
|
614
|
+
AssertNS.deepStrictEqual( parent.children.length, 2 );
|
|
615
|
+
} );
|
|
616
|
+
TestNS.test( 'TreeItem.prototype.stat/hash', async function( t ){
|
|
617
|
+
await createFixture();
|
|
618
|
+
try{
|
|
619
|
+
var file_item = defaultExport.TreeItem( { path: PathNS.join( FIXTURE.path, 'a.txt' ) } );
|
|
620
|
+
var returned = await file_item.stat();
|
|
621
|
+
AssertNS.deepStrictEqual( returned, file_item );
|
|
622
|
+
AssertNS.deepStrictEqual( file_item.unixType, 'file' );
|
|
623
|
+
AssertNS.deepStrictEqual( file_item.size, FIXTURE.sizes['a.txt'] );
|
|
624
|
+
// `stat` with `bigint: true` yields a `BigInt` size.
|
|
625
|
+
var big_item = defaultExport.TreeItem( { path: PathNS.join( FIXTURE.path, 'b.txt' ) } );
|
|
626
|
+
await big_item.stat( { bigint: true } );
|
|
627
|
+
AssertNS.deepStrictEqual( typeof( big_item.size ), 'bigint' );
|
|
628
|
+
// Statting a missing item rejects with a wrapped error.
|
|
629
|
+
var missing_item = defaultExport.TreeItem( { path: PathNS.join( FIXTURE.path, 'nope' ) } );
|
|
630
|
+
await AssertNS.rejects( missing_item.stat(), ( error ) => error.constructor === Error && error.cause != null );
|
|
631
|
+
// Hashing stores a Base16 `sha256` property and resolves to the same string.
|
|
632
|
+
var hash_item = defaultExport.TreeItem( { path: PathNS.join( FIXTURE.path, 'a.txt' ) } );
|
|
633
|
+
var hash_string = await hash_item.hash();
|
|
634
|
+
AssertNS.deepStrictEqual( hash_string, getBase16FromBuffer( await getSHA256FromString( 'AAAA' ) ) );
|
|
635
|
+
AssertNS.deepStrictEqual( hash_item.sha256, hash_string );
|
|
636
|
+
// Base64 hashing is also supported.
|
|
637
|
+
var base64_item = defaultExport.TreeItem( { path: PathNS.join( FIXTURE.path, 'a.txt' ) } );
|
|
638
|
+
var base64_string = await base64_item.hash( { formatOptions: { format: 'base64' } } );
|
|
639
|
+
AssertNS.deepStrictEqual( base64_string, getBase64FromBuffer( await getSHA256FromString( 'AAAA' ) ) );
|
|
640
|
+
AssertNS.deepStrictEqual( base64_item.sha256, base64_string );
|
|
641
|
+
// Unsupported algorithms and formats throw.
|
|
642
|
+
AssertNS.throws( () => hash_item.hash( { hashOptions: { hash: 'md5' } } ), ( error ) => error.code === 'ERR_INVALID_ARG_VALUE' );
|
|
643
|
+
AssertNS.throws( () => hash_item.hash( { formatOptions: { format: 'base32' } } ), ( error ) => error.code === 'ERR_INVALID_ARG_VALUE' );
|
|
644
|
+
// Hashing a missing file rejects with a wrapped error.
|
|
645
|
+
await AssertNS.rejects( missing_item.hash(), ( error ) => error.constructor === Error && error.cause != null );
|
|
646
|
+
// `noop` resolves to `null` and stores nothing.
|
|
647
|
+
AssertNS.deepStrictEqual( await hash_item.hash( { noop: true } ), null );
|
|
648
|
+
// A `validationFunction` returning `false` short-circuits to `null` without hashing.
|
|
649
|
+
AssertNS.deepStrictEqual( await hash_item.hash( { validationFunction: () => false } ), null );
|
|
650
|
+
} finally{
|
|
651
|
+
await removeFixture();
|
|
652
|
+
}
|
|
653
|
+
} );
|
|
654
|
+
TestNS.test( 'TreeItem.prototype.processAsync', async function( t ){
|
|
655
|
+
await createFixture();
|
|
656
|
+
try{
|
|
657
|
+
// Non-recursive processing stats only the root.
|
|
658
|
+
var shallow_root = defaultExport.TreeItem( { path: FIXTURE.path } );
|
|
659
|
+
var shallow_returned = await shallow_root.processAsync();
|
|
660
|
+
AssertNS.deepStrictEqual( shallow_returned, shallow_root );
|
|
661
|
+
AssertNS.deepStrictEqual( shallow_root.unixType, 'directory' );
|
|
662
|
+
AssertNS.deepStrictEqual( shallow_root.children.length, 0 );
|
|
663
|
+
// Recursive processing populates the whole tree.
|
|
664
|
+
var root = defaultExport.TreeItem( { path: FIXTURE.path } );
|
|
665
|
+
await root.processAsync( { recursive: true } );
|
|
666
|
+
AssertNS.deepStrictEqual( root.children.length, 5 );
|
|
667
|
+
containsOnlyNames( root, [ 'a.txt', 'b.txt', 'link.txt', 'empty', 'sub' ] );
|
|
668
|
+
var sub = findChildByName( root, 'sub' );
|
|
669
|
+
containsOnlyNames( sub, [ 'c.bin', 'nested' ] );
|
|
670
|
+
containsOnlyNames( findChildByName( sub, 'nested' ), [ 'd.txt' ] );
|
|
671
|
+
AssertNS.deepStrictEqual( findChildByName( root, 'empty' ).children.length, 0 );
|
|
672
|
+
// Exclusion regexes omit matching entries and their subtrees.
|
|
673
|
+
var excluded_root = defaultExport.TreeItem( { path: FIXTURE.path } );
|
|
674
|
+
await excluded_root.processAsync( { recursive: true, excludeRegExpArray: [ /\.txt$/ ] } );
|
|
675
|
+
containsOnlyNames( excluded_root, [ 'empty', 'sub' ] );
|
|
676
|
+
containsOnlyNames( findChildByName( excluded_root, 'sub' ), [ 'c.bin', 'nested' ] );
|
|
677
|
+
containsOnlyNames( findChildByName( findChildByName( excluded_root, 'sub' ), 'nested' ), [] );
|
|
678
|
+
// Excluding by root-relative path also works.
|
|
679
|
+
var path_excluded_root = defaultExport.TreeItem( { path: FIXTURE.path } );
|
|
680
|
+
await path_excluded_root.processAsync( { recursive: true, rootPath: FIXTURE.path, excludeRegExpArray: [ /sub\/nested/ ] } );
|
|
681
|
+
containsOnlyNames( findChildByName( path_excluded_root, 'sub' ), [ 'c.bin' ] );
|
|
682
|
+
// `maxDepth` limits descent: children are listed, descendants aren't.
|
|
683
|
+
var depth_root = defaultExport.TreeItem( { path: FIXTURE.path } );
|
|
684
|
+
await depth_root.processAsync( { recursive: true, maxDepth: 1 } );
|
|
685
|
+
AssertNS.deepStrictEqual( depth_root.children.length, 5 );
|
|
686
|
+
AssertNS.deepStrictEqual( findChildByName( depth_root, 'sub' ).children.length, 0 );
|
|
687
|
+
// Hashes are computed for regular files when requested.
|
|
688
|
+
var hashed_root = defaultExport.TreeItem( { path: FIXTURE.path } );
|
|
689
|
+
await hashed_root.processAsync( { recursive: true, hash: true } );
|
|
690
|
+
var hashed_file = findChildByName( hashed_root, 'a.txt' );
|
|
691
|
+
AssertNS.deepStrictEqual( hashed_file.sha256, getBase16FromBuffer( await getSHA256FromString( 'AAAA' ) ) );
|
|
692
|
+
AssertNS.deepStrictEqual( findChildByName( hashed_root, 'empty' ).sha256, undefined );
|
|
693
|
+
// `stat: false` leaves the item un-statted.
|
|
694
|
+
var unstat_root = defaultExport.TreeItem( { path: FIXTURE.path } );
|
|
695
|
+
await unstat_root.processAsync( { stat: false } );
|
|
696
|
+
AssertNS.deepStrictEqual( unstat_root.unixType, undefined );
|
|
697
|
+
// `readdirOptions` are passed through to `readdir`.
|
|
698
|
+
var options_root = defaultExport.TreeItem( { path: FIXTURE.path } );
|
|
699
|
+
await options_root.processAsync( { recursive: true, readdirOptions: { encoding: 'utf8' } } );
|
|
700
|
+
AssertNS.deepStrictEqual( options_root.children.length, 5 );
|
|
701
|
+
// Invalid options throw.
|
|
702
|
+
AssertNS.throws( () => root.processAsync( { excludeRegExpArray: 'nope' } ), ( error ) => error.code === 'ERR_INVALID_ARG_TYPE' );
|
|
703
|
+
AssertNS.throws( () => root.processAsync( { excludeRegExpArray: [ 'nope' ] } ), ( error ) => error.code === 'ERR_INVALID_ARG_VALUE' );
|
|
704
|
+
AssertNS.throws( () => root.processAsync( { maxDepth: -1 } ), ( error ) => error.code === 'ERR_INVALID_ARG_TYPE' );
|
|
705
|
+
AssertNS.throws( () => root.processAsync( { maxDepth: Number.NaN } ), ( error ) => error.code === 'ERR_INVALID_ARG_TYPE' );
|
|
706
|
+
// `noop` resolves to the item without processing it.
|
|
707
|
+
var noop_root = defaultExport.TreeItem( { path: FIXTURE.path } );
|
|
708
|
+
AssertNS.deepStrictEqual( await noop_root.processAsync( { noop: true } ), noop_root );
|
|
709
|
+
AssertNS.deepStrictEqual( noop_root.unixType, undefined );
|
|
710
|
+
// A readdir failure during recursion rejects with a wrapped error.
|
|
711
|
+
var broken_root = defaultExport.TreeItem( { path: FIXTURE.path } );
|
|
712
|
+
await AssertNS.rejects( broken_root.processAsync( { recursive: true, readdirOptions: { encoding: 'not-a-real-encoding' } } ), ( error ) => error.constructor === Error && error.cause != null );
|
|
713
|
+
} finally{
|
|
714
|
+
await removeFixture();
|
|
715
|
+
}
|
|
716
|
+
} );
|
|
717
|
+
TestNS.test( 'TreeItem.createFromDirectory', async function( t ){
|
|
718
|
+
await createFixture();
|
|
719
|
+
try{
|
|
720
|
+
var root = await defaultExport.TreeItem.createFromDirectory( FIXTURE.path );
|
|
721
|
+
AssertNS.assertInstanceof( root, defaultExport.TreeItem );
|
|
722
|
+
AssertNS.deepStrictEqual( root.unixType, 'directory' );
|
|
723
|
+
AssertNS.deepStrictEqual( root.children.length, 5 );
|
|
724
|
+
containsOnlyNames( findChildByName( root, 'sub' ), [ 'c.bin', 'nested' ] );
|
|
725
|
+
await AssertNS.rejects( defaultExport.TreeItem.createFromDirectory( PathNS.join( FIXTURE.path, 'nope' ) ), ( error ) => error.constructor === Error );
|
|
726
|
+
} finally{
|
|
727
|
+
await removeFixture();
|
|
728
|
+
}
|
|
729
|
+
} );
|
|
730
|
+
TestNS.test( 'TreeItem.prototype.aggregate', async function( t ){
|
|
731
|
+
await createFixture();
|
|
732
|
+
try{
|
|
733
|
+
var root = await defaultExport.TreeItem.createFromDirectory( FIXTURE.path );
|
|
734
|
+
var returned = root.aggregate();
|
|
735
|
+
AssertNS.deepStrictEqual( returned, root );
|
|
736
|
+
// The root's `percentageOfParent` is always 100.
|
|
737
|
+
AssertNS.deepStrictEqual( root.percentageOfParent, 100 );
|
|
738
|
+
// `createFromDirectory` doesn't follow symlinks, so `link.txt` is counted as other rather than as a file.
|
|
739
|
+
AssertNS.deepStrictEqual( root.fileCount, 4 ); // a.txt, b.txt, c.bin, d.txt
|
|
740
|
+
AssertNS.deepStrictEqual( root.directoryCount, 4 ); // the root itself, sub, empty, nested
|
|
741
|
+
AssertNS.deepStrictEqual( root.otherCount, 1 ); // link.txt, an unfollowed symlink
|
|
742
|
+
// Aggregates are self-consistent at every node.
|
|
743
|
+
AssertNS.deepStrictEqual( everyNodeSatisfies( root, ( node ) => {
|
|
744
|
+
var expected_size = node.size ?? 0;
|
|
745
|
+
var expected_files = node.unixType === 'file' ? 1 : 0;
|
|
746
|
+
var expected_directories = node.unixType === 'directory' ? 1 : 0;
|
|
747
|
+
var expected_others = node.unixType != null && node.unixType !== 'file' && node.unixType !== 'directory' ? 1 : 0;
|
|
748
|
+
for( const child of node.children ){
|
|
749
|
+
expected_size += child.totalSize;
|
|
750
|
+
expected_files += child.fileCount;
|
|
751
|
+
expected_directories += child.directoryCount;
|
|
752
|
+
expected_others += child.otherCount;
|
|
753
|
+
}
|
|
754
|
+
return node.totalSize === expected_size && node.fileCount === expected_files && node.directoryCount === expected_directories && node.otherCount === expected_others;
|
|
755
|
+
} ), true );
|
|
756
|
+
// A child's `percentageOfParent` is its share of its parent's `totalSize`.
|
|
757
|
+
for( const child of root.children ){
|
|
758
|
+
AssertNS.deepStrictEqual( child.percentageOfParent, ( child.totalSize / root.totalSize ) * 100 );
|
|
759
|
+
}
|
|
760
|
+
// `sortBySize` orders each directory's children largest-first.
|
|
761
|
+
var sorted_root = await defaultExport.TreeItem.createFromDirectory( FIXTURE.path );
|
|
762
|
+
sorted_root.aggregate( { sortBySize: true } );
|
|
763
|
+
for( const node of [ sorted_root, findChildByName( sorted_root, 'sub' ) ] ){
|
|
764
|
+
for( var index = 1; index < node.children.length; index++ ){
|
|
765
|
+
AssertNS.ok( node.children[index - 1].totalSize >= node.children[index].totalSize );
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
} finally{
|
|
769
|
+
await removeFixture();
|
|
770
|
+
}
|
|
771
|
+
} );
|
|
772
|
+
TestNS.test( 'TreeItem.prototype.serialise', async function( t ){
|
|
773
|
+
await createFixture();
|
|
774
|
+
try{
|
|
775
|
+
var root = await defaultExport.dirstat( { path: FIXTURE.path, statOptions: { bigint: true }, hash: true } );
|
|
776
|
+
var serialised = root.serialise();
|
|
777
|
+
// The serialised form is JSON-safe.
|
|
778
|
+
var parsed = JSON.parse( JSON.stringify( serialised ) );
|
|
779
|
+
AssertNS.deepStrictEqual( parsed.name, root.name );
|
|
780
|
+
AssertNS.deepStrictEqual( parsed.url, String( root.url ) );
|
|
781
|
+
AssertNS.deepStrictEqual( parsed.totalSize, root.totalSize.toString() );
|
|
782
|
+
AssertNS.deepStrictEqual( typeof( parsed.children[0].size ), 'string' );
|
|
783
|
+
AssertNS.deepStrictEqual( parsed.children.length, root.children.length );
|
|
784
|
+
// Non-recursive serialisation drops the children.
|
|
785
|
+
AssertNS.deepStrictEqual( root.serialise( { recursive: false } ).children, [] );
|
|
786
|
+
// `sha256` appears only when a hash was computed.
|
|
787
|
+
AssertNS.deepStrictEqual( typeof( findByName( parsed, 'a.txt' ).sha256 ), 'string' );
|
|
788
|
+
AssertNS.deepStrictEqual( findByName( parsed, 'empty' ).sha256, undefined );
|
|
789
|
+
} finally{
|
|
790
|
+
await removeFixture();
|
|
791
|
+
}
|
|
792
|
+
} );
|
|
793
|
+
TestNS.test( 'dirstat', async function( t ){
|
|
794
|
+
await createFixture();
|
|
795
|
+
try{
|
|
796
|
+
var root = await defaultExport.dirstat( { path: FIXTURE.path } );
|
|
797
|
+
AssertNS.assertInstanceof( root, defaultExport.TreeItem );
|
|
798
|
+
AssertNS.deepStrictEqual( root.name, PathNS.basename( FIXTURE.path ) );
|
|
799
|
+
AssertNS.deepStrictEqual( root.unixType, 'directory' );
|
|
800
|
+
AssertNS.deepStrictEqual( root.relativePath, FIXTURE.path );
|
|
801
|
+
// Recurse by default.
|
|
802
|
+
containsOnlyNames( root, [ 'a.txt', 'b.txt', 'link.txt', 'empty', 'sub' ] );
|
|
803
|
+
containsOnlyNames( findChildByName( findChildByName( root, 'sub' ), 'nested' ), [ 'd.txt' ] );
|
|
804
|
+
AssertNS.deepStrictEqual( findChildByName( root, 'a.txt' ).size, FIXTURE.sizes['a.txt'] );
|
|
805
|
+
AssertNS.ok( findChildByName( root, 'sub' ).size < FIXTURE.directorySizeUpperBound );
|
|
806
|
+
// Symlinks are followed by default.
|
|
807
|
+
AssertNS.deepStrictEqual( findChildByName( root, 'link.txt' ).unixType, 'file' );
|
|
808
|
+
AssertNS.deepStrictEqual( findChildByName( root, 'link.txt' ).size, FIXTURE.sizes['a.txt'] );
|
|
809
|
+
// With `followSymbolicLinks: false`, the symlink is reported as a symlink.
|
|
810
|
+
var non_followed = await defaultExport.dirstat( { path: FIXTURE.path, statOptions: { followSymbolicLinks: false } } );
|
|
811
|
+
AssertNS.deepStrictEqual( findChildByName( non_followed, 'link.txt' ).unixType, 'symbolic-link' );
|
|
812
|
+
// `recursive: false` processes only the root.
|
|
813
|
+
var shallow = await defaultExport.dirstat( { path: FIXTURE.path, recursive: false } );
|
|
814
|
+
AssertNS.deepStrictEqual( shallow.children.length, 0 );
|
|
815
|
+
AssertNS.deepStrictEqual( shallow.unixType, 'directory' );
|
|
816
|
+
AssertNS.deepStrictEqual( shallow.fileCount, 0 );
|
|
817
|
+
AssertNS.deepStrictEqual( shallow.directoryCount, 1 );
|
|
818
|
+
// `maxDepth: 1` lists the root's children but not the grandchildren.
|
|
819
|
+
var depth_one = await defaultExport.dirstat( { path: FIXTURE.path, maxDepth: 1 } );
|
|
820
|
+
AssertNS.deepStrictEqual( depth_one.children.length, 5 );
|
|
821
|
+
AssertNS.deepStrictEqual( findChildByName( depth_one, 'sub' ).children.length, 0 );
|
|
822
|
+
// Exclusion regexes omit entries and their subtrees.
|
|
823
|
+
var excluded = await defaultExport.dirstat( { path: FIXTURE.path, excludeRegExpArray: [ /b\.txt$/ ] } );
|
|
824
|
+
containsOnlyNames( excluded, [ 'a.txt', 'link.txt', 'empty', 'sub' ] );
|
|
825
|
+
// Hashing stores a Base16 `sha256` for every regular file.
|
|
826
|
+
var hashed = await defaultExport.dirstat( { path: FIXTURE.path, hash: true } );
|
|
827
|
+
AssertNS.deepStrictEqual( findChildByName( hashed, 'a.txt' ).sha256, getBase16FromBuffer( await getSHA256FromString( 'AAAA' ) ) );
|
|
828
|
+
AssertNS.deepStrictEqual( findChildByName( findChildByName( findChildByName( hashed, 'sub' ), 'nested' ), 'd.txt' ).sha256, getBase16FromBuffer( await getSHA256FromString( 'D' ) ) );
|
|
829
|
+
// `hashOptions`/`formatOptions` are honoured.
|
|
830
|
+
var base64_hashed = await defaultExport.dirstat( { path: FIXTURE.path, hash: true, formatOptions: { format: 'base64' } } );
|
|
831
|
+
AssertNS.deepStrictEqual( findChildByName( base64_hashed, 'a.txt' ).sha256, getBase64FromBuffer( await getSHA256FromString( 'AAAA' ) ) );
|
|
832
|
+
// `bigint: true` produces `BigInt` sizes and `BigInt` totals, which serialise to strings.
|
|
833
|
+
var big = await defaultExport.dirstat( { path: FIXTURE.path, statOptions: { bigint: true } } );
|
|
834
|
+
AssertNS.deepStrictEqual( typeof( big.totalSize ), 'bigint' );
|
|
835
|
+
AssertNS.deepStrictEqual( typeof( findChildByName( big, 'a.txt' ).size ), 'bigint' );
|
|
836
|
+
AssertNS.deepStrictEqual( typeof( big.serialise().totalSize ), 'string' );
|
|
837
|
+
// `sortBySize` orders children largest-first.
|
|
838
|
+
var sorted = await defaultExport.dirstat( { path: FIXTURE.path, sortBySize: true } );
|
|
839
|
+
for( var index = 1; index < sorted.children.length; index++ ){
|
|
840
|
+
AssertNS.ok( sorted.children[index - 1].totalSize >= sorted.children[index].totalSize );
|
|
841
|
+
}
|
|
842
|
+
// A single file can be processed as its own root.
|
|
843
|
+
var file_root = await defaultExport.dirstat( { path: PathNS.join( FIXTURE.path, 'a.txt' ) } );
|
|
844
|
+
AssertNS.deepStrictEqual( file_root.unixType, 'file' );
|
|
845
|
+
AssertNS.deepStrictEqual( file_root.totalSize, FIXTURE.sizes['a.txt'] );
|
|
846
|
+
AssertNS.deepStrictEqual( file_root.fileCount, 1 );
|
|
847
|
+
AssertNS.deepStrictEqual( file_root.percentageOfParent, 100 );
|
|
848
|
+
AssertNS.deepStrictEqual( file_root.children.length, 0 );
|
|
849
|
+
// `readdirOptions` are accepted.
|
|
850
|
+
var options_root = await defaultExport.dirstat( { path: FIXTURE.path, maxDepth: 1, readdirOptions: { encoding: 'utf8' } } );
|
|
851
|
+
AssertNS.deepStrictEqual( options_root.children.length, 5 );
|
|
852
|
+
// `normalisePath: false` leaves the path as-is.
|
|
853
|
+
var unnormalised = await defaultExport.dirstat( { path: PathNS.join( FIXTURE.path, '.', 'sub', '..' ), normalisePath: false } );
|
|
854
|
+
AssertNS.deepStrictEqual( unnormalised.absolutePath, PathNS.resolve( FIXTURE.path, 'sub', '..' ) );
|
|
855
|
+
// `noop` resolves to `null` without touching the filesystem.
|
|
856
|
+
AssertNS.deepStrictEqual( await defaultExport.dirstat( { path: FIXTURE.path, noop: true } ), null );
|
|
857
|
+
// The current working directory is used when no path is given.
|
|
858
|
+
var cwd_root = await defaultExport.dirstat( { maxDepth: 0 } );
|
|
859
|
+
AssertNS.deepStrictEqual( cwd_root.absolutePath, PathNS.resolve( process.cwd() ) );
|
|
860
|
+
AssertNS.deepStrictEqual( cwd_root.unixType, 'directory' );
|
|
861
|
+
// A missing path rejects with a wrapped error.
|
|
862
|
+
await AssertNS.rejects( defaultExport.dirstat( { path: PathNS.join( FIXTURE.path, 'nope' ) } ), ( error ) => error.constructor === Error && error.cause != null );
|
|
863
|
+
// A readdir failure during recursion rejects with a wrapped error.
|
|
864
|
+
await AssertNS.rejects( defaultExport.dirstat( { path: FIXTURE.path, readdirOptions: { encoding: 'not-a-real-encoding' } } ), ( error ) => error.constructor === Error && error.cause != null );
|
|
865
|
+
} finally{
|
|
866
|
+
await removeFixture();
|
|
867
|
+
}
|
|
868
|
+
} );
|
|
869
|
+
TestNS.test( 'dirstat:invalid-options', async function( t ){
|
|
870
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
871
|
+
tm.addFunction( { name: 'defaultExport', func: defaultExport.dirstat } );
|
|
872
|
+
tm.addFunction( { name: 'namespaceExport', func: namespaceExport.dirstat } );
|
|
873
|
+
tm.addCondition( { name: 'invalid-parametre', success: false, args: [ 'not-an-object' ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
874
|
+
tm.addCondition( { name: 'invalid-path', success: false, args: [ { path: 5 } ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
875
|
+
tm.addCondition( { name: 'invalid-normalisePath', success: false, args: [ { normalisePath: 'yes' } ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
876
|
+
tm.addCondition( { name: 'invalid-recursive', success: false, args: [ { recursive: 'yes' } ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
877
|
+
tm.addCondition( { name: 'invalid-maxDepth-negative', success: false, args: [ { maxDepth: -1 } ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
878
|
+
tm.addCondition( { name: 'invalid-maxDepth-nan', success: false, args: [ { maxDepth: Number.NaN } ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
879
|
+
tm.addCondition( { name: 'invalid-maxDepth-type', success: false, args: [ { maxDepth: '1' } ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
880
|
+
tm.addCondition( { name: 'invalid-excludeRegExpArray', success: false, args: [ { excludeRegExpArray: 'nope' } ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
881
|
+
tm.addCondition( { name: 'invalid-excludeRegExpArray-member', success: false, args: [ { excludeRegExpArray: [ 'nope' ] } ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_VALUE' } } );
|
|
882
|
+
tm.addCondition( { name: 'invalid-hash', success: false, args: [ { hash: 'yes' } ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
883
|
+
tm.addCondition( { name: 'invalid-sortBySize', success: false, args: [ { sortBySize: 'yes' } ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
884
|
+
tm.addCondition( { name: 'invalid-statOptions', success: false, args: [ { statOptions: 'nope' } ], expected: { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } } );
|
|
885
|
+
await tm.run();
|
|
886
|
+
} );
|
|
887
|
+
TestNS.test( 'AmEntryModule', async function( t ){
|
|
888
|
+
var result = await defaultExport.AmEntryModule();
|
|
889
|
+
// When the test runner is the entry module, `process.argv[1]` doesn't resolve to `cno-files.js`, so this should be `false`, but it must always resolve to a boolean.
|
|
890
|
+
AssertNS.deepStrictEqual( typeof( result ), 'boolean' );
|
|
891
|
+
AssertNS.deepStrictEqual( await namespaceExport.AmEntryModule(), result );
|
|
892
|
+
// `process.argv[1]` that isn't a string (e.g. `node -e`) resolves to `false`.
|
|
893
|
+
var saved_argv1 = process.argv[1];
|
|
894
|
+
try{
|
|
895
|
+
delete process.argv[1];
|
|
896
|
+
AssertNS.deepStrictEqual( await defaultExport.AmEntryModule(), false );
|
|
897
|
+
// A `process.argv[1]` that can't be resolved rejects with a wrapped error.
|
|
898
|
+
process.argv[1] = PathNS.join( OSNS.tmpdir(), 'cno-files-test-no-such-entry-module.js' );
|
|
899
|
+
await AssertNS.rejects( defaultExport.AmEntryModule(), ( error ) => error.constructor === Error && error.cause != null );
|
|
900
|
+
} finally{
|
|
901
|
+
process.argv[1] = saved_argv1;
|
|
902
|
+
}
|
|
903
|
+
} );
|
|
904
|
+
|
|
905
|
+
/**## Helpers*/
|
|
906
|
+
/**
|
|
907
|
+
### findByName
|
|
908
|
+
> Returns the first node in the given serialised tree whose `name` matches the given name, or `undefined` if there is none.
|
|
909
|
+
|
|
910
|
+
#### Parametres
|
|
911
|
+
| name | type | description |
|
|
912
|
+
| --- | --- | --- |
|
|
913
|
+
| node | object | A serialised tree node. |
|
|
914
|
+
| name | string | The name of the node to find. |
|
|
915
|
+
|
|
916
|
+
#### Returns
|
|
917
|
+
| type | description |
|
|
918
|
+
| --- | --- |
|
|
919
|
+
| object\|undefined | The matching node, or `undefined`. |
|
|
920
|
+
|
|
921
|
+
#### History
|
|
922
|
+
| version | change |
|
|
923
|
+
| --- | --- |
|
|
924
|
+
| 0.1.1 | Introduced. |
|
|
925
|
+
*/
|
|
926
|
+
function findByName( node, name ){
|
|
927
|
+
if( node.name === name ){
|
|
928
|
+
return node;
|
|
929
|
+
}
|
|
930
|
+
for( const child of node.children ?? [] ){
|
|
931
|
+
var found = findByName( child, name );
|
|
932
|
+
if( found != null ){
|
|
933
|
+
return found;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
return undefined;
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
// cno-files.test.js EOF
|