Skip to content

Commit 5331f86

Browse files
author
Danny McCormick
authored
Merge branch 'master' into features/matcher
2 parents 9241f8a + cd65450 commit 5331f86

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+8225
-7742
lines changed

__tests__/installer.test.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,65 @@ process.env['RUNNER_TOOLSDIRECTORY'] = toolDir;
1010
process.env['RUNNER_TEMPDIRECTORY'] = tempDir;
1111
import * as installer from '../src/installer';
1212

13+
const IS_WINDOWS = process.platform === 'win32';
14+
1315
describe('installer tests', () => {
14-
beforeAll(() => {});
1516
beforeAll(async () => {
1617
await io.rmRF(toolDir);
1718
await io.rmRF(tempDir);
19+
}, 100000);
20+
21+
afterAll(async () => {
22+
try {
23+
await io.rmRF(toolDir);
24+
await io.rmRF(tempDir);
25+
} catch {
26+
console.log('Failed to remove test directories');
27+
}
28+
}, 100000);
29+
30+
it('Acquires version of go if no matching version is installed', async () => {
31+
await installer.getGo('1.10');
32+
const goDir = path.join(toolDir, 'go', '1.10.0', os.arch());
33+
34+
expect(fs.existsSync(`${goDir}.complete`)).toBe(true);
35+
if (IS_WINDOWS) {
36+
expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true);
37+
} else {
38+
expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true);
39+
}
40+
}, 100000);
41+
42+
it('Throws if no location contains correct go version', async () => {
43+
let thrown = false;
44+
try {
45+
await installer.getGo('1000.0');
46+
} catch {
47+
thrown = true;
48+
}
49+
expect(thrown).toBe(true);
50+
});
51+
52+
it('Uses version of go installed in cache', async () => {
53+
const goDir: string = path.join(toolDir, 'go', '250.0.0', os.arch());
54+
await io.mkdirP(goDir);
55+
fs.writeFileSync(`${goDir}.complete`, 'hello');
56+
// This will throw if it doesn't find it in the cache (because no such version exists)
57+
await installer.getGo('250.0');
58+
return;
1859
});
1960

20-
it('TODO - Add tests', async () => {});
61+
it('Doesnt use version of go that was only partially installed in cache', async () => {
62+
const goDir: string = path.join(toolDir, 'go', '251.0.0', os.arch());
63+
await io.mkdirP(goDir);
64+
let thrown = false;
65+
try {
66+
// This will throw if it doesn't find it in the cache (because no such version exists)
67+
await installer.getGo('251.0');
68+
} catch {
69+
thrown = true;
70+
}
71+
expect(thrown).toBe(true);
72+
return;
73+
});
2174
});

lib/installer.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
return new (P || (P = Promise))(function (resolve, reject) {
4+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7+
step((generator = generator.apply(thisArg, _arguments || [])).next());
8+
});
9+
};
10+
var __importStar = (this && this.__importStar) || function (mod) {
11+
if (mod && mod.__esModule) return mod;
12+
var result = {};
13+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
14+
result["default"] = mod;
15+
return result;
16+
};
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
// Load tempDirectory before it gets wiped by tool-cache
19+
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || '';
20+
const core = __importStar(require("@actions/core"));
21+
const tc = __importStar(require("@actions/tool-cache"));
22+
const os = __importStar(require("os"));
23+
const path = __importStar(require("path"));
24+
const util = __importStar(require("util"));
25+
let osPlat = os.platform();
26+
let osArch = os.arch();
27+
if (!tempDirectory) {
28+
let baseLocation;
29+
if (process.platform === 'win32') {
30+
// On windows use the USERPROFILE env variable
31+
baseLocation = process.env['USERPROFILE'] || 'C:\\';
32+
}
33+
else {
34+
if (process.platform === 'darwin') {
35+
baseLocation = '/Users';
36+
}
37+
else {
38+
baseLocation = '/home';
39+
}
40+
}
41+
tempDirectory = path.join(baseLocation, 'actions', 'temp');
42+
}
43+
function getGo(version) {
44+
return __awaiter(this, void 0, void 0, function* () {
45+
// check cache
46+
let toolPath;
47+
toolPath = tc.find('go', normalizeVersion(version));
48+
if (!toolPath) {
49+
// download, extract, cache
50+
toolPath = yield acquireGo(version);
51+
core.debug('Go tool is cached under ' + toolPath);
52+
}
53+
setGoEnvironmentVariables(toolPath);
54+
toolPath = path.join(toolPath, 'bin');
55+
//
56+
// prepend the tools path. instructs the agent to prepend for future tasks
57+
//
58+
core.addPath(toolPath);
59+
});
60+
}
61+
exports.getGo = getGo;
62+
function acquireGo(version) {
63+
return __awaiter(this, void 0, void 0, function* () {
64+
//
65+
// Download - a tool installer intimately knows how to get the tool (and construct urls)
66+
//
67+
let fileName = getFileName(version);
68+
let downloadUrl = getDownloadUrl(fileName);
69+
let downloadPath = null;
70+
try {
71+
downloadPath = yield tc.downloadTool(downloadUrl);
72+
}
73+
catch (error) {
74+
core.debug(error);
75+
throw `Failed to download version ${version}: ${error}`;
76+
}
77+
//
78+
// Extract
79+
//
80+
let extPath = tempDirectory;
81+
if (!extPath) {
82+
throw new Error('Temp directory not set');
83+
}
84+
if (osPlat == 'win32') {
85+
extPath = yield tc.extractZip(downloadPath);
86+
}
87+
else {
88+
extPath = yield tc.extractTar(downloadPath);
89+
}
90+
//
91+
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
92+
//
93+
const toolRoot = path.join(extPath, 'go');
94+
version = normalizeVersion(version);
95+
return yield tc.cacheDir(toolRoot, 'go', version);
96+
});
97+
}
98+
function getFileName(version) {
99+
const platform = osPlat == 'win32' ? 'windows' : osPlat;
100+
const arch = osArch == 'x64' ? 'amd64' : '386';
101+
const ext = osPlat == 'win32' ? 'zip' : 'tar.gz';
102+
const filename = util.format('go%s.%s-%s.%s', version, platform, arch, ext);
103+
return filename;
104+
}
105+
function getDownloadUrl(filename) {
106+
return util.format('https://storage.googleapis.com/golang/%s', filename);
107+
}
108+
function setGoEnvironmentVariables(goRoot) {
109+
core.exportVariable('GOROOT', goRoot);
110+
const goPath = process.env['GOPATH'] || '';
111+
const goBin = process.env['GOBIN'] || '';
112+
// set GOPATH and GOBIN as user value
113+
if (goPath) {
114+
core.exportVariable('GOPATH', goPath);
115+
}
116+
if (goBin) {
117+
core.exportVariable('GOBIN', goBin);
118+
}
119+
}
120+
// This function is required to convert the version 1.10 to 1.10.0.
121+
// Because caching utility accept only sementic version,
122+
// which have patch number as well.
123+
function normalizeVersion(version) {
124+
const versionPart = version.split('.');
125+
if (versionPart[1] == null) {
126+
//append minor and patch version if not available
127+
return version.concat('.0.0');
128+
}
129+
else if (versionPart[2] == null) {
130+
//append patch version if not available
131+
return version.concat('.0');
132+
}
133+
return version;
134+
}

node_modules/@actions/core/README.md

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@actions/core/lib/command.d.ts

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@actions/core/lib/command.js

Lines changed: 65 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)