From dbfbe9b6dab362ff4d3dacc32758182a21c603bf Mon Sep 17 00:00:00 2001 From: Jef LeCompte Date: Wed, 29 Jun 2022 15:37:53 -0700 Subject: [PATCH] refactor: move volta logic --- __tests__/installer.test.ts | 24 ++++++++++++++++++++---- dist/setup/index.js | 18 +++++++++--------- src/installer.ts | 8 +++++++- src/main.ts | 14 ++++---------- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 26cf7d7..42c6760 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -560,7 +560,7 @@ describe('setup-node', () => { expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0); }); - it('reads node-version-file if provided', async () => { + it('reads node-version-file if provided (.nvmrc)', async () => { // Arrange const versionSpec = 'v14'; const versionFile = '.nvmrc'; @@ -572,6 +572,7 @@ describe('setup-node', () => { existsSpy.mockImplementationOnce( input => input === path.join(__dirname, 'data', versionFile) ); + // Act await main.run(); @@ -584,22 +585,37 @@ describe('setup-node', () => { ); }); - it('reads node-version-file if provided with volta', async () => { + it('reads node-version-file if provided (package.json, volta)', async () => { // Arrange - const expectedVersionSpec = '16.15.1'; + const versionSpec = `{ + "name": "test", + "version": "1.0.0", + "private": true, + "scripts": { + "test": "echo test" + }, + "volta": { + "node": "16.15.1" + } +} +`; const versionFile = 'package.json'; + const expectedVersionSpec = '16.15.1'; process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); - inputs['node-version-file'] = 'volta'; + inputs['node-version-file'] = versionFile; + parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec); existsSpy.mockImplementationOnce( input => input === path.join(__dirname, 'data', versionFile) ); + // Act await main.run(); // Assert expect(existsSpy).toHaveBeenCalledTimes(1); expect(existsSpy).toHaveReturnedWith(true); + expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec); expect(logSpy).toHaveBeenCalledWith( `Resolved ${versionFile} as ${expectedVersionSpec}` ); diff --git a/dist/setup/index.js b/dist/setup/index.js index cae0cf5..0a98b4d 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -71768,7 +71768,13 @@ function translateArchToDistUrl(arch) { } } function parseNodeVersionFile(contents) { - let nodeVersion = contents.trim(); + let nodeVersion; + if (contents.includes('volta')) { + nodeVersion = JSON.parse(contents).volta.node; + } + else { + nodeVersion = contents.trim(); + } if (/^v\d/.test(nodeVersion)) { nodeVersion = nodeVersion.substring(1); } @@ -71862,8 +71868,7 @@ function run() { exports.run = run; function resolveVersionInput() { let version = core.getInput('node-version'); - const nodeVersionFile = core.getInput('node-version-file'); - const versionFileInput = nodeVersionFile === 'volta' ? 'package.json' : nodeVersionFile; + const versionFileInput = core.getInput('node-version-file'); if (version && versionFileInput) { core.warning('Both node-version and node-version-file inputs are specified, only node-version will be used'); } @@ -71875,12 +71880,7 @@ function resolveVersionInput() { if (!fs_1.default.existsSync(versionFilePath)) { throw new Error(`The specified node version file at: ${versionFilePath} does not exist`); } - if (nodeVersionFile === 'volta') { - version = JSON.parse(fs_1.default.readFileSync(versionFilePath, 'utf8')).volta.node; - } - else { - version = installer.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8')); - } + version = installer.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8')); core.info(`Resolved ${versionFileInput} as ${version}`); } return version; diff --git a/src/installer.ts b/src/installer.ts index 97e3bca..c9fd0d2 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -495,7 +495,13 @@ function translateArchToDistUrl(arch: string): string { } export function parseNodeVersionFile(contents: string): string { - let nodeVersion = contents.trim(); + let nodeVersion; + + if (contents.includes('volta')) { + nodeVersion = JSON.parse(contents).volta.node; + } else { + nodeVersion = contents.trim(); + } if (/^v\d/.test(nodeVersion)) { nodeVersion = nodeVersion.substring(1); diff --git a/src/main.ts b/src/main.ts index 4ad4e71..4257637 100644 --- a/src/main.ts +++ b/src/main.ts @@ -65,9 +65,7 @@ export async function run() { function resolveVersionInput(): string { let version = core.getInput('node-version'); - const nodeVersionFile = core.getInput('node-version-file'); - const versionFileInput = - nodeVersionFile === 'volta' ? 'package.json' : nodeVersionFile; + const versionFileInput = core.getInput('node-version-file'); if (version && versionFileInput) { core.warning( @@ -91,13 +89,9 @@ function resolveVersionInput(): string { ); } - if (nodeVersionFile === 'volta') { - version = JSON.parse(fs.readFileSync(versionFilePath, 'utf8')).volta.node; - } else { - version = installer.parseNodeVersionFile( - fs.readFileSync(versionFilePath, 'utf8') - ); - } + version = installer.parseNodeVersionFile( + fs.readFileSync(versionFilePath, 'utf8') + ); core.info(`Resolved ${versionFileInput} as ${version}`); }