Skip to content

Commit 1c06f0e

Browse files
couple tests
1 parent a030287 commit 1c06f0e

File tree

3 files changed

+94
-40
lines changed

3 files changed

+94
-40
lines changed

__tests__/setup-go.test.ts

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import * as tc from '@actions/tool-cache';
21
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';
46
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';
89
import * as im from '../src/installer';
9-
import * as sys from '../src/system';
10-
import {ITypedResponse} from '@actions/http-client/interfaces';
1110

1211
let 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

dist/index.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,14 +1274,17 @@ var __importStar = (this && this.__importStar) || function (mod) {
12741274
result["default"] = mod;
12751275
return result;
12761276
};
1277+
var __importDefault = (this && this.__importDefault) || function (mod) {
1278+
return (mod && mod.__esModule) ? mod : { "default": mod };
1279+
};
12771280
Object.defineProperty(exports, "__esModule", { value: true });
12781281
const core = __importStar(__webpack_require__(470));
12791282
const io = __importStar(__webpack_require__(1));
12801283
const tc = __importStar(__webpack_require__(533));
12811284
const installer = __importStar(__webpack_require__(749));
1282-
const path = __importStar(__webpack_require__(622));
1283-
const cp = __importStar(__webpack_require__(129));
1284-
const fs = __importStar(__webpack_require__(747));
1285+
const path_1 = __importDefault(__webpack_require__(622));
1286+
const child_process_1 = __importDefault(__webpack_require__(129));
1287+
const fs_1 = __importDefault(__webpack_require__(747));
12851288
function run() {
12861289
return __awaiter(this, void 0, void 0, function* () {
12871290
try {
@@ -1303,7 +1306,7 @@ function run() {
13031306
}
13041307
if (installDir) {
13051308
core.exportVariable('GOROOT', installDir);
1306-
core.addPath(path.join(installDir, 'bin'));
1309+
core.addPath(path_1.default.join(installDir, 'bin'));
13071310
console.log('Added go to the path');
13081311
let added = addBinToPath();
13091312
core.debug(`add bin ${added}`);
@@ -1313,7 +1316,7 @@ function run() {
13131316
}
13141317
}
13151318
// add problem matchers
1316-
const matchersPath = path.join(__dirname, '..', 'matchers.json');
1319+
const matchersPath = path_1.default.join(__dirname, '..', 'matchers.json');
13171320
console.log(`##[add-matcher]${matchersPath}`);
13181321
}
13191322
catch (error) {
@@ -1326,23 +1329,23 @@ function addBinToPath() {
13261329
return __awaiter(this, void 0, void 0, function* () {
13271330
let added = false;
13281331
let g = yield io.which('go');
1329-
core.debug(`which go :${g}:`);
1332+
_debug(`which go :${g}:`);
13301333
if (!g) {
1331-
core.debug('go not in the path');
1334+
_debug('go not in the path');
13321335
return added;
13331336
}
1334-
let buf = cp.execSync('go env GOPATH');
1337+
let buf = child_process_1.default.execSync('go env GOPATH');
13351338
if (buf) {
13361339
let gp = buf.toString().trim();
1337-
core.debug(`go env GOPATH :${gp}:`);
1338-
if (!fs.existsSync(gp)) {
1340+
_debug(`go env GOPATH :${gp}:`);
1341+
if (!fs_1.default.existsSync(gp)) {
13391342
// some of the hosted images have go install but not profile dir
1340-
core.debug(`creating ${gp}`);
1343+
_debug(`creating ${gp}`);
13411344
io.mkdirP(gp);
13421345
}
1343-
let bp = path.join(gp, 'bin');
1344-
if (!fs.existsSync(bp)) {
1345-
core.debug(`creating ${bp}`);
1346+
let bp = path_1.default.join(gp, 'bin');
1347+
if (!fs_1.default.existsSync(bp)) {
1348+
_debug(`creating ${bp}`);
13461349
io.mkdirP(bp);
13471350
}
13481351
core.addPath(bp);
@@ -1351,6 +1354,11 @@ function addBinToPath() {
13511354
return added;
13521355
});
13531356
}
1357+
exports.addBinToPath = addBinToPath;
1358+
function _debug(message) {
1359+
core.debug(message);
1360+
}
1361+
exports._debug = _debug;
13541362

13551363

13561364
/***/ }),

src/main.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as core from '@actions/core';
22
import * as io from '@actions/io';
33
import * as tc from '@actions/tool-cache';
44
import * as installer from './installer';
5-
import * as path from 'path';
6-
import * as cp from 'child_process';
7-
import * as fs from 'fs';
5+
import path from 'path';
6+
import cp from 'child_process';
7+
import fs from 'fs';
88

99
export async function run() {
1010
try {
@@ -55,28 +55,28 @@ export async function run() {
5555
}
5656
}
5757

58-
async function addBinToPath(): Promise<boolean> {
58+
export async function addBinToPath(): Promise<boolean> {
5959
let added = false;
6060
let g = await io.which('go');
61-
core.debug(`which go :${g}:`);
61+
_debug(`which go :${g}:`);
6262
if (!g) {
63-
core.debug('go not in the path');
63+
_debug('go not in the path');
6464
return added;
6565
}
6666

6767
let buf = cp.execSync('go env GOPATH');
6868
if (buf) {
6969
let gp = buf.toString().trim();
70-
core.debug(`go env GOPATH :${gp}:`);
70+
_debug(`go env GOPATH :${gp}:`);
7171
if (!fs.existsSync(gp)) {
7272
// some of the hosted images have go install but not profile dir
73-
core.debug(`creating ${gp}`);
73+
_debug(`creating ${gp}`);
7474
io.mkdirP(gp);
7575
}
7676

7777
let bp = path.join(gp, 'bin');
7878
if (!fs.existsSync(bp)) {
79-
core.debug(`creating ${bp}`);
79+
_debug(`creating ${bp}`);
8080
io.mkdirP(bp);
8181
}
8282

@@ -85,3 +85,7 @@ async function addBinToPath(): Promise<boolean> {
8585
}
8686
return added;
8787
}
88+
89+
export function _debug(message: string) {
90+
core.debug(message);
91+
}

0 commit comments

Comments
 (0)