Skip to content

Commit

Permalink
module: block requiring test/reporters without scheme
Browse files Browse the repository at this point in the history
PR-URL: #47831
Fixes: #47828
Reviewed-By: Filip Skokan <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Jacob Smith <[email protected]>
  • Loading branch information
MoLow authored and danielleadams committed Jul 6, 2023
1 parent 45e2a2f commit 8afdc5b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/internal/bootstrap/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ const legacyWrapperList = new SafeSet([
// Modules that can only be imported via the node: scheme.
const schemelessBlockList = new SafeSet([
'test',
'test/reporters',
]);

// Set up process.binding() and process._linkedBinding().
Expand Down
51 changes: 33 additions & 18 deletions test/parallel/test-runner-import-no-scheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 8afdc5b

Please sign in to comment.