From 7d279e1d70655cc38b436cdda491c44620218ce3 Mon Sep 17 00:00:00 2001 From: "mannie.exe" Date: Fri, 6 May 2022 09:31:10 -0700 Subject: [PATCH 1/4] Allow reading 'package.json' as node-version-file --- __tests__/data/package.json | 5 +++++ __tests__/installer.test.ts | 29 +++++++++++++++++++++++++++++ src/installer.ts | 22 +++++++++++++++------- 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 __tests__/data/package.json diff --git a/__tests__/data/package.json b/__tests__/data/package.json new file mode 100644 index 000000000..e537e2005 --- /dev/null +++ b/__tests__/data/package.json @@ -0,0 +1,5 @@ +{ + "engines": { + "node": ">=14.0.0" + } +} diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index cd5361929..d04eb5822 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -591,6 +591,35 @@ describe('setup-node', () => { ); }); + it('reads package.json as node-version-file if provided', async () => { + // Arrange + const versionSpec = `{ + \"engines\": { + \"node\": \">=14.0.0\" + } +} +`; + const versionFile = 'package.json'; + const expectedVersionSpec = '14'; + process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); + 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}` + ); + }); + it('both node-version-file and node-version are provided', async () => { inputs['node-version'] = '12'; const versionSpec = 'v14'; diff --git a/src/installer.ts b/src/installer.ts index 019f2edab..8fa44b018 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -495,16 +495,24 @@ function translateArchToDistUrl(arch: string): string { } export function parseNodeVersionFile(contents: string): string { - const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); - const nodeVersion = found?.groups?.version; + // In the case of an unknown format, + // return as is and evaluate the version separately. + let nodeVersion = contents.trim(); + + try { + // Assume all content parseable as JSON is a valid, + // NPM `package.json` file + const packageJson = JSON.parse(contents); + nodeVersion = packageJson.engines.node || nodeVersion; - if (nodeVersion) { - return nodeVersion; + if (!nodeVersion) throw + } catch (err) { + // If not, assume it is a node version file + const found = nodeVersion.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); + nodeVersion = found?.groups?.version || nodeVersion; } - // In the case of an unknown format, - // return as is and evaluate the version separately. - return contents.trim(); + return nodeVersion; } function isLatestSyntax(versionSpec): boolean { From 57edf6c8ba71f6ebf19394a8724946d592c61704 Mon Sep 17 00:00:00 2001 From: "mannie.exe" Date: Fri, 6 May 2022 09:31:23 -0700 Subject: [PATCH 2/4] Run 'npm run build' --- dist/setup/index.js | 24 +++++++++++++++++------- src/installer.ts | 30 ++++++++++++++++-------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index 28d5dc8fd..0a0c0bade 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -71768,15 +71768,25 @@ function translateArchToDistUrl(arch) { } } function parseNodeVersionFile(contents) { - var _a; + var _a, _b; + let nodeVersion; const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); - const nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version; - if (nodeVersion) { - return nodeVersion; + nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version; + if (!nodeVersion) { + try { + // Try parsing the file as an NPM `package.json` + // file. + nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node; + if (!nodeVersion) + throw new Error(); + } + catch (err) { + // In the case of an unknown format, + // return as is and evaluate the version separately. + nodeVersion = contents.trim(); + } } - // In the case of an unknown format, - // return as is and evaluate the version separately. - return contents.trim(); + return nodeVersion; } exports.parseNodeVersionFile = parseNodeVersionFile; function isLatestSyntax(versionSpec) { diff --git a/src/installer.ts b/src/installer.ts index 8fa44b018..193ff16a7 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -495,24 +495,26 @@ function translateArchToDistUrl(arch: string): string { } export function parseNodeVersionFile(contents: string): string { - // In the case of an unknown format, - // return as is and evaluate the version separately. - let nodeVersion = contents.trim(); + let nodeVersion: string | undefined; - try { - // Assume all content parseable as JSON is a valid, - // NPM `package.json` file - const packageJson = JSON.parse(contents); - nodeVersion = packageJson.engines.node || nodeVersion; + const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); + nodeVersion = found?.groups?.version; - if (!nodeVersion) throw - } catch (err) { - // If not, assume it is a node version file - const found = nodeVersion.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); - nodeVersion = found?.groups?.version || nodeVersion; + if (!nodeVersion) { + try { + // Try parsing the file as an NPM `package.json` + // file. + nodeVersion = JSON.parse(contents).engines?.node; + + if (!nodeVersion) throw new Error(); + } catch (err) { + // In the case of an unknown format, + // return as is and evaluate the version separately. + nodeVersion = contents.trim(); + } } - return nodeVersion; + return nodeVersion as string; } function isLatestSyntax(versionSpec): boolean { From f8cdefe984b404261e8b055007f7a6623b16e615 Mon Sep 17 00:00:00 2001 From: schmannie Date: Sun, 15 May 2022 02:42:47 -0700 Subject: [PATCH 3/4] Read package.json contents directly during tests - this eliminates OS-specific line-ending issues --- __tests__/installer.test.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index d04eb5822..ceb34aaaf 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -593,12 +593,7 @@ describe('setup-node', () => { it('reads package.json as node-version-file if provided', async () => { // Arrange - const versionSpec = `{ - \"engines\": { - \"node\": \">=14.0.0\" - } -} -`; + const versionSpec = fs.readFileSync(path.join(__dirname, 'data/package.json'), 'utf-8'); const versionFile = 'package.json'; const expectedVersionSpec = '14'; process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); From 3bf06e207ba17768f6adb1c35dc69a82ae3a500a Mon Sep 17 00:00:00 2001 From: schmannie Date: Mon, 16 May 2022 09:32:19 -0700 Subject: [PATCH 4/4] =?UTF-8?q?Run=20project=20Prettier=20=F0=9F=92=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/installer.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index ceb34aaaf..c1e16b5fc 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -593,7 +593,10 @@ describe('setup-node', () => { it('reads package.json as node-version-file if provided', async () => { // Arrange - const versionSpec = fs.readFileSync(path.join(__dirname, 'data/package.json'), 'utf-8'); + const versionSpec = fs.readFileSync( + path.join(__dirname, 'data/package.json'), + 'utf-8' + ); const versionFile = 'package.json'; const expectedVersionSpec = '14'; process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');