Skip to content

Commit fabed9b

Browse 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

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

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

Lines 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+
constModule=require('module');
7+
constfs=require('fs');
8+
consttmpdir=require('../common/tmpdir');
9+
const{ ok, strictEqual }=require('assert');
10+
const{ join }=require('path');
11+
12+
constdirectory=join(tmpdir.path,'directory');
13+
constdoesNotExist=join(tmpdir.path,'does-not-exist');
14+
constfile=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.

‎test/parallel/test-vfs.js‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
4+
// This tests the creation of a vfs by monkey-patching fs and Module._stat.
5+
6+
constModule=require('module');
7+
constfs=require('fs');
8+
consttmpdir=require('../common/tmpdir');
9+
const{ deepStrictEqual, ok, strictEqual, throws }=require('assert');
10+
const{ join }=require('path');
11+
12+
constdirectory=join(tmpdir.path,'directory');
13+
constdoesNotExist=join(tmpdir.path,'does-not-exist');
14+
constfile=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+
constvfsDirectory=join(process.execPath,'directory');
25+
constvfsDoesNotExist=join(process.execPath,'does-not-exist');
26+
constvfsFile=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+
constoriginalStat=Module._stat;
42+
Module._stat=function(filename){
43+
if(!filename.startsWith(process.execPath)){
44+
returnoriginalStat(filename);
45+
}
46+
47+
if(filename===process.execPath){
48+
return1;
49+
}
50+
51+
switch(filename){
52+
casevfsDirectory:
53+
return1;
54+
casevfsDoesNotExist:
55+
return-2;
56+
casevfsFile:
57+
return0;
58+
}
59+
};
60+
61+
constoriginalReadFileSync=fs.readFileSync;
62+
// TODO(aduh95): We'd like to have a better way to achieve this without monkey-patching fs.
63+
fs.readFileSync=functionreadFileSync(pathArgument,options){
64+
if(!pathArgument.startsWith(process.execPath)){
65+
returnoriginalReadFileSync.apply(this,arguments);
66+
}
67+
if(pathArgument===vfsFile){
68+
return"module.exports = { x: 'y' };";
69+
}
70+
thrownewError();
71+
};
72+
73+
fs.realpathSync=functionrealpathSync(pathArgument,options){
74+
returnpathArgument;
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)