From f89df17cc97b927ef92b95bd161f18dc87f957b6 Mon Sep 17 00:00:00 2001 From: Steve Lacey Date: Mon, 30 Jan 2023 14:41:29 +0800 Subject: [PATCH] Support .node-version and .nvmrc by default; closes #683 --- __tests__/data/.node-version | 1 + __tests__/main.test.ts | 55 ++++++++++++++++++++++++++++++------ dist/setup/index.js | 17 +++++++++-- src/main.ts | 23 +++++++++++++-- 4 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 __tests__/data/.node-version diff --git a/__tests__/data/.node-version b/__tests__/data/.node-version new file mode 100644 index 000000000..5edcff036 --- /dev/null +++ b/__tests__/data/.node-version @@ -0,0 +1 @@ +v16 \ No newline at end of file diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index abd3c022b..9609769fb 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -155,14 +155,6 @@ describe('main tests', () => { expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0); }, 10000); - it('not used if node-version-file not provided', async () => { - // Act - await main.run(); - - // Assert - expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0); - }); - it('reads node-version-file if provided', async () => { // Arrange const versionSpec = 'v14'; @@ -215,6 +207,53 @@ describe('main tests', () => { ); }, 10000); + it('reads .node-version if node-version and node-version-file were not provided', async () => { + // Arrange + const versionSpec = 'v16'; + const versionFile = '.node-version'; + const expectedVersionSpec = '16'; + process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); + + 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(infoSpy).toHaveBeenCalledWith( + `Resolved ${versionFile} as ${expectedVersionSpec}` + ); + }, 10000); + + it('reads .nvmrc if node-version and node-version-file were not provided', async () => { + // Arrange + const versionSpec = 'v14'; + const versionFile = '.nvmrc'; + const expectedVersionSpec = '14'; + process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); + + parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec); + existsSpy.mockImplementation( + input => input === path.join(__dirname, 'data', versionFile) + ); + + // Act + await main.run(); + + // Assert + expect(existsSpy).toHaveBeenCalledTimes(2); + expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec); + expect(infoSpy).toHaveBeenCalledWith( + `Resolved ${versionFile} as ${expectedVersionSpec}` + ); + }, 10000); + it('both node-version-file and node-version are provided', async () => { inputs['node-version'] = '12'; const versionSpec = 'v14'; diff --git a/dist/setup/index.js b/dist/setup/index.js index ff4bec264..a2c09edb6 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -74003,11 +74003,22 @@ function resolveVersionInput() { } if (versionFileInput) { const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFileInput); - if (!fs_1.default.existsSync(versionFilePath)) { + if (fs_1.default.existsSync(versionFilePath)) { + version = util_1.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8')); + core.info(`Resolved ${versionFileInput} as ${version}`); + return version; + } + else { throw new Error(`The specified node version file at: ${versionFilePath} does not exist`); } - version = util_1.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8')); - core.info(`Resolved ${versionFileInput} as ${version}`); + } + for (const versionFile of ['.node-version', '.nvmrc']) { + const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFile); + if (fs_1.default.existsSync(versionFilePath)) { + version = util_1.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8')); + core.info(`Resolved ${versionFile} as ${version}`); + return version; + } } return version; } diff --git a/src/main.ts b/src/main.ts index 90cd1d9d9..a95153eaa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -97,15 +97,32 @@ function resolveVersionInput(): string { versionFileInput ); - if (!fs.existsSync(versionFilePath)) { + if (fs.existsSync(versionFilePath)) { + version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8')); + + core.info(`Resolved ${versionFileInput} as ${version}`); + + return version; + } else { throw new Error( `The specified node version file at: ${versionFilePath} does not exist` ); } + } - version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8')); + for (const versionFile of ['.node-version', '.nvmrc']) { + const versionFilePath = path.join( + process.env.GITHUB_WORKSPACE!, + versionFile + ); + + if (fs.existsSync(versionFilePath)) { + version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8')); - core.info(`Resolved ${versionFileInput} as ${version}`); + core.info(`Resolved ${versionFile} as ${version}`); + + return version; + } } return version;