|
| 1 | +#!/usr/bin/env node |
| 2 | +/* |
| 3 | + * Copyright 2025 The Closure Compiler Authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +/** |
| 18 | + * @fileoverview |
| 19 | + * |
| 20 | + * Execute jasmine with the arguments in the correct order. |
| 21 | + * Extra arguments passed to test specs must be preceded by an '--' argument. |
| 22 | + * We want to keep the arguments in the same order, but arguments for the jasmine runner itself must |
| 23 | + * come first followed by a '--' argument and then finally by any extra arguments. |
| 24 | + */ |
| 25 | + |
| 26 | +import {spawn} from 'node:child_process'; |
| 27 | +import parseArgs from 'minimist'; |
| 28 | + |
| 29 | +const supportedJasmineFlags = new Set([ |
| 30 | + 'parallel', |
| 31 | + 'no-color', |
| 32 | + 'color', |
| 33 | + 'filter', |
| 34 | + 'helper', |
| 35 | + 'require', |
| 36 | + 'fail-fast', |
| 37 | + 'config', |
| 38 | + 'reporter', |
| 39 | + 'verbose', |
| 40 | +]); |
| 41 | + |
| 42 | +const cliFlags = parseArgs(process.argv.slice(2)); |
| 43 | +const jasmineFlags = []; |
| 44 | +const extraFlags = []; |
| 45 | +for (const [name, value] of Object.entries(cliFlags)) { |
| 46 | + const normalizedValues = Array.isArray(value) ? value : [value]; |
| 47 | + if (name === '_') { |
| 48 | + jasmineFlags.push(...value); |
| 49 | + } else if (supportedJasmineFlags.has(name)) { |
| 50 | + for (const normalizedValue of normalizedValues) { |
| 51 | + jasmineFlags.push(`--${name}${typeof normalizedValue === 'boolean' ? '' : `=${normalizedValue}`}`); |
| 52 | + } |
| 53 | + } else { |
| 54 | + for (const normalizedValue of normalizedValues) { |
| 55 | + extraFlags.push(`--${name}${typeof normalizedValue === 'boolean' ? '' : `=${normalizedValue}`}`); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +const flagName = (flag) => { |
| 61 | + if (flag.startsWith('--')) { |
| 62 | + const valStart = flag.indexOf('=', 2); |
| 63 | + return flag.slice(0, valStart > 0 ? valStart : flag.length); |
| 64 | + } |
| 65 | + return flag; |
| 66 | +} |
| 67 | +const flagSorter = (a, b) => { |
| 68 | + const aFlagName = flagName(a); |
| 69 | + const bFlagName = flagName(b); |
| 70 | + const aIndex = process.argv.findIndex((arg) => arg === aFlagName || arg.startsWith(`${aFlagName}=`)); |
| 71 | + const bIndex = process.argv.findIndex((arg) => arg === bFlagName || arg.startsWith(`${bFlagName}=`)); |
| 72 | + return aIndex - bIndex; |
| 73 | +}; |
| 74 | +jasmineFlags.sort(flagSorter); |
| 75 | +extraFlags.sort(flagSorter); |
| 76 | +if (extraFlags.length > 0) { |
| 77 | + jasmineFlags.push('--', ...extraFlags); |
| 78 | +} |
| 79 | + |
| 80 | +const jasmineProcess = spawn( |
| 81 | + 'jasmine', |
| 82 | + jasmineFlags, |
| 83 | + { |
| 84 | + stdio: 'inherit', |
| 85 | + }, |
| 86 | +); |
| 87 | + |
| 88 | +jasmineProcess.on('close', (exitCode) => { |
| 89 | + process.exitCode = exitCode; |
| 90 | +}); |
0 commit comments