@@ -19,14 +19,22 @@ import { expect } from 'chai';
19
19
import * as sinon from 'sinon' ;
20
20
21
21
import YAML from 'yaml' ;
22
+ import * as path from 'path' ;
23
+ import * as fs from 'fs/promises' ;
22
24
23
25
import * as core from '@actions/core' ;
24
26
import * as exec from '@actions/exec' ;
25
27
import * as setupGcloud from '@google-github-actions/setup-cloud-sdk' ;
26
28
import { TestToolCache } from '@google-github-actions/setup-cloud-sdk' ;
27
- import { errorMessage , KVPair } from '@google-github-actions/actions-utils' ;
29
+ import {
30
+ errorMessage ,
31
+ forceRemove ,
32
+ KVPair ,
33
+ randomFilepath ,
34
+ writeSecureFile ,
35
+ } from '@google-github-actions/actions-utils' ;
28
36
29
- import { run , findAppYaml , updateEnvVars } from '../src/main' ;
37
+ import { run , findAppYaml , updateEnvVars , parseDeliverables } from '../src/main' ;
30
38
31
39
// These are mock data for github actions inputs, where camel case is expected.
32
40
const fakeInputs : { [ key : string ] : string } = {
@@ -198,49 +206,120 @@ describe('#run', function () {
198
206
} ) ;
199
207
200
208
describe ( '#findAppYaml' , ( ) => {
209
+ beforeEach ( async function ( ) {
210
+ this . parent = randomFilepath ( ) ;
211
+ await fs . mkdir ( this . parent , { recursive : true } ) ;
212
+ } ) ;
213
+
214
+ afterEach ( async function ( ) {
215
+ if ( this . parent ) {
216
+ forceRemove ( this . parent ) ;
217
+ }
218
+ } ) ;
219
+
201
220
const cases : {
202
221
only ?: boolean ;
203
222
name : string ;
204
- list : string [ ] ;
223
+ files : Record < string , string > ;
205
224
expected ?: string ;
206
225
error ?: string ;
207
226
} [ ] = [
208
227
{
209
- name : 'empty list ' ,
210
- list : [ ] ,
211
- error : 'Could not find' ,
228
+ name : 'no deployables ' ,
229
+ files : { } ,
230
+ error : 'could not find an appyaml ' ,
212
231
} ,
213
232
{
214
- name : 'non-existent' ,
215
- list : [ 'a' , 'b' , 'c' ] ,
216
- error : 'Could not find' ,
233
+ name : 'no appyaml single' ,
234
+ files : {
235
+ 'my-file' : `
236
+ this is a file
237
+ ` ,
238
+ } ,
239
+ error : 'could not find an appyaml' ,
217
240
} ,
218
241
{
219
- name : 'finds app.yml' ,
220
- list : [ 'a' , 'b' , 'c' , 'app.yml' ] ,
221
- expected : 'app.yml' ,
242
+ name : 'no appyaml multiple' ,
243
+ files : {
244
+ 'my-file' : `
245
+ this is a file
246
+ ` ,
247
+ 'my-other-file' : `
248
+ this is another file
249
+ ` ,
250
+ } ,
251
+ error : 'could not find an appyaml' ,
222
252
} ,
223
253
{
224
- name : 'finds app.yaml' ,
225
- list : [ 'a' , 'b' , 'c' , 'app.yaml' ] ,
226
- expected : 'app.yaml' ,
254
+ name : 'single appyaml' ,
255
+ files : {
256
+ 'app-dev.yaml' : `
257
+ runtime: 'node'
258
+ service: 'my-service'
259
+ ` ,
260
+ } ,
261
+ expected : 'app-dev.yaml' ,
227
262
} ,
228
263
{
229
- name : 'finds nested' ,
230
- list : [ 'foo/bar/app.yaml' ] ,
231
- expected : 'foo/bar/app.yaml' ,
264
+ name : 'multiple files with appyaml' ,
265
+ files : {
266
+ 'my-file' : `
267
+ this is a file
268
+ ` ,
269
+ 'my-other-file' : `
270
+ this is another file
271
+ ` ,
272
+ 'app-prod.yaml' : `
273
+ runtime: 'node'
274
+ service: 'my-service'
275
+ ` ,
276
+ } ,
277
+ expected : 'app-prod.yaml' ,
278
+ } ,
279
+ {
280
+ name : 'multiple appyaml uses first' ,
281
+ files : {
282
+ 'app.yaml' : `
283
+ runtime: 'node'
284
+ service: 'my-service'
285
+ ` ,
286
+ 'app-dev.yaml' : `
287
+ runtime: 'node'
288
+ service: 'my-service'
289
+ ` ,
290
+ 'app-prod.yaml' : `
291
+ runtime: 'node'
292
+ service: 'my-service'
293
+ ` ,
294
+ } ,
295
+ expected : 'app.yaml' ,
232
296
} ,
233
297
] ;
234
298
235
299
cases . forEach ( ( tc ) => {
236
300
const fn = tc . only ? it . only : it ;
237
- fn ( tc . name , ( ) => {
301
+ fn ( tc . name , async function ( ) {
302
+ Object . keys ( tc . files ) . map ( ( key ) => {
303
+ const newKey = path . join ( this . parent , key ) ;
304
+ tc . files [ newKey ] = tc . files [ key ] ;
305
+ delete tc . files [ key ] ;
306
+ } ) ;
307
+
308
+ await Promise . all (
309
+ Object . entries ( tc . files ) . map ( async ( [ pth , contents ] ) => {
310
+ await writeSecureFile ( pth , contents ) ;
311
+ } ) ,
312
+ ) ;
313
+
314
+ const filepaths = Object . keys ( tc . files ) ;
238
315
if ( tc . error ) {
239
- expect ( ( ) => {
240
- findAppYaml ( tc . list ) ;
241
- } ) . to . throw ( tc . error ) ;
242
- } else {
243
- expect ( findAppYaml ( tc . list ) ) . to . eql ( tc . expected ) ;
316
+ expectError ( async ( ) => {
317
+ await findAppYaml ( filepaths ) ;
318
+ } , tc . error ) ;
319
+ } else if ( tc . expected ) {
320
+ const expected = path . join ( this . parent , tc . expected ) ;
321
+ const result = await findAppYaml ( filepaths ) ;
322
+ expect ( result ) . to . eql ( expected ) ;
244
323
}
245
324
} ) ;
246
325
} ) ;
@@ -333,6 +412,53 @@ describe('#updateEnvVars', () => {
333
412
} ) ;
334
413
} ) ;
335
414
415
+ describe ( '#parseDeliverables' , ( ) => {
416
+ const cases : {
417
+ only ?: boolean ;
418
+ name : string ;
419
+ input : string ;
420
+ expected ?: string [ ] ;
421
+ } [ ] = [
422
+ {
423
+ name : 'empty' ,
424
+ input : '' ,
425
+ expected : [ ] ,
426
+ } ,
427
+ {
428
+ name : 'single' ,
429
+ input : 'app.yaml' ,
430
+ expected : [ 'app.yaml' ] ,
431
+ } ,
432
+ {
433
+ name : 'multi space' ,
434
+ input : 'app.yaml foo.yaml' ,
435
+ expected : [ 'app.yaml' , 'foo.yaml' ] ,
436
+ } ,
437
+ {
438
+ name : 'multi comma' ,
439
+ input : 'app.yaml, foo.yaml' ,
440
+ expected : [ 'app.yaml' , 'foo.yaml' ] ,
441
+ } ,
442
+ {
443
+ name : 'multi comma space' ,
444
+ input : 'app.yaml,foo.yaml, bar.yaml' ,
445
+ expected : [ 'app.yaml' , 'foo.yaml' , 'bar.yaml' ] ,
446
+ } ,
447
+ {
448
+ name : 'multi-line comma space' ,
449
+ input : 'app.yaml,\nfoo.yaml, bar.yaml' ,
450
+ expected : [ 'app.yaml' , 'foo.yaml' , 'bar.yaml' ] ,
451
+ } ,
452
+ ] ;
453
+
454
+ cases . forEach ( ( tc ) => {
455
+ const fn = tc . only ? it . only : it ;
456
+ fn ( tc . name , ( ) => {
457
+ expect ( parseDeliverables ( tc . input ) ) . to . eql ( tc . expected ) ;
458
+ } ) ;
459
+ } ) ;
460
+ } ) ;
461
+
336
462
async function expectError ( fn : ( ) => Promise < void > , want : string ) {
337
463
try {
338
464
await fn ( ) ;
0 commit comments