@@ -13,7 +13,7 @@ describe('addNewMask', () => {
13
13
14
14
const stubGetServerAddress = sinon . stub ( ) ;
15
15
const stubProcessExit = sinon . stub ( ) ;
16
- const stubConsole = {
16
+ const stubLogger = {
17
17
debug : sinon . stub ( ) ,
18
18
error : sinon . stub ( ) ,
19
19
warn : sinon . stub ( ) ,
@@ -30,16 +30,15 @@ describe('addNewMask', () => {
30
30
31
31
before ( async ( ) => {
32
32
process . exit = stubProcessExit ;
33
- console = stubConsole ;
34
33
const { default : httpClient } = await import ( 'got' ) ;
35
34
sinon . stub ( httpClient , 'post' ) . callsFake ( stubGot . post ) ;
36
35
} ) ;
37
36
38
37
beforeEach ( ( ) => {
39
38
stubProcessExit . resetHistory ( ) ;
40
39
stubGetServerAddress . resetHistory ( ) ;
41
- for ( const stub in stubConsole ) {
42
- stubConsole [ stub ] . resetHistory ( ) ;
40
+ for ( const stub in stubLogger ) {
41
+ stubLogger [ stub ] . resetHistory ( ) ;
43
42
}
44
43
for ( const stub in stubGot ) {
45
44
stubGot [ stub ] . resetHistory ( ) ;
@@ -78,6 +77,9 @@ describe('addNewMask', () => {
78
77
it ( 'should fail if the server address is not available' , async ( ) => {
79
78
stubGetServerAddress . rejects ( 'could not get server address' ) ;
80
79
const { updateMasks, exitHandler } = proxyquire ( '../lib/addNewMask' , {
80
+ '@codefresh-io/cf-telemetry/logs' : {
81
+ Logger : function ( ) { return stubLogger } ,
82
+ } ,
81
83
'./helpers' : {
82
84
getServerAddress : stubGetServerAddress ,
83
85
} ,
@@ -88,13 +90,16 @@ describe('addNewMask', () => {
88
90
}
89
91
} ) ;
90
92
await updateMasks ( secret ) ;
91
- expect ( stubConsole . error ) . to . have . been . calledOnceWith ( 'could not create mask for secret: 123. Error: could not get server address' ) ;
93
+ expect ( stubLogger . error ) . to . have . been . calledOnceWith ( 'could not create mask for secret: 123. Error: could not get server address' ) ;
92
94
expect ( stubProcessExit ) . to . have . been . calledOnceWith ( 1 ) ;
93
95
} ) ;
94
96
95
97
it ( 'should fail if the server address is not valid URL' , async ( ) => {
96
98
stubGetServerAddress . resolves ( 'foo' ) ;
97
99
const { updateMasks, exitHandler } = proxyquire ( '../lib/addNewMask' , {
100
+ '@codefresh-io/cf-telemetry/logs' : {
101
+ Logger : function ( ) { return stubLogger } ,
102
+ } ,
98
103
'./helpers' : {
99
104
getServerAddress : stubGetServerAddress ,
100
105
} ,
@@ -105,7 +110,7 @@ describe('addNewMask', () => {
105
110
}
106
111
} ) ;
107
112
await updateMasks ( secret ) ;
108
- expect ( stubConsole . error ) . to . have . been . calledOnceWith ( 'could not create mask for secret: 123. Error: TypeError: Invalid URL' ) ;
113
+ expect ( stubLogger . error ) . to . have . been . calledOnceWith ( 'could not create mask for secret: 123. Error: TypeError: Invalid URL' ) ;
109
114
expect ( stubProcessExit ) . to . have . been . calledOnceWith ( 1 ) ;
110
115
} ) ;
111
116
@@ -117,6 +122,9 @@ describe('addNewMask', () => {
117
122
body : 'Internal Server Error' ,
118
123
} ) ;
119
124
const { updateMasks, exitHandler } = proxyquire ( '../lib/addNewMask' , {
125
+ '@codefresh-io/cf-telemetry/logs' : {
126
+ Logger : function ( ) { return stubLogger } ,
127
+ } ,
120
128
'./helpers' : { getServerAddress : stubGetServerAddress } ,
121
129
} ) ;
122
130
process . listeners ( 'exit' ) . forEach ( ( listener ) => {
@@ -125,27 +133,35 @@ describe('addNewMask', () => {
125
133
}
126
134
} ) ;
127
135
await updateMasks ( secret ) ;
128
- expect ( stubConsole . error ) . to . have . been . calledOnceWith ( 'could not create mask for secret: 123. Server responded with: 500\n\nInternal Server Error' ) ;
136
+ expect ( stubLogger . error ) . to . have . been . calledOnceWith ( 'could not create mask for secret: 123. Server responded with: 500\n\nInternal Server Error' ) ;
129
137
expect ( stubProcessExit ) . to . have . been . calledOnceWith ( 1 ) ;
130
138
} ) ;
131
139
} ) ;
132
140
133
141
describe ( 'exitHandler' , ( ) => {
134
142
it ( 'should set exit code to 3 if the original exit code is 0 and variable was not masked' , ( ) => {
135
- const { exitHandler } = proxyquire ( '../lib/addNewMask' , { } ) ;
143
+ const { exitHandler } = proxyquire ( '../lib/addNewMask' , {
144
+ '@codefresh-io/cf-telemetry/logs' : {
145
+ Logger : function ( ) { return stubLogger } ,
146
+ } ,
147
+ } ) ;
136
148
process . listeners ( 'exit' ) . forEach ( ( listener ) => {
137
149
if ( listener === exitHandler ) {
138
150
process . removeListener ( 'exit' , listener ) ;
139
151
}
140
152
} ) ;
141
153
exitHandler ( 0 ) ;
142
154
expect ( process . exitCode ) . to . be . equal ( 3 ) ;
143
- expect ( stubConsole . warn ) . to . have . been . calledOnceWith ( 'Unexpected exit with code 0. Exiting with 3 instead' ) ;
155
+ expect ( stubLogger . warn ) . to . have . been . calledOnceWith ( 'Unexpected exit with code 0. Exiting with 3 instead' ) ;
144
156
process . exitCode = undefined ;
145
157
} ) ;
146
158
147
159
it ( 'should set exit code to 3 if the original exit code is 0 and variable was not masked' , ( ) => {
148
- const { exitHandler } = proxyquire ( '../lib/addNewMask' , { } ) ;
160
+ const { exitHandler } = proxyquire ( '../lib/addNewMask' , {
161
+ '@codefresh-io/cf-telemetry/logs' : {
162
+ Logger : function ( ) { return stubLogger } ,
163
+ } ,
164
+ } ) ;
149
165
process . listeners ( 'exit' ) . forEach ( ( listener ) => {
150
166
if ( listener === exitHandler ) {
151
167
process . removeListener ( 'exit' , listener ) ;
@@ -157,7 +173,7 @@ describe('addNewMask', () => {
157
173
process . exitCode = 0 ;
158
174
exitHandler ( ) ;
159
175
expect ( process . exitCode ) . to . be . equal ( 3 ) ;
160
- expect ( stubConsole . warn ) . to . have . been . calledOnceWith ( 'Unexpected exit with code 0. Exiting with 3 instead' ) ;
176
+ expect ( stubLogger . warn ) . to . have . been . calledOnceWith ( 'Unexpected exit with code 0. Exiting with 3 instead' ) ;
161
177
process . exitCode = 0 ;
162
178
} ) ;
163
179
@@ -175,7 +191,7 @@ describe('addNewMask', () => {
175
191
} ) ;
176
192
await updateMasks ( secret ) ;
177
193
expect ( process . exitCode ) . not . to . be . equal ( 3 ) ;
178
- expect ( stubConsole . warn ) . not . to . have . been . calledOnceWith ( 'Unexpected exit with code 0. Exiting with 3 instead' ) ;
194
+ expect ( stubLogger . warn ) . not . to . have . been . calledOnceWith ( 'Unexpected exit with code 0. Exiting with 3 instead' ) ;
179
195
} ) ;
180
196
} ) ;
181
197
} ) ;
0 commit comments