Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand current syntax to support aliases for latest version (current/latest/node) #483

Merged
merged 23 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add unit tests
  • Loading branch information
panticmilos committed Apr 27, 2022
commit fdd541042be1bb391a2c056ee54d601d796baf87
40 changes: 40 additions & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
);

});
});
});
14 changes: 8 additions & 6 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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest omitting this import statement.


//
// Node versions interface
Expand Down Expand Up @@ -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,
Expand All @@ -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> {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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> {
Expand All @@ -371,10 +372,11 @@ async function queryDistForMatch(
}

let versions: string[] = [];
let nodeVersions = await getVersionsFromDist();
let nodeVersions = await installer.getVersionsFromDist();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we omit the import statement above, we can omit that also:

Suggested change
let nodeVersions = await installer.getVersionsFromDist();
let nodeVersions = await 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) => {
Expand Down