@@ -6,84 +6,74 @@ import * as path from 'path'
6
6
import * as core from '@actions/core'
7
7
8
8
describe ( 'run.ts' , ( ) => {
9
+ const downloadBaseURL = 'https://test.tld'
10
+
11
+ // Cleanup mocks after each test to ensure that subsequent tests are not affected by the mocks.
12
+ afterEach ( ( ) => {
13
+ jest . restoreAllMocks ( )
14
+ } )
15
+
9
16
test ( 'getExecutableExtension() - return .exe when os is Windows' , ( ) => {
10
- jest . spyOn ( os , 'type ' ) . mockReturnValue ( 'Windows_NT ' )
17
+ jest . spyOn ( os , 'platform ' ) . mockReturnValue ( 'win32 ' )
11
18
12
19
expect ( run . getExecutableExtension ( ) ) . toBe ( '.exe' )
13
- expect ( os . type ) . toHaveBeenCalled ( )
20
+ expect ( os . platform ) . toHaveBeenCalled ( )
14
21
} )
15
22
16
23
test ( 'getExecutableExtension() - return empty string for non-windows OS' , ( ) => {
17
- jest . spyOn ( os , 'type ' ) . mockReturnValue ( 'Darwin ' )
24
+ jest . spyOn ( os , 'platform ' ) . mockReturnValue ( 'darwin ' )
18
25
19
26
expect ( run . getExecutableExtension ( ) ) . toBe ( '' )
20
- expect ( os . type ) . toHaveBeenCalled ( )
27
+ expect ( os . platform ) . toHaveBeenCalled ( )
21
28
} )
22
29
23
- test ( 'getHelmDownloadURL() - return the URL to download helm for Linux' , ( ) => {
24
- const downloadBaseURL = 'https://test.tld'
30
+ test ( 'getHelmDownloadURL() - return the URL to download helm for Linux amd64' , ( ) => {
31
+ jest . spyOn ( os , 'platform' ) . mockReturnValue ( 'linux' )
32
+ jest . spyOn ( os , 'arch' ) . mockReturnValue ( 'x64' )
33
+ const expected = 'https://test.tld/helm-v3.8.0-linux-amd64.tar.gz'
25
34
26
- jest . spyOn ( os , 'type' ) . mockReturnValue ( 'Linux' )
27
- jest . spyOn ( os , 'arch' ) . mockReturnValueOnce ( 'unknown' )
28
- const helmLinuxUrl = 'https://test.tld/helm-v3.8.0-linux-amd64.zip'
29
-
30
- expect ( run . getHelmDownloadURL ( downloadBaseURL , 'v3.8.0' ) ) . toBe (
31
- helmLinuxUrl
32
- )
33
- expect ( os . type ) . toHaveBeenCalled ( )
35
+ expect ( run . getHelmDownloadURL ( downloadBaseURL , 'v3.8.0' ) ) . toBe ( expected )
36
+ expect ( os . platform ) . toHaveBeenCalled ( )
34
37
expect ( os . arch ) . toHaveBeenCalled ( )
38
+ } )
35
39
36
- // arm64
37
- jest . spyOn ( os , 'type ' ) . mockReturnValue ( 'Linux ' )
38
- jest . spyOn ( os , 'arch' ) . mockReturnValueOnce ( 'arm64' )
39
- const helmLinuxArm64Url = 'https://test.tld/helm-v3.8.0-linux-arm64.zip '
40
+ test ( 'getHelmDownloadURL() - return the URL to download helm for Linux arm64' , ( ) => {
41
+ jest . spyOn ( os , 'platform ' ) . mockReturnValue ( 'linux ' )
42
+ jest . spyOn ( os , 'arch' ) . mockReturnValue ( 'arm64' )
43
+ const expected = 'https://test.tld/helm-v3.8.0-linux-arm64.tar.gz '
40
44
41
- expect ( run . getHelmDownloadURL ( downloadBaseURL , 'v3.8.0' ) ) . toBe (
42
- helmLinuxArm64Url
43
- )
44
- expect ( os . type ) . toHaveBeenCalled ( )
45
+ expect ( run . getHelmDownloadURL ( downloadBaseURL , 'v3.8.0' ) ) . toBe ( expected )
46
+ expect ( os . platform ) . toHaveBeenCalled ( )
45
47
expect ( os . arch ) . toHaveBeenCalled ( )
46
48
} )
47
49
48
- test ( 'getHelmDownloadURL() - return the URL to download helm for Darwin' , ( ) => {
49
- const downloadBaseURL = 'https://test.tld'
50
-
51
- jest . spyOn ( os , 'type' ) . mockReturnValue ( 'Darwin' )
52
- jest . spyOn ( os , 'arch' ) . mockReturnValueOnce ( 'unknown' )
53
- const helmDarwinUrl = 'https://test.tld/helm-v3.8.0-darwin-amd64.zip'
50
+ test ( 'getHelmDownloadURL() - return the URL to download helm for Darwin x64' , ( ) => {
51
+ jest . spyOn ( os , 'platform' ) . mockReturnValue ( 'darwin' )
52
+ jest . spyOn ( os , 'arch' ) . mockReturnValue ( 'x64' )
53
+ const expected = 'https://test.tld/helm-v3.8.0-darwin-amd64.tar.gz'
54
54
55
- expect ( run . getHelmDownloadURL ( downloadBaseURL , 'v3.8.0' ) ) . toBe (
56
- helmDarwinUrl
57
- )
58
- expect ( os . type ) . toHaveBeenCalled ( )
55
+ expect ( run . getHelmDownloadURL ( downloadBaseURL , 'v3.8.0' ) ) . toBe ( expected )
56
+ expect ( os . platform ) . toHaveBeenCalled ( )
59
57
expect ( os . arch ) . toHaveBeenCalled ( )
58
+ } )
60
59
61
- // arm64
62
- jest . spyOn ( os , 'type ' ) . mockReturnValue ( 'Darwin ' )
63
- jest . spyOn ( os , 'arch' ) . mockReturnValueOnce ( 'arm64' )
64
- const helmDarwinArm64Url = 'https://test.tld/helm-v3.8.0-darwin-arm64.zip '
60
+ test ( 'getHelmDownloadURL() - return the URL to download helm for Darwin arm64' , ( ) => {
61
+ jest . spyOn ( os , 'platform ' ) . mockReturnValue ( 'darwin ' )
62
+ jest . spyOn ( os , 'arch' ) . mockReturnValue ( 'arm64' )
63
+ const expected = 'https://test.tld/helm-v3.8.0-darwin-arm64.tar.gz '
65
64
66
- expect ( run . getHelmDownloadURL ( downloadBaseURL , 'v3.8.0' ) ) . toBe (
67
- helmDarwinArm64Url
68
- )
69
- expect ( os . type ) . toHaveBeenCalled ( )
65
+ expect ( run . getHelmDownloadURL ( downloadBaseURL , 'v3.8.0' ) ) . toBe ( expected )
66
+ expect ( os . platform ) . toHaveBeenCalled ( )
70
67
expect ( os . arch ) . toHaveBeenCalled ( )
71
68
} )
72
69
73
- test ( 'getValidVersion() - return version with v prepended' , ( ) => {
74
- expect ( run . getValidVersion ( '3.8.0' ) ) . toBe ( 'v3.8.0' )
75
- } )
76
-
77
70
test ( 'getHelmDownloadURL() - return the URL to download helm for Windows' , ( ) => {
78
- const downloadBaseURL = 'https://test.tld'
79
-
80
- jest . spyOn ( os , 'type' ) . mockReturnValue ( 'Windows_NT' )
71
+ jest . spyOn ( os , 'platform' ) . mockReturnValue ( 'win32' )
72
+ jest . spyOn ( os , 'arch' ) . mockReturnValue ( 'x64' )
81
73
82
- const helmWindowsUrl = 'https://test.tld/helm-v3.8.0-windows-amd64.zip'
83
- expect ( run . getHelmDownloadURL ( downloadBaseURL , 'v3.8.0' ) ) . toBe (
84
- helmWindowsUrl
85
- )
86
- expect ( os . type ) . toHaveBeenCalled ( )
74
+ const expected = 'https://test.tld/helm-v3.8.0-windows-amd64.zip'
75
+ expect ( run . getHelmDownloadURL ( downloadBaseURL , 'v3.8.0' ) ) . toBe ( expected )
76
+ expect ( os . platform ) . toHaveBeenCalled ( )
87
77
} )
88
78
89
79
test ( 'getLatestHelmVersion() - return the latest version of HELM' , async ( ) => {
@@ -101,6 +91,10 @@ describe('run.ts', () => {
101
91
expect ( await run . getLatestHelmVersion ( ) ) . toBe ( 'v3.13.3' )
102
92
} )
103
93
94
+ test ( 'getValidVersion() - return version with v prepended' , ( ) => {
95
+ expect ( run . getValidVersion ( '3.8.0' ) ) . toBe ( 'v3.8.0' )
96
+ } )
97
+
104
98
test ( 'walkSync() - return path to the all files matching fileToFind in dir' , ( ) => {
105
99
jest . spyOn ( fs , 'readdirSync' ) . mockImplementation ( ( file , _ ) => {
106
100
if ( file == 'mainFolder' )
@@ -120,6 +114,7 @@ describe('run.ts', () => {
120
114
'file21' as unknown as fs . Dirent ,
121
115
'file22' as unknown as fs . Dirent
122
116
]
117
+ return [ ]
123
118
} )
124
119
jest . spyOn ( core , 'debug' ) . mockImplementation ( )
125
120
jest . spyOn ( fs , 'statSync' ) . mockImplementation ( ( file ) => {
@@ -154,6 +149,7 @@ describe('run.ts', () => {
154
149
'file21' as unknown as fs . Dirent ,
155
150
'file22' as unknown as fs . Dirent
156
151
]
152
+ return [ ]
157
153
} )
158
154
jest . spyOn ( core , 'debug' ) . mockImplementation ( )
159
155
jest . spyOn ( fs , 'statSync' ) . mockImplementation ( ( file ) => {
@@ -171,13 +167,14 @@ describe('run.ts', () => {
171
167
jest . spyOn ( fs , 'chmodSync' ) . mockImplementation ( ( ) => { } )
172
168
jest . spyOn ( fs , 'readdirSync' ) . mockImplementation ( ( file , _ ) => {
173
169
if ( file == 'mainFolder' ) return [ 'helm.exe' as unknown as fs . Dirent ]
170
+ return [ ]
174
171
} )
175
172
jest . spyOn ( fs , 'statSync' ) . mockImplementation ( ( file ) => {
176
173
const isDirectory =
177
174
( file as string ) . indexOf ( 'folder' ) == - 1 ? false : true
178
175
return { isDirectory : ( ) => isDirectory } as fs . Stats
179
176
} )
180
- jest . spyOn ( os , 'type ' ) . mockReturnValue ( 'Windows_NT ' )
177
+ jest . spyOn ( os , 'platform ' ) . mockReturnValue ( 'win32 ' )
181
178
182
179
expect ( run . findHelm ( 'mainFolder' ) ) . toBe (
183
180
path . join ( 'mainFolder' , 'helm.exe' )
@@ -188,11 +185,13 @@ describe('run.ts', () => {
188
185
jest . spyOn ( fs , 'chmodSync' ) . mockImplementation ( ( ) => { } )
189
186
jest . spyOn ( fs , 'readdirSync' ) . mockImplementation ( ( file , _ ) => {
190
187
if ( file == 'mainFolder' ) return [ ]
188
+ return [ ]
191
189
} )
192
190
jest . spyOn ( fs , 'statSync' ) . mockImplementation ( ( file ) => {
193
191
return { isDirectory : ( ) => true } as fs . Stats
194
192
} )
195
- jest . spyOn ( os , 'type' ) . mockReturnValue ( 'Windows_NT' )
193
+ jest . spyOn ( os , 'platform' ) . mockReturnValue ( 'win32' )
194
+
196
195
expect ( ( ) => run . findHelm ( 'mainFolder' ) ) . toThrow (
197
196
'Helm executable not found in path mainFolder'
198
197
)
@@ -203,11 +202,9 @@ describe('run.ts', () => {
203
202
jest . spyOn ( toolCache , 'downloadTool' ) . mockResolvedValue ( 'pathToTool' )
204
203
const response = JSON . stringify ( [ { tag_name : 'v4.0.0' } ] )
205
204
jest . spyOn ( fs , 'readFileSync' ) . mockReturnValue ( response )
206
- jest . spyOn ( os , 'type ' ) . mockReturnValue ( 'Windows_NT ' )
205
+ jest . spyOn ( os , 'platform ' ) . mockReturnValue ( 'win32 ' )
207
206
jest . spyOn ( fs , 'chmodSync' ) . mockImplementation ( ( ) => { } )
208
- jest
209
- . spyOn ( toolCache , 'extractZip' )
210
- . mockResolvedValue ( 'pathToUnzippedHelm' )
207
+ jest . spyOn ( toolCache , 'extractZip' ) . mockResolvedValue ( 'extractedPath' )
211
208
jest . spyOn ( toolCache , 'cacheDir' ) . mockResolvedValue ( 'pathToCachedDir' )
212
209
jest
213
210
. spyOn ( fs , 'readdirSync' )
@@ -218,9 +215,7 @@ describe('run.ts', () => {
218
215
return { isDirectory : ( ) => isDirectory } as fs . Stats
219
216
} )
220
217
221
- const baseURL = 'https://test.tld'
222
-
223
- expect ( await run . downloadHelm ( baseURL , 'v4.0.0' ) ) . toBe (
218
+ expect ( await run . downloadHelm ( downloadBaseURL , 'v4.0.0' ) ) . toBe (
224
219
path . join ( 'pathToCachedDir' , 'helm.exe' )
225
220
)
226
221
expect ( toolCache . find ) . toHaveBeenCalledWith ( 'helm' , 'v4.0.0' )
@@ -240,26 +235,33 @@ describe('run.ts', () => {
240
235
jest . spyOn ( toolCache , 'downloadTool' ) . mockImplementation ( async ( ) => {
241
236
throw 'Unable to download'
242
237
} )
243
- jest . spyOn ( os , 'type' ) . mockReturnValue ( 'Windows_NT' )
244
-
245
- const baseURL = 'https://test.tld'
238
+ jest . spyOn ( os , 'platform' ) . mockReturnValue ( 'win32' )
246
239
247
- await expect ( run . downloadHelm ( baseURL , 'v3.2.1' ) ) . rejects . toThrow (
248
- 'Failed to download Helm from location https://test.tld/helm-v3.2.1-windows-amd64.zip'
240
+ const downloadUrl = 'https://test.tld/helm-v3.2.1-windows-amd64.zip'
241
+ await expect ( run . downloadHelm ( downloadBaseURL , 'v3.2.1' ) ) . rejects . toThrow (
242
+ `Failed to download Helm from location ${ downloadUrl } `
249
243
)
250
244
expect ( toolCache . find ) . toHaveBeenCalledWith ( 'helm' , 'v3.2.1' )
251
- expect ( toolCache . downloadTool ) . toHaveBeenCalledWith (
252
- 'https://test.tld/helm-v3.2.1-windows-amd64.zip'
253
- )
245
+ expect ( toolCache . downloadTool ) . toHaveBeenCalledWith ( `${ downloadUrl } ` )
254
246
} )
255
247
256
248
test ( 'downloadHelm() - return path to helm tool with same version from toolCache' , async ( ) => {
257
249
jest . spyOn ( toolCache , 'find' ) . mockReturnValue ( 'pathToCachedDir' )
250
+ jest . spyOn ( toolCache , 'cacheDir' ) . mockResolvedValue ( 'pathToCachedDir' )
251
+ jest . spyOn ( toolCache , 'downloadTool' ) . mockResolvedValue ( 'pathToTool' )
252
+ jest . spyOn ( toolCache , 'extractZip' ) . mockResolvedValue ( 'extractedPath' )
253
+ jest . spyOn ( os , 'platform' ) . mockReturnValue ( 'win32' )
258
254
jest . spyOn ( fs , 'chmodSync' ) . mockImplementation ( ( ) => { } )
255
+ jest
256
+ . spyOn ( fs , 'readdirSync' )
257
+ . mockReturnValue ( [ 'helm.exe' as unknown as fs . Dirent ] )
258
+ jest . spyOn ( fs , 'statSync' ) . mockImplementation ( ( file ) => {
259
+ const isDirectory =
260
+ ( file as string ) . indexOf ( 'folder' ) == - 1 ? false : true
261
+ return { isDirectory : ( ) => isDirectory } as fs . Stats
262
+ } )
259
263
260
- const baseURL = 'https://test.tld'
261
-
262
- expect ( await run . downloadHelm ( baseURL , 'v3.2.1' ) ) . toBe (
264
+ expect ( await run . downloadHelm ( downloadBaseURL , 'v3.2.1' ) ) . toBe (
263
265
path . join ( 'pathToCachedDir' , 'helm.exe' )
264
266
)
265
267
expect ( toolCache . find ) . toHaveBeenCalledWith ( 'helm' , 'v3.2.1' )
@@ -272,22 +274,19 @@ describe('run.ts', () => {
272
274
test ( 'downloadHelm() - throw error is helm is not found in path' , async ( ) => {
273
275
jest . spyOn ( toolCache , 'find' ) . mockReturnValue ( '' )
274
276
jest . spyOn ( toolCache , 'downloadTool' ) . mockResolvedValue ( 'pathToTool' )
275
- jest . spyOn ( os , 'type' ) . mockReturnValue ( 'Windows_NT' )
276
- jest . spyOn ( fs , 'chmodSync' ) . mockImplementation ( )
277
- jest
278
- . spyOn ( toolCache , 'extractZip' )
279
- . mockResolvedValue ( 'pathToUnzippedHelm' )
280
277
jest . spyOn ( toolCache , 'cacheDir' ) . mockResolvedValue ( 'pathToCachedDir' )
278
+ jest . spyOn ( toolCache , 'downloadTool' ) . mockResolvedValue ( 'pathToTool' )
279
+ jest . spyOn ( toolCache , 'extractZip' ) . mockResolvedValue ( 'extractedPath' )
280
+ jest . spyOn ( os , 'platform' ) . mockReturnValue ( 'win32' )
281
+ jest . spyOn ( fs , 'chmodSync' ) . mockImplementation ( )
281
282
jest . spyOn ( fs , 'readdirSync' ) . mockImplementation ( ( file , _ ) => [ ] )
282
283
jest . spyOn ( fs , 'statSync' ) . mockImplementation ( ( file ) => {
283
284
const isDirectory =
284
285
( file as string ) . indexOf ( 'folder' ) == - 1 ? false : true
285
286
return { isDirectory : ( ) => isDirectory } as fs . Stats
286
287
} )
287
288
288
- const baseURL = 'https://test.tld'
289
-
290
- await expect ( run . downloadHelm ( baseURL , 'v3.2.1' ) ) . rejects . toThrow (
289
+ await expect ( run . downloadHelm ( downloadBaseURL , 'v3.2.1' ) ) . rejects . toThrow (
291
290
'Helm executable not found in path pathToCachedDir'
292
291
)
293
292
expect ( toolCache . find ) . toHaveBeenCalledWith ( 'helm' , 'v3.2.1' )
0 commit comments