11import { create as createBedrockYaml } from "../../lib/bedrockYaml" ;
2+ import { read as loadBedrockFile } from "../../lib/bedrockYaml" ;
23import { createTempDir } from "../../lib/ioUtil" ;
34import { disableVerboseLogging , enableVerboseLogging } from "../../logger" ;
45
6+ import * as fileUtils from "../../lib/fileutils" ;
7+
8+ import { IBedrockFile } from "../../types" ;
59import { checkDependencies , execute } from "./create" ;
610
711beforeAll ( ( ) => {
@@ -12,19 +16,50 @@ afterAll(() => {
1216 disableVerboseLogging ( ) ;
1317} ) ;
1418
15- describe ( "test valid function" , ( ) => {
16- it ( "negative test" , async ( ) => {
17- try {
18- const tmpDir = createBedrockYaml ( ) ;
19- checkDependencies ( tmpDir ) ;
20- expect ( true ) . toBe ( false ) ;
21- } catch ( e ) {
22- expect ( e ) . not . toBeNull ( ) ;
23- }
19+ describe ( "checkDependencies" , ( ) => {
20+ it ( "Project not initialized, it should fail." , async ( ) => {
21+ const tmpDir = createTempDir ( ) ;
22+ expect ( ( ) => {
23+ checkDependencies ( tmpDir , "" ) ;
24+ } ) . toThrow ( ) ;
25+ } ) ;
26+ it ( "Project initialized, it should pass." , async ( ) => {
27+ const tmpDir = createBedrockYaml ( ) ;
28+ createBedrockYaml ( tmpDir , {
29+ rings : {
30+ master : {
31+ isDefault : true
32+ }
33+ } ,
34+ services : { } ,
35+ variableGroups : [ "testvg" ]
36+ } ) ;
37+ checkDependencies ( tmpDir , "not-master" ) ;
38+ // No errors thrown, this is a pass for the function.
39+ } ) ;
40+ it ( "Project initialized, but ring already exists, it should fail." , async ( ) => {
41+ const tmpDir = createBedrockYaml ( undefined , {
42+ rings : {
43+ master : {
44+ isDefault : true
45+ }
46+ } ,
47+ services : { } ,
48+ variableGroups : [ "testvg" ]
49+ } ) ;
50+ expect ( ( ) => {
51+ checkDependencies ( tmpDir , "master" ) ;
52+ } ) . toThrow ( ) ;
2453 } ) ;
2554} ) ;
2655
2756describe ( "test execute function and logic" , ( ) => {
57+ it ( "test execute function: missing ring input" , async ( ) => {
58+ const exitFn = jest . fn ( ) ;
59+ await execute ( "" , "someprojectpath" , exitFn ) ;
60+ expect ( exitFn ) . toBeCalledTimes ( 1 ) ;
61+ expect ( exitFn . mock . calls ) . toEqual ( [ [ 1 ] ] ) ;
62+ } ) ;
2863 it ( "test execute function: missing project path" , async ( ) => {
2964 const exitFn = jest . fn ( ) ;
3065 await execute ( "ring" , "" , exitFn ) ;
@@ -33,19 +68,47 @@ describe("test execute function and logic", () => {
3368 } ) ;
3469 it ( "test execute function: working path with bedrock.yaml" , async ( ) => {
3570 const exitFn = jest . fn ( ) ;
36-
71+ const mockPipelineUpdate = jest . spyOn (
72+ fileUtils ,
73+ "updateTriggerBranchesForServiceBuildAndUpdatePipeline"
74+ ) ;
75+ mockPipelineUpdate . mockImplementation ( ) ;
3776 const tmpDir = createTempDir ( ) ;
77+
3878 createBedrockYaml ( tmpDir , {
3979 rings : {
4080 master : {
4181 isDefault : true
4282 }
4383 } ,
44- services : { } ,
84+ services : {
85+ "./my-service" : {
86+ helm : {
87+ chart : {
88+ branch : "master" ,
89+ git : "https://github.com/catalystcode/spk-demo-repo.git" ,
90+ path : "my-service"
91+ }
92+ } ,
93+ k8sBackendPort : 80
94+ }
95+ } ,
4596 variableGroups : [ "testvg" ]
4697 } ) ;
47- await execute ( "ring" , tmpDir , exitFn ) ;
4898
99+ const newRingName = "my-new-ring" ;
100+ const oldBedrockFile : IBedrockFile = loadBedrockFile ( tmpDir ) ;
101+ expect (
102+ Object . entries ( oldBedrockFile . rings ) . map ( ( [ ring ] ) => ring )
103+ ) . not . toContain ( newRingName ) ;
104+
105+ await execute ( newRingName , tmpDir , exitFn ) ;
106+
107+ const updatedBedrockFile : IBedrockFile = loadBedrockFile ( tmpDir ) ;
108+ expect (
109+ Object . entries ( updatedBedrockFile . rings ) . map ( ( [ ring ] ) => ring )
110+ ) . toContain ( newRingName ) ;
111+ expect ( mockPipelineUpdate ) . toBeCalledTimes ( 1 ) ;
49112 expect ( exitFn ) . toBeCalledTimes ( 1 ) ;
50113 expect ( exitFn . mock . calls ) . toEqual ( [ [ 0 ] ] ) ;
51114 } ) ;
0 commit comments