-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
test: add test for Module._stat
#44713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
389ec8c
b135612
69a23d7
f40bbb2
1550841
c19ddb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
require('../common'); | ||
|
||
// This tests Module._stat. | ||
|
||
const Module = require('module'); | ||
const fs = require('fs'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const { ok, strictEqual } = require('assert'); | ||
const { join } = require('path'); | ||
|
||
const directory = join(tmpdir.path, 'directory'); | ||
const doesNotExist = join(tmpdir.path, 'does-not-exist'); | ||
const file = join(tmpdir.path, 'file.js'); | ||
|
||
tmpdir.refresh(); | ||
fs.writeFileSync(file, "module.exports = { a: 'b' }"); | ||
fs.mkdirSync(directory); | ||
|
||
strictEqual(Module._stat(directory), 1); // Returns 1 for directories. | ||
strictEqual(Module._stat(file), 0); // Returns 0 for files. | ||
ok(Module._stat(doesNotExist) < 0); // Returns a negative integer for any other kind of strings. | ||
|
||
// TODO(RaisinTen): Add tests that make sure that Module._stat() does not crash when called | ||
// with a non-string data type. It crashes currently. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
// This tests the creation of a vfs by monkey-patching fs and Module._stat. | ||
|
||
const Module = require('module'); | ||
const fs = require('fs'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const { deepStrictEqual, ok, strictEqual, throws } = require('assert'); | ||
const { join } = require('path'); | ||
|
||
const directory = join(tmpdir.path, 'directory'); | ||
const doesNotExist = join(tmpdir.path, 'does-not-exist'); | ||
const file = join(tmpdir.path, 'file.js'); | ||
|
||
tmpdir.refresh(); | ||
fs.writeFileSync(file, "module.exports = { a: 'b' }"); | ||
fs.mkdirSync(directory); | ||
|
||
strictEqual(Module._stat(directory), 1); | ||
ok(Module._stat(doesNotExist) < 0); | ||
strictEqual(Module._stat(file), 0); | ||
|
||
const vfsDirectory = join(process.execPath, 'directory'); | ||
const vfsDoesNotExist = join(process.execPath, 'does-not-exist'); | ||
const vfsFile = join(process.execPath, 'file.js'); | ||
|
||
ok(Module._stat(vfsDirectory) < 0); | ||
ok(Module._stat(vfsDoesNotExist) < 0); | ||
ok(Module._stat(vfsFile) < 0); | ||
|
||
deepStrictEqual(require(file), { a: 'b' }); | ||
throws(() => require(vfsFile), { code: 'MODULE_NOT_FOUND' }); | ||
|
||
common.expectWarning( | ||
'ExperimentalWarning', | ||
'Module._stat is an experimental feature. This feature could change at any time'); | ||
|
||
process.on('warning', common.mustCall()); | ||
|
||
const originalStat = Module._stat; | ||
Module._stat = function(filename) { | ||
RaisinTen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!filename.startsWith(process.execPath)) { | ||
return originalStat(filename); | ||
} | ||
|
||
if (filename === process.execPath) { | ||
return 1; | ||
} | ||
|
||
switch (filename) { | ||
case vfsDirectory: | ||
return 1; | ||
case vfsDoesNotExist: | ||
return -2; | ||
case vfsFile: | ||
return 0; | ||
} | ||
}; | ||
|
||
const originalReadFileSync = fs.readFileSync; | ||
// TODO(aduh95): We'd like to have a better way to achieve this without monkey-patching fs. | ||
fs.readFileSync = function readFileSync(pathArgument, options) { | ||
RaisinTen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!pathArgument.startsWith(process.execPath)) { | ||
return originalReadFileSync.apply(this, arguments); | ||
} | ||
if (pathArgument === vfsFile) { | ||
return "module.exports = { x: 'y' };"; | ||
} | ||
throw new Error(); | ||
}; | ||
|
||
fs.realpathSync = function realpathSync(pathArgument, options) { | ||
return pathArgument; | ||
}; | ||
Comment on lines
+63
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, that's not a use case we want to support, mutating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually how VFSs are implemented in the ecosystem (pkg, electron, etc.) currently and fixing that would break a lot of packages and I believe the intention behind exposing FWIW, we are also trying to find better ways of doing this without monkey-patching in nodejs/single-executable#37. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It shows that we probably also need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aduh95 if we start exposing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fwiw I personally have an expectation that Node.js should abide to its own It's probably never been discussed formally before though, and perhaps doing so would be a good thing (if only to get this use case formally recognized, supported, and covered by tests). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that the lack of consistency is quite bad. IMHO Node.js internals should not be affected by user-land actions, however I could see that we still want to support the use case of alternative |
||
|
||
strictEqual(Module._stat(directory), 1); | ||
ok(Module._stat(doesNotExist) < 0); | ||
strictEqual(Module._stat(file), 0); | ||
|
||
strictEqual(Module._stat(vfsDirectory), 1); | ||
ok(Module._stat(vfsDoesNotExist) < 0); | ||
strictEqual(Module._stat(vfsFile), 0); | ||
|
||
strictEqual(Module._stat(process.execPath), 1); | ||
|
||
deepStrictEqual(require(file), { a: 'b' }); | ||
deepStrictEqual(require(vfsFile), { x: 'y' }); |
Uh oh!
There was an error while loading. Please reload this page.