Merge pull request #3 from actions/users/damccorm/testing

Add testing
This commit is contained in:
Danny McCormick 2019-06-04 16:14:55 -04:00 committed by GitHub
commit d83862c273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 5224 additions and 52 deletions

3
.gitignore vendored
View File

@ -3,4 +3,5 @@
node_modules/.bin
node_modules/typescript
node_modules/@types
node_modules/prettier
node_modules/prettier
__tests__/runner/*

View File

@ -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
jest.config.js Normal file
View File

@ -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
}

View File

@ -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;
}

5113
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@
"scripts": {
"build": "tsc",
"format": "prettier --write **/*.ts",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"repository": {
"type": "git",
@ -29,9 +29,13 @@
"semver": "^6.1.1"
},
"devDependencies": {
"@types/jest": "^24.0.13",
"@types/node": "^12.0.4",
"@types/semver": "^6.0.0",
"jest": "^24.8.0",
"jest-circus": "^24.7.1",
"prettier": "^1.17.1",
"ts-jest": "^24.0.2",
"typescript": "^3.5.1"
}
}

View File

@ -61,9 +61,7 @@ export async function getNode(versionSpec: string) {
//
// 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);
}
async function queryLatestMatch(versionSpec: string): Promise<string> {
@ -159,13 +157,8 @@ async function acquireNode(version: string): Promise<string> {
//
let extPath: string;
if (osPlat == 'win32') {
extPath = getAgentTemp();
if (!extPath) {
throw new Error('Expected Agent.TempDirectory to be set');
}
let _7zPath = path.join(__dirname, '7zr.exe');
extPath = await tc.extract7z(downloadPath, extPath);
extPath = await tc.extract7z(downloadPath);
} else {
extPath = await tc.extractTar(downloadPath);
}
@ -195,7 +188,7 @@ async function acquireNodeFromFallbackLocation(
// Create temporary folder to download in to
let tempDownloadFolder: string =
'temp_' + Math.floor(Math.random() * 2000000000);
let tempDir: string = path.join(getAgentTemp(), tempDownloadFolder);
let tempDir: string = path.join(__dirname, tempDownloadFolder);
await io.mkdirP(tempDir);
let exeUrl: string;
let libUrl: string;
@ -222,13 +215,3 @@ async function acquireNodeFromFallbackLocation(
}
return await tc.cacheDir(tempDir, 'node', version);
}
function getAgentTemp(): string {
// 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;
}

View File

@ -61,5 +61,6 @@
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
}
},
"exclude": ["node_modules", "**/*.test.ts"]
}