diff --git a/build-scripts/jasmine.sh b/build-scripts/jasmine.sh new file mode 100755 index 00000000..733bf727 --- /dev/null +++ b/build-scripts/jasmine.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +# Run the test commands and fail the script if any of them failed +EXIT_STATUS=0 +./packages/google-closure-compiler/test/support/jasmine-launcher.js "$@" || EXIT_STATUS=$? +exit $EXIT_STATUS diff --git a/build-scripts/test.sh b/build-scripts/test.sh index bcb74ebe..733bf727 100755 --- a/build-scripts/test.sh +++ b/build-scripts/test.sh @@ -1,6 +1,5 @@ #!/usr/bin/env bash # Run the test commands and fail the script if any of them failed EXIT_STATUS=0 -yarn workspaces run test "$@" || EXIT_STATUS=$? -jasmine --reporter=jasmine-console-reporter test/*.js "$@" || EXIT_STATUS=$? +./packages/google-closure-compiler/test/support/jasmine-launcher.js "$@" || EXIT_STATUS=$? exit $EXIT_STATUS diff --git a/package.json b/package.json index a081923a..d2c78be7 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "scripts": { "build": "./build-scripts/build.sh", "test": "./build-scripts/test.sh", - "test:root": "jasmine --reporter=jasmine-console-reporter test/*.js", + "test:root": "./build-scripts/jasmine.sh --reporter=jasmine-console-reporter test/*.js", "clean": "./build-scripts/clean.sh", "version": "./build-scripts/version-packages.js", "publish-packages": "./build-scripts/publish.js" diff --git a/packages/google-closure-compiler/package.json b/packages/google-closure-compiler/package.json index 9a451d84..2b5a0c11 100644 --- a/packages/google-closure-compiler/package.json +++ b/packages/google-closure-compiler/package.json @@ -64,7 +64,7 @@ }, "scripts": { "build": "echo \"google-closure-compiler build\"", - "test": "jasmine --reporter=jasmine-console-reporter test/*.js --" + "test": "./test/support/jasmine.sh --reporter=jasmine-console-reporter 'test/*.js'" }, "engines": { "node": ">=18" diff --git a/packages/google-closure-compiler/test/support/jasmine-launcher.js b/packages/google-closure-compiler/test/support/jasmine-launcher.js new file mode 100755 index 00000000..a220ac72 --- /dev/null +++ b/packages/google-closure-compiler/test/support/jasmine-launcher.js @@ -0,0 +1,90 @@ +#!/usr/bin/env node +/* + * Copyright 2025 The Closure Compiler Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @fileoverview + * + * Execute jasmine with the arguments in the correct order. + * Extra arguments passed to test specs must be preceded by an '--' argument. + * We want to keep the arguments in the same order, but arguments for the jasmine runner itself must + * come first followed by a '--' argument and then finally by any extra arguments. + */ + +import {spawn} from 'node:child_process'; +import parseArgs from 'minimist'; + +const supportedJasmineFlags = new Set([ + 'parallel', + 'no-color', + 'color', + 'filter', + 'helper', + 'require', + 'fail-fast', + 'config', + 'reporter', + 'verbose', +]); + +const cliFlags = parseArgs(process.argv.slice(2)); +const jasmineFlags = []; +const extraFlags = []; +for (const [name, value] of Object.entries(cliFlags)) { + const normalizedValues = Array.isArray(value) ? value : [value]; + if (name === '_') { + jasmineFlags.push(...value); + } else if (supportedJasmineFlags.has(name)) { + for (const normalizedValue of normalizedValues) { + jasmineFlags.push(`--${name}${typeof normalizedValue === 'boolean' ? '' : `=${normalizedValue}`}`); + } + } else { + for (const normalizedValue of normalizedValues) { + extraFlags.push(`--${name}${typeof normalizedValue === 'boolean' ? '' : `=${normalizedValue}`}`); + } + } +} + +const flagName = (flag) => { + if (flag.startsWith('--')) { + const valStart = flag.indexOf('=', 2); + return flag.slice(0, valStart > 0 ? valStart : flag.length); + } + return flag; +} +const flagSorter = (a, b) => { + const aFlagName = flagName(a); + const bFlagName = flagName(b); + const aIndex = process.argv.findIndex((arg) => arg === aFlagName || arg.startsWith(`${aFlagName}=`)); + const bIndex = process.argv.findIndex((arg) => arg === bFlagName || arg.startsWith(`${bFlagName}=`)); + return aIndex - bIndex; +}; +jasmineFlags.sort(flagSorter); +extraFlags.sort(flagSorter); +if (extraFlags.length > 0) { + jasmineFlags.push('--', ...extraFlags); +} + +const jasmineProcess = spawn( + 'jasmine', + jasmineFlags, + { + stdio: 'inherit', + }, +); + +jasmineProcess.on('close', (exitCode) => { + process.exitCode = exitCode; +}); diff --git a/packages/google-closure-compiler/test/support/jasmine.sh b/packages/google-closure-compiler/test/support/jasmine.sh new file mode 100755 index 00000000..8fec70db --- /dev/null +++ b/packages/google-closure-compiler/test/support/jasmine.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +# Run the test commands and fail the script if any of them failed +EXIT_STATUS=0 +./test/support/jasmine-launcher.js "$@" || EXIT_STATUS=$? +exit $EXIT_STATUS