add unit tests

This commit is contained in:
panticmilos 2022-04-27 17:28:52 +02:00
parent 983e751c52
commit fdd541042b
2 changed files with 48 additions and 6 deletions

View file

@ -909,4 +909,44 @@ describe('setup-node', () => {
);
});
});
describe('latest alias syntax', () => {
it.each(['latest', 'current', 'node'])('download the %s version if alias is provided', async (inputVersion) => {
// Arrange
inputs['node-version'] = inputVersion;
os.platform = 'darwin';
os.arch = 'x64';
const expectedVersion = nodeTestDist[0];
let expectedUrl = `https://nodejs.org/dist/${expectedVersion.version}/node-${expectedVersion.version}-${os.platform}-${os.arch}.tar.gz`;
findSpy.mockImplementation(() => '');
getManifestSpy.mockImplementation(() => {
throw new Error('Unable to download manifest');
});
// Act
await main.run();
// Assert
expect(logSpy).toHaveBeenCalledWith(
`Attempting to download ${inputVersion}...`
);
expect(logSpy).toHaveBeenCalledWith(
'Unable to download manifest'
);
expect(logSpy).toHaveBeenCalledWith(
'getting latest node version...'
);
expect(logSpy).toHaveBeenCalledWith(
`Acquiring ${expectedVersion.version.substring(1, expectedVersion.version.length)} - ${os.arch} from ${expectedUrl}`
);
});
});
});

View file

@ -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
@ -237,7 +238,7 @@ function resolveLtsAliasFromManifest(
return release.version.split('.')[0];
}
async function getInfoFromManifest(
export async function getInfoFromManifest(
versionSpec: string,
stable: boolean,
auth: string | undefined,
@ -263,7 +264,7 @@ async function getInfoFromManifest(
return info;
}
async function getInfoFromDist(
export async function getInfoFromDist(
versionSpec: string,
arch: string = os.arch()
): Promise<INodeVersionInfo | null> {
@ -320,7 +321,7 @@ async function resolveVersionFromManifest(
}
// TODO - should we just export this from @actions/tool-cache? Lifted directly from there
function evaluateVersions(versions: string[], versionSpec: string): string {
export function evaluateVersions(versions: string[], versionSpec: string): string {
let version = '';
core.debug(`evaluating ${versions.length} versions`);
versions = versions.sort((a, b) => {
@ -347,7 +348,7 @@ function evaluateVersions(versions: string[], versionSpec: string): string {
return version;
}
async function queryDistForMatch(
export async function queryDistForMatch(
versionSpec: string,
arch: string = os.arch()
): Promise<string> {
@ -371,10 +372,11 @@ async function queryDistForMatch(
}
let versions: string[] = [];
let nodeVersions = await getVersionsFromDist();
let nodeVersions = await installer.getVersionsFromDist();
if (versionSpec === 'current' || versionSpec === 'latest' || versionSpec === 'node') {
return nodeVersions[0].version
core.info(`getting latest node version...`);
return nodeVersions[0].version;
}
nodeVersions.forEach((nodeVersion: INodeVersion) => {