Skip to content

Commit 8279826

Browse files
BridgeARMylesBorins
authored andcommitted
test: verify input flags
This makes sure all required flags are passed through to the test. If that's not the case an error is thrown to inform the user what flag is missing. PR-URL: #24876 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 155d1d5 commit 8279826

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

test/common/index.js

+38
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,44 @@ const isMainThread = (() => {
4545
}
4646
})();
4747

48+
// Check for flags. Skip this for workers (both, the `cluster` module and
49+
// `worker_threads`) and child processes.
50+
if (process.argv.length === 2 &&
51+
isMainThread &&
52+
module.parent &&
53+
require('cluster').isMaster) {
54+
// The copyright notice is relatively big and the flags could come afterwards.
55+
const bytesToRead = 1500;
56+
const buffer = Buffer.allocUnsafe(bytesToRead);
57+
const fd = fs.openSync(module.parent.filename, 'r');
58+
fs.readSync(fd, buffer, 0, bytesToRead);
59+
fs.closeSync(fd);
60+
const source = buffer.toString();
61+
62+
const flagStart = source.indexOf('// Flags: --') + 10;
63+
if (flagStart !== 9) {
64+
let flagEnd = source.indexOf('\n', flagStart);
65+
// Normalize different EOL.
66+
if (source[flagEnd - 1] === '\r') {
67+
flagEnd--;
68+
}
69+
const flags = source
70+
.substring(flagStart, flagEnd)
71+
.replace(/_/g, '-')
72+
.split(' ');
73+
const args = process.execArgv.map((arg) => arg.replace(/_/g, '-'));
74+
for (const flag of flags) {
75+
if (!args.includes(flag) &&
76+
// If the binary is build without `intl` the inspect option is
77+
// invalid. The test itself should handle this case.
78+
(process.config.variables.v8_enable_inspector !== 0 ||
79+
!flag.startsWith('--inspect'))) {
80+
throw new Error(`Test has to be started with the flag: '${flag}'`);
81+
}
82+
}
83+
}
84+
}
85+
4886
const isWindows = process.platform === 'win32';
4987
const isAIX = process.platform === 'aix';
5088
const isLinuxPPCBE = (process.platform === 'linux') &&

0 commit comments

Comments
 (0)