2
2
* Copyright SeatGeek
3
3
* Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms.
4
4
*/
5
- import { getVoidLogger } from '@backstage/backend-common ' ;
5
+ import { mockServices } from '@backstage/backend-test-utils ' ;
6
6
import { randomBytes } from 'crypto' ;
7
7
import { writeFileSync } from 'fs-extra' ;
8
8
import { tmpdir } from 'os' ;
9
9
import { PassThrough } from 'stream' ;
10
10
import { createHclMergeAction , createHclMergeFilesAction } from './hcl' ;
11
11
12
- // Since we have to
13
12
const TMP_DIR = tmpdir ( ) ;
14
13
14
+ beforeEach ( ( ) => {
15
+ jest . clearAllMocks ( ) ;
16
+ } ) ;
17
+
15
18
describe ( 'createHclMergeAction' , ( ) => {
16
19
const mockContext = {
17
- logger : getVoidLogger ( ) ,
20
+ logger : mockServices . logger . mock ( ) ,
18
21
logStream : new PassThrough ( ) ,
19
22
output : jest . fn ( ) ,
20
23
createTemporaryDirectory : jest . fn ( ) ,
@@ -29,18 +32,79 @@ describe('createHclMergeAction', () => {
29
32
description = "Name to be used on all the resources as identifier"
30
33
type = string
31
34
default = ""
35
+
36
+ map = {
37
+ foo = "bar"
38
+ }
32
39
}` ;
33
40
34
41
const b = `
35
42
variable "name" {
36
43
type = string
37
44
default = "my-name"
45
+
46
+ map = {
47
+ bar = "baz"
48
+ }
38
49
}` ;
39
50
51
+ // with mergeMapKeys set to false, the map will be overridden
40
52
const expected = `variable "name" {
41
53
default = "my-name"
42
54
description = "Name to be used on all the resources as identifier"
43
- type = string
55
+ map = {
56
+ bar = "baz"
57
+ }
58
+ type = string
59
+ }
60
+
61
+ ` ;
62
+
63
+ const mockCtx = {
64
+ ...mockContext ,
65
+ input : {
66
+ aSourceContent : a ,
67
+ bSourceContent : b ,
68
+ options : {
69
+ mergeMapKeys : false ,
70
+ } ,
71
+ } ,
72
+ } ;
73
+
74
+ // @ts -expect-error
75
+ await createHclMergeAction ( ) . handler ( mockCtx ) ;
76
+
77
+ expect ( mockCtx . output . mock . calls [ 0 ] [ 0 ] ) . toEqual ( 'hcl' ) ;
78
+ expect ( mockCtx . output . mock . calls [ 0 ] [ 1 ] ) . toEqual ( expected ) ;
79
+ } ) ;
80
+
81
+ it ( 'should merge HCL files with mergeMapKeys set to true' , async ( ) => {
82
+ const a = `
83
+ variable "name" {
84
+ type = string
85
+
86
+ map1 = {
87
+ foo = "bar"
88
+ }
89
+ }` ;
90
+
91
+ const b = `
92
+ variable "name" {
93
+ default = "my-name"
94
+
95
+ map1 = {
96
+ bar = "baz"
97
+ }
98
+ }` ;
99
+
100
+ // with mergeMapKeys set to true, the map will be merged
101
+ const expected = `variable "name" {
102
+ default = "my-name"
103
+ map1 = {
104
+ bar = "baz"
105
+ foo = "bar"
106
+ }
107
+ type = string
44
108
}
45
109
46
110
` ;
@@ -50,9 +114,13 @@ describe('createHclMergeAction', () => {
50
114
input : {
51
115
aSourceContent : a ,
52
116
bSourceContent : b ,
117
+ options : {
118
+ mergeMapKeys : true ,
119
+ } ,
53
120
} ,
54
121
} ;
55
122
123
+ // @ts -expect-error
56
124
await createHclMergeAction ( ) . handler ( mockCtx ) ;
57
125
58
126
expect ( mockCtx . output . mock . calls [ 0 ] [ 0 ] ) . toEqual ( 'hcl' ) ;
@@ -62,7 +130,7 @@ describe('createHclMergeAction', () => {
62
130
63
131
describe ( 'createHclMergeFilesAction' , ( ) => {
64
132
const mockContext = {
65
- logger : getVoidLogger ( ) ,
133
+ logger : mockServices . logger . mock ( ) ,
66
134
logStream : new PassThrough ( ) ,
67
135
output : jest . fn ( ) ,
68
136
createTemporaryDirectory : jest . fn ( ) ,
@@ -108,9 +176,13 @@ describe('createHclMergeFilesAction', () => {
108
176
input : {
109
177
aSourcePath : aPath ,
110
178
bSourcePath : bPath ,
179
+ options : {
180
+ mergeMapKeys : false ,
181
+ } ,
111
182
} ,
112
183
} ;
113
184
185
+ // @ts -expect-error
114
186
await createHclMergeFilesAction ( ) . handler ( mockCtx ) ;
115
187
116
188
expect ( mockCtx . output . mock . calls [ 0 ] [ 0 ] ) . toEqual ( 'hcl' ) ;
0 commit comments