1
- import * as tc from '@actions/tool-cache' ;
2
1
import * as core from '@actions/core' ;
3
- import fs = require( 'fs' ) ;
2
+ import * as io from '@actions/io' ;
3
+ import * as tc from '@actions/tool-cache' ;
4
+ import fs from 'fs' ;
5
+ import cp from 'child_process' ;
4
6
import osm = require( 'os' ) ;
5
- import path = require( 'path' ) ;
6
- import { run } from '../src/main' ;
7
- import * as httpm from '@actions/http-client' ;
7
+ import path from 'path' ;
8
+ import * as main from '../src/main' ;
8
9
import * as im from '../src/installer' ;
9
- import * as sys from '../src/system' ;
10
- import { ITypedResponse } from '@actions/http-client/interfaces' ;
11
10
12
11
let goJsonData = require ( './data/golang-dl.json' ) ;
13
12
@@ -25,19 +24,25 @@ describe('setup-go', () => {
25
24
let dlSpy : jest . SpyInstance ;
26
25
let exSpy : jest . SpyInstance ;
27
26
let cacheSpy : jest . SpyInstance ;
27
+ let dbgSpy : jest . SpyInstance ;
28
+ let whichSpy : jest . SpyInstance ;
29
+ let existsSpy : jest . SpyInstance ;
30
+ let mkdirpSpy : jest . SpyInstance ;
31
+ let execSpy : jest . SpyInstance ;
28
32
29
33
beforeEach ( ( ) => {
30
34
// @actions /core
31
35
inputs = { } ;
32
36
inSpy = jest . spyOn ( core , 'getInput' ) ;
33
37
inSpy . mockImplementation ( name => inputs [ name ] ) ;
34
38
35
- // node 'os'
39
+ // node
36
40
os = { } ;
37
41
platSpy = jest . spyOn ( osm , 'platform' ) ;
38
42
platSpy . mockImplementation ( ( ) => os [ 'platform' ] ) ;
39
43
archSpy = jest . spyOn ( osm , 'arch' ) ;
40
44
archSpy . mockImplementation ( ( ) => os [ 'arch' ] ) ;
45
+ execSpy = jest . spyOn ( cp , 'execSync' ) ;
41
46
42
47
// @actions /tool-cache
43
48
findSpy = jest . spyOn ( tc , 'find' ) ;
@@ -46,9 +51,15 @@ describe('setup-go', () => {
46
51
cacheSpy = jest . spyOn ( tc , 'cacheDir' ) ;
47
52
getSpy = jest . spyOn ( im , 'getVersions' ) ;
48
53
54
+ // io
55
+ whichSpy = jest . spyOn ( io , 'which' ) ;
56
+ existsSpy = jest . spyOn ( fs , 'existsSync' ) ;
57
+ mkdirpSpy = jest . spyOn ( io , 'mkdirP' ) ;
58
+
49
59
// writes
50
60
cnSpy = jest . spyOn ( process . stdout , 'write' ) ;
51
61
logSpy = jest . spyOn ( console , 'log' ) ;
62
+ dbgSpy = jest . spyOn ( main , '_debug' ) ;
52
63
getSpy . mockImplementation ( ( ) => < im . IGoVersion [ ] > goJsonData ) ;
53
64
cnSpy . mockImplementation ( line => {
54
65
// uncomment to debug
@@ -58,11 +69,16 @@ describe('setup-go', () => {
58
69
// uncomment to debug
59
70
// process.stderr.write('log:' + line + '\n');
60
71
} ) ;
72
+ dbgSpy . mockImplementation ( msg => {
73
+ // uncomment to see debug output
74
+ // process.stderr.write(msg + '\n');
75
+ } ) ;
61
76
} ) ;
62
77
63
78
afterEach ( ( ) => {
64
79
jest . resetAllMocks ( ) ;
65
80
jest . clearAllMocks ( ) ;
81
+ //jest.restoreAllMocks();
66
82
} ) ;
67
83
68
84
afterAll ( async ( ) => { } , 100000 ) ;
@@ -164,7 +180,7 @@ describe('setup-go', () => {
164
180
165
181
let toolPath = path . normalize ( '/cache/go/1.13.0/x64' ) ;
166
182
findSpy . mockImplementation ( ( ) => toolPath ) ;
167
- await run ( ) ;
183
+ await main . run ( ) ;
168
184
169
185
expect ( logSpy ) . toHaveBeenCalledWith ( `Setup go stable version spec 1.13.0` ) ;
170
186
} ) ;
@@ -176,7 +192,7 @@ describe('setup-go', () => {
176
192
177
193
let toolPath = path . normalize ( '/cache/go/1.13.0/x64' ) ;
178
194
findSpy . mockImplementation ( ( ) => toolPath ) ;
179
- await run ( ) ;
195
+ await main . run ( ) ;
180
196
181
197
expect ( logSpy ) . toHaveBeenCalledWith ( `Setup go stable version spec 1.13.0` ) ;
182
198
} ) ;
@@ -186,7 +202,7 @@ describe('setup-go', () => {
186
202
187
203
let toolPath = path . normalize ( '/cache/go/1.13.0/x64' ) ;
188
204
findSpy . mockImplementation ( ( ) => toolPath ) ;
189
- await run ( ) ;
205
+ await main . run ( ) ;
190
206
191
207
let expPath = path . join ( toolPath , 'bin' ) ;
192
208
} ) ;
@@ -195,7 +211,7 @@ describe('setup-go', () => {
195
211
inputs [ 'go-version' ] = '1.13.0' ;
196
212
let toolPath = path . normalize ( '/cache/go/1.13.0/x64' ) ;
197
213
findSpy . mockImplementation ( ( ) => toolPath ) ;
198
- await run ( ) ;
214
+ await main . run ( ) ;
199
215
200
216
let expPath = path . join ( toolPath , 'bin' ) ;
201
217
expect ( cnSpy ) . toHaveBeenCalledWith ( `::add-path::${ expPath } ${ osm . EOL } ` ) ;
@@ -208,7 +224,7 @@ describe('setup-go', () => {
208
224
findSpy . mockImplementation ( ( ) => {
209
225
throw new Error ( errMsg ) ;
210
226
} ) ;
211
- await run ( ) ;
227
+ await main . run ( ) ;
212
228
expect ( cnSpy ) . toHaveBeenCalledWith ( '::error::' + errMsg + osm . EOL ) ;
213
229
} ) ;
214
230
@@ -223,7 +239,7 @@ describe('setup-go', () => {
223
239
let toolPath = path . normalize ( '/cache/go/1.13.0/x64' ) ;
224
240
exSpy . mockImplementation ( ( ) => '/some/other/temp/path' ) ;
225
241
cacheSpy . mockImplementation ( ( ) => toolPath ) ;
226
- await run ( ) ;
242
+ await main . run ( ) ;
227
243
228
244
let expPath = path . join ( toolPath , 'bin' ) ;
229
245
@@ -239,7 +255,7 @@ describe('setup-go', () => {
239
255
inputs [ 'go-version' ] = '9.99.9' ;
240
256
241
257
findSpy . mockImplementation ( ( ) => '' ) ;
242
- await run ( ) ;
258
+ await main . run ( ) ;
243
259
244
260
expect ( cnSpy ) . toHaveBeenCalledWith (
245
261
`::error::Could not find a version that satisfied version spec: 9.99.9${ osm . EOL } `
@@ -257,7 +273,7 @@ describe('setup-go', () => {
257
273
dlSpy . mockImplementation ( ( ) => {
258
274
throw new Error ( errMsg ) ;
259
275
} ) ;
260
- await run ( ) ;
276
+ await main . run ( ) ;
261
277
262
278
expect ( cnSpy ) . toHaveBeenCalledWith (
263
279
`::error::Failed to download version 1.13.1: Error: ${ errMsg } ${ osm . EOL } `
@@ -273,13 +289,39 @@ describe('setup-go', () => {
273
289
274
290
findSpy . mockImplementation ( ( ) => '' ) ;
275
291
getSpy . mockImplementation ( ( ) => null ) ;
276
- await run ( ) ;
292
+ await main . run ( ) ;
277
293
278
294
expect ( cnSpy ) . toHaveBeenCalledWith (
279
295
`::error::Failed to download version 1.13.1: Error: golang download url did not return results${ osm . EOL } `
280
296
) ;
281
297
} ) ;
282
298
299
+ it ( 'does not add BIN if go is not in path' , async ( ) => {
300
+ whichSpy . mockImplementation ( async ( ) => {
301
+ return '' ;
302
+ } ) ;
303
+ let added = await main . addBinToPath ( ) ;
304
+ expect ( added ) . toBeFalsy ( ) ;
305
+ } ) ;
306
+
307
+ it ( 'adds bin if dir not exists' , async ( ) => {
308
+ whichSpy . mockImplementation ( async ( ) => {
309
+ return '/usr/local/go/bin/go' ;
310
+ } ) ;
311
+
312
+ execSpy . mockImplementation ( ( ) => {
313
+ return '/Users/testuser/go' ;
314
+ } ) ;
315
+
316
+ mkdirpSpy . mockImplementation ( async ( ) => { } ) ;
317
+ existsSpy . mockImplementation ( path => {
318
+ return false ;
319
+ } ) ;
320
+
321
+ let added = await main . addBinToPath ( ) ;
322
+ expect ( added ) . toBeTruthy ;
323
+ } ) ;
324
+
283
325
// 1.13.1 => 1.13.1
284
326
// 1.13 => 1.13.0
285
327
// 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1
0 commit comments