11#!/usr/bin/env -S npx ts-node -P ci/tsconfig.json
22
3- import * as https from "https"
3+ import cp from "child_process"
4+ import * as events from "events"
5+ import * as readline from "readline"
46import replaceInFile from "replace-in-file"
5- import { exec , main , selectCtx , spawn } from "./lib"
7+ import { exec , main , selectCtx , spawn , wasmEnv } from "./lib"
68
7- if ( process . argv [ 1 ] === __filename ) {
9+ if ( require . main === module ) {
810 main ( test )
911}
1012
@@ -26,32 +28,67 @@ export async function test(ctx: Promise<unknown>) {
2628 args . push ( "./..." )
2729 }
2830
29- await spawn ( ctx , "go" , [ "test" , ...args ] , {
30- timeout : 60_000 ,
31+ const p1 = spawn ( ctx , "go" , [ "test" , ...args ] , {
3132 stdio : "inherit" ,
3233 } )
34+ const p2 = wasmTest ( ctx )
35+ await Promise . all ( [ p1 , p2 ] )
3336
3437 // Depending on the code tested, we may not have replaced anything so we do not
3538 // check whether anything was replaced.
3639 await selectCtx ( ctx , replaceInFile ( {
3740 files : "./ci/out/coverage.prof" ,
3841 from : [
39- / .+ f r a m e _ s t r i n g e r .g o : .+ \n / g,
40- / .+ w s j s t e s t : .+ \n / g,
41- / .+ w s e c h o : .+ \n / g,
42+ / .+ f r a m e _ s t r i n g e r .g o .+ \n / g,
43+ / .+ w s j s t e s t .+ \n / g,
44+ / .+ w s e c h o .+ \n / g,
4245 ] ,
4346 to : "" ,
4447 } ) )
4548
4649 let p : Promise < unknown > = exec ( ctx , "go tool cover -html=ci/out/coverage.prof -o=ci/out/coverage.html" )
4750
4851 if ( process . env . CI ) {
49- const script = https . get ( "https://codecov.io/bash" )
50- const p2 = spawn ( ctx , "bash -Z -R . -f ci/out/coverage.prof" , [ ] , {
51- stdio : [ script ] ,
52- } )
53- p = Promise . all ( [ p , p2 ] )
52+ p = Promise . all ( [ p , codecov ( ctx ) ] )
5453 }
5554
5655 await p
5756}
57+
58+
59+ async function wasmTest ( ctx : Promise < unknown > ) {
60+ await Promise . all ( [
61+ exec ( ctx , "go install ./internal/wsjstest" ) ,
62+ exec ( ctx , "go install github.com/agnivade/wasmbrowsertest" ) ,
63+ ] )
64+
65+ const url = await startWasmTestServer ( ctx )
66+
67+ await exec ( ctx , "go test -exec=wasmbrowsertest ./..." , {
68+ env : {
69+ ...wasmEnv ,
70+ WS_ECHO_SERVER_URL : url ,
71+ } ,
72+ } )
73+ }
74+
75+ async function startWasmTestServer ( ctx : Promise < unknown > ) : Promise < string > {
76+ const wsjstest = cp . spawn ( "wsjstest" )
77+ ctx . finally ( wsjstest . kill . bind ( wsjstest ) )
78+
79+ const rl = readline . createInterface ( {
80+ input : wsjstest . stdout ! ,
81+ } )
82+
83+ try {
84+ const p = events . once ( rl , "line" )
85+ const a = await selectCtx ( ctx , p )
86+ return a [ 0 ]
87+ } finally {
88+ rl . close ( )
89+ }
90+ }
91+
92+ function codecov ( ctx : Promise < unknown > ) {
93+ return exec ( ctx , "curl -s https://codecov.io/bash | bash -s -- -Z -f ci/out/coverage.prof" )
94+ }
0 commit comments