Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
panticmilos committed May 19, 2022
1 parent 9a03ebd commit fd1b409
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
27 changes: 27 additions & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...');
}
);
});
});
12 changes: 7 additions & 5 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...`);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -62692,6 +62691,9 @@ function parseNodeVersionFile(contents) {
return nodeVersion;
}
exports.parseNodeVersionFile = parseNodeVersionFile;
function isLatestSyntax(versionSpec) {
return ['current', 'latest', 'node'].includes(versionSpec);
}


/***/ }),
Expand Down
17 changes: 9 additions & 8 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...`);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -487,3 +484,7 @@ export function parseNodeVersionFile(contents: string): string {
}
return nodeVersion;
}

function isLatestSyntax(versionSpec): boolean {
return ['current', 'latest', 'node'].includes(versionSpec);
}

0 comments on commit fd1b409

Please sign in to comment.