diff --git a/src/models/config.test.ts b/src/models/config.test.ts index ab3abd5..b812b2e 100644 --- a/src/models/config.test.ts +++ b/src/models/config.test.ts @@ -92,3 +92,36 @@ test('Should have empty args if not set', () => { expect(actual.taskName).toBe(expectedName); expect(actual.jobId).toBe(expectedId); }); + +test('Should gracefully handle runCmd with spaces', () => { + const originalCmd = 'node command subcommand'; + const expectedCmd = 'node'; + const expectedArgs = ['command', 'subcommand']; + + process.env.RUN_BASE_DIR = '/dir'; + process.env.RUN_CMD = originalCmd; + process.env.SWARMER_ADDRESS = 'swarmer'; + process.env.TASK_NAME = 'task'; + process.env.SWARMER_JOB_ID = 'abc123'; + + const actual = RunnerConfig.fromEnvironment(); + expect(actual.runCmd).toBe(expectedCmd); + expect(actual.runArgs).toEqual(expectedArgs); +}); + +test('Should prepend elements after first in runCmd with spaces to args', () => { + const originalCmd = 'node command subcommand'; + const expectedCmd = 'node'; + const expectedArgs = ['command', 'subcommand', 'argOne', '--something', 'value']; + + process.env.RUN_BASE_DIR = '/dir'; + process.env.RUN_CMD = originalCmd; + process.env.SWARMER_ADDRESS = 'swarmer'; + process.env.RUN_ARGS = 'argOne,--something,value'; + process.env.TASK_NAME = 'task'; + process.env.SWARMER_JOB_ID = 'abc123'; + + const actual = RunnerConfig.fromEnvironment(); + expect(actual.runCmd).toBe(expectedCmd); + expect(actual.runArgs).toEqual(expectedArgs); +}); diff --git a/src/models/config.ts b/src/models/config.ts index 100afa0..86c08ef 100644 --- a/src/models/config.ts +++ b/src/models/config.ts @@ -58,8 +58,8 @@ export class RunnerConfig { * @throws An error if any of the listed variables except for `RUN_ARGS` are missing */ public static fromEnvironment() { + let runCmd = process.env.RUN_CMD; const runBase = process.env.RUN_BASE_DIR; - const runCmd = process.env.RUN_CMD; const swarmerAddr = process.env.SWARMER_ADDRESS; const taskName = process.env.TASK_NAME; const swarmerJobId = process.env.SWARMER_JOB_ID; @@ -85,8 +85,17 @@ export class RunnerConfig { } const envArgs = process.env.RUN_ARGS; + const argsArr = []; - const argsArr = envArgs ? envArgs.split(',').map((arg) => arg.trim()) : []; + if (/\s+/g.test(runCmd)) { + const parts = runCmd.match(/\S+/g) || [runCmd]; + runCmd = parts.shift() || runCmd; + argsArr.push(...parts); + } + + if (envArgs) { + argsArr.push(...envArgs.split(',').map((arg) => arg.trim())); + } return new RunnerConfig(runBase, runCmd,