@@ -4,41 +4,49 @@ import { describe, expect, it } from 'vitest'
44
55const script = fileURLToPath ( new URL ( '../.github/scripts/wait-for-e2e-build.sh' , import . meta. url ) )
66
7- interface Job {
7+ interface CheckRun {
88 name : string
99 status : string
1010 conclusion : string | null
1111}
1212
13- const job = ( status : string , conclusion : string | null = null , name = 'checks / build (e2e)' ) => ( {
13+ const run = ( status : string , conclusion : string | null = null , name = 'checks / build (e2e)' ) => ( {
1414 name,
1515 status,
1616 conclusion,
1717} )
1818
1919/**
20- * Runs the real script against a `gh` stub whose jq filter is the script's own: each poll answers
21- * with the next listing (the last one repeats), and `sleep` only counts polls.
20+ * Runs the real script against a `gh` stub that applies the script's own jq filters: the run lookup
21+ * answers with check suite 42, and each poll's check-run lookups answer from the next listing (the
22+ * last one repeats), filtered by the requested `check_name`. The first `failingPolls` polls' API
23+ * calls fail; `sleep` only counts polls.
2224 */
23- function runWait ( listings : Job [ ] [ ] , apiStatus = 0 ) {
25+ function runWait ( listings : CheckRun [ ] [ ] , failingPolls = 0 ) {
2426 return spawnSync (
2527 'bash' ,
2628 [
2729 '-c' ,
2830 `set -euo pipefail
2931 poll=0
3032 gh() {
31- [ "$1" = api ] && [ "$2" = --paginate ] || return 97
32- case "$3" in
33- 'repos/example/project/actions/runs/7/jobs?filter=latest&per_page=100') ;;
33+ [ "$1" = api ] && [ "$3" = --jq ] || return 97
34+ if [ "$poll" -lt "$FAILING_POLLS" ]; then return 1; fi
35+ case "$2" in
36+ 'repos/example/project/actions/runs/7')
37+ jq -r "$4" <<< '{"check_suite_id": 42}' ;;
38+ 'repos/example/project/check-suites/42/check-runs?check_name=checks%20%2F%20build%20(e2e)&filter=latest&per_page=10')
39+ checks_for 'checks / build (e2e)' "$4" ;;
40+ 'repos/example/project/check-suites/42/check-runs?check_name=build%20(e2e)&filter=latest&per_page=10')
41+ checks_for 'build (e2e)' "$4" ;;
3442 *) return 98 ;;
3543 esac
36- if [ "$API_STATUS" -ne 0 ]; then return "$API_STATUS"; fi
37- local listings count
38- listings=$(jq -c '.' <<< "$LISTINGS")
39- count=$(jq 'length' <<< "$listings ")
40- local index=$(( poll < count ? poll : count - 1 ))
41- jq -r "{jobs: .[$index]} | $5 " <<< "$listings "
44+ }
45+ checks_for() {
46+ local count index
47+ count=$(jq 'length' <<< "$LISTINGS ")
48+ index=$(( poll < count ? poll : count - 1 ))
49+ jq -r --arg name "$1" "{check_runs: [ .[$index][] | select(.name == \\$name)] } | $2 " <<< "$LISTINGS "
4250 }
4351 sleep() { poll=$((poll + 1)); }
4452 source "$1"` ,
@@ -53,7 +61,7 @@ function runWait(listings: Job[][], apiStatus = 0) {
5361 GITHUB_REPOSITORY : 'example/project' ,
5462 GITHUB_RUN_ID : '7' ,
5563 LISTINGS : JSON . stringify ( listings ) ,
56- API_STATUS : String ( apiStatus ) ,
64+ FAILING_POLLS : String ( failingPolls ) ,
5765 } ,
5866 }
5967 )
@@ -62,44 +70,54 @@ function runWait(listings: Job[][], apiStatus = 0) {
6270describe ( 'wait-for-e2e-build.sh' , ( ) => {
6371 it ( 'returns once an in-progress build succeeds' , ( ) => {
6472 const result = runWait ( [
65- [ job ( 'queued' ) , job ( 'in_progress' , null , 'checks / lint' ) ] ,
66- [ job ( 'in_progress' ) ] ,
67- [ job ( 'completed' , 'success' ) ] ,
73+ [ run ( 'queued' ) , run ( 'in_progress' , null , 'checks / lint' ) ] ,
74+ [ run ( 'in_progress' ) ] ,
75+ [ run ( 'completed' , 'success' ) ] ,
6876 ] )
6977 expect ( result . status ) . toBe ( 0 )
7078 expect ( result . stdout ) . toContain ( 'build (e2e) finished' )
7179 } )
7280
73- it ( 'accepts the unprefixed job name of a directly dispatched checks run' , ( ) => {
74- expect ( runWait ( [ [ job ( 'completed' , 'success' , 'build (e2e)' ) ] ] ) . status ) . toBe ( 0 )
81+ it ( 'accepts the unprefixed check name of a directly dispatched checks run' , ( ) => {
82+ expect ( runWait ( [ [ run ( 'completed' , 'success' , 'build (e2e)' ) ] ] ) . status ) . toBe ( 0 )
7583 } )
7684
7785 it . each ( [ 'failure' , 'cancelled' , 'skipped' , 'timed_out' ] ) (
7886 'fails when the build ends %s' ,
7987 ( end ) => {
80- const result = runWait ( [ [ job ( 'in_progress' ) ] , [ job ( 'completed' , end ) ] ] )
88+ const result = runWait ( [ [ run ( 'in_progress' ) ] , [ run ( 'completed' , end ) ] ] )
8189 expect ( result . status ) . toBe ( 1 )
8290 expect ( result . stdout ) . toContain ( `finished with '${ end } '` )
8391 }
8492 )
8593
86- it ( 'fails when the run has no build job instead of waiting forever' , ( ) => {
87- const result = runWait ( [ [ job ( 'completed' , 'success' , 'checks / build' ) ] ] )
94+ it ( 'waits for a build check run that has not been created yet' , ( ) => {
95+ expect ( runWait ( [ [ ] , [ ] , [ run ( 'completed' , 'success' ) ] ] ) . status ) . toBe ( 0 )
96+ } )
97+
98+ it ( 'fails when the run never gets a build check run instead of waiting forever' , ( ) => {
99+ const result = runWait ( [ [ run ( 'completed' , 'success' , 'checks / build' ) ] ] )
88100 expect ( result . status ) . toBe ( 1 )
89101 expect ( result . stdout ) . toContain ( 'found 0' )
90102 } )
91103
92- it ( 'fails when two jobs claim the build name' , ( ) => {
104+ it ( 'fails when two check runs claim the build name' , ( ) => {
93105 const result = runWait ( [
94- [ job ( 'completed' , 'success' ) , job ( 'completed' , 'success' , 'build (e2e)' ) ] ,
106+ [ run ( 'completed' , 'success' ) , run ( 'completed' , 'success' , 'build (e2e)' ) ] ,
95107 ] )
96108 expect ( result . status ) . toBe ( 1 )
97109 expect ( result . stdout ) . toContain ( 'found 2' )
98110 } )
99111
100- it ( 'fails on an API error rather than treating it as still building' , ( ) => {
101- const result = runWait ( [ [ job ( 'in_progress' ) ] ] , 1 )
102- expect ( result . status ) . not . toBe ( 0 )
112+ it ( 'rides out a few failed API calls' , ( ) => {
113+ const result = runWait ( [ [ run ( 'completed' , 'success' ) ] ] , 3 )
114+ expect ( result . status ) . toBe ( 0 )
115+ expect ( result . stdout ) . toContain ( 'trying again' )
116+ } )
117+
118+ it ( 'fails on an API that keeps failing rather than treating it as still building' , ( ) => {
119+ const result = runWait ( [ [ run ( 'in_progress' ) ] ] , 100 )
120+ expect ( result . status ) . toBe ( 1 )
103121 expect ( result . stdout ) . not . toContain ( 'build (e2e) finished' )
104122 } )
105123} )
0 commit comments