1+ import fs from "fs" ;
12import path from "path" ;
23import { loadConfigurationFromLocalEnv , readYaml } from "../../config" ;
4+ import { safeGitUrlForLogging } from "../../lib/gitutils" ;
35import { removeDir } from "../../lib/ioUtil" ;
46import {
57 disableVerboseLogging ,
@@ -8,17 +10,28 @@ import {
810} from "../../logger" ;
911import { IInfraConfigYaml } from "../../types" ;
1012import {
13+ checkRemoteGitExist ,
14+ createGenerated ,
1115 DefinitionYAMLExistence ,
1216 dirIteration ,
1317 execute ,
1418 fetchValues ,
1519 generateConfig ,
1620 generateTfvars ,
21+ gitCheckout ,
22+ gitFetchPull ,
1723 validateDefinition ,
1824 validateRemoteSource ,
1925 validateTemplateSources
2026} from "./generate" ;
2127import * 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+ }
2235
2336beforeAll ( ( ) => {
2437 enableVerboseLogging ( ) ;
@@ -353,8 +366,124 @@ describe("Validate sources in definition.yaml files", () => {
353366 } ) ;
354367} ) ;
355368
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+
356473describe ( "Validate remote git source" , ( ) => {
357474 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+
358487 const mockParentPath = "src/commands/infra/mocks/discovery-service" ;
359488 const mockProjectPath = "src/commands/infra/mocks/discovery-service/west" ;
360489 const sourceConfiguration = validateDefinition (
0 commit comments