Skip to content

Commit

Permalink
Adding tests (#2)
Browse files Browse the repository at this point in the history
* add tests

* fixing tests

* change names of it

* resolve comments

Co-authored-by: Dmitry Shibanov <v-dmshib@microsoft.com>
  • Loading branch information
dmitry-shibanov and Dmitry Shibanov authored Jun 24, 2020
1 parent ddc7688 commit 60dce67
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 59 deletions.
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
248 changes: 195 additions & 53 deletions __tests__/setup-go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('setup-go', () => {
let mkdirpSpy: jest.SpyInstance;
let execSpy: jest.SpyInstance;
let getManifestSpy: jest.SpyInstance;
let getInfoManifestSpy: jest.SpyInstance;

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

// io
whichSpy = jest.spyOn(io, 'which');
Expand All @@ -67,15 +65,11 @@ describe('setup-go', () => {
// gets
getManifestSpy.mockImplementation(() => <tc.IToolRelease[]>goTestManifest);

// getInfoManifestSpy.mockImplementation(
// () => <im.IGoVersionInfo | null>
// )

// writes
cnSpy = jest.spyOn(process.stdout, 'write');
logSpy = jest.spyOn(console, 'log');
dbgSpy = jest.spyOn(core, 'debug');
getSpy.mockImplementation(() => <im.IGoVersion[]>goJsonData);
getSpy.mockImplementation(() => <im.IGoVersion[] | null>goJsonData);
cnSpy.mockImplementation(line => {
// uncomment to debug
// process.stderr.write('write:' + line + '\n');
Expand All @@ -98,65 +92,43 @@ 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 All @@ -172,6 +144,61 @@ describe('setup-go', () => {
expect(fileName).toBe('go1.13.darwin-amd64.tar.gz');
});

it('finds latest patch version for minor version spec', async () => {
os.platform = 'linux';
os.arch = 'x64';

// 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 () => {
os.platform = 'linux';
os.arch = '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 () => {
os.platform = 'win32';
os.arch = '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');
});

it('finds unstable pre-release version', async () => {
os.platform = 'linux';
os.arch = 'x64';

// spec: 1.14, stable=false => go1.14rc1
let match: im.IGoVersion | undefined = await im.findMatch(
'1.14.0-rc1',
false
);
expect(match).toBeDefined();
let version: string = match ? match.version : '';
expect(version).toBe('go1.14rc1');
let fileName = match ? match.files[0].filename : '';
expect(fileName).toBe('go1.14rc1.linux-amd64.tar.gz');
});

it('evaluates to stable with input as true', async () => {
inputs['go-version'] = '1.13.0';
inputs.stable = 'true';
Expand Down Expand Up @@ -203,6 +230,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 +288,120 @@ 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
5 changes: 3 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4899,7 +4899,7 @@ module.exports = require("fs");
/***/ }),

/***/ 749:
/***/ (function(__unusedmodule, exports, __webpack_require__) {
/***/ (function(module, exports, __webpack_require__) {

"use strict";

Expand Down 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 Expand Up @@ -5067,7 +5068,7 @@ function findMatch(versionSpec, stable) {
let result;
let match;
const dlUrl = 'https://golang.org/dl/?mode=json&include=all';
let candidates = yield getVersionsDist(dlUrl);
let candidates = yield module.exports.getVersionsDist(dlUrl);
if (!candidates) {
throw new Error(`golang download url did not return results`);
}
Expand Down
5 changes: 4 additions & 1 deletion 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 Expand Up @@ -178,7 +179,9 @@ export async function findMatch(
let match: IGoVersion | undefined;

const dlUrl: string = 'https://golang.org/dl/?mode=json&include=all';
let candidates: IGoVersion[] | null = await getVersionsDist(dlUrl);
let candidates: IGoVersion[] | null = await module.exports.getVersionsDist(
dlUrl
);
if (!candidates) {
throw new Error(`golang download url did not return results`);
}
Expand Down

0 comments on commit 60dce67

Please sign in to comment.