Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fabed9b

Browse filesBrowse files
RaisinTenRafaelGSS
authored andcommitted
test: add test for Module._stat
Module._stat landed in #44537 without a test, so this change adds one. Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: #44713 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 94b7f53 commit fabed9b
Copy full SHA for fabed9b

File tree

Expand file treeCollapse file tree

2 files changed

+113
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+113
-0
lines changed
Open diff view settings
Collapse file

‎test/parallel/test-module-stat.js‎

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
require('../common');
3+
4+
// This tests Module._stat.
5+
6+
const Module = require('module');
7+
const fs = require('fs');
8+
const tmpdir = require('../common/tmpdir');
9+
const { ok, strictEqual } = 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); // Returns 1 for directories.
21+
strictEqual(Module._stat(file), 0); // Returns 0 for files.
22+
ok(Module._stat(doesNotExist) < 0); // Returns a negative integer for any other kind of strings.
23+
24+
// TODO(RaisinTen): Add tests that make sure that Module._stat() does not crash when called
25+
// with a non-string data type. It crashes currently.
Collapse file

‎test/parallel/test-vfs.js‎

Copy file name to clipboard
+88Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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, 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+
common.expectWarning(
36+
'ExperimentalWarning',
37+
'Module._stat is an experimental feature. This feature could change at any time');
38+
39+
process.on('warning', common.mustCall());
40+
41+
const originalStat = Module._stat;
42+
Module._stat = function(filename) {
43+
if (!filename.startsWith(process.execPath)) {
44+
return originalStat(filename);
45+
}
46+
47+
if (filename === process.execPath) {
48+
return 1;
49+
}
50+
51+
switch (filename) {
52+
case vfsDirectory:
53+
return 1;
54+
case vfsDoesNotExist:
55+
return -2;
56+
case vfsFile:
57+
return 0;
58+
}
59+
};
60+
61+
const originalReadFileSync = fs.readFileSync;
62+
// TODO(aduh95): We'd like to have a better way to achieve this without monkey-patching fs.
63+
fs.readFileSync = function readFileSync(pathArgument, options) {
64+
if (!pathArgument.startsWith(process.execPath)) {
65+
return originalReadFileSync.apply(this, arguments);
66+
}
67+
if (pathArgument === vfsFile) {
68+
return "module.exports = { x: 'y' };";
69+
}
70+
throw new Error();
71+
};
72+
73+
fs.realpathSync = function realpathSync(pathArgument, options) {
74+
return pathArgument;
75+
};
76+
77+
strictEqual(Module._stat(directory), 1);
78+
ok(Module._stat(doesNotExist) < 0);
79+
strictEqual(Module._stat(file), 0);
80+
81+
strictEqual(Module._stat(vfsDirectory), 1);
82+
ok(Module._stat(vfsDoesNotExist) < 0);
83+
strictEqual(Module._stat(vfsFile), 0);
84+
85+
strictEqual(Module._stat(process.execPath), 1);
86+
87+
deepStrictEqual(require(file), { a: 'b' });
88+
deepStrictEqual(require(vfsFile), { x: 'y' });

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.