From 8279826ce6c6690d655db493c84f936fd34cfa94 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 6 Dec 2018 16:52:33 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/24876 Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott --- test/common/index.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/common/index.js b/test/common/index.js index aea33b4c7f078d..d71af30f10a2bd 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -45,6 +45,44 @@ const isMainThread = (() => { } })(); +// Check for flags. Skip this for workers (both, the `cluster` module and +// `worker_threads`) and child processes. +if (process.argv.length === 2 && + isMainThread && + module.parent && + require('cluster').isMaster) { + // The copyright notice is relatively big and the flags could come afterwards. + const bytesToRead = 1500; + const buffer = Buffer.allocUnsafe(bytesToRead); + const fd = fs.openSync(module.parent.filename, 'r'); + fs.readSync(fd, buffer, 0, bytesToRead); + fs.closeSync(fd); + const source = buffer.toString(); + + const flagStart = source.indexOf('// Flags: --') + 10; + if (flagStart !== 9) { + let flagEnd = source.indexOf('\n', flagStart); + // Normalize different EOL. + if (source[flagEnd - 1] === '\r') { + flagEnd--; + } + const flags = source + .substring(flagStart, flagEnd) + .replace(/_/g, '-') + .split(' '); + const args = process.execArgv.map((arg) => arg.replace(/_/g, '-')); + for (const flag of flags) { + if (!args.includes(flag) && + // If the binary is build without `intl` the inspect option is + // invalid. The test itself should handle this case. + (process.config.variables.v8_enable_inspector !== 0 || + !flag.startsWith('--inspect'))) { + throw new Error(`Test has to be started with the flag: '${flag}'`); + } + } + } +} + const isWindows = process.platform === 'win32'; const isAIX = process.platform === 'aix'; const isLinuxPPCBE = (process.platform === 'linux') &&