diff --git a/src/utils/execution-runtimes/withMacHeavyTestLane.ts b/src/utils/execution-runtimes/withMacHeavyTestLane.ts index f303a00f36c..409b1a11035 100644 --- a/src/utils/execution-runtimes/withMacHeavyTestLane.ts +++ b/src/utils/execution-runtimes/withMacHeavyTestLane.ts @@ -21,20 +21,42 @@ const lockBusy = 75; // The runner argv and shell scripts used by Harness package commands. const heavyRunner = /\bplaywright\s+test\b|\btest:(?:e2e|native|package|all)\b/; const vitestE2e = /\bvitest\s+run\b[^;\n&|]*\.e2e\.test\./; -const shellExclusion = /(^|\s)--exclude(?:=|[ \t]+)(?:"[^"\n]*"|'[^'\n]*'|[^\s;&|]+)/g; +const shellToken = /(?:\\[\s\S]|"(?:\\[\s\S]|[^"\\])*"|'[^']*'|[^\s"'\\;&|])+|[\n;&|]/g; +const shellQuotes = /\\([\s\S])|"((?:\\[\s\S]|[^"\\])*)"|'([^']*)'/g; +const shellContinuation = '\\\n'; export function isHeavyTestCommand(args: readonly string[]): boolean { if (args[0] === 'bun' && args[1] === 'scripts/run-checks.ts') return true; - if ((args[0] === 'sh' || args[0] === '/bin/sh') && args[1] === '-c') { - const script = args[2] ?? ''; - return heavyRunner.test(script) || vitestE2e.test(script.replace(shellExclusion, '$1')); - } - if (heavyRunner.test(args.join(' '))) return true; - const filters = args.filter( - (arg, index) => - arg !== '--exclude' && !arg.startsWith('--exclude=') && args[index - 1] !== '--exclude' - ); - return vitestE2e.test(filters.join(' ')); + const shell = (args[0] === 'sh' || args[0] === '/bin/sh') && args[1] === '-c'; + const words = shell ? shellCommandWords(args[2] ?? '') : args; + const command = words + .filter( + (word, index) => + word !== '--exclude' && !word.startsWith('--exclude=') && words[index - 1] !== '--exclude' + ) + .join(' '); + return heavyRunner.test(command) || vitestE2e.test(command); +} + +// Normalize static shell words without evaluating expansions or dropping command separators. +function shellCommandWords(script: string): string[] { + return (script.match(shellToken) ?? []) + .filter((word) => !/^(?:\\\n)+$/.test(word)) + .map((word) => + word.replace( + shellQuotes, + (match, escaped, doubleQuoted: string | undefined, singleQuoted) => { + if (escaped !== undefined) return escaped === '\n' ? '' : escaped; + return ( + doubleQuoted?.replace(/\\[\s\S]/g, (pair) => + pair === shellContinuation ? '' : pair + ) ?? + singleQuoted ?? + match + ); + } + ) + ); } /** Apply the same lane budget across worktrees and resource homes for the OS account. */ diff --git a/test/utils/execution-runtimes/with-mac-heavy-test-lane.test.ts b/test/utils/execution-runtimes/with-mac-heavy-test-lane.test.ts index 212d4139ad8..b633f6e013e 100644 --- a/test/utils/execution-runtimes/with-mac-heavy-test-lane.test.ts +++ b/test/utils/execution-runtimes/with-mac-heavy-test-lane.test.ts @@ -16,6 +16,8 @@ test('recognizes the standard suite commands without queuing routine type checks ['sh', '-c', 'bun run --cwd packages/hub check && bun run --cwd packages/hub test:e2e'], ['vitest', 'run', 'test/resources-cli.e2e.test.ts'], ['/bin/sh', '-c', "vitest run --exclude='test/desktop e2e/*.e2e.test.ts' test/cli.e2e.test.ts"], + ['sh', '-c', "vitest run '--exclude=*.e2e.test.ts' 'test/cli.e2e.test.ts'"], + ['sh', '-c', 'vitest run --exclude "*.e2e.test.ts" test/queue.test.ts\nbun run test:e2e'], ['vitest', 'run', '--exclude', 'test/**/*.e2e.test.ts', 'test/cli.e2e.test.ts'] ]; expect(commands.every(isHeavyTestCommand)).toBe(true); @@ -25,7 +27,24 @@ test('recognizes the standard suite commands without queuing routine type checks ['/bin/sh', '-c', 'vitest run --exclude "test/**/*.e2e.test.ts" test/queue.test.ts'], ['sh', '-c', "vitest run --exclude='test/desktop e2e/*.e2e.test.ts' test/queue.test.ts"], ['vitest', 'run', '--exclude', 'test/desktop e2e/*.e2e.test.ts', 'test/queue.test.ts'], - ['vitest', 'run', '--exclude=test/**/*.e2e.test.ts', 'test/queue.test.ts'] + ['vitest', 'run', '--exclude=test/**/*.e2e.test.ts', 'test/queue.test.ts'], + ['vitest', 'run', '--exclude', 'test/fixtures/test:e2e/*.test.ts', 'test/queue.test.ts'], + ['vitest', 'run', '--exclude=test/fixtures/test:e2e/*.test.ts', 'test/queue.test.ts'], + ['sh', '-c', 'vitest run --exclude "test/fixtures/test:e2e/*.test.ts" test/queue.test.ts'], + ['/bin/sh', '-c', "vitest run --exclude='test/fixtures/test:e2e/*.test.ts' test/queue.test.ts"], + ['sh', '-c', 'vitest run "--exclude=**/*.e2e.test.ts" test/queue.test.ts'], + [ + '/bin/sh', + '-c', + "vitest run '--exclude' 'test/fixtures/test:e2e/*.test.ts' test/queue.test.ts" + ], + ['sh', '-c', "vitest run '--exclude='test/fixtures/test:e2e/*.test.ts test/queue.test.ts"], + [ + '/bin/sh', + '-c', + "vitest run --exclude \\\n 'test/fixtures/test:e2e/*.test.ts' test/queue.test.ts" + ], + ['sh', '-c', 'vitest run "--exclu\\\nde=**/*.e2e.test.ts" test/queue.test.ts'] ]; for (const command of unitCommands) expect(isHeavyTestCommand(command), command.join(' ')).toBe(false);