1
1
#! /usr/bin/env node
2
2
3
3
import commandLineArgs from "command-line-args" ;
4
- import commandLineUsage from "command-line-usage" ;
5
4
import { spawnSync } from "child_process" ;
6
5
import { fileURLToPath } from "url" ;
7
6
import { styleText } from "node:util" ;
8
7
import * as path from "path" ;
9
8
import * as fs from "fs" ;
10
9
import * 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" ;
12
13
13
14
const optionDefinitions = [
14
15
{ name : "shell" , type : String , description : "Set the shell to test, choices are [jsc, v8, spidermonkey]." } ,
15
16
{ name : "help" , alias : "h" , description : "Print this help text." } ,
16
17
] ;
17
18
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
-
39
19
const options = commandLineArgs ( optionDefinitions ) ;
40
20
41
21
if ( "help" in options )
42
- printHelp ( ) ;
22
+ printHelp ( optionDefinitions ) ;
43
23
44
24
const JS_SHELL = options ?. shell ;
45
25
if ( ! JS_SHELL )
46
- printHelp ( "No javascript shell specified, use --shell" ) ;
26
+ printHelp ( "No javascript shell specified, use --shell" , optionDefinitions ) ;
47
27
48
28
const SHELL_NAME = ( function ( ) {
49
29
switch ( JS_SHELL ) {
@@ -70,49 +50,16 @@ const UNIT_TEST_PATH = path.join(SRC_DIR, "tests", "unit-tests.js");
70
50
71
51
function convertCliArgs ( cli , ...cliArgs ) {
72
52
if ( SHELL_NAME == "spidermonkey" )
73
- return [ cli , ...cliArgs ]
53
+ return [ cli , ...cliArgs ] ;
74
54
return [ cli , "--" , ...cliArgs ] ;
75
55
}
76
56
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
- }
110
57
111
58
const SPAWN_OPTIONS = {
112
59
stdio : [ "inherit" , "inherit" , "inherit" ]
113
60
} ;
114
61
115
- function sh ( binary , args ) {
62
+ function sh ( binary , ... args ) {
116
63
const cmd = `${ binary } ${ args . join ( " " ) } ` ;
117
64
if ( GITHUB_ACTIONS_OUTPUT ) {
118
65
core . startGroup ( binary ) ;
@@ -128,21 +75,19 @@ function sh(binary, args) {
128
75
}
129
76
} finally {
130
77
if ( GITHUB_ACTIONS_OUTPUT )
131
- core . endGroup ( )
78
+ core . endGroup ( ) ;
132
79
}
133
80
}
134
81
135
82
async function runTests ( ) {
136
- const shellBinary = logGroup ( `Installing JavaScript Shell: ${ SHELL_NAME } ` , testSetup ) ;
83
+ const shellBinary = await logGroup ( `Installing JavaScript Shell: ${ SHELL_NAME } ` , testSetup ) ;
137
84
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 ) ;
146
91
}
147
92
148
93
function jsvuOSName ( ) {
@@ -161,30 +106,24 @@ function jsvuOSName() {
161
106
default : throw new Error ( "Unsupported architecture" ) ;
162
107
}
163
108
} ;
164
- return `${ osName ( ) } ${ osArch ( ) } `
109
+ return `${ osName ( ) } ${ osArch ( ) } ` ;
165
110
}
166
111
167
112
const DEFAULT_JSC_LOCATION = "/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc"
168
113
169
114
function testSetup ( ) {
170
- sh ( "jsvu" , [ `--engines=${ SHELL_NAME } ` , `--os=${ jsvuOSName ( ) } ` ] ) ;
115
+ sh ( "jsvu" , `--engines=${ SHELL_NAME } ` , `--os=${ jsvuOSName ( ) } ` ) ;
171
116
let shellBinary = path . join ( os . homedir ( ) , ".jsvu/bin" , SHELL_NAME ) ;
172
117
if ( ! fs . existsSync ( shellBinary ) && SHELL_NAME == "javascriptcore" )
173
- shellBinary = DEFAULT_JSC_LOCATION
118
+ shellBinary = DEFAULT_JSC_LOCATION ;
174
119
if ( ! fs . existsSync ( shellBinary ) )
175
120
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 ;
178
123
}
179
124
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 ) ) ) ;
188
127
}
189
128
190
129
setImmediate ( runTests ) ;
0 commit comments