Skip to content

Commit 4388031

Browse files
CR feedback
1 parent 7ea80d8 commit 4388031

File tree

4 files changed

+95
-42
lines changed

4 files changed

+95
-42
lines changed

__tests__/setup-go.test.ts

Lines changed: 87 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as tc from '@actions/tool-cache';
22
import * as core from '@actions/core';
33
import fs = require('fs');
4-
import os = require('os');
4+
import osm = require('os');
55
import path = require('path');
66
import {run} from '../src/main';
77
import * as httpm from '@actions/http-client';
@@ -12,9 +12,13 @@ import {ITypedResponse} from '@actions/http-client/interfaces';
1212
let goJsonData = require('./data/golang-dl.json');
1313

1414
describe('setup-go', () => {
15+
let inputs = {} as any;
16+
let os = {} as any;
17+
1518
let inSpy: jest.SpyInstance;
1619
let findSpy: jest.SpyInstance;
1720
let cnSpy: jest.SpyInstance;
21+
let logSpy: jest.SpyInstance;
1822
let getSpy: jest.SpyInstance;
1923
let platSpy: jest.SpyInstance;
2024
let archSpy: jest.SpyInstance;
@@ -23,19 +27,36 @@ describe('setup-go', () => {
2327
let cacheSpy: jest.SpyInstance;
2428

2529
beforeEach(() => {
26-
findSpy = jest.spyOn(tc, 'find');
30+
// @actions/core
31+
inputs = {};
2732
inSpy = jest.spyOn(core, 'getInput');
28-
cnSpy = jest.spyOn(process.stdout, 'write');
29-
platSpy = jest.spyOn(os, 'platform');
30-
archSpy = jest.spyOn(os, 'arch');
33+
inSpy.mockImplementation(name => inputs[name]);
34+
35+
// node 'os'
36+
os = {};
37+
platSpy = jest.spyOn(osm, 'platform');
38+
platSpy.mockImplementation(() => os['platform']);
39+
archSpy = jest.spyOn(osm, 'arch');
40+
archSpy.mockImplementation(() => os['arch']);
41+
42+
// @actions/tool-cache
43+
findSpy = jest.spyOn(tc, 'find');
3144
dlSpy = jest.spyOn(tc, 'downloadTool');
3245
exSpy = jest.spyOn(tc, 'extractTar');
3346
cacheSpy = jest.spyOn(tc, 'cacheDir');
3447
getSpy = jest.spyOn(im, 'getVersions');
48+
49+
// writes
50+
cnSpy = jest.spyOn(process.stdout, 'write');
51+
logSpy = jest.spyOn(console, 'log');
3552
getSpy.mockImplementation(() => <im.IGoVersion[]>goJsonData);
3653
cnSpy.mockImplementation(line => {
3754
// uncomment to debug
38-
// process.stderr.write('write2:' + line + '\n');
55+
// process.stderr.write('write:' + line + '\n');
56+
});
57+
logSpy.mockImplementation(line => {
58+
// uncomment to debug
59+
// process.stderr.write('log:' + line + '\n');
3960
});
4061
});
4162

@@ -47,8 +68,8 @@ describe('setup-go', () => {
4768
afterAll(async () => {}, 100000);
4869

4970
it('finds stable match for exact dot zero version', async () => {
50-
platSpy.mockImplementation(() => 'darwin');
51-
archSpy.mockImplementation(() => 'x64');
71+
os.platform = 'darwin';
72+
os.arch = 'x64';
5273

5374
// spec: 1.13.0 => 1.13
5475
let match: im.IGoVersion | undefined = await im.findMatch('1.13.0', true);
@@ -60,9 +81,8 @@ describe('setup-go', () => {
6081
});
6182

6283
it('finds latest patch version for minor version spec', async () => {
63-
platSpy.mockImplementation(() => 'linux');
64-
archSpy.mockImplementation(() => 'x64');
65-
core.debug('plat mocks ok');
84+
os.platform = 'linux';
85+
os.arch = 'x64';
6686

6787
// spec: 1.13 => 1.13.7 (latest)
6888
let match: im.IGoVersion | undefined = await im.findMatch('1.13', true);
@@ -74,8 +94,8 @@ describe('setup-go', () => {
7494
});
7595

7696
it('finds latest patch version for caret version spec', async () => {
77-
platSpy.mockImplementation(() => 'linux');
78-
archSpy.mockImplementation(() => 'x64');
97+
os.platform = 'linux';
98+
os.arch = 'x64';
7999

80100
// spec: ^1.13.6 => 1.13.7
81101
let match: im.IGoVersion | undefined = await im.findMatch('^1.13.6', true);
@@ -87,8 +107,8 @@ describe('setup-go', () => {
87107
});
88108

89109
it('finds latest version for major version spec', async () => {
90-
platSpy.mockImplementation(() => 'windows');
91-
archSpy.mockImplementation(() => 'x32');
110+
os.platform = 'win32';
111+
os.arch = 'x32';
92112

93113
// spec: 1 => 1.13.7 (latest)
94114
let match: im.IGoVersion | undefined = await im.findMatch('1', true);
@@ -99,8 +119,32 @@ describe('setup-go', () => {
99119
expect(fileName).toBe('go1.13.7.windows-386.zip');
100120
});
101121

122+
it('evaluates to stable with input as true', async () => {
123+
inputs['go-version'] = '1.13.0';
124+
inputs.stable = 'true';
125+
126+
let toolPath = path.normalize('/cache/go/1.13.0/x64');
127+
findSpy.mockImplementation(() => toolPath);
128+
await run();
129+
130+
expect(logSpy).toHaveBeenCalledWith(`Setup go stable version spec 1.13.0`);
131+
});
132+
133+
it('evaluates to stable with no input', async () => {
134+
inputs['go-version'] = '1.13.0';
135+
136+
inSpy.mockImplementation(name => inputs[name]);
137+
138+
let toolPath = path.normalize('/cache/go/1.13.0/x64');
139+
findSpy.mockImplementation(() => toolPath);
140+
await run();
141+
142+
expect(logSpy).toHaveBeenCalledWith(`Setup go stable version spec 1.13.0`);
143+
});
144+
102145
it('finds a version of go already in the cache', async () => {
103-
inSpy.mockImplementation(() => '1.13.0');
146+
inputs['go-version'] = '1.13.0';
147+
104148
let toolPath = path.normalize('/cache/go/1.13.0/x64');
105149
findSpy.mockImplementation(() => toolPath);
106150
await run();
@@ -109,30 +153,32 @@ describe('setup-go', () => {
109153
});
110154

111155
it('finds a version in the cache and adds it to the path', async () => {
156+
inputs['go-version'] = '1.13.0';
112157
let toolPath = path.normalize('/cache/go/1.13.0/x64');
113-
inSpy.mockImplementation(() => '1.13.0');
114158
findSpy.mockImplementation(() => toolPath);
115159
await run();
116160

117161
let expPath = path.join(toolPath, 'bin');
118-
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${os.EOL}`);
162+
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
119163
});
120164

121165
it('handles unhandled error and reports error', async () => {
122166
let errMsg = 'unhandled error message';
123-
inSpy.mockImplementation(() => '1.13.0');
167+
inputs['go-version'] = '1.13.0';
168+
124169
findSpy.mockImplementation(() => {
125170
throw new Error(errMsg);
126171
});
127172
await run();
128-
expect(cnSpy).toHaveBeenCalledWith('::error::' + errMsg + os.EOL);
173+
expect(cnSpy).toHaveBeenCalledWith('::error::' + errMsg + osm.EOL);
129174
});
130175

131176
it('downloads a version not in the cache', async () => {
132-
platSpy.mockImplementation(() => 'linux');
133-
archSpy.mockImplementation(() => 'x64');
177+
os.platform = 'linux';
178+
os.arch = 'x64';
179+
180+
inputs['go-version'] = '1.13.1';
134181

135-
inSpy.mockImplementation(() => '1.13.1');
136182
findSpy.mockImplementation(() => '');
137183
dlSpy.mockImplementation(() => '/some/temp/path');
138184
let toolPath = path.normalize('/cache/go/1.13.0/x64');
@@ -144,51 +190,54 @@ describe('setup-go', () => {
144190

145191
expect(dlSpy).toHaveBeenCalled();
146192
expect(exSpy).toHaveBeenCalled();
147-
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${os.EOL}`);
193+
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
148194
});
149195

150196
it('does not find a version that does not exist', async () => {
151-
platSpy.mockImplementation(() => 'linux');
152-
archSpy.mockImplementation(() => 'x64');
197+
os.platform = 'linux';
198+
os.arch = 'x64';
199+
200+
inputs['go-version'] = '9.99.9';
153201

154-
inSpy.mockImplementation(() => '9.99.9');
155202
findSpy.mockImplementation(() => '');
156203
await run();
157204

158205
expect(cnSpy).toHaveBeenCalledWith(
159-
`::error::Could not find a version that satisfied version spec: 9.99.9${os.EOL}`
206+
`::error::Could not find a version that satisfied version spec: 9.99.9${osm.EOL}`
160207
);
161208
});
162209

163210
it('reports a failed download', async () => {
164211
let errMsg = 'unhandled download message';
165-
platSpy.mockImplementation(() => 'linux');
166-
archSpy.mockImplementation(() => 'x64');
212+
os.platform = 'linux';
213+
os.arch = 'x64';
214+
215+
inputs['go-version'] = '1.13.1';
167216

168-
inSpy.mockImplementation(() => '1.13.1');
169217
findSpy.mockImplementation(() => '');
170218
dlSpy.mockImplementation(() => {
171219
throw new Error(errMsg);
172220
});
173221
await run();
174222

175223
expect(cnSpy).toHaveBeenCalledWith(
176-
`::error::Failed to download version 1.13.1: Error: ${errMsg}${os.EOL}`
224+
`::error::Failed to download version 1.13.1: Error: ${errMsg}${osm.EOL}`
177225
);
178226
});
179227

180228
it('reports empty query results', async () => {
181229
let errMsg = 'unhandled download message';
182-
platSpy.mockImplementation(() => 'linux');
183-
archSpy.mockImplementation(() => 'x64');
230+
os.platform = 'linux';
231+
os.arch = 'x64';
232+
233+
inputs['go-version'] = '1.13.1';
184234

185-
inSpy.mockImplementation(() => '1.13.1');
186235
findSpy.mockImplementation(() => '');
187236
getSpy.mockImplementation(() => null);
188237
await run();
189238

190239
expect(cnSpy).toHaveBeenCalledWith(
191-
`::error::Failed to download version 1.13.1: Error: golang download url did not return results${os.EOL}`
240+
`::error::Failed to download version 1.13.1: Error: golang download url did not return results${osm.EOL}`
192241
);
193242
});
194243

@@ -202,8 +251,8 @@ describe('setup-go', () => {
202251
});
203252

204253
it('finds stable match for exact version', async () => {
205-
platSpy.mockImplementation(() => 'win32');
206-
archSpy.mockImplementation(() => 'x64');
254+
os.platform = 'win32';
255+
os.arch = 'x64';
207256

208257
// get request is already mocked
209258
// spec: 1.13.7 => 1.13.7 (exact)

dist/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ function run() {
12891289
let versionSpec = core.getInput('go-version');
12901290
// stable will be true unless false is the exact input
12911291
// since getting unstable versions should be explicit
1292-
let stable = Boolean(core.getInput('stable') || 'true');
1292+
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
12931293
console.log(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`);
12941294
if (versionSpec) {
12951295
let installDir = tc.find('go', versionSpec);
@@ -4633,7 +4633,8 @@ function findMatch(versionSpec, stable) {
46334633
version = version + '.0';
46344634
}
46354635
core_1.debug(`check ${version} satisfies ${versionSpec}`);
4636-
if (semver.satisfies(version, versionSpec) && candidate.stable == stable) {
4636+
if (semver.satisfies(version, versionSpec) &&
4637+
(!stable || candidate.stable === stable)) {
46374638
goFile = candidate.files.find(file => {
46384639
core_1.debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
46394640
return file.arch === archFilter && file.os === platFilter;

src/installer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ export async function findMatch(
8484
}
8585

8686
debug(`check ${version} satisfies ${versionSpec}`);
87-
if (semver.satisfies(version, versionSpec) && candidate.stable == stable) {
87+
if (
88+
semver.satisfies(version, versionSpec) &&
89+
(!stable || candidate.stable === stable)
90+
) {
8891
goFile = candidate.files.find(file => {
8992
debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
9093
return file.arch === archFilter && file.os === platFilter;

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function run() {
1313

1414
// stable will be true unless false is the exact input
1515
// since getting unstable versions should be explicit
16-
let stable = Boolean(core.getInput('stable') || 'true');
16+
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
1717

1818
console.log(
1919
`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`

0 commit comments

Comments
 (0)