diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index 321f6efe..459f33cd 100644 --- a/.github/workflows/versions.yml +++ b/.github/workflows/versions.yml @@ -89,7 +89,7 @@ jobs: - name: Setup node from node version file uses: ./ with: - node-version-file: '.nvmrc' + node-version-file: '__tests__/data/.nvmrc' - name: Verify node run: __tests__/verify-node.sh 14 diff --git a/README.md b/README.md index 28636268..3e01b7fe 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # setup-node +

build-test status versions status proxy status

@@ -71,7 +72,6 @@ steps: - run: npm test ``` - ## Matrix Testing: ```yaml jobs: @@ -93,7 +93,7 @@ jobs: ## Advanced usage 1. [Check latest version](docs/advanced-usage.md#check-latest-version) -2. [Using a node version file](docs/advanced-usage.md#Node-version-file) +2. [Using a node version file](docs/advanced-usage.md#node-version-file) 3. [Using different architectures](docs/advanced-usage.md#architecture) 4. [Caching packages dependencies](docs/advanced-usage.md#caching-packages-dependencies) 5. [Using multiple operating systems and architectures](docs/advanced-usage.md#multiple-operating-systems-and-architectures) diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 6d91faf3..4565dd4c 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -65,7 +65,6 @@ describe('setup-node', () => { // io whichSpy = jest.spyOn(io, 'which'); existsSpy = jest.spyOn(fs, 'existsSync'); - readFileSyncSpy = jest.spyOn(fs, 'readFileSync'); mkdirpSpy = jest.spyOn(io, 'mkdirP'); // disable authentication portion for installer tests @@ -95,6 +94,10 @@ describe('setup-node', () => { // uncomment to see debug output // process.stderr.write(msg + '\n'); }); + warningSpy.mockImplementation(msg => { + // uncomment to debug + // process.stderr.write('log:' + line + '\n'); + }); }); afterEach(() => { @@ -563,7 +566,7 @@ describe('setup-node', () => { await main.run(); // Assert - expect(readFileSyncSpy).toHaveBeenCalledTimes(0); + expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0); }); it('not used if node-version-file not provided', async () => { @@ -571,32 +574,73 @@ describe('setup-node', () => { await main.run(); // Assert - expect(readFileSyncSpy).toHaveBeenCalledTimes(0); + expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0); }); it('reads node-version-file if provided', async () => { // Arrange - const versionSpec = 'v12'; + const versionSpec = 'v14'; const versionFile = '.nvmrc'; - const expectedVersionSpec = '12'; + 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'; + const versionFile = '.nvmrc'; + const expectedVersionSpec = '14'; process.env['GITHUB_WORKSPACE'] = path.join(__dirname, '..'); inputs['node-version-file'] = versionFile; - readFileSyncSpy.mockImplementation(() => versionSpec); parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec); // Act await main.run(); // Assert - expect(readFileSyncSpy).toHaveBeenCalledTimes(1); - expect(readFileSyncSpy).toHaveBeenCalledWith( - path.join(process.env.GITHUB_WORKSPACE, versionFile), - 'utf8' + expect(existsSpy).toHaveBeenCalledTimes(0); + expect(parseNodeVersionSpy).not.toHaveBeenCalled(); + expect(warningSpy).toHaveBeenCalledWith( + 'Both node-version and node-version-file inputs are specified, only node-version will be used' ); - expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec); - expect(logSpy).toHaveBeenCalledWith( - `Resolved ${versionFile} as ${expectedVersionSpec}` + }); + + it('should throw an error if node-version-file is not found', async () => { + const versionFile = '.nvmrc'; + const versionFilePath = path.join(__dirname, '..', versionFile); + inputs['node-version-file'] = versionFile; + + inSpy.mockImplementation(name => inputs[name]); + existsSpy.mockImplementationOnce( + input => input === path.join(__dirname, 'data', versionFile) + ); + + // Act + await main.run(); + + // Assert + expect(existsSpy).toHaveBeenCalled(); + expect(existsSpy).toHaveReturnedWith(false); + expect(parseNodeVersionSpy).not.toHaveBeenCalled(); + expect(cnSpy).toHaveBeenCalledWith( + `::error::The specified node version file at: ${versionFilePath} does not exist${osm.EOL}` ); }); }); diff --git a/action.yml b/action.yml index 66b170ca..900588b2 100644 --- a/action.yml +++ b/action.yml @@ -8,7 +8,7 @@ inputs: node-version: description: 'Version Spec of the version to use. Examples: 12.x, 10.15.1, >=10.15.0' node-version-file: - description: 'File containing the version Spec of the version to use. Examples: .nvmrc' + description: 'File containing the version Spec of the version to use. Examples: .nvmrc, .node-version, .n-node-version' architecture: description: 'Target architecture for Node to use. Examples: x86, x64. Will use system architecture by default.' check-latest: diff --git a/dist/setup/index.js b/dist/setup/index.js index 65317ba5..fc9ea9fa 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -7006,7 +7006,7 @@ function resolveVersionInput() { let version = core.getInput('node-version') || core.getInput('version'); const versionFileInput = core.getInput('node-version-file'); if (version && versionFileInput) { - core.warning('Both node-version and node-version-file are specified'); + core.warning('Both node-version and node-version-file inputs are specified, only node-version will be used'); } if (version) { return version; @@ -7014,7 +7014,7 @@ function resolveVersionInput() { if (versionFileInput) { const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFileInput); if (!fs_1.default.existsSync(versionFilePath)) { - throw new Error('No specified file exists'); + throw new Error(`The specified node version file at: ${versionFilePath} does not exist`); } version = installer.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8')); core.info(`Resolved ${versionFileInput} as ${version}`); diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index a7eb6383..960772f5 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -21,9 +21,9 @@ steps: ## Node version file -The `node-version-file` input contains the version of node used by project, for example `.nvmrc`. If both the `node-version` and the `node-version-file` inputs are provided the `node-version` input is used. -You can check [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax) -> The action will search for the node version file relative to repository root. +The `node-version-file` input accepts a path to a file containing the version of Node.js to be used by a project, for example `.nvmrc` or `.node-version`. If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used. +See [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax) +> The action will search for the node version file relative to the repository root. ```yaml steps: diff --git a/src/main.ts b/src/main.ts index 434b868e..58c902b8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -78,7 +78,9 @@ function resolveVersionInput(): string { const versionFileInput = core.getInput('node-version-file'); if (version && versionFileInput) { - core.warning('Both node-version and node-version-file are specified'); + core.warning( + 'Both node-version and node-version-file inputs are specified, only node-version will be used' + ); } if (version) { @@ -91,7 +93,9 @@ function resolveVersionInput(): string { versionFileInput ); if (!fs.existsSync(versionFilePath)) { - throw new Error('No specified file exists'); + throw new Error( + `The specified node version file at: ${versionFilePath} does not exist` + ); } version = installer.parseNodeVersionFile( fs.readFileSync(versionFilePath, 'utf8')