0
1
Fork 0
setup-go/__tests__/setup-go.test.ts

267 lines
8.0 KiB
TypeScript
Raw Normal View History

2020-02-09 06:21:39 +01:00
import * as tc from '@actions/tool-cache';
import * as core from '@actions/core';
import fs = require('fs');
2020-02-10 21:21:04 +01:00
import osm = require('os');
2020-02-09 06:21:39 +01:00
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';
2020-02-09 06:29:21 +01:00
import {ITypedResponse} from '@actions/http-client/interfaces';
2020-02-09 06:21:39 +01:00
let goJsonData = require('./data/golang-dl.json');
describe('setup-go', () => {
2020-02-10 21:21:04 +01:00
let inputs = {} as any;
let os = {} as any;
2020-02-09 06:21:39 +01:00
let inSpy: jest.SpyInstance;
2020-02-10 00:09:15 +01:00
let findSpy: jest.SpyInstance;
2020-02-09 06:21:39 +01:00
let cnSpy: jest.SpyInstance;
2020-02-10 21:21:04 +01:00
let logSpy: jest.SpyInstance;
2020-02-09 06:21:39 +01:00
let getSpy: jest.SpyInstance;
let platSpy: jest.SpyInstance;
let archSpy: jest.SpyInstance;
let dlSpy: jest.SpyInstance;
let exSpy: jest.SpyInstance;
2020-02-10 00:09:15 +01:00
let cacheSpy: jest.SpyInstance;
2020-02-09 06:21:39 +01:00
beforeEach(() => {
2020-02-10 21:21:04 +01:00
// @actions/core
inputs = {};
2020-02-09 06:21:39 +01:00
inSpy = jest.spyOn(core, 'getInput');
2020-02-10 21:21:04 +01:00
inSpy.mockImplementation(name => inputs[name]);
// node 'os'
os = {};
platSpy = jest.spyOn(osm, 'platform');
platSpy.mockImplementation(() => os['platform']);
archSpy = jest.spyOn(osm, 'arch');
archSpy.mockImplementation(() => os['arch']);
// @actions/tool-cache
findSpy = jest.spyOn(tc, 'find');
2020-02-09 06:29:21 +01:00
dlSpy = jest.spyOn(tc, 'downloadTool');
exSpy = jest.spyOn(tc, 'extractTar');
2020-02-10 00:09:15 +01:00
cacheSpy = jest.spyOn(tc, 'cacheDir');
2020-02-09 15:25:20 +01:00
getSpy = jest.spyOn(im, 'getVersions');
2020-02-10 21:21:04 +01:00
// writes
cnSpy = jest.spyOn(process.stdout, 'write');
logSpy = jest.spyOn(console, 'log');
2020-02-09 20:39:34 +01:00
getSpy.mockImplementation(() => <im.IGoVersion[]>goJsonData);
2020-02-09 06:21:39 +01:00
cnSpy.mockImplementation(line => {
// uncomment to debug
2020-02-10 21:21:04 +01:00
// process.stderr.write('write:' + line + '\n');
});
logSpy.mockImplementation(line => {
// uncomment to debug
// process.stderr.write('log:' + line + '\n');
2020-02-09 06:21:39 +01:00
});
});
afterEach(() => {
2020-02-09 20:39:34 +01:00
jest.resetAllMocks();
2020-02-09 06:21:39 +01:00
jest.clearAllMocks();
});
afterAll(async () => {}, 100000);
2020-02-10 01:05:20 +01:00
it('finds stable match for exact dot zero version', async () => {
2020-02-10 21:21:04 +01:00
os.platform = 'darwin';
os.arch = 'x64';
2020-02-10 01:05:20 +01:00
// 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.darwin-amd64.tar.gz');
});
it('finds latest patch version for minor version spec', async () => {
2020-02-10 21:21:04 +01:00
os.platform = 'linux';
os.arch = 'x64';
2020-02-10 01:05:20 +01:00
// 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 () => {
2020-02-10 21:21:04 +01:00
os.platform = 'linux';
os.arch = 'x64';
2020-02-10 01:05:20 +01:00
// 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 () => {
2020-02-10 21:21:04 +01:00
os.platform = 'win32';
os.arch = 'x32';
2020-02-10 01:05:20 +01:00
// 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.windows-386.zip');
});
2020-02-10 21:21:04 +01:00
it('evaluates to stable with input as true', async () => {
inputs['go-version'] = '1.13.0';
inputs.stable = 'true';
let toolPath = path.normalize('/cache/go/1.13.0/x64');
findSpy.mockImplementation(() => toolPath);
await run();
expect(logSpy).toHaveBeenCalledWith(`Setup go stable version spec 1.13.0`);
});
it('evaluates to stable with no input', async () => {
inputs['go-version'] = '1.13.0';
inSpy.mockImplementation(name => inputs[name]);
let toolPath = path.normalize('/cache/go/1.13.0/x64');
findSpy.mockImplementation(() => toolPath);
await run();
expect(logSpy).toHaveBeenCalledWith(`Setup go stable version spec 1.13.0`);
});
2020-02-09 06:21:39 +01:00
it('finds a version of go already in the cache', async () => {
2020-02-10 21:21:04 +01:00
inputs['go-version'] = '1.13.0';
2020-02-09 06:21:39 +01:00
let toolPath = path.normalize('/cache/go/1.13.0/x64');
2020-02-10 00:09:15 +01:00
findSpy.mockImplementation(() => toolPath);
2020-02-09 06:21:39 +01:00
await run();
let expPath = path.join(toolPath, 'bin');
});
it('finds a version in the cache and adds it to the path', async () => {
2020-02-10 21:21:04 +01:00
inputs['go-version'] = '1.13.0';
2020-02-09 06:21:39 +01:00
let toolPath = path.normalize('/cache/go/1.13.0/x64');
2020-02-10 00:09:15 +01:00
findSpy.mockImplementation(() => toolPath);
2020-02-09 06:21:39 +01:00
await run();
let expPath = path.join(toolPath, 'bin');
2020-02-10 21:21:04 +01:00
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
2020-02-09 06:21:39 +01:00
});
it('handles unhandled error and reports error', async () => {
let errMsg = 'unhandled error message';
2020-02-10 21:21:04 +01:00
inputs['go-version'] = '1.13.0';
2020-02-10 00:09:15 +01:00
findSpy.mockImplementation(() => {
2020-02-09 06:21:39 +01:00
throw new Error(errMsg);
});
await run();
2020-02-10 21:21:04 +01:00
expect(cnSpy).toHaveBeenCalledWith('::error::' + errMsg + osm.EOL);
2020-02-09 06:21:39 +01:00
});
2020-02-10 00:09:15 +01:00
it('downloads a version not in the cache', async () => {
2020-02-10 21:21:04 +01:00
os.platform = 'linux';
os.arch = 'x64';
inputs['go-version'] = '1.13.1';
2020-02-10 00:09:15 +01:00
findSpy.mockImplementation(() => '');
dlSpy.mockImplementation(() => '/some/temp/path');
let toolPath = path.normalize('/cache/go/1.13.0/x64');
exSpy.mockImplementation(() => '/some/other/temp/path');
cacheSpy.mockImplementation(() => toolPath);
await run();
let expPath = path.join(toolPath, 'bin');
expect(dlSpy).toHaveBeenCalled();
expect(exSpy).toHaveBeenCalled();
2020-02-10 21:21:04 +01:00
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
2020-02-10 00:09:15 +01:00
});
it('does not find a version that does not exist', async () => {
2020-02-10 21:21:04 +01:00
os.platform = 'linux';
os.arch = 'x64';
inputs['go-version'] = '9.99.9';
2020-02-10 00:09:15 +01:00
findSpy.mockImplementation(() => '');
await run();
expect(cnSpy).toHaveBeenCalledWith(
2020-02-10 21:21:04 +01:00
`::error::Could not find a version that satisfied version spec: 9.99.9${osm.EOL}`
2020-02-10 00:09:15 +01:00
);
});
2020-02-10 00:48:40 +01:00
it('reports a failed download', async () => {
let errMsg = 'unhandled download message';
2020-02-10 21:21:04 +01:00
os.platform = 'linux';
os.arch = 'x64';
inputs['go-version'] = '1.13.1';
2020-02-10 00:48:40 +01:00
findSpy.mockImplementation(() => '');
dlSpy.mockImplementation(() => {
throw new Error(errMsg);
});
await run();
expect(cnSpy).toHaveBeenCalledWith(
2020-02-10 21:21:04 +01:00
`::error::Failed to download version 1.13.1: Error: ${errMsg}${osm.EOL}`
2020-02-10 00:48:40 +01:00
);
});
it('reports empty query results', async () => {
let errMsg = 'unhandled download message';
2020-02-10 21:21:04 +01:00
os.platform = 'linux';
os.arch = 'x64';
inputs['go-version'] = '1.13.1';
2020-02-10 00:48:40 +01:00
findSpy.mockImplementation(() => '');
getSpy.mockImplementation(() => null);
await run();
expect(cnSpy).toHaveBeenCalledWith(
2020-02-10 21:21:04 +01:00
`::error::Failed to download version 1.13.1: Error: golang download url did not return results${osm.EOL}`
2020-02-10 00:48:40 +01:00
);
});
2020-02-09 20:39:34 +01:00
it('can query versions', async () => {
let versions: im.IGoVersion[] | null = await im.getVersions(
'https://non.existant.com/path'
2020-02-09 15:25:20 +01:00
);
2020-02-09 06:21:39 +01:00
expect(versions).toBeDefined();
2020-02-09 06:29:21 +01:00
let l: number = versions ? versions.length : 0;
2020-02-09 15:25:20 +01:00
expect(l).toBe(91);
2020-02-09 06:21:39 +01:00
});
2020-02-09 06:29:21 +01:00
2020-02-09 06:21:39 +01:00
it('finds stable match for exact version', async () => {
2020-02-10 21:21:04 +01:00
os.platform = 'win32';
os.arch = 'x64';
2020-02-09 06:21:39 +01:00
// get request is already mocked
2020-02-10 00:09:15 +01:00
// spec: 1.13.7 => 1.13.7 (exact)
let match: im.IGoVersion | undefined = await im.findMatch('1.13.7', true);
2020-02-09 06:21:39 +01:00
expect(match).toBeDefined();
let version: string = match ? match.version : '';
2020-02-10 00:09:15 +01:00
expect(version).toBe('go1.13.7');
2020-02-09 06:29:21 +01:00
let fileName = match ? match.files[0].filename : '';
2020-02-10 00:09:15 +01:00
expect(fileName).toBe('go1.13.7.windows-amd64.zip');
2020-02-09 06:21:39 +01:00
});
});