1- import * as tc from '@actions/tool-cache' ;
21import * 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' ;
46import 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' ;
89import * as im from '../src/installer' ;
9- import * as sys from '../src/system' ;
10- import { ITypedResponse } from '@actions/http-client/interfaces' ;
1110
1211let goJsonData = require ( './data/golang-dl.json' ) ;
1312
@@ -25,19 +24,25 @@ describe('setup-go', () => {
2524 let dlSpy : jest . SpyInstance ;
2625 let exSpy : jest . SpyInstance ;
2726 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 ;
2832
2933 beforeEach ( ( ) => {
3034 // @actions /core
3135 inputs = { } ;
3236 inSpy = jest . spyOn ( core , 'getInput' ) ;
3337 inSpy . mockImplementation ( name => inputs [ name ] ) ;
3438
35- // node 'os'
39+ // node
3640 os = { } ;
3741 platSpy = jest . spyOn ( osm , 'platform' ) ;
3842 platSpy . mockImplementation ( ( ) => os [ 'platform' ] ) ;
3943 archSpy = jest . spyOn ( osm , 'arch' ) ;
4044 archSpy . mockImplementation ( ( ) => os [ 'arch' ] ) ;
45+ execSpy = jest . spyOn ( cp , 'execSync' ) ;
4146
4247 // @actions /tool-cache
4348 findSpy = jest . spyOn ( tc , 'find' ) ;
@@ -46,9 +51,15 @@ describe('setup-go', () => {
4651 cacheSpy = jest . spyOn ( tc , 'cacheDir' ) ;
4752 getSpy = jest . spyOn ( im , 'getVersions' ) ;
4853
54+ // io
55+ whichSpy = jest . spyOn ( io , 'which' ) ;
56+ existsSpy = jest . spyOn ( fs , 'existsSync' ) ;
57+ mkdirpSpy = jest . spyOn ( io , 'mkdirP' ) ;
58+
4959 // writes
5060 cnSpy = jest . spyOn ( process . stdout , 'write' ) ;
5161 logSpy = jest . spyOn ( console , 'log' ) ;
62+ dbgSpy = jest . spyOn ( main , '_debug' ) ;
5263 getSpy . mockImplementation ( ( ) => < im . IGoVersion [ ] > goJsonData ) ;
5364 cnSpy . mockImplementation ( line => {
5465 // uncomment to debug
@@ -58,11 +69,16 @@ describe('setup-go', () => {
5869 // uncomment to debug
5970 // process.stderr.write('log:' + line + '\n');
6071 } ) ;
72+ dbgSpy . mockImplementation ( msg => {
73+ // uncomment to see debug output
74+ // process.stderr.write(msg + '\n');
75+ } ) ;
6176 } ) ;
6277
6378 afterEach ( ( ) => {
6479 jest . resetAllMocks ( ) ;
6580 jest . clearAllMocks ( ) ;
81+ //jest.restoreAllMocks();
6682 } ) ;
6783
6884 afterAll ( async ( ) => { } , 100000 ) ;
@@ -164,7 +180,7 @@ describe('setup-go', () => {
164180
165181 let toolPath = path . normalize ( '/cache/go/1.13.0/x64' ) ;
166182 findSpy . mockImplementation ( ( ) => toolPath ) ;
167- await run ( ) ;
183+ await main . run ( ) ;
168184
169185 expect ( logSpy ) . toHaveBeenCalledWith ( `Setup go stable version spec 1.13.0` ) ;
170186 } ) ;
@@ -176,7 +192,7 @@ describe('setup-go', () => {
176192
177193 let toolPath = path . normalize ( '/cache/go/1.13.0/x64' ) ;
178194 findSpy . mockImplementation ( ( ) => toolPath ) ;
179- await run ( ) ;
195+ await main . run ( ) ;
180196
181197 expect ( logSpy ) . toHaveBeenCalledWith ( `Setup go stable version spec 1.13.0` ) ;
182198 } ) ;
@@ -186,7 +202,7 @@ describe('setup-go', () => {
186202
187203 let toolPath = path . normalize ( '/cache/go/1.13.0/x64' ) ;
188204 findSpy . mockImplementation ( ( ) => toolPath ) ;
189- await run ( ) ;
205+ await main . run ( ) ;
190206
191207 let expPath = path . join ( toolPath , 'bin' ) ;
192208 } ) ;
@@ -195,7 +211,7 @@ describe('setup-go', () => {
195211 inputs [ 'go-version' ] = '1.13.0' ;
196212 let toolPath = path . normalize ( '/cache/go/1.13.0/x64' ) ;
197213 findSpy . mockImplementation ( ( ) => toolPath ) ;
198- await run ( ) ;
214+ await main . run ( ) ;
199215
200216 let expPath = path . join ( toolPath , 'bin' ) ;
201217 expect ( cnSpy ) . toHaveBeenCalledWith ( `::add-path::${ expPath } ${ osm . EOL } ` ) ;
@@ -208,7 +224,7 @@ describe('setup-go', () => {
208224 findSpy . mockImplementation ( ( ) => {
209225 throw new Error ( errMsg ) ;
210226 } ) ;
211- await run ( ) ;
227+ await main . run ( ) ;
212228 expect ( cnSpy ) . toHaveBeenCalledWith ( '::error::' + errMsg + osm . EOL ) ;
213229 } ) ;
214230
@@ -223,7 +239,7 @@ describe('setup-go', () => {
223239 let toolPath = path . normalize ( '/cache/go/1.13.0/x64' ) ;
224240 exSpy . mockImplementation ( ( ) => '/some/other/temp/path' ) ;
225241 cacheSpy . mockImplementation ( ( ) => toolPath ) ;
226- await run ( ) ;
242+ await main . run ( ) ;
227243
228244 let expPath = path . join ( toolPath , 'bin' ) ;
229245
@@ -239,7 +255,7 @@ describe('setup-go', () => {
239255 inputs [ 'go-version' ] = '9.99.9' ;
240256
241257 findSpy . mockImplementation ( ( ) => '' ) ;
242- await run ( ) ;
258+ await main . run ( ) ;
243259
244260 expect ( cnSpy ) . toHaveBeenCalledWith (
245261 `::error::Could not find a version that satisfied version spec: 9.99.9${ osm . EOL } `
@@ -257,7 +273,7 @@ describe('setup-go', () => {
257273 dlSpy . mockImplementation ( ( ) => {
258274 throw new Error ( errMsg ) ;
259275 } ) ;
260- await run ( ) ;
276+ await main . run ( ) ;
261277
262278 expect ( cnSpy ) . toHaveBeenCalledWith (
263279 `::error::Failed to download version 1.13.1: Error: ${ errMsg } ${ osm . EOL } `
@@ -273,13 +289,39 @@ describe('setup-go', () => {
273289
274290 findSpy . mockImplementation ( ( ) => '' ) ;
275291 getSpy . mockImplementation ( ( ) => null ) ;
276- await run ( ) ;
292+ await main . run ( ) ;
277293
278294 expect ( cnSpy ) . toHaveBeenCalledWith (
279295 `::error::Failed to download version 1.13.1: Error: golang download url did not return results${ osm . EOL } `
280296 ) ;
281297 } ) ;
282298
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+
283325 // 1.13.1 => 1.13.1
284326 // 1.13 => 1.13.0
285327 // 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1
0 commit comments