Skip to content
Closed
Show file tree
Hide file tree
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
32 changes: 31 additions & 1 deletion __tests__/installer.test.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,7 +109,7 @@ describe('setup-node', () => {

// @actions/exec
getExecOutputSpy = jest.spyOn(exec, 'getExecOutput');
getExecOutputSpy.mockImplementation(() => 'v16.15.0');
getExecOutputSpy.mockImplementation(() => ({stdout: 'v12.16.1'}));
});

afterEach(() => {
Expand DownExpand Up@@ -205,6 +205,35 @@ describe('setup-node', () => {
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
});

it('finds incorrect version in cache, and adds correct version the path', async () => {
os.platform = 'linux';
os.arch = 'x64';
inputs['token'] = 'faketoken';

let versionSpec = '12.16.2';
inputs['node-version'] = versionSpec;

let toolPath = path.normalize('/cache/node/12.16.2/x64');
let expPath = path.join(toolPath, 'bin');

inSpy.mockImplementation(name => inputs[name]);
cacheSpy.mockImplementation(async () => toolPath);
getExecOutputSpy.mockReturnValueOnce({stdout: `v14.0.0`});
findSpy.mockImplementationOnce(() => toolPath);
findSpy.mockImplementationOnce(() => '');

await main.run();

expect(logSpy).toHaveBeenCalledWith(
`Found v14.0.0 in cache @ ${expPath} but it does not satisfy the requested version (12.16.2)`
);
expect(logSpy).toHaveBeenCalledWith(
`Attempting to download ${versionSpec}...`
);
expect(logSpy).toHaveBeenCalledWith(`Adding to the cache ...`);
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
});

it('handles unhandled find error and reports error', async () => {
let errMsg = 'unhandled error message';
inputs['node-version'] = '12';
Expand DownExpand Up@@ -695,6 +724,7 @@ describe('setup-node', () => {
inputs['node-version'] = `lts/${lts}`;

const toolPath = path.normalize(`/cache/node/${expectedVersion}/x64`);
getExecOutputSpy.mockReturnValueOnce({stdout: `v${expectedVersion}`});
findSpy.mockReturnValue(toolPath);

// act
Expand Down
13 changes: 12 additions & 1 deletion dist/setup/index.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -71409,6 +71409,7 @@ const core = __importStar(__nccwpck_require__(2186));
const hc = __importStar(__nccwpck_require__(9925));
const io = __importStar(__nccwpck_require__(7436));
const tc = __importStar(__nccwpck_require__(7784));
const exec = __importStar(__nccwpck_require__(1514));
const path = __importStar(__nccwpck_require__(1017));
const semver = __importStar(__nccwpck_require__(5911));
const fs = __nccwpck_require__(7147);
Expand DownExpand Up@@ -71447,8 +71448,18 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) {
// If not found in cache, download
if (toolPath) {
core.info(`Found in cache @ ${toolPath}`);
if (osPlat != 'win32') {
toolPath = path.join(toolPath, 'bin');
}
core.addPath(toolPath);
const { stdout: installedVersion } = yield exec.getExecOutput('node', ['--version'], { ignoreReturnCode: true });
if (semver.satisfies(installedVersion, versionSpec)) {
return;
}
core.info(`Found ${installedVersion} in cache @ ${toolPath} but it does not satisfy the requested version (${versionSpec})`);
toolPath = '';
}
else {
if (!toolPath) {
core.info(`Attempting to download ${versionSpec}...`);
let downloadPath = '';
let info = null;
Expand Down
25 changes: 24 additions & 1 deletion src/installer.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@ import * as core from '@actions/core';
import * as hc from '@actions/http-client';
import * as io from '@actions/io';
import * as tc from '@actions/tool-cache';
import * as exec from '@actions/exec';
import * as path from 'path';
import * as semver from 'semver';
import fs = require('fs');
Expand DownExpand Up@@ -80,7 +81,29 @@ export async function getNode(
// If not found in cache, download
if (toolPath) {
core.info(`Found in cache @ ${toolPath}`);
} else {

if (osPlat != 'win32') {
toolPath = path.join(toolPath, 'bin');
}

core.addPath(toolPath);

const {stdout: installedVersion} = await exec.getExecOutput(
'node',
['--version'],
{ignoreReturnCode: true}
);

if (semver.satisfies(installedVersion, versionSpec)) {
return;
}
core.info(
`Found ${installedVersion} in cache @ ${toolPath} but it does not satisfy the requested version (${versionSpec})`
);
toolPath = '';
}

if (!toolPath) {
core.info(`Attempting to download ${versionSpec}...`);
let downloadPath = '';
let info: INodeVersionInfo | null = null;
Expand Down