diff --git a/scripts/ito.js b/scripts/ito.js index eec7a650f..507bc18e0 100755 --- a/scripts/ito.js +++ b/scripts/ito.js @@ -7,6 +7,7 @@ const path = require("path"); const { spawnSync } = require("child_process"); const { createSafeItoInvocationEnvironment, + getInvocationCommand, } = require("./lib/ito-environment"); const SUPPORTED_COMMANDS = Object.freeze(["auth", "find", "status", "evals"]); @@ -252,7 +253,7 @@ function buildInvocation(executable, args) { function invokeIto(executable, args, environment = process.env) { const invocation = buildInvocation(executable, args); - const command = args[0] === "--json" ? args[1] : args[0]; + const command = getInvocationCommand(args); const isNodeQualification = command === "evals"; const result = spawnSync(invocation.executable, invocation.args, { cwd: process.cwd(), diff --git a/scripts/lib/ito-environment.js b/scripts/lib/ito-environment.js index 93f6490f1..03a944119 100644 --- a/scripts/lib/ito-environment.js +++ b/scripts/lib/ito-environment.js @@ -80,12 +80,16 @@ function createSafeItoEnvironment(source = process.env, options = {}) { return Object.freeze(safe); } +function getInvocationCommand(args = []) { + return args.filter((value) => value !== "--json")[0]; +} + function createSafeItoInvocationEnvironment( source = process.env, args = [], options = {}, ) { - const command = args.filter((value) => value !== "--json")[0]; + const command = getInvocationCommand(args); return createSafeItoEnvironment(source, { includeControls: options.includeControls === true, includeItoRuntime: ITO_RUNTIME_COMMANDS.has(command), @@ -100,4 +104,5 @@ module.exports = Object.freeze({ SYSTEM_ENVIRONMENT_KEYS, createSafeItoEnvironment, createSafeItoInvocationEnvironment, + getInvocationCommand, }); diff --git a/tests/scripts/ito-cli-bridge.test.js b/tests/scripts/ito-cli-bridge.test.js index 7f52d469b..37e2ce1a0 100644 --- a/tests/scripts/ito-cli-bridge.test.js +++ b/tests/scripts/ito-cli-bridge.test.js @@ -20,6 +20,7 @@ const { } = require("../../scripts/ito"); const { createSafeItoInvocationEnvironment, + getInvocationCommand, } = require("../../scripts/lib/ito-environment"); function runCli(args, environment = {}) { @@ -323,6 +324,11 @@ function main() { assert.strictEqual(safe.ITO_API_KEY, undefined); assert.strictEqual(safe.SIXTYTWO_TOKEN, undefined); }], + ["detects the Itō command consistently with or without the global JSON flag", () => { + assert.strictEqual(getInvocationCommand(["auth"]), "auth"); + assert.strictEqual(getInvocationCommand(["--json", "evals"]), "evals"); + assert.strictEqual(getInvocationCommand([]), undefined); + }], ["bounds the outer node-qualification process beyond the canonical timeout", () => { assert.strictEqual(NODE_QUALIFICATION_TIMEOUT_MS, 31 * 60 * 1000); const source = fs.readFileSync(ITO_SCRIPT, "utf8");