From 389ec8cf33f4f9d893528b091d2d7ae083de5577 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 18 Sep 2022 17:57:34 +0530 Subject: [PATCH 1/6] test: add test for Module._stat Module._stat landed in https://github.com/nodejs/node/pull/44537 without a test, so this change adds one. Signed-off-by: Darshan Sen --- test/parallel/test-vfs.js | 85 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 test/parallel/test-vfs.js diff --git a/test/parallel/test-vfs.js b/test/parallel/test-vfs.js new file mode 100644 index 00000000000000..ab1ac006a0a0ab --- /dev/null +++ b/test/parallel/test-vfs.js @@ -0,0 +1,85 @@ +'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, match, 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' }); + +process.on('warning', common.mustCall((warning) => { + match(warning.message, /is an experimental feature/); +}, 1)); + +const originalStat = Module._stat; +Module._stat = function(filename) { + 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; +fs.readFileSync = function readFileSync(pathArgument, options) { + 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; +}; + +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' }); From b135612a2643244c1f15c91f83380146a3dc681d Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Mon, 19 Sep 2022 13:09:27 +0530 Subject: [PATCH 2/6] test: use common.expectWarning() Signed-off-by: Darshan Sen --- test/parallel/test-vfs.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-vfs.js b/test/parallel/test-vfs.js index ab1ac006a0a0ab..37ae3232335849 100644 --- a/test/parallel/test-vfs.js +++ b/test/parallel/test-vfs.js @@ -6,7 +6,7 @@ const common = require('../common'); const Module = require('module'); const fs = require('fs'); const tmpdir = require('../common/tmpdir'); -const { deepStrictEqual, match, ok, strictEqual, throws } = require('assert'); +const { deepStrictEqual, ok, strictEqual, throws } = require('assert'); const { join } = require('path'); const directory = join(tmpdir.path, 'directory'); @@ -32,9 +32,11 @@ ok(Module._stat(vfsFile) < 0); deepStrictEqual(require(file), { a: 'b' }); throws(() => require(vfsFile), { code: 'MODULE_NOT_FOUND' }); -process.on('warning', common.mustCall((warning) => { - match(warning.message, /is an experimental feature/); -}, 1)); +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) { From 69a23d7cb91539641ce0dc1d8782b6d9110128f4 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 22 Oct 2022 19:28:21 +0530 Subject: [PATCH 3/6] fixup! test: add comment --- test/parallel/test-vfs.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/parallel/test-vfs.js b/test/parallel/test-vfs.js index 37ae3232335849..3a33a4c8cdf5c1 100644 --- a/test/parallel/test-vfs.js +++ b/test/parallel/test-vfs.js @@ -39,6 +39,7 @@ common.expectWarning( process.on('warning', common.mustCall()); const originalStat = Module._stat; +TODO(aduh95): We'd like to have a better way to achieve this without monkey-patching. Module._stat = function(filename) { if (!filename.startsWith(process.execPath)) { return originalStat(filename); From f40bbb2fb203ffba6d1b24afa3cdeebafede72f9 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 22 Oct 2022 19:29:55 +0530 Subject: [PATCH 4/6] fixup! test: add comment --- test/parallel/test-vfs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-vfs.js b/test/parallel/test-vfs.js index 3a33a4c8cdf5c1..0140d9b4050f7d 100644 --- a/test/parallel/test-vfs.js +++ b/test/parallel/test-vfs.js @@ -39,7 +39,7 @@ common.expectWarning( process.on('warning', common.mustCall()); const originalStat = Module._stat; -TODO(aduh95): We'd like to have a better way to achieve this without monkey-patching. +// TODO(aduh95): We'd like to have a better way to achieve this without monkey-patching. Module._stat = function(filename) { if (!filename.startsWith(process.execPath)) { return originalStat(filename); From 15508413af97712ac155bb695ece133f0c7fda23 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 22 Oct 2022 19:51:15 +0530 Subject: [PATCH 5/6] fixup! test: add comment --- test/parallel/test-vfs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-vfs.js b/test/parallel/test-vfs.js index 0140d9b4050f7d..c4842be3a839d0 100644 --- a/test/parallel/test-vfs.js +++ b/test/parallel/test-vfs.js @@ -39,7 +39,6 @@ common.expectWarning( process.on('warning', common.mustCall()); const originalStat = Module._stat; -// TODO(aduh95): We'd like to have a better way to achieve this without monkey-patching. Module._stat = function(filename) { if (!filename.startsWith(process.execPath)) { return originalStat(filename); @@ -60,6 +59,7 @@ Module._stat = function(filename) { }; 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) { if (!pathArgument.startsWith(process.execPath)) { return originalReadFileSync.apply(this, arguments); From c19ddb992506ea34d4f8680965ba117c9a1a0aac Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 23 Oct 2022 10:57:54 +0530 Subject: [PATCH 6/6] fixup! test: add a more isolated Module._stat test Signed-off-by: Darshan Sen --- test/parallel/test-module-stat.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test/parallel/test-module-stat.js diff --git a/test/parallel/test-module-stat.js b/test/parallel/test-module-stat.js new file mode 100644 index 00000000000000..eaaeb898afd603 --- /dev/null +++ b/test/parallel/test-module-stat.js @@ -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.