Skip to content

Commit b814716

Browse files
committed
test: add test for Module._stat
Module._stat landed in nodejs#44537 without a test, so this change adds one. Signed-off-by: Darshan Sen <[email protected]>
1 parent bf47abe commit b814716

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

test/parallel/test-vfs.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// This tests the creation of a vfs by monkey-patching fs and Module._stat.
5+
6+
const Module = require('module');
7+
const fs = require('fs');
8+
const tmpdir = require('../common/tmpdir');
9+
const { deepStrictEqual, match, ok, strictEqual, throws } = require('assert');
10+
const { join } = require('path');
11+
12+
const directory = join(tmpdir.path, 'directory');
13+
const doesNotExist = join(tmpdir.path, 'does-not-exist');
14+
const file = join(tmpdir.path, 'file.js');
15+
16+
tmpdir.refresh();
17+
fs.writeFileSync(file, "module.exports = { a: 'b' }");
18+
fs.mkdirSync(directory);
19+
20+
strictEqual(Module._stat(directory), 1);
21+
ok(Module._stat(doesNotExist) < 0);
22+
strictEqual(Module._stat(file), 0);
23+
24+
const vfsDirectory = join(process.execPath, 'directory');
25+
const vfsDoesNotExist = join(process.execPath, 'does-not-exist');
26+
const vfsFile = join(process.execPath, 'file.js');
27+
28+
ok(Module._stat(vfsDirectory) < 0);
29+
ok(Module._stat(vfsDoesNotExist) < 0);
30+
ok(Module._stat(vfsFile) < 0);
31+
32+
deepStrictEqual(require(file), { a: 'b' });
33+
throws(() => require(vfsFile), { code: 'MODULE_NOT_FOUND' });
34+
35+
process.on('warning', common.mustCall((warning) => {
36+
match(warning.message, /is an experimental feature/);
37+
}, 1));
38+
39+
const originalStat = Module._stat;
40+
Module._stat = function(filename) {
41+
if (!filename.startsWith(process.execPath)) {
42+
return originalStat(filename);
43+
}
44+
45+
if (filename === process.execPath) {
46+
return 1;
47+
}
48+
49+
switch (filename) {
50+
case vfsDirectory:
51+
return 1;
52+
case vfsDoesNotExist:
53+
return -2;
54+
case vfsFile:
55+
return 0;
56+
}
57+
};
58+
59+
const originalReadFileSync = fs.readFileSync;
60+
fs.readFileSync = function readFileSync(pathArgument, options) {
61+
if (!pathArgument.startsWith(process.execPath)) {
62+
return originalReadFileSync.apply(this, arguments);
63+
}
64+
if (pathArgument === vfsFile) {
65+
return "module.exports = { x: 'y' };";
66+
}
67+
throw new Error();
68+
};
69+
70+
fs.realpathSync = function realpathSync(pathArgument, options) {
71+
return pathArgument;
72+
};
73+
74+
strictEqual(Module._stat(directory), 1);
75+
ok(Module._stat(doesNotExist) < 0);
76+
strictEqual(Module._stat(file), 0);
77+
78+
strictEqual(Module._stat(vfsDirectory), 1);
79+
ok(Module._stat(vfsDoesNotExist) < 0);
80+
strictEqual(Module._stat(vfsFile), 0);
81+
82+
strictEqual(Module._stat(process.execPath), 1);
83+
84+
deepStrictEqual(require(file), { a: 'b' });
85+
deepStrictEqual(require(vfsFile), { x: 'y' });

0 commit comments

Comments
 (0)