Skip to content

Commit fd1b409

Browse files
committed
unit tests
1 parent 9a03ebd commit fd1b409

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

__tests__/installer.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -913,4 +913,31 @@ describe('setup-node', () => {
913913
}
914914
);
915915
});
916+
917+
describe('latest alias syntax from cache', () => {
918+
it.each(['latest', 'current', 'node'])(
919+
'download the %s version if alias is provided',
920+
async inputVersion => {
921+
// Arrange
922+
inputs['node-version'] = inputVersion;
923+
const expectedVersion = nodeTestDist[0];
924+
925+
os.platform = 'darwin';
926+
os.arch = 'x64';
927+
928+
const toolPath = path.normalize(
929+
`/cache/node/${expectedVersion.version}/x64`
930+
);
931+
findSpy.mockReturnValue(toolPath);
932+
933+
// Act
934+
await main.run();
935+
936+
// assert
937+
expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
938+
939+
expect(logSpy).toHaveBeenCalledWith('getting latest node version...');
940+
}
941+
);
942+
});
916943
});

dist/setup/index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -62339,6 +62339,7 @@ const tc = __importStar(__webpack_require__(533));
6233962339
const path = __importStar(__webpack_require__(622));
6234062340
const semver = __importStar(__webpack_require__(280));
6234162341
const fs = __webpack_require__(747);
62342+
const installer = __importStar(__webpack_require__(923));
6234262343
function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) {
6234362344
return __awaiter(this, void 0, void 0, function* () {
6234462345
// Store manifest data to avoid multiple calls
@@ -62362,7 +62363,7 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) {
6236262363
core.info(`Failed to resolve version ${versionSpec} from manifest`);
6236362364
}
6236462365
}
62365-
if (['current', 'latest', 'node'].includes(versionSpec)) {
62366+
if (isLatestSyntax(versionSpec)) {
6236662367
versionSpec = yield queryDistForMatch(versionSpec, arch);
6236762368
core.info(`getting latest node version...`);
6236862369
}
@@ -62590,10 +62591,8 @@ function queryDistForMatch(versionSpec, arch = os.arch()) {
6259062591
throw new Error(`Unexpected OS '${osPlat}'`);
6259162592
}
6259262593
let versions = [];
62593-
let nodeVersions = yield getVersionsFromDist();
62594-
if (versionSpec === 'current' ||
62595-
versionSpec === 'latest' ||
62596-
versionSpec === 'node') {
62594+
let nodeVersions = yield installer.getVersionsFromDist();
62595+
if (isLatestSyntax(versionSpec)) {
6259762596
core.info(`getting latest node version...`);
6259862597
return nodeVersions[0].version;
6259962598
}
@@ -62692,6 +62691,9 @@ function parseNodeVersionFile(contents) {
6269262691
return nodeVersion;
6269362692
}
6269462693
exports.parseNodeVersionFile = parseNodeVersionFile;
62694+
function isLatestSyntax(versionSpec) {
62695+
return ['current', 'latest', 'node'].includes(versionSpec);
62696+
}
6269562697

6269662698

6269762699
/***/ }),

src/installer.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as tc from '@actions/tool-cache';
77
import * as path from 'path';
88
import * as semver from 'semver';
99
import fs = require('fs');
10+
import * as installer from './installer';
1011

1112
//
1213
// Node versions interface
@@ -66,8 +67,8 @@ export async function getNode(
6667
}
6768
}
6869

69-
if(['current', 'latest', 'node'].includes(versionSpec)) {
70-
versionSpec = await queryDistForMatch(versionSpec, arch);
70+
if (isLatestSyntax(versionSpec)) {
71+
versionSpec = await queryDistForMatch(versionSpec, arch);
7172
core.info(`getting latest node version...`);
7273
}
7374

@@ -376,13 +377,9 @@ async function queryDistForMatch(
376377
}
377378

378379
let versions: string[] = [];
379-
let nodeVersions = await getVersionsFromDist();
380+
let nodeVersions = await installer.getVersionsFromDist();
380381

381-
if (
382-
versionSpec === 'current' ||
383-
versionSpec === 'latest' ||
384-
versionSpec === 'node'
385-
) {
382+
if (isLatestSyntax(versionSpec)) {
386383
core.info(`getting latest node version...`);
387384
return nodeVersions[0].version;
388385
}
@@ -487,3 +484,7 @@ export function parseNodeVersionFile(contents: string): string {
487484
}
488485
return nodeVersion;
489486
}
487+
488+
function isLatestSyntax(versionSpec): boolean {
489+
return ['current', 'latest', 'node'].includes(versionSpec);
490+
}

0 commit comments

Comments
 (0)