1
+ import fs from "fs" ;
1
2
import path from "path" ;
2
3
import { loadConfigurationFromLocalEnv , readYaml } from "../../config" ;
4
+ import { safeGitUrlForLogging } from "../../lib/gitutils" ;
3
5
import { removeDir } from "../../lib/ioUtil" ;
4
6
import {
5
7
disableVerboseLogging ,
@@ -8,17 +10,28 @@ import {
8
10
} from "../../logger" ;
9
11
import { IInfraConfigYaml } from "../../types" ;
10
12
import {
13
+ checkRemoteGitExist ,
14
+ createGenerated ,
11
15
DefinitionYAMLExistence ,
12
16
dirIteration ,
13
17
execute ,
14
18
fetchValues ,
15
19
generateConfig ,
16
20
generateTfvars ,
21
+ gitCheckout ,
22
+ gitFetchPull ,
17
23
validateDefinition ,
18
24
validateRemoteSource ,
19
25
validateTemplateSources
20
26
} from "./generate" ;
21
27
import * as generate from "./generate" ;
28
+ import * as infraCommon from "./infra_common" ;
29
+
30
+ interface IGitTestData {
31
+ source : string ;
32
+ sourcePath : string ;
33
+ safeLoggingUrl : string ;
34
+ }
22
35
23
36
beforeAll ( ( ) => {
24
37
enableVerboseLogging ( ) ;
@@ -353,8 +366,124 @@ describe("Validate sources in definition.yaml files", () => {
353
366
} ) ;
354
367
} ) ;
355
368
369
+ const getMockedDataForGitTests = async (
370
+ positive : boolean
371
+ ) : Promise < IGitTestData > => {
372
+ const mockParentPath = "src/commands/infra/mocks/discovery-service" ;
373
+ const mockProjectPath = "src/commands/infra/mocks/discovery-service/west" ;
374
+ const sourceConfiguration = validateDefinition (
375
+ mockParentPath ,
376
+ mockProjectPath
377
+ ) ;
378
+ const sourceConfig = validateTemplateSources (
379
+ sourceConfiguration ,
380
+ mockParentPath ,
381
+ mockProjectPath
382
+ ) ;
383
+ let source = sourceConfig . source ! ;
384
+ if ( ! positive ) {
385
+ source += "dummy" ;
386
+ }
387
+
388
+ // Converting source name to storable folder name
389
+ const sourceFolder = await infraCommon . repoCloneRegex ( source ) ;
390
+ const sourcePath = path . join ( infraCommon . spkTemplatesPath , sourceFolder ) ;
391
+ const safeLoggingUrl = safeGitUrlForLogging ( source ) ;
392
+
393
+ return {
394
+ safeLoggingUrl,
395
+ source,
396
+ sourcePath
397
+ } ;
398
+ } ;
399
+
400
+ const testCheckRemoteGitExist = async ( positive : boolean ) => {
401
+ const { safeLoggingUrl, source, sourcePath } = await getMockedDataForGitTests (
402
+ positive
403
+ ) ;
404
+ if ( ! fs . existsSync ( sourcePath ) ) {
405
+ createGenerated ( sourcePath ) ;
406
+ }
407
+ await checkRemoteGitExist ( sourcePath , source , safeLoggingUrl ) ;
408
+ } ;
409
+
410
+ describe ( "test checkRemoteGitExist function" , ( ) => {
411
+ it ( "postive Test" , async ( ) => {
412
+ await testCheckRemoteGitExist ( true ) ;
413
+ // no exception thrown
414
+ } ) ;
415
+ // cannot do negative test because it will take too long
416
+ // and timeout
417
+ xit ( "negative Test" , async ( ) => {
418
+ try {
419
+ await testCheckRemoteGitExist ( false ) ;
420
+ expect ( true ) . toBe ( false ) ;
421
+ } catch ( e ) {
422
+ expect ( e ) . toBeDefined ( ) ;
423
+ }
424
+ } ) ;
425
+ } ) ;
426
+
427
+ const testGitFetchPull = async ( positive : boolean ) => {
428
+ const { safeLoggingUrl, sourcePath } = await getMockedDataForGitTests (
429
+ positive
430
+ ) ;
431
+ if ( ! positive || fs . existsSync ( path . join ( sourcePath , ".git" ) ) ) {
432
+ await gitFetchPull ( sourcePath , safeLoggingUrl ) ;
433
+ }
434
+ } ;
435
+
436
+ describe ( "test gitFetchPull function" , ( ) => {
437
+ it ( "postive Test" , async ( ) => {
438
+ await testGitFetchPull ( true ) ;
439
+ // no exception thrown
440
+ } ) ;
441
+ it ( "negative Test" , async ( ) => {
442
+ try {
443
+ await testGitFetchPull ( false ) ;
444
+ expect ( true ) . toBe ( false ) ;
445
+ } catch ( e ) {
446
+ expect ( e ) . toBeDefined ( ) ;
447
+ }
448
+ } ) ;
449
+ } ) ;
450
+
451
+ const testGitCheckout = async ( positive : boolean ) => {
452
+ const { sourcePath } = await getMockedDataForGitTests ( positive ) ;
453
+ if ( ! positive || fs . existsSync ( path . join ( sourcePath , ".git" ) ) ) {
454
+ await gitCheckout ( sourcePath , "v0.0.1" ) ;
455
+ }
456
+ } ;
457
+
458
+ describe ( "test gitCheckout function" , ( ) => {
459
+ it ( "postive Test" , async ( ) => {
460
+ await testGitCheckout ( true ) ;
461
+ // no exception thrown
462
+ } ) ;
463
+ it ( "negative Test" , async ( ) => {
464
+ try {
465
+ await testGitCheckout ( false ) ;
466
+ expect ( true ) . toBe ( false ) ;
467
+ } catch ( e ) {
468
+ expect ( e ) . toBeDefined ( ) ;
469
+ }
470
+ } ) ;
471
+ } ) ;
472
+
356
473
describe ( "Validate remote git source" , ( ) => {
357
474
test ( "Validating that a git source is cloned to .spk/templates" , async ( ) => {
475
+ jest
476
+ . spyOn ( generate , "checkRemoteGitExist" )
477
+ . mockImplementationOnce ( async ( ) => {
478
+ return ;
479
+ } ) ;
480
+ jest . spyOn ( generate , "gitFetchPull" ) . mockImplementationOnce ( async ( ) => {
481
+ return ;
482
+ } ) ;
483
+ jest . spyOn ( generate , "gitCheckout" ) . mockImplementationOnce ( async ( ) => {
484
+ return ;
485
+ } ) ;
486
+
358
487
const mockParentPath = "src/commands/infra/mocks/discovery-service" ;
359
488
const mockProjectPath = "src/commands/infra/mocks/discovery-service/west" ;
360
489
const sourceConfiguration = validateDefinition (
0 commit comments