From 3b92b5dbcceaa916b8b8f3f3a01c0b0ac82e95ab Mon Sep 17 00:00:00 2001 From: Michael Kauzmann Date: Tue, 12 Nov 2024 14:50:58 -0700 Subject: [PATCH] use spawn instead, https://github.com/phetsims/perennial/issues/384 --- js/test/test-exec-sync.ts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/js/test/test-exec-sync.ts b/js/test/test-exec-sync.ts index 47d70d94..d838a047 100644 --- a/js/test/test-exec-sync.ts +++ b/js/test/test-exec-sync.ts @@ -1,13 +1,14 @@ // Copyright 2024, University of Colorado Boulder /** - * Test that we can invoke a grunt task from the command line, and make sure the options are passed correctly. + * Test that we can invoke a task from the command line, and make sure the options are passed correctly. * * @author Sam Reid (PhET Interactive Simulations) * @author Michael Kauzmann (PhET Interactive Simulations) */ -import { execSync } from 'child_process'; +import { spawnSync } from 'child_process'; +import fs from 'fs'; import _ from 'lodash'; import qunit from 'qunit'; import gruntCommand from '../common/gruntCommand.js'; @@ -15,18 +16,20 @@ import tsxCommand from '../common/tsxCommand.js'; const SIM = 'acid-base-solutions'; -const EXEC_SYNC_OPTIONS = { - encoding: 'utf-8' +const SPAWN_SYNC_OPTIONS = { + encoding: 'utf-8', + shell: true } as const; -const EXEC_AND_SIM_OPTIONS = _.assignIn( { +const SPAWN_FOR_SIM_OPTIONS = _.assignIn( { cwd: `../${SIM}` -}, EXEC_SYNC_OPTIONS ); +}, SPAWN_SYNC_OPTIONS ); qunit.module( 'test-exec-sync' ); -const checkOutput = ( result: string, assert: Assert ) => { - assert.ok( result.includes( ` +const checkOutput = ( result: ReturnType, assert: Assert ) => { + assert.ok( result.status === 0 ); + assert.ok( result.stdout.includes( ` brands: a,b,c lint: false noTSC: true @@ -35,27 +38,27 @@ omitted: undefined }; qunit.test( 'grunt no args', ( assert: Assert ) => { - const result = execSync( `${gruntCommand} test-grunt`, EXEC_SYNC_OPTIONS ); - assert.ok( result.match( /: undefined/g )?.length === 4, 'no args, all undefined' ); + const result = spawnSync( `${gruntCommand} test-grunt`, SPAWN_SYNC_OPTIONS ); + assert.ok( result.stdout.match( /: undefined/g )?.length === 4, 'no args, all undefined' ); } ); qunit.test( 'grunt args', ( assert: Assert ) => { - const result = execSync( `${gruntCommand} test-grunt --brands=a,b,c --lint=false --noTSC`, EXEC_SYNC_OPTIONS ); + const result = spawnSync( `${gruntCommand} test-grunt --brands=a,b,c --lint=false --noTSC`, SPAWN_SYNC_OPTIONS ); checkOutput( result, assert ); } ); qunit.test( 'tsx', assert => { - const result = execSync( `${tsxCommand} ../perennial-alias/js/grunt/tasks/test-grunt.ts --brands=a,b,c --lint=false --noTSC`, EXEC_SYNC_OPTIONS ); + const result = spawnSync( `${tsxCommand} ./js/grunt/tasks/test-grunt.ts --brands=a,b,c --lint=false --noTSC`, SPAWN_SYNC_OPTIONS ); checkOutput( result, assert ); } ); qunit.test( 'sage run', assert => { - const result = execSync( 'bash ../perennial-alias/bin/sage run ../perennial-alias/js/grunt/tasks/test-grunt.ts --brands=a,b,c --lint=false --noTSC', EXEC_SYNC_OPTIONS ); + const result = spawnSync( 'bash ./bin/sage run ./js/grunt/tasks/test-grunt.ts --brands=a,b,c --lint=false --noTSC', SPAWN_SYNC_OPTIONS ); checkOutput( result, assert ); } ); // Sim-specific qunit.test( `grunt ${SIM}`, ( assert: Assert ) => { - const result = execSync( `${gruntCommand} --brands=a,b,c --lint=false --noTSC --test-options`, EXEC_AND_SIM_OPTIONS ); + const result = spawnSync( `${gruntCommand} --brands=a,b,c --lint=false --noTSC --test-options`, SPAWN_FOR_SIM_OPTIONS ); checkOutput( result, assert ); } ); \ No newline at end of file