From fd1b409bc3adb8a843e511763a0160bfa08e1aea Mon Sep 17 00:00:00 2001 From: panticmilos Date: Thu, 19 May 2022 17:33:13 +0200 Subject: [PATCH] unit tests --- __tests__/installer.test.ts | 27 +++++++++++++++++++++++++++ dist/setup/index.js | 12 +++++++----- src/installer.ts | 17 +++++++++-------- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 004362d0..f3eb8db9 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -913,4 +913,31 @@ describe('setup-node', () => { } ); }); + + describe('latest alias syntax from cache', () => { + it.each(['latest', 'current', 'node'])( + 'download the %s version if alias is provided', + async inputVersion => { + // Arrange + inputs['node-version'] = inputVersion; + const expectedVersion = nodeTestDist[0]; + + os.platform = 'darwin'; + os.arch = 'x64'; + + const toolPath = path.normalize( + `/cache/node/${expectedVersion.version}/x64` + ); + findSpy.mockReturnValue(toolPath); + + // Act + await main.run(); + + // assert + expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`); + + expect(logSpy).toHaveBeenCalledWith('getting latest node version...'); + } + ); + }); }); diff --git a/dist/setup/index.js b/dist/setup/index.js index 6740015e..0a513f92 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -62339,6 +62339,7 @@ const tc = __importStar(__webpack_require__(533)); const path = __importStar(__webpack_require__(622)); const semver = __importStar(__webpack_require__(280)); const fs = __webpack_require__(747); +const installer = __importStar(__webpack_require__(923)); function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) { return __awaiter(this, void 0, void 0, function* () { // Store manifest data to avoid multiple calls @@ -62362,7 +62363,7 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) { core.info(`Failed to resolve version ${versionSpec} from manifest`); } } - if (['current', 'latest', 'node'].includes(versionSpec)) { + if (isLatestSyntax(versionSpec)) { versionSpec = yield queryDistForMatch(versionSpec, arch); core.info(`getting latest node version...`); } @@ -62590,10 +62591,8 @@ function queryDistForMatch(versionSpec, arch = os.arch()) { throw new Error(`Unexpected OS '${osPlat}'`); } let versions = []; - let nodeVersions = yield getVersionsFromDist(); - if (versionSpec === 'current' || - versionSpec === 'latest' || - versionSpec === 'node') { + let nodeVersions = yield installer.getVersionsFromDist(); + if (isLatestSyntax(versionSpec)) { core.info(`getting latest node version...`); return nodeVersions[0].version; } @@ -62692,6 +62691,9 @@ function parseNodeVersionFile(contents) { return nodeVersion; } exports.parseNodeVersionFile = parseNodeVersionFile; +function isLatestSyntax(versionSpec) { + return ['current', 'latest', 'node'].includes(versionSpec); +} /***/ }), diff --git a/src/installer.ts b/src/installer.ts index 4029b596..4f7603ae 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -7,6 +7,7 @@ import * as tc from '@actions/tool-cache'; import * as path from 'path'; import * as semver from 'semver'; import fs = require('fs'); +import * as installer from './installer'; // // Node versions interface @@ -66,8 +67,8 @@ export async function getNode( } } - if(['current', 'latest', 'node'].includes(versionSpec)) { - versionSpec = await queryDistForMatch(versionSpec, arch); + if (isLatestSyntax(versionSpec)) { + versionSpec = await queryDistForMatch(versionSpec, arch); core.info(`getting latest node version...`); } @@ -376,13 +377,9 @@ async function queryDistForMatch( } let versions: string[] = []; - let nodeVersions = await getVersionsFromDist(); + let nodeVersions = await installer.getVersionsFromDist(); - if ( - versionSpec === 'current' || - versionSpec === 'latest' || - versionSpec === 'node' - ) { + if (isLatestSyntax(versionSpec)) { core.info(`getting latest node version...`); return nodeVersions[0].version; } @@ -487,3 +484,7 @@ export function parseNodeVersionFile(contents: string): string { } return nodeVersion; } + +function isLatestSyntax(versionSpec): boolean { + return ['current', 'latest', 'node'].includes(versionSpec); +}