From c5f0b687c590351a5edeb8702c4450e59f1566a5 Mon Sep 17 00:00:00 2001 From: Ben Sterling <25365573+ben-styling@users.noreply.github.com> Date: Mon, 11 Jul 2022 18:42:06 +0100 Subject: [PATCH] re-download if cached version is incorrect --- __tests__/installer.test.ts | 21 +++++++++++++++++++++ dist/setup/index.js | 8 +++++++- src/installer.ts | 18 +++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index cd536192..3d44c158 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -205,6 +205,27 @@ describe('setup-node', () => { expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`); }); + it('finds incorrect version in cache and adds it to the path', async () => { + let versionSpec = '12.16.2'; + inputs['node-version'] = versionSpec; + + inSpy.mockImplementation(name => inputs[name]); + getExecOutputSpy.mockImplementation(() => 'v12.0.0'); + + let toolPath = path.normalize('/cache/node/12.16.1/x64'); + findSpy.mockImplementation(() => toolPath); + await main.run(); + + let expPath = path.join(toolPath, 'bin'); + expect(logSpy).toHaveBeenCalledWith( + `Found v12.0.0 in cache @ ${toolPath} but it does not satisfy the requested version (${versionSpec})` + ); + expect(logSpy).toHaveBeenCalledWith( + `Attempting to download ${versionSpec}...` + ); + 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'; diff --git a/dist/setup/index.js b/dist/setup/index.js index 1b41467d..addddba6 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -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); @@ -71447,8 +71448,13 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) { // If not found in cache, download if (toolPath) { core.info(`Found in cache @ ${toolPath}`); + const { stdout: installedVersion } = yield exec.getExecOutput('node', ['--version'], { ignoreReturnCode: true }); + if (!semver.satisfies(installedVersion, versionSpec)) { + 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; diff --git a/src/installer.ts b/src/installer.ts index 019f2eda..fae39a8b 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -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'); @@ -80,7 +81,22 @@ export async function getNode( // If not found in cache, download if (toolPath) { core.info(`Found in cache @ ${toolPath}`); - } else { + + const {stdout: installedVersion} = await exec.getExecOutput( + 'node', + ['--version'], + {ignoreReturnCode: true} + ); + + if (!semver.satisfies(installedVersion, versionSpec)) { + 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;