mirror of
https://github.com/actions/setup-go.git
synced 2025-05-04 14:14:34 +00:00
starting v2 and proxy support
This commit is contained in:
@ -1,126 +0,0 @@
|
||||
import io = require('@actions/io');
|
||||
import fs = require('fs');
|
||||
import os = require('os');
|
||||
import path = require('path');
|
||||
import nock = require('nock');
|
||||
|
||||
const toolDir = path.join(__dirname, 'runner', 'tools');
|
||||
const tempDir = path.join(__dirname, 'runner', 'temp');
|
||||
const dataDir = path.join(__dirname, 'data');
|
||||
|
||||
process.env['RUNNER_TOOL_CACHE'] = toolDir;
|
||||
process.env['RUNNER_TEMP'] = tempDir;
|
||||
import * as installer from '../src/installer';
|
||||
|
||||
const IS_WINDOWS = process.platform === 'win32';
|
||||
|
||||
describe('installer tests', () => {
|
||||
beforeAll(async () => {
|
||||
await io.rmRF(toolDir);
|
||||
await io.rmRF(tempDir);
|
||||
}, 100000);
|
||||
|
||||
afterAll(async () => {
|
||||
try {
|
||||
await io.rmRF(toolDir);
|
||||
await io.rmRF(tempDir);
|
||||
} catch {
|
||||
console.log('Failed to remove test directories');
|
||||
}
|
||||
}, 100000);
|
||||
|
||||
it('Acquires version of go if no matching version is installed', async () => {
|
||||
await installer.getGo('1.10.8');
|
||||
const goDir = path.join(toolDir, 'go', '1.10.8', os.arch());
|
||||
|
||||
expect(fs.existsSync(`${goDir}.complete`)).toBe(true);
|
||||
if (IS_WINDOWS) {
|
||||
expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true);
|
||||
} else {
|
||||
expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true);
|
||||
}
|
||||
}, 100000);
|
||||
|
||||
describe('the latest release of a go version', () => {
|
||||
beforeEach(() => {
|
||||
nock('https://golang.org')
|
||||
.get('/dl/')
|
||||
.query({mode: 'json', include: 'all'})
|
||||
.replyWithFile(200, path.join(dataDir, 'golang-dl.json'));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
nock.cleanAll();
|
||||
nock.enableNetConnect();
|
||||
});
|
||||
|
||||
it('Acquires latest release version of go 1.10 if using 1.10 and no matching version is installed', async () => {
|
||||
await installer.getGo('1.10');
|
||||
const goDir = path.join(toolDir, 'go', '1.10.8', os.arch());
|
||||
|
||||
expect(fs.existsSync(`${goDir}.complete`)).toBe(true);
|
||||
if (IS_WINDOWS) {
|
||||
expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true);
|
||||
} else {
|
||||
expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true);
|
||||
}
|
||||
}, 100000);
|
||||
|
||||
it('Acquires latest release version of go 1.10 if using 1.10.x and no matching version is installed', async () => {
|
||||
await installer.getGo('1.10.x');
|
||||
const goDir = path.join(toolDir, 'go', '1.10.8', os.arch());
|
||||
|
||||
expect(fs.existsSync(`${goDir}.complete`)).toBe(true);
|
||||
if (IS_WINDOWS) {
|
||||
expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true);
|
||||
} else {
|
||||
expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true);
|
||||
}
|
||||
}, 100000);
|
||||
|
||||
it('Acquires latest release version of go if using 1.x and no matching version is installed', async () => {
|
||||
await installer.getGo('1.x');
|
||||
const goDir = path.join(toolDir, 'go', '1.13.0', os.arch());
|
||||
|
||||
expect(fs.existsSync(`${goDir}.complete`)).toBe(true);
|
||||
if (IS_WINDOWS) {
|
||||
expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true);
|
||||
} else {
|
||||
expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true);
|
||||
}
|
||||
}, 100000);
|
||||
});
|
||||
|
||||
it('Throws if no location contains correct go version', async () => {
|
||||
let thrown = false;
|
||||
try {
|
||||
await installer.getGo('1000.0');
|
||||
} catch {
|
||||
thrown = true;
|
||||
}
|
||||
expect(thrown).toBe(true);
|
||||
});
|
||||
|
||||
it('Uses version of go installed in cache', async () => {
|
||||
const goDir: string = path.join(toolDir, 'go', '250.0.0', os.arch());
|
||||
await io.mkdirP(goDir);
|
||||
fs.writeFileSync(`${goDir}.complete`, 'hello');
|
||||
// This will throw if it doesn't find it in the cache (because no such version exists)
|
||||
await installer.getGo('250.0');
|
||||
return;
|
||||
});
|
||||
|
||||
it('Doesnt use version of go that was only partially installed in cache', async () => {
|
||||
const goDir: string = path.join(toolDir, 'go', '251.0.0', os.arch());
|
||||
await io.mkdirP(goDir);
|
||||
let thrown = false;
|
||||
try {
|
||||
// This will throw if it doesn't find it in the cache (because no such version exists)
|
||||
await installer.getGo('251.0');
|
||||
} catch {
|
||||
thrown = true;
|
||||
}
|
||||
expect(thrown).toBe(true);
|
||||
return;
|
||||
});
|
||||
});
|
155
__tests__/setup-go.test.ts
Normal file
155
__tests__/setup-go.test.ts
Normal file
@ -0,0 +1,155 @@
|
||||
import * as tc from '@actions/tool-cache';
|
||||
import * as core from '@actions/core';
|
||||
import fs = require('fs');
|
||||
import os = require('os');
|
||||
import path = require('path');
|
||||
import {run} from '../src/main';
|
||||
import * as httpm from '@actions/http-client';
|
||||
import * as im from '../src/installer';
|
||||
import * as sys from '../src/system';
|
||||
import { ITypedResponse } from '@actions/http-client/interfaces';
|
||||
|
||||
let goJsonData = require('./data/golang-dl.json');
|
||||
|
||||
describe('setup-go', () => {
|
||||
let inSpy: jest.SpyInstance;
|
||||
let tcSpy: jest.SpyInstance;
|
||||
let cnSpy: jest.SpyInstance;
|
||||
let getSpy: jest.SpyInstance;
|
||||
let platSpy: jest.SpyInstance;
|
||||
let archSpy: jest.SpyInstance;
|
||||
let dlSpy: jest.SpyInstance;
|
||||
let exSpy: jest.SpyInstance;
|
||||
let http: httpm.HttpClient = new httpm.HttpClient('setup-go-tests');
|
||||
|
||||
beforeEach(() => {
|
||||
tcSpy = jest.spyOn(tc, 'find');
|
||||
inSpy = jest.spyOn(core, 'getInput');
|
||||
cnSpy = jest.spyOn(process.stdout, 'write');
|
||||
platSpy = jest.spyOn(sys, 'getPlatform');
|
||||
archSpy = jest.spyOn(sys, 'getArch');
|
||||
dlSpy = jest.spyOn(tc, "downloadTool");
|
||||
exSpy = jest.spyOn(tc, "extractTar");
|
||||
getSpy = jest.spyOn(http, 'getJson');
|
||||
getSpy.mockImplementation(() => <ITypedResponse<im.IGoVersion[]>>{
|
||||
result: goJsonData
|
||||
});
|
||||
cnSpy.mockImplementation(line => {
|
||||
// uncomment to debug
|
||||
//process.stderr.write('write2:' + line + '\n');
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
tcSpy.mockClear();
|
||||
cnSpy.mockClear();
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
afterAll(async () => {}, 100000);
|
||||
|
||||
it('finds a version of go already in the cache', async () => {
|
||||
inSpy.mockImplementation(() => '1.13.0')
|
||||
let toolPath = path.normalize('/cache/go/1.13.0/x64');
|
||||
tcSpy.mockImplementation(() => toolPath);
|
||||
await run();
|
||||
|
||||
let expPath = path.join(toolPath, 'bin');
|
||||
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${os.EOL}`);
|
||||
});
|
||||
|
||||
it('finds a version in the cache and adds it to the path', async () => {
|
||||
let toolPath = path.normalize('/cache/go/1.13.0/x64');
|
||||
inSpy.mockImplementation(() => '1.13.0');
|
||||
tcSpy.mockImplementation(() => toolPath);
|
||||
await run();
|
||||
|
||||
let expPath = path.join(toolPath, 'bin');
|
||||
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${os.EOL}`);
|
||||
});
|
||||
|
||||
it('handles unhandled error and reports error', async () => {
|
||||
let errMsg = 'unhandled error message';
|
||||
inSpy.mockImplementation(() => '1.13.0');
|
||||
tcSpy.mockImplementation(() => {
|
||||
throw new Error(errMsg);
|
||||
});
|
||||
await run();
|
||||
expect(cnSpy).toHaveBeenCalledWith('::error::' + errMsg + os.EOL);
|
||||
});
|
||||
|
||||
it('can mock go versions query', async () => {
|
||||
let r: ITypedResponse<im.IGoVersion[]> = await http.getJson<im.IGoVersion[]>('https://asite.notexist.com/path');
|
||||
expect(r).toBeDefined();
|
||||
let versions = r.result;
|
||||
expect(versions).toBeDefined();
|
||||
let l:number = versions? versions.length: 0;
|
||||
expect(l).toBe(76);
|
||||
});
|
||||
|
||||
it('finds stable match for exact version', async () => {
|
||||
platSpy.mockImplementation(() => 'linux');
|
||||
archSpy.mockImplementation(() => 'amd64');
|
||||
|
||||
// get request is already mocked
|
||||
// spec: 1.13.1 => 1.13.1 (exact)
|
||||
let match: im.IGoVersion | undefined = await im.findMatch('1.13.1', true);
|
||||
expect(match).toBeDefined();
|
||||
let version: string = match ? match.version : '';
|
||||
expect(version).toBe('go1.13.1');
|
||||
let fileName = match ? match.files[0].filename: '';
|
||||
expect(fileName).toBe('go1.13.1.linux-amd64.tar.gz');
|
||||
});
|
||||
|
||||
it('finds stable match for exact dot zero version', async () => {
|
||||
platSpy.mockImplementation(() => 'linux');
|
||||
archSpy.mockImplementation(() => 'amd64');
|
||||
|
||||
// spec: 1.13.0 => 1.13
|
||||
let match: im.IGoVersion | undefined = await im.findMatch('1.13.0', true);
|
||||
expect(match).toBeDefined();
|
||||
let version: string = match ? match.version : '';
|
||||
expect(version).toBe('go1.13');
|
||||
let fileName = match ? match.files[0].filename: '';
|
||||
expect(fileName).toBe('go1.13.linux-amd64.tar.gz');
|
||||
});
|
||||
|
||||
it('finds latest patch version for minor version spec', async () => {
|
||||
platSpy.mockImplementation(() => 'linux');
|
||||
archSpy.mockImplementation(() => 'amd64');
|
||||
|
||||
// spec: 1.13 => 1.13.7 (latest)
|
||||
let match: im.IGoVersion | undefined = await im.findMatch('1.13', true);
|
||||
expect(match).toBeDefined();
|
||||
let version: string = match ? match.version : '';
|
||||
expect(version).toBe('go1.13.7');
|
||||
let fileName = match ? match.files[0].filename: '';
|
||||
expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz');
|
||||
});
|
||||
|
||||
it('finds latest patch version for caret version spec', async () => {
|
||||
platSpy.mockImplementation(() => 'linux');
|
||||
archSpy.mockImplementation(() => 'amd64');
|
||||
|
||||
// spec: ^1.13.6 => 1.13.7
|
||||
let match: im.IGoVersion | undefined = await im.findMatch('^1.13.6', true);
|
||||
expect(match).toBeDefined();
|
||||
let version: string = match ? match.version : '';
|
||||
expect(version).toBe('go1.13.7');
|
||||
let fileName = match ? match.files[0].filename: '';
|
||||
expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz');
|
||||
});
|
||||
|
||||
it('finds latest version for major version spec', async () => {
|
||||
platSpy.mockImplementation(() => 'linux');
|
||||
archSpy.mockImplementation(() => 'amd64');
|
||||
|
||||
// spec: 1 => 1.13.7 (latest)
|
||||
let match: im.IGoVersion | undefined = await im.findMatch('1', true);
|
||||
expect(match).toBeDefined();
|
||||
let version: string = match ? match.version : '';
|
||||
expect(version).toBe('go1.13.7');
|
||||
let fileName = match ? match.files[0].filename: '';
|
||||
expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz');
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user