summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMoshe Atlow <moshe@atlow.co.il>2023-05-05 10:59:25 +0300
committerGitHub <noreply@github.com>2023-05-05 07:59:25 +0000
commitd55b84ba34ff891976bb7535f92b6136a694fe64 (patch)
tree5a7ad901798049f9b18e87aafb5fbb78f4c94a60
parentaf9b48a2f17b11b66a8b81beeaba3c408b863795 (diff)
downloadnode-new-d55b84ba34ff891976bb7535f92b6136a694fe64.tar.gz
module: block requiring `test/reporters` without scheme
PR-URL: https://github.com/nodejs/node/pull/47831 Fixes: https://github.com/nodejs/node/issues/47828 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Michaƫl Zasso <targos@protonmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
-rw-r--r--lib/internal/bootstrap/realm.js1
-rw-r--r--test/parallel/test-runner-import-no-scheme.js51
2 files changed, 34 insertions, 18 deletions
diff --git a/lib/internal/bootstrap/realm.js b/lib/internal/bootstrap/realm.js
index 9dc0aee751..8e35213234 100644
--- a/lib/internal/bootstrap/realm.js
+++ b/lib/internal/bootstrap/realm.js
@@ -132,6 +132,7 @@ const legacyWrapperList = new SafeSet([
// Modules that can only be imported via the node: scheme.
const schemelessBlockList = new SafeSet([
'test',
+ 'test/reporters',
]);
// Modules that will only be enabled at run time.
const experimentalModuleList = new SafeSet();
diff --git a/test/parallel/test-runner-import-no-scheme.js b/test/parallel/test-runner-import-no-scheme.js
index 45dd83d025..85953af77b 100644
--- a/test/parallel/test-runner-import-no-scheme.js
+++ b/test/parallel/test-runner-import-no-scheme.js
@@ -7,45 +7,60 @@ const fs = require('fs');
const path = require('path');
const { createRequire } = require('module');
-assert.throws(
- () => require('test'),
- common.expectsError({ code: 'MODULE_NOT_FOUND' }),
-);
-
-(async () => {
- await assert.rejects(
- async () => import('test'),
- common.expectsError({ code: 'ERR_MODULE_NOT_FOUND' }),
+for (const name in ['test', 'test/reporters']) {
+ assert.throws(
+ () => require(name),
+ common.expectsError({ code: 'MODULE_NOT_FOUND' }),
);
-})().then(common.mustCall());
-assert.throws(
- () => require.resolve('test'),
- common.expectsError({ code: 'MODULE_NOT_FOUND' }),
-);
+ (async () => {
+ await assert.rejects(
+ async () => import(name),
+ common.expectsError({ code: 'ERR_MODULE_NOT_FOUND' }),
+ );
+ })().then(common.mustCall());
+
+ assert.throws(
+ () => require.resolve(name),
+ common.expectsError({ code: 'MODULE_NOT_FOUND' }),
+ );
+}
// Verify that files in node_modules can be resolved.
tmpdir.refresh();
const packageRoot = path.join(tmpdir.path, 'node_modules', 'test');
+const reportersDir = path.join(tmpdir.path, 'node_modules', 'test', 'reporters');
const indexFile = path.join(packageRoot, 'index.js');
+const reportersIndexFile = path.join(reportersDir, 'index.js');
-fs.mkdirSync(packageRoot, { recursive: true });
+fs.mkdirSync(reportersDir, { recursive: true });
fs.writeFileSync(indexFile, 'module.exports = { marker: 1 };');
+fs.writeFileSync(reportersIndexFile, 'module.exports = { marker: 1 };');
-function test(argv) {
+function test(argv, expectedToFail = false) {
const child = spawnSync(process.execPath, argv, { cwd: tmpdir.path });
- assert.strictEqual(child.status, 0);
- assert.strictEqual(child.stdout.toString().trim(), '{ marker: 1 }');
+ if (expectedToFail) {
+ assert.strictEqual(child.status, 1);
+ assert.strictEqual(child.stdout.toString().trim(), '');
+ } else {
+ assert.strictEqual(child.status, 0);
+ assert.strictEqual(child.stdout.toString().trim(), '{ marker: 1 }');
+ }
}
test(['-e', 'console.log(require("test"))']);
+test(['-e', 'console.log(require("test/reporters"))']);
test(['-e', 'import("test").then(m=>console.log(m.default))']);
+test(['-e', 'import("test/reporters").then(m=>console.log(m.default))'], true);
test(['--input-type=module', '-e', 'import test from "test";console.log(test)']);
+test(['--input-type=module', '-e', 'import test from "test/reporters";console.log(test)'], true);
test(['--input-type=module', '-e', 'console.log((await import("test")).default)']);
+test(['--input-type=module', '-e', 'console.log((await import("test/reporters")).default)'], true);
{
const dummyFile = path.join(tmpdir.path, 'file.js');
const require = createRequire(dummyFile);
assert.strictEqual(require.resolve('test'), indexFile);
+ assert.strictEqual(require.resolve('test/reporters'), reportersIndexFile);
}