11#! /usr/bin/env node
22
33import commandLineArgs from "command-line-args" ;
4- import commandLineUsage from "command-line-usage" ;
54import { spawnSync } from "child_process" ;
65import { fileURLToPath } from "url" ;
76import { styleText } from "node:util" ;
87import * as path from "path" ;
98import * as fs from "fs" ;
109import * as os from "os" ;
11- import core from "@actions/core"
10+ import core from "@actions/core" ;
11+
12+ import { logInfo , logError , logGroup , printHelp , runTest , GITHUB_ACTIONS_OUTPUT } from "./helper.mjs" ;
1213
1314const optionDefinitions = [
1415 { name : "shell" , type : String , description : "Set the shell to test, choices are [jsc, v8, spidermonkey]." } ,
1516 { name : "help" , alias : "h" , description : "Print this help text." } ,
1617] ;
1718
18- function printHelp ( message = "" ) {
19- const usage = commandLineUsage ( [
20- {
21- header : "Run all tests" ,
22- } ,
23- {
24- header : "Options" ,
25- optionList : optionDefinitions ,
26- } ,
27- ] ) ;
28- if ( ! message ) {
29- console . log ( usage ) ;
30- process . exit ( 0 ) ;
31- } else {
32- console . error ( message ) ;
33- console . error ( ) ;
34- console . error ( usage ) ;
35- process . exit ( 1 ) ;
36- }
37- }
38-
3919const options = commandLineArgs ( optionDefinitions ) ;
4020
4121if ( "help" in options )
42- printHelp ( ) ;
22+ printHelp ( optionDefinitions ) ;
4323
4424const JS_SHELL = options ?. shell ;
4525if ( ! JS_SHELL )
46- printHelp ( "No javascript shell specified, use --shell" ) ;
26+ printHelp ( "No javascript shell specified, use --shell" , optionDefinitions ) ;
4727
4828const SHELL_NAME = ( function ( ) {
4929 switch ( JS_SHELL ) {
@@ -70,49 +50,16 @@ const UNIT_TEST_PATH = path.join(SRC_DIR, "tests", "unit-tests.js");
7050
7151function convertCliArgs ( cli , ...cliArgs ) {
7252 if ( SHELL_NAME == "spidermonkey" )
73- return [ cli , ...cliArgs ]
53+ return [ cli , ...cliArgs ] ;
7454 return [ cli , "--" , ...cliArgs ] ;
7555}
7656
77- const GITHUB_ACTIONS_OUTPUT = "GITHUB_ACTIONS_OUTPUT" in process . env ;
78-
79- function log ( ...args ) {
80- const text = args . join ( " " )
81- if ( GITHUB_ACTIONS_OUTPUT )
82- core . info ( styleText ( "yellow" , text ) )
83- else
84- console . log ( styleText ( "yellow" , text ) )
85- }
86-
87- function logError ( ...args ) {
88- const text = args . join ( " " )
89- if ( GITHUB_ACTIONS_OUTPUT )
90- core . error ( styleText ( "red" , text ) )
91- else
92- console . error ( styleText ( "red" , text ) )
93- }
94-
95- function logGroup ( name , body ) {
96- if ( GITHUB_ACTIONS_OUTPUT ) {
97- core . startGroup ( name ) ;
98- } else {
99- log ( "=" . repeat ( 80 ) )
100- log ( name ) ;
101- log ( "." . repeat ( 80 ) )
102- }
103- try {
104- return body ( ) ;
105- } finally {
106- if ( GITHUB_ACTIONS_OUTPUT )
107- core . endGroup ( ) ;
108- }
109- }
11057
11158const SPAWN_OPTIONS = {
11259 stdio : [ "inherit" , "inherit" , "inherit" ]
11360} ;
11461
115- function sh ( binary , args ) {
62+ function sh ( binary , ... args ) {
11663 const cmd = `${ binary } ${ args . join ( " " ) } ` ;
11764 if ( GITHUB_ACTIONS_OUTPUT ) {
11865 core . startGroup ( binary ) ;
@@ -128,21 +75,19 @@ function sh(binary, args) {
12875 }
12976 } finally {
13077 if ( GITHUB_ACTIONS_OUTPUT )
131- core . endGroup ( )
78+ core . endGroup ( ) ;
13279 }
13380}
13481
13582async function runTests ( ) {
136- const shellBinary = logGroup ( `Installing JavaScript Shell: ${ SHELL_NAME } ` , testSetup ) ;
83+ const shellBinary = await logGroup ( `Installing JavaScript Shell: ${ SHELL_NAME } ` , testSetup ) ;
13784 let success = true ;
138- success &&= runTest ( "Run UnitTests" , ( ) => sh ( shellBinary , [ UNIT_TEST_PATH ] ) ) ;
139- success &&= runTest ( "Run Complete Suite" , ( ) => sh ( shellBinary , convertCliArgs ( CLI_PATH ) ) ) ;
140- success &&= runTest ( "Run Single Suite" , ( ) => {
141- sh ( shellBinary , convertCliArgs ( CLI_PATH , "proxy-mobx" ) ) ;
142- } ) ;
143- if ( ! success ) {
144- process . exit ( 1 )
145- }
85+ success &&= await runTest ( "Run UnitTests" , ( ) => sh ( shellBinary , UNIT_TEST_PATH ) ) ;
86+ success &&= await runCLITest ( "Run Single Suite" , shellBinary , "proxy-mobx" ) ;
87+ success &&= await runCLITest ( "Run Disabled Suite" , shellBinary , "disabled" ) ;
88+ success &&= await runCLITest ( "Run Default Suite" , shellBinary ) ;
89+ if ( ! success )
90+ process . exit ( 1 ) ;
14691}
14792
14893function jsvuOSName ( ) {
@@ -161,30 +106,24 @@ function jsvuOSName() {
161106 default : throw new Error ( "Unsupported architecture" ) ;
162107 }
163108 } ;
164- return `${ osName ( ) } ${ osArch ( ) } `
109+ return `${ osName ( ) } ${ osArch ( ) } ` ;
165110}
166111
167112const DEFAULT_JSC_LOCATION = "/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc"
168113
169114function testSetup ( ) {
170- sh ( "jsvu" , [ `--engines=${ SHELL_NAME } ` , `--os=${ jsvuOSName ( ) } ` ] ) ;
115+ sh ( "jsvu" , `--engines=${ SHELL_NAME } ` , `--os=${ jsvuOSName ( ) } ` ) ;
171116 let shellBinary = path . join ( os . homedir ( ) , ".jsvu/bin" , SHELL_NAME ) ;
172117 if ( ! fs . existsSync ( shellBinary ) && SHELL_NAME == "javascriptcore" )
173- shellBinary = DEFAULT_JSC_LOCATION
118+ shellBinary = DEFAULT_JSC_LOCATION ;
174119 if ( ! fs . existsSync ( shellBinary ) )
175120 throw new Error ( `Could not find shell binary: ${ shellBinary } ` ) ;
176- log ( `Installed JavaScript Shell: ${ shellBinary } ` ) ;
177- return shellBinary
121+ logInfo ( `Installed JavaScript Shell: ${ shellBinary } ` ) ;
122+ return shellBinary ;
178123}
179124
180- function runTest ( testName , test ) {
181- try {
182- logGroup ( testName , test )
183- } catch ( e ) {
184- logError ( "TEST FAILED" )
185- return false
186- }
187- return true
125+ function runCLITest ( name , shellBinary , ...args ) {
126+ return runTest ( name , ( ) => sh ( shellBinary , ...convertCliArgs ( CLI_PATH , ...args ) ) ) ;
188127}
189128
190129setImmediate ( runTests ) ;
0 commit comments