Support lts/-n aliases

This commit is contained in:
Jack Bates 2022-05-02 11:40:03 -07:00
parent ed1a46e9f2
commit 0ae8776763
4 changed files with 100 additions and 12 deletions

View file

@ -39,7 +39,7 @@ The `node-version` input supports the following values:
- Major versions: `12`, `14`, `16` - Major versions: `12`, `14`, `16`
- More specific versions: `10.15`, `14.2.0`, `16.3.0` - More specific versions: `10.15`, `14.2.0`, `16.3.0`
- NVM LTS syntax: `lts/erbium`, `lts/fermium`, `lts/*` - NVM LTS syntax: `lts/erbium`, `lts/fermium`, `lts/*`, `lts/-n`
- Latest release: `latest`/`current`/`node` - Latest release: `latest`/`current`/`node`
**Note:** Since the latest release will not be cached always, there is possibility of hitting rate limit when downloading from dist **Note:** Since the latest release will not be cached always, there is possibility of hitting rate limit when downloading from dist

View file

@ -77,9 +77,9 @@ describe('setup-node', () => {
authSpy.mockImplementation(() => {}); authSpy.mockImplementation(() => {});
// gets // gets
getManifestSpy.mockImplementation( getManifestSpy.mockImplementation(() => [
() => <tc.IToolRelease[]>nodeTestManifest ...(<tc.IToolRelease[]>nodeTestManifest)
); ]);
getDistSpy.mockImplementation(() => <im.INodeVersion>nodeTestDist); getDistSpy.mockImplementation(() => <im.INodeVersion>nodeTestDist);
// writes // writes
@ -817,6 +817,76 @@ describe('setup-node', () => {
); );
}); });
it('find latest LTS version and resolve it from local cache (lts/-2)', async () => {
// arrange
inputs['node-version'] = 'lts/-2';
const toolPath = path.normalize('/cache/node/12.16.2/x64');
findSpy.mockReturnValue(toolPath);
// act
await main.run();
// assert
expect(logSpy).toHaveBeenCalledWith(
'Attempt to resolve LTS alias from manifest...'
);
expect(dbgSpy).toHaveBeenCalledWith(
'Getting manifest from actions/node-versions@main'
);
expect(dbgSpy).not.toHaveBeenCalledWith('No manifest cached');
expect(dbgSpy).toHaveBeenCalledWith(
`LTS alias '-2' for Node version 'lts/-2'`
);
expect(dbgSpy).toHaveBeenCalledWith(
`Found LTS release '12.16.2' for Node version 'lts/-2'`
);
expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
expect(cnSpy).toHaveBeenCalledWith(
`::add-path::${path.join(toolPath, 'bin')}${osm.EOL}`
);
});
it('find latest LTS version and install it from manifest (lts/-2)', async () => {
// arrange
inputs['node-version'] = 'lts/-2';
const toolPath = path.normalize('/cache/node/12.16.2/x64');
findSpy.mockImplementation(() => '');
dlSpy.mockImplementation(async () => '/some/temp/path');
exSpy.mockImplementation(async () => '/some/other/temp/path');
cacheSpy.mockImplementation(async () => toolPath);
const expectedUrl =
'https://github.com/actions/node-versions/releases/download/12.16.2-20200423.28/node-12.16.2-linux-x64.tar.gz';
// act
await main.run();
// assert
expect(logSpy).toHaveBeenCalledWith(
'Attempt to resolve LTS alias from manifest...'
);
expect(dbgSpy).toHaveBeenCalledWith(
'Getting manifest from actions/node-versions@main'
);
expect(dbgSpy).not.toHaveBeenCalledWith('No manifest cached');
expect(dbgSpy).toHaveBeenCalledWith(
`LTS alias '-2' for Node version 'lts/-2'`
);
expect(dbgSpy).toHaveBeenCalledWith(
`Found LTS release '12.16.2' for Node version 'lts/-2'`
);
expect(logSpy).toHaveBeenCalledWith('Attempting to download 12...');
expect(logSpy).toHaveBeenCalledWith(
`Acquiring 12.16.2 - ${os.arch} from ${expectedUrl}`
);
expect(logSpy).toHaveBeenCalledWith('Extracting ...');
expect(logSpy).toHaveBeenCalledWith('Adding to the cache ...');
expect(cnSpy).toHaveBeenCalledWith(
`::add-path::${path.join(toolPath, 'bin')}${osm.EOL}`
);
});
it('fail with unable to parse LTS alias (lts/)', async () => { it('fail with unable to parse LTS alias (lts/)', async () => {
// arrange // arrange
inputs['node-version'] = 'lts/'; inputs['node-version'] = 'lts/';

15
dist/setup/index.js vendored
View file

@ -70626,6 +70626,8 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) {
core.info('Attempt to resolve LTS alias from manifest...'); core.info('Attempt to resolve LTS alias from manifest...');
// No try-catch since it's not possible to resolve LTS alias without manifest // No try-catch since it's not possible to resolve LTS alias without manifest
manifest = yield getManifest(auth); manifest = yield getManifest(auth);
// Reverse it so later Object.fromEntries() gets the latest version of each LTS
manifest.reverse();
versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest); versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest);
} }
if (isLatestSyntax(versionSpec)) { if (isLatestSyntax(versionSpec)) {
@ -70756,10 +70758,17 @@ function resolveLtsAliasFromManifest(versionSpec, stable, manifest) {
throw new Error(`Unable to parse LTS alias for Node version '${versionSpec}'`); throw new Error(`Unable to parse LTS alias for Node version '${versionSpec}'`);
} }
core.debug(`LTS alias '${alias}' for Node version '${versionSpec}'`); core.debug(`LTS alias '${alias}' for Node version '${versionSpec}'`);
// Supported formats are `lts/<alias>` and `lts/*`. Where asterisk means highest possible LTS. // Supported formats are `lts/<alias>`, `lts/*`, and `lts/-n`. Where asterisk means highest possible LTS and -n means the nth-highest.
const n = Number(alias);
const aliases = Object.fromEntries(manifest
.filter(x => x.stable === stable)
.map(x => { var _a; return [(_a = x.lts) === null || _a === void 0 ? void 0 : _a.toLowerCase(), x]; }));
const numbered = Object.values(aliases);
const release = alias === '*' const release = alias === '*'
? manifest.find(x => !!x.lts && x.stable === stable) ? numbered[numbered.length - 1]
: manifest.find(x => { var _a; return ((_a = x.lts) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === alias && x.stable === stable; }); : n < 0
? numbered[numbered.length - 1 + n]
: aliases[alias];
if (!release) { if (!release) {
throw new Error(`Unable to find LTS release '${alias}' for Node version '${versionSpec}'.`); throw new Error(`Unable to find LTS release '${alias}' for Node version '${versionSpec}'.`);
} }

View file

@ -46,6 +46,8 @@ export async function getNode(
// No try-catch since it's not possible to resolve LTS alias without manifest // No try-catch since it's not possible to resolve LTS alias without manifest
manifest = await getManifest(auth); manifest = await getManifest(auth);
// Reverse it so later Object.fromEntries() gets the latest version of each LTS
manifest.reverse();
versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest); versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest);
} }
@ -223,13 +225,20 @@ function resolveLtsAliasFromManifest(
core.debug(`LTS alias '${alias}' for Node version '${versionSpec}'`); core.debug(`LTS alias '${alias}' for Node version '${versionSpec}'`);
// Supported formats are `lts/<alias>` and `lts/*`. Where asterisk means highest possible LTS. // Supported formats are `lts/<alias>`, `lts/*`, and `lts/-n`. Where asterisk means highest possible LTS and -n means the nth-highest.
const n = Number(alias);
const aliases = Object.fromEntries(
manifest
.filter(x => x.stable === stable)
.map(x => [x.lts?.toLowerCase(), x])
);
const numbered = Object.values(aliases);
const release = const release =
alias === '*' alias === '*'
? manifest.find(x => !!x.lts && x.stable === stable) ? numbered[numbered.length - 1]
: manifest.find( : n < 0
x => x.lts?.toLowerCase() === alias && x.stable === stable ? numbered[numbered.length - 1 + n]
); : aliases[alias];
if (!release) { if (!release) {
throw new Error( throw new Error(