Skip to content

Commit ead4bb6

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 4875e88 commit ead4bb6

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Diff for: test/common/index.js

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

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

0 commit comments

Comments
 (0)