Skip to content

Commit 71a4b24

Browse files
addaleaxBridgeAR
authored andcommitted
test: remove usage of process.binding()
Prefer `internalBinding` or other equivalents over `process.binding()` (except in tests checking `process.binding()` itself). PR-URL: #26304 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 8c864de commit 71a4b24

12 files changed

+37
-34
lines changed

Diff for: test/abort/test-zlib-invalid-internals-usage.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ const os = require('os');
55
const cp = require('child_process');
66

77
if (process.argv[2] === 'child') {
8+
const { internalBinding } = require('internal/test/binding');
89
// This is the heart of the test.
9-
new (process.binding('zlib').Zlib)(0).init(1, 2, 3, 4, 5);
10+
new (internalBinding('zlib').Zlib)(0).init(1, 2, 3, 4, 5);
1011
} else {
11-
const child = cp.spawnSync(`${process.execPath}`, [`${__filename}`, 'child']);
12+
const child = cp.spawnSync(
13+
`${process.execPath}`, ['--expose-internals', `${__filename}`, 'child']);
1214

1315
assert.strictEqual(child.stdout.toString(), '');
1416
assert.ok(child.stderr.includes(

Diff for: test/async-hooks/test-zlib.zlib-binding.deflate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const hooks = initHooks();
1111
hooks.enable();
1212
const { internalBinding } = require('internal/test/binding');
1313
const { Zlib } = internalBinding('zlib');
14-
const constants = internalBinding('constants').zlib;
14+
const constants = require('zlib').constants;
1515

1616
const handle = new Zlib(constants.DEFLATE);
1717

Diff for: test/common/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ const os = require('os');
2929
const { exec, execSync, spawnSync } = require('child_process');
3030
const util = require('util');
3131
const tmpdir = require('./tmpdir');
32-
const {
33-
bits,
34-
hasIntl
35-
} = process.binding('config');
32+
const bits = ['arm64', 'mips', 'mipsel', 'ppc64', 's390x', 'x64']
33+
.includes(process.arch) ? 64 : 32;
34+
const hasIntl = !!process.config.variables.v8_enable_i18n_support;
3635
const { isMainThread } = require('worker_threads');
3736

3837
// Some tests assume a umask of 0o022 so set that up front. Tests that need a

Diff for: test/common/inspector-helper.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ class InspectorSession {
314314
}
315315

316316
class NodeInstance extends EventEmitter {
317-
constructor(inspectorFlags = ['--inspect-brk=0'],
317+
constructor(inspectorFlags = ['--inspect-brk=0', '--expose-internals'],
318318
scriptContents = '',
319319
scriptFile = _MAINSCRIPT) {
320320
super();
@@ -348,7 +348,8 @@ class NodeInstance extends EventEmitter {
348348

349349
static async startViaSignal(scriptContents) {
350350
const instance = new NodeInstance(
351-
[], `${scriptContents}\nprocess._rawDebug('started');`, undefined);
351+
['--expose-internals'],
352+
`${scriptContents}\nprocess._rawDebug('started');`, undefined);
352353
const msg = 'Timed out waiting for process to start';
353354
while (await fires(instance.nextStderrString(), msg, TIMEOUT) !==
354355
'started') {}

Diff for: test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import module from 'module';
22

3-
const builtins = new Set(
4-
Object.keys(process.binding('natives')).filter(str =>
5-
/^(?!(?:internal|node|v8)\/)/.test(str))
6-
);
7-
83
export function dynamicInstantiate(url) {
94
const builtinInstance = module._load(url.substr(5));
105
const builtinExports = ['default', ...Object.keys(builtinInstance)];
@@ -19,7 +14,7 @@ export function dynamicInstantiate(url) {
1914
}
2015

2116
export function resolve(specifier, base, defaultResolver) {
22-
if (builtins.has(specifier)) {
17+
if (module.builtinModules.includes(specifier)) {
2318
return {
2419
url: `node:${specifier}`,
2520
format: 'dynamic'

Diff for: test/fixtures/es-module-loaders/example-loader.mjs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import url from 'url';
22
import path from 'path';
33
import process from 'process';
4+
import { builtinModules } from 'module';
45

5-
const builtins = new Set(
6-
Object.keys(process.binding('natives')).filter((str) =>
7-
/^(?!(?:internal|node|v8)\/)/.test(str))
8-
);
96
const JS_EXTENSIONS = new Set(['.js', '.mjs']);
107

118
const baseURL = new url.URL('file://');
129
baseURL.pathname = process.cwd() + '/';
1310

1411
export function resolve(specifier, parentModuleURL = baseURL /*, defaultResolve */) {
15-
if (builtins.has(specifier)) {
12+
if (builtinModules.includes(specifier)) {
1613
return {
1714
url: specifier,
1815
format: 'builtin'

Diff for: test/fixtures/es-module-loaders/js-loader.mjs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { URL } from 'url';
2-
3-
const builtins = new Set(
4-
Object.keys(process.binding('natives')).filter(str =>
5-
/^(?!(?:internal|node|v8)\/)/.test(str))
6-
)
2+
import { builtinModules } from 'module';
73

84
const baseURL = new URL('file://');
95
baseURL.pathname = process.cwd() + '/';
106

117
export function resolve (specifier, base = baseURL) {
12-
if (builtins.has(specifier)) {
8+
if (builtinModules.includes(specifier)) {
139
return {
1410
url: specifier,
1511
format: 'builtin'

Diff for: test/parallel/test-trace-events-api.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
const common = require('../common');
55

6-
if (!process.binding('config').hasTracing)
6+
try {
7+
require('trace_events');
8+
} catch {
79
common.skip('missing trace events');
10+
}
11+
812
common.skipIfWorker(); // https://github.com/nodejs/node/issues/22767
913

1014
const assert = require('assert');

Diff for: test/parallel/test-trace-events-async-hooks-dynamic.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
'use strict';
22

33
const common = require('../common');
4-
if (!process.binding('config').hasTracing)
4+
try {
5+
require('trace_events');
6+
} catch {
57
common.skip('missing trace events');
8+
}
69

710
const assert = require('assert');
811
const cp = require('child_process');

Diff for: test/parallel/test-trace-events-async-hooks-worker.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
'use strict';
22

33
const common = require('../common');
4-
if (!process.binding('config').hasTracing)
4+
try {
5+
require('trace_events');
6+
} catch {
57
common.skip('missing trace events');
8+
}
69

710
const assert = require('assert');
811
const cp = require('child_process');

Diff for: test/sequential/test-inspector-async-call-stack-abort.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ if (process.argv[2] === 'child') {
1212
common.disableCrashOnUnhandledRejection();
1313
const { Session } = require('inspector');
1414
const { promisify } = require('util');
15-
const { registerAsyncHook } = process.binding('inspector');
15+
const { internalBinding } = require('internal/test/binding');
16+
const { registerAsyncHook } = internalBinding('inspector');
1617
(async () => {
1718
let enabled = 0;
1819
registerAsyncHook(() => ++enabled, () => {});
@@ -28,7 +29,8 @@ if (process.argv[2] === 'child') {
2829
} else {
2930
const { spawnSync } = require('child_process');
3031
const options = { encoding: 'utf8' };
31-
const proc = spawnSync(process.execPath, [__filename, 'child'], options);
32+
const proc = spawnSync(
33+
process.execPath, ['--expose-internals', __filename, 'child'], options);
3234
strictEqual(proc.status, 0);
3335
strictEqual(proc.signal, null);
3436
strictEqual(proc.stderr.includes(eyecatcher), true);

Diff for: test/sequential/test-inspector-async-hook-setup-at-signal.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ process._rawDebug('Waiting until a signal enables the inspector...');
1313
let waiting = setInterval(waitUntilDebugged, 50);
1414
1515
function waitUntilDebugged() {
16-
if (!process.binding('inspector').isEnabled()) return;
16+
const { internalBinding } = require('internal/test/binding');
17+
if (!internalBinding('inspector').isEnabled()) return;
1718
clearInterval(waiting);
1819
// At this point, even though the Inspector is enabled, the default async
1920
// call stack depth is 0. We need a chance to call
@@ -36,7 +37,7 @@ function setupTimeoutWithBreak() {
3637

3738
async function waitForInitialSetup(session) {
3839
console.error('[test]', 'Waiting for initial setup');
39-
await session.waitForBreakOnLine(15, '[eval]');
40+
await session.waitForBreakOnLine(16, '[eval]');
4041
}
4142

4243
async function setupTimeoutForStackTrace(session) {
@@ -50,7 +51,7 @@ async function setupTimeoutForStackTrace(session) {
5051

5152
async function checkAsyncStackTrace(session) {
5253
console.error('[test]', 'Verify basic properties of asyncStackTrace');
53-
const paused = await session.waitForBreakOnLine(22, '[eval]');
54+
const paused = await session.waitForBreakOnLine(23, '[eval]');
5455
assert(paused.params.asyncStackTrace,
5556
`${Object.keys(paused.params)} contains "asyncStackTrace" property`);
5657
assert(paused.params.asyncStackTrace.description, 'Timeout');

0 commit comments

Comments
 (0)