@@ -8,7 +8,9 @@ import { retry, expBackoff } from 'zx/experimental'
88import { compareDownstreamResponse } from "./compare-downstream-response.js" ;
99import { argv , exit } from "node:process" ;
1010import { existsSync } from "node:fs" ;
11- import { copyFile , readdir , readFile } from "node:fs/promises" ;
11+ import { copyFile , readdir , readFile , writeFile } from "node:fs/promises" ;
12+ import core from '@actions/core' ;
13+ import TOML from '@iarna/toml'
1214
1315const startTime = Date . now ( ) ;
1416const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
@@ -73,38 +75,53 @@ if (process.env.FASTLY_API_TOKEN === undefined) {
7375}
7476const FASTLY_API_TOKEN = process . env . FASTLY_API_TOKEN ;
7577zx . verbose = true ;
78+ const branchName = ( await $ `git branch --show-current` ) . stdout . trim ( ) . replace ( / [ ^ a - z A - Z 0 - 9 _ - ] / g, '_' )
7679
7780for ( const fixture of testFixtures ) {
81+ const serviceName = `${ fixture } --${ branchName } `
82+ let domain ;
7883 await within ( async ( ) => {
7984 const fixturePath = join ( __dirname , 'fixtures' , fixture )
8085 try {
8186 const startTime = Date . now ( ) ;
8287 await cd ( fixturePath ) ;
8388 await copyFile ( join ( fixturePath , 'fastly.toml.in' ) , join ( fixturePath , 'fastly.toml' ) )
89+ const config = TOML . parse ( await readFile ( join ( fixturePath , 'fastly.toml' ) , 'utf-8' ) )
90+ config . name = serviceName ;
91+ await writeFile ( join ( fixturePath , 'fastly.toml' ) , TOML . stringify ( config ) , 'utf-8' )
92+ core . startGroup ( 'Delete service if already exists' )
8493 try {
85- await zx `fastly service delete --quiet --force -- service-name ${ fixture } --token $FASTLY_API_TOKEN`
94+ await zx `fastly service delete --quiet --service-name " ${ serviceName } " --force --token $FASTLY_API_TOKEN`
8695 } catch { }
87- // build and deploy application to compute@edge
96+ core . endGroup ( )
97+ core . startGroup ( 'Build and deploy service' )
8898 await zx `npm i`
8999 await zx `fastly compute publish -i --quiet --token $FASTLY_API_TOKEN`
90-
100+ core . endGroup ( )
101+
91102 // get the public domain of the deployed application
92- const domain = JSON . parse ( await $ `fastly domain list --quiet --version latest --json` ) [ 0 ] . Name
93-
103+ domain = JSON . parse ( await $ `fastly domain list --quiet --version latest --json` ) [ 0 ] . Name
104+ core . notice ( `Service is running on https://${ domain } ` )
105+
94106 const setupPath = join ( fixturePath , 'setup.js' )
95107 if ( existsSync ( setupPath ) ) {
108+ core . startGroup ( 'Extra set-up steps for the service' )
96109 await $ `${ setupPath } `
110+ core . endGroup ( )
97111 }
98-
112+
113+ core . startGroup ( 'Check service is up and running' )
99114 await retry ( 10 , expBackoff ( '60s' , '30s' ) , async ( ) => {
100115 const response = await request ( `https://${ domain } ` )
101116 if ( response . statusCode !== 200 ) {
102117 throw new Error ( `Application "${ fixture } " :: Not yet available on domain: ${ domain } ` )
103118 }
104119 } )
105-
120+ core . endGroup ( )
121+
106122 const { default : tests } = await import ( join ( fixturePath , 'tests.json' ) , { assert : { type : 'json' } } ) ;
107-
123+
124+ core . startGroup ( 'Running tests' )
108125 let counter = 0 ;
109126 await Promise . all ( Object . entries ( tests ) . map ( async ( [ title , test ] ) => {
110127 if ( test . environments . includes ( "c@e" ) ) {
@@ -122,19 +139,26 @@ for (const fixture of testFixtures) {
122139 } )
123140 }
124141 } ) )
142+ core . endGroup ( )
125143 console . log ( `Application "${ fixture } " :: All ${ counter } tests passed! Took ${ ( Date . now ( ) - startTime ) / 1000 } seconds to complete` )
126- } catch ( error ) {
127- console . error ( `Application "${ fixture } " :: ${ error . message } ` )
128- process . exitCode = 1 ;
129- } finally {
130144 const teardownPath = join ( fixturePath , 'teardown.js' )
131145 if ( existsSync ( teardownPath ) ) {
146+ core . startGroup ( 'Tear down the extra set-up for the service' )
132147 await $ `${ teardownPath } `
148+ core . endGroup ( )
133149 }
134-
135-
150+
151+ core . startGroup ( 'Delete service' )
136152 // Delete the service now the tests have finished
137- await zx `fastly service delete --quiet --force --service-name ${ fixture } --token $FASTLY_API_TOKEN`
153+ await zx `fastly service delete --quiet --service-name "${ serviceName } " --force --token $FASTLY_API_TOKEN`
154+ core . endGroup ( )
155+ } catch ( error ) {
156+ console . error ( `Application "${ fixture } " :: ${ error . message } ` )
157+ core . notice ( `Tests failed, the service is named "${ serviceName } "` )
158+ if ( domain ) {
159+ core . notice ( `You can debug the service on https://${ domain } ` )
160+ }
161+ process . exitCode = 1 ;
138162 }
139163
140164 } )
0 commit comments