Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests #2

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions __tests__/data/versions-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@
"filename": "go-1.9.7-darwin-x64.tar.gz",
"arch": "x64",
"platform": "darwin",
"download_url": "https://github.com/actions/go-versions/releases/download/1.9.7-20200616.1/go-1.9.7-darwin-x64.tar.gz"
"download_url": "https://github.com/actions/go-versions/releases/download/1.9.7/go-1.9.7-darwin-x64.tar.gz"
},
{
"filename": "go-1.9.7-linux-x64.tar.gz",
"arch": "x64",
"platform": "linux",
"download_url": "https://github.com/actions/go-versions/releases/download/1.9.7-20200616.1/go-1.9.7-linux-x64.tar.gz"
"download_url": "https://github.com/actions/go-versions/releases/download/1.9.7/go-1.9.7-linux-x64.tar.gz"
},
{
"filename": "go-1.9.7-win32-x64.zip",
"arch": "x64",
"platform": "win32",
"download_url": "https://github.com/actions/go-versions/releases/download/1.9.7-20200616.1/go-1.9.7-win32-x64.zip"
"download_url": "https://github.com/actions/go-versions/releases/download/1.9.7/go-1.9.7-win32-x64.zip"
}
]
}
Expand Down
194 changes: 146 additions & 48 deletions __tests__/setup-go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import osm = require('os');
import path from 'path';
import * as main from '../src/main';
import * as im from '../src/installer';
import { version } from 'punycode';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

punycode???


let goJsonData = require('./data/golang-dl.json');
let matchers = require('../matchers.json');
Expand Down Expand Up @@ -34,7 +35,7 @@ describe('setup-go', () => {
let mkdirpSpy: jest.SpyInstance;
let execSpy: jest.SpyInstance;
let getManifestSpy: jest.SpyInstance;
let getInfoManifestSpy: jest.SpyInstance;
// let getInfoManifestSpy: jest.SpyInstance;

beforeEach(() => {
// @actions/core
Expand All @@ -57,7 +58,7 @@ describe('setup-go', () => {
cacheSpy = jest.spyOn(tc, 'cacheDir');
getSpy = jest.spyOn(im, 'getVersionsDist');
getManifestSpy = jest.spyOn(tc, 'getManifestFromRepo');
getInfoManifestSpy = jest.spyOn(im, 'getInfoFromManifest');
// getInfoManifestSpy = jest.spyOn(im, 'getInfoFromManifest');

// io
whichSpy = jest.spyOn(io, 'which');
Expand Down Expand Up @@ -98,65 +99,37 @@ describe('setup-go', () => {

afterAll(async () => {}, 100000);

it('can query versions', async () => {
let versions: im.IGoVersion[] | null = await im.getVersionsDist(
'https://non.existant.com/path'
);
expect(versions).toBeDefined();
let l: number = versions ? versions.length : 0;
expect(l).toBe(91);
});

it('can mock manifest versions', async () => {
let versions: tc.IToolRelease[] | null = await tc.getManifestFromRepo(
'actions',
'go-versions',
'mocktoken'
);
expect(versions).toBeDefined();
expect(versions.length).toBe(3);
});

it('can find 1.12.17 from manifest on osx', async () => {
it('can find 1.9.7 from manifest on osx', async () => {
os.platform = 'darwin';
os.arch = 'x64';
let versions: tc.IToolRelease[] | null = await tc.getManifestFromRepo(
'actions',
'go-versions',
'mocktoken'
);
expect(versions).toBeDefined();
let match = await tc.findFromManifest('1.12.17', true, versions);

let match = await im.getInfoFromManifest('1.9.7', true, 'mocktoken');
expect(match).toBeDefined();
expect(match!.version).toBe('1.12.17');
expect(match!.resolvedVersion).toBe('1.9.7');
expect(match!.type).toBe('manifest');
expect(match!.downloadUrl).toBe('https://github.com/actions/go-versions/releases/download/1.9.7/go-1.9.7-darwin-x64.tar.gz');
});

it('can find 12 from manifest on linux', async () => {
it('can find 1.9 from manifest on linux', async () => {
os.platform = 'linux';
os.arch = 'x64';
let versions: tc.IToolRelease[] | null = await tc.getManifestFromRepo(
'actions',
'go-versions',
'mocktoken'
);
expect(versions).toBeDefined();
let match = await tc.findFromManifest('1.12.17', true, versions);

let match = await im.getInfoFromManifest('1.9.7', true, 'mocktoken');
expect(match).toBeDefined();
expect(match!.version).toBe('1.12.17');
expect(match!.resolvedVersion).toBe('1.9.7');
expect(match!.type).toBe('manifest');
expect(match!.downloadUrl).toBe('https://github.com/actions/go-versions/releases/download/1.9.7/go-1.9.7-linux-x64.tar.gz');
});

it('can find 10 from manifest on windows', async () => {
it('can find 1.9 from manifest on windows', async () => {
os.platform = 'win32';
os.arch = 'x64';
let versions: tc.IToolRelease[] | null = await tc.getManifestFromRepo(
'actions',
'go-versions',
'mocktoken'
);
expect(versions).toBeDefined();
let match = await tc.findFromManifest('1.12', true, versions);

let match = await im.getInfoFromManifest('1.9.7', true, 'mocktoken');
expect(match).toBeDefined();
expect(match!.version).toBe('1.12.17');
expect(match!.resolvedVersion).toBe('1.9.7');
expect(match!.type).toBe('manifest');
expect(match!.downloadUrl).toBe('https://github.com/actions/go-versions/releases/download/1.9.7/go-1.9.7-win32-x64.zip');
});

it('finds stable match for exact dot zero version', async () => {
Expand Down Expand Up @@ -203,6 +176,7 @@ describe('setup-go', () => {
await main.run();

let expPath = path.join(toolPath, 'bin');
expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
});

it('finds a version in the cache and adds it to the path', async () => {
Expand Down Expand Up @@ -260,6 +234,130 @@ describe('setup-go', () => {
);
});

it('downloads a version from a manifest match', async () => {
os.platform = 'linux';
os.arch = 'x64';

// a version which is in the manifest
let versionSpec = '1.12.16';

inputs['go-version'] = versionSpec;
inputs['token'] = 'faketoken';

let expectedUrl =
'https://github.com/actions/go-versions/releases/download/1.12.16-20200616.20/go-1.12.16-linux-x64.tar.gz';

// ... but not in the local cache
findSpy.mockImplementation(() => '');

dlSpy.mockImplementation(async () => '/some/temp/path');
let toolPath = path.normalize('/cache/go/1.12.16/x64');
exSpy.mockImplementation(async () => '/some/other/temp/path');
cacheSpy.mockImplementation(async () => toolPath);

await main.run();

let expPath = path.join(toolPath, 'bin');

expect(dlSpy).toHaveBeenCalled();
expect(exSpy).toHaveBeenCalled();
expect(logSpy).not.toHaveBeenCalledWith(
'Not found in manifest. Falling back to download directly from Go'
);
expect(logSpy).toHaveBeenCalledWith(
`Acquiring 1.12.16 from ${expectedUrl}`
);

expect(logSpy).toHaveBeenCalledWith(
`Added go to the path`
);
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
});

it('downloads a major and minor from a manifest match', async () => {
os.platform = 'linux';
os.arch = 'x64';

// a version which is in the manifest
let versionSpec = '1.12';

inputs['go-version'] = versionSpec;
inputs['token'] = 'faketoken';

let expectedUrl =
'https://github.com/actions/go-versions/releases/download/1.12.17-20200616.21/go-1.12.17-linux-x64.tar.gz';

// ... but not in the local cache
findSpy.mockImplementation(() => '');

dlSpy.mockImplementation(async () => '/some/temp/path');
let toolPath = path.normalize('/cache/go/1.12.17/x64');
exSpy.mockImplementation(async () => '/some/other/temp/path');
cacheSpy.mockImplementation(async () => toolPath);

await main.run();

let expPath = path.join(toolPath, 'bin');

expect(dlSpy).toHaveBeenCalled();
expect(exSpy).toHaveBeenCalled();
expect(logSpy).not.toHaveBeenCalledWith(
'Not found in manifest. Falling back to download directly from Go'
);
expect(logSpy).toHaveBeenCalledWith(
`Acquiring 1.12.17 from ${expectedUrl}`
);

expect(logSpy).toHaveBeenCalledWith(
`Added go to the path`
);
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
});

it('falls back to a version from node dist', async () => {
os.platform = 'linux';
os.arch = 'x64';

// a version which is not in the manifest but is in node dist
let versionSpec = '1.12.14';

inputs['go-version'] = versionSpec;
inputs['token'] = 'faketoken';

let expectedUrl =
'https://github.com/actions/go-versions/releases/download/1.12.14-20200616.18/go-1.12.14-linux-x64.tar.gz';

// ... but not in the local cache
findSpy.mockImplementation(() => '');

dlSpy.mockImplementation(async () => '/some/temp/path');
let toolPath = path.normalize('/cache/go/1.12.14/x64');
exSpy.mockImplementation(async () => '/some/other/temp/path');
cacheSpy.mockImplementation(async () => toolPath);

await main.run();

let expPath = path.join(toolPath, 'bin');
expect(logSpy).toHaveBeenCalledWith(
'Setup go stable version spec 1.12.14'
);
expect(findSpy).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.12.14...');
expect(dlSpy).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalledWith('matching 1.12.14...');
expect(exSpy).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalledWith(
'Not found in manifest. Falling back to download directly from Go'
);
expect(logSpy).toHaveBeenCalledWith(
`Install from dist`
);
expect(logSpy).toHaveBeenCalledWith(
`Added go to the path`
);
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
});

it('reports a failed download', async () => {
let errMsg = 'unhandled download message';
os.platform = 'linux';
Expand Down
1 change: 1 addition & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4990,6 +4990,7 @@ function getGo(versionSpec, stable, auth) {
throw new Error(`Unable to find Go version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`);
}
try {
console.log("Install from dist");
downloadPath = yield installGoVersion(info, undefined);
}
catch (err) {
Expand Down
1 change: 1 addition & 0 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export async function getGo(
}

try {
console.log("Install from dist");
downloadPath = await installGoVersion(info, undefined);
} catch (err) {
throw new Error(`Failed to download version ${versionSpec}: ${err}`);
Expand Down