3
0
Fork 0
mirror of https://gitea.com/actions/setup-go.git synced 2024-11-23 01:59:31 +01:00
setup-go/__tests__/setup-go.test.ts

218 lines
7.3 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');
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';
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', () => {
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;
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 00:09:15 +01:00
findSpy = jest.spyOn(tc, 'find');
2020-02-09 06:21:39 +01:00
inSpy = jest.spyOn(core, 'getInput');
cnSpy = jest.spyOn(process.stdout, 'write');
2020-02-10 00:22:24 +01:00
platSpy = jest.spyOn(os, 'platform');
archSpy = jest.spyOn(os, 'arch');
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-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 00:09:15 +01:00
// process.stderr.write('write2:' + 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 () => {
platSpy.mockImplementation(() => 'darwin');
archSpy.mockImplementation(() => 'x64');
// 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 () => {
platSpy.mockImplementation(() => 'linux');
archSpy.mockImplementation(() => 'x64');
core.debug('plat mocks ok');
// 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(() => 'x64');
// 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(() => 'windows');
archSpy.mockImplementation(() => 'x32');
// 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-09 06:21:39 +01:00
it('finds a version of go already in the cache', async () => {
2020-02-09 06:29:21 +01:00
inSpy.mockImplementation(() => '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 () => {
let toolPath = path.normalize('/cache/go/1.13.0/x64');
inSpy.mockImplementation(() => '1.13.0');
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');
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');
2020-02-10 00:09:15 +01:00
findSpy.mockImplementation(() => {
2020-02-09 06:21:39 +01:00
throw new Error(errMsg);
});
await run();
expect(cnSpy).toHaveBeenCalledWith('::error::' + errMsg + os.EOL);
});
2020-02-10 00:09:15 +01:00
it('downloads a version not in the cache', async () => {
platSpy.mockImplementation(() => 'linux');
2020-02-10 00:22:24 +01:00
archSpy.mockImplementation(() => 'x64');
2020-02-10 00:09:15 +01:00
inSpy.mockImplementation(() => '1.13.1');
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();
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${os.EOL}`);
});
it('does not find a version that does not exist', async () => {
platSpy.mockImplementation(() => 'linux');
2020-02-10 00:22:24 +01:00
archSpy.mockImplementation(() => 'x64');
2020-02-10 00:09:15 +01:00
inSpy.mockImplementation(() => '9.99.9');
findSpy.mockImplementation(() => '');
await run();
expect(cnSpy).toHaveBeenCalledWith(
`::error::Could not find a version that satisfied version spec: 9.99.9${os.EOL}`
);
});
2020-02-10 00:48:40 +01:00
it('reports a failed download', async () => {
let errMsg = 'unhandled download message';
platSpy.mockImplementation(() => 'linux');
archSpy.mockImplementation(() => 'x64');
inSpy.mockImplementation(() => '1.13.1');
findSpy.mockImplementation(() => '');
dlSpy.mockImplementation(() => {
throw new Error(errMsg);
});
await run();
expect(cnSpy).toHaveBeenCalledWith(
`::error::Failed to download version 1.13.1: Error: ${errMsg}${os.EOL}`
);
});
it('reports empty query results', async () => {
let errMsg = 'unhandled download message';
platSpy.mockImplementation(() => 'linux');
archSpy.mockImplementation(() => 'x64');
inSpy.mockImplementation(() => '1.13.1');
findSpy.mockImplementation(() => '');
getSpy.mockImplementation(() => null);
await run();
expect(cnSpy).toHaveBeenCalledWith(
`::error::Failed to download version 1.13.1: Error: golang download url did not return results${os.EOL}`
);
});
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 00:22:24 +01:00
platSpy.mockImplementation(() => 'win32');
archSpy.mockImplementation(() => '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
});
});