Skip to content

Add testing #3

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

Merged
merged 2 commits into from
Jun 4, 2019
Merged
Changes from all 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -3,4 +3,5 @@
node_modules/.bin
node_modules/typescript
node_modules/@types
node_modules/prettier
node_modules/prettier
__tests__/runner/*
97 changes: 97 additions & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import installer = require('../src/installer');
import io = require('@actions/io');
import fs = require('fs');
import os = require('os');
import path = require('path');

const toolDir = path.join(__dirname, 'runner', 'tools');

const tempDir = path.join(__dirname, 'runner', 'temp');

describe('installer tests', () => {
beforeAll(() => {});
beforeAll(async () => {
// TODO - these should eventually be changed to match new method of loading dir
process.env['Runner.ToolsDirectory'] = toolDir;
process.env['Runner.TempDirectory'] = tempDir;
await io.rmRF(toolDir);
await io.rmRF(tempDir);
});

it('Acquires version of node if no matching version is installed', async () => {
await installer.getNode('10.16.0');
const nodeDir = path.join(toolDir, 'node', '10.16.0', os.arch());

expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true);
}, 100000);

if (process.platform === 'win32') {
it('Falls back to backup location if first one doesnt contain correct version', async () => {
await installer.getNode('5.10.1');
const nodeDir = path.join(toolDir, 'node', '5.10.1', os.arch());

expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true);
}, 100000);

it('Falls back to third location if second one doesnt contain correct version', async () => {
await installer.getNode('0.12.18');
const nodeDir = path.join(toolDir, 'node', '0.12.18', os.arch());

expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true);
}, 100000);
}

it('Throws if no location contains correct node version', async () => {
let thrown = false;
try {
await installer.getNode('1000');
} catch {
thrown = true;
}
expect(thrown).toBe(true);
});

it('Acquires version of node with long paths', async () => {
const toolpath = await installer.getNode('8.8.1');
const nodeDir = path.join(toolDir, 'node', '8.8.1', os.arch());

expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true);
}, 100000);

it('Uses version of node installed in cache', async () => {
const nodeDir: string = path.join(toolDir, '250.0.0', os.arch());
await io.mkdirP(nodeDir);
fs.writeFileSync(`${nodeDir}.complete`, 'hello');
// This will throw if it doesn't find it in the cache (because no such version exists)
await installer.getNode('250.0.0');
return;
});

it('Doesnt use version of node that was only partially installed in cache', async () => {
const nodeDir: string = path.join(toolDir, '250.0.0', os.arch());
await io.mkdirP(nodeDir);
let thrown = false;
try {
// This will throw if it doesn't find it in the cache (because no such version exists)
await installer.getNode('251.0.0');
} catch {
thrown = true;
}
expect(thrown).toBe(true);
return;
});

it('Resolves semantic versions of node installed in cache', async () => {
const nodeDir: string = path.join(toolDir, '250.0.0', os.arch());
await io.mkdirP(nodeDir);
fs.writeFileSync(`${nodeDir}.complete`, 'hello');
// These will throw if it doesn't find it in the cache (because no such version exists)
await installer.getNode('250.0.0');
await installer.getNode('250');
await installer.getNode('250.0');
});
});
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
}
20 changes: 3 additions & 17 deletions lib/installer.js
Original file line number Diff line number Diff line change
@@ -62,9 +62,7 @@ function getNode(versionSpec) {
}
//
// prepend the tools path. instructs the agent to prepend for future tasks
//
// TODO - addPath not implemented yet (this should probably actually be in core)
// tc.addPath(toolPath);
core.addPath(toolPath);
});
}
exports.getNode = getNode;
@@ -152,12 +150,8 @@ function acquireNode(version) {
//
let extPath;
if (osPlat == 'win32') {
extPath = getAgentTemp();
if (!extPath) {
throw new Error('Expected Agent.TempDirectory to be set');
}
let _7zPath = path.join(__dirname, '7zr.exe');
extPath = yield tc.extract7z(downloadPath, extPath);
extPath = yield tc.extract7z(downloadPath);
}
else {
extPath = yield tc.extractTar(downloadPath);
@@ -185,7 +179,7 @@ function acquireNodeFromFallbackLocation(version) {
return __awaiter(this, void 0, void 0, function* () {
// Create temporary folder to download in to
let tempDownloadFolder = 'temp_' + Math.floor(Math.random() * 2000000000);
let tempDir = path.join(getAgentTemp(), tempDownloadFolder);
let tempDir = path.join(__dirname, tempDownloadFolder);
yield io.mkdirP(tempDir);
let exeUrl;
let libUrl;
@@ -213,11 +207,3 @@ function acquireNodeFromFallbackLocation(version) {
return yield tc.cacheDir(tempDir, 'node', version);
});
}
function getAgentTemp() {
// TODO - we need an actual protocol for this (this is just a placeholder)
const tempDirectory = process.env['Runner.TempDirectory'];
if (!tempDirectory) {
throw new Error('Runner.TempDirectory is not set');
}
return tempDirectory;
}
Loading
Oops, something went wrong.