node/test/parallel/test-internal-module-require.js
James M Snell 8caa1dcee6 test: rely less on duplicative common test harness utilities
There are several cleanups here that are not just style nits...

1. The `common.isMainThread` was just a passthrough to the
   `isMainThread` export on the worker_thread module. It's
   use was inconsistent and just obfuscated the fact that
   the test file depend on the `worker_threads` built-in.
   By eliminating it we simplify the test harness a bit and
   make it clearer which tests depend on the worker_threads
   check.
2. The `common.isDumbTerminal` is fairly unnecesary since
   that just wraps a public API check.
3. Several of the `common.skipIf....` checks were inconsistently
   used and really don't need to be separate utility functions.

A key part of the motivation here is to work towards making more
of the tests more self-contained and less reliant on the common
test harness where possible.

PR-URL: https://github.com/nodejs/node/pull/56712
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-01-25 07:23:09 +00:00

117 lines
2.6 KiB
JavaScript

'use strict';
// Flags: --expose-internals
// This verifies that
// 1. We do not leak internal modules unless the --require-internals option
// is on.
// 2. We do not accidentally leak any modules to the public global scope.
// 3. Deprecated modules are properly deprecated.
const common = require('../common');
const { isMainThread } = require('worker_threads');
if (!isMainThread) {
common.skip('Cannot test the existence of --expose-internals from worker');
}
const assert = require('assert');
const fork = require('child_process').fork;
const expectedPublicModules = new Set([
'_http_agent',
'_http_client',
'_http_common',
'_http_incoming',
'_http_outgoing',
'_http_server',
'_stream_duplex',
'_stream_passthrough',
'_stream_readable',
'_stream_transform',
'_stream_wrap',
'_stream_writable',
'_tls_common',
'_tls_wrap',
'assert',
'async_hooks',
'buffer',
'child_process',
'cluster',
'console',
'constants',
'crypto',
'dgram',
'dns',
'domain',
'events',
'fs',
'http',
'http2',
'https',
'inspector',
'module',
'net',
'os',
'path',
'perf_hooks',
'process',
'punycode',
'querystring',
'readline',
'repl',
'stream',
'string_decoder',
'sys',
'timers',
'tls',
'trace_events',
'tty',
'url',
'util',
'v8',
'vm',
'worker_threads',
'zlib',
]);
if (process.argv[2] === 'child') {
assert(!process.execArgv.includes('--expose-internals'));
process.once('message', ({ allBuiltins }) => {
const publicModules = new Set();
for (const id of allBuiltins) {
if (id.startsWith('internal/')) {
assert.throws(() => {
require(id);
}, {
code: 'MODULE_NOT_FOUND',
message: `Cannot find module '${id}'`
});
} else {
require(id);
if (!id.startsWith('node:')) {
require(`node:${id}`);
}
publicModules.add(id);
}
}
assert(allBuiltins.length > publicModules.size);
// Make sure all the public modules are available through
// require('module').builtinModules
assert.deepStrictEqual(
publicModules,
new Set(require('module').builtinModules)
);
assert.deepStrictEqual(publicModules, expectedPublicModules);
});
} else {
assert(process.execArgv.includes('--expose-internals'));
const child = fork(__filename, ['child'], {
execArgv: []
});
const { builtinModules } = require('module');
// When --expose-internals is on, require('module').builtinModules
// contains internal modules.
const message = { allBuiltins: builtinModules };
child.send(message);
}