Implemented support for repository defined node version files such as '.nvmrc'

This commit is contained in:
Taylor McCarthy 2020-11-01 17:04:22 -05:00
parent c6fd00ceb9
commit c3812bd36a
5 changed files with 83 additions and 0 deletions

View file

@ -55,6 +55,21 @@ steps:
- run: npm test
```
Node version file:
The `node-version-file` input allows you to use a file within your repository which contains the version of node your project uses for example `.nvmrc`. If both the `node-version` and the `node-version-file` inputs are provided the `node-version` input is used.
> The node version file is read from the project root
```yaml
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version-file: '.nvmrc'
- run: npm install
- run: npm test
```
Matrix Testing:
```yaml
jobs:

View file

@ -36,6 +36,7 @@ describe('setup-node', () => {
let dbgSpy: jest.SpyInstance;
let whichSpy: jest.SpyInstance;
let existsSpy: jest.SpyInstance;
let readFileSyncSpy: jest.SpyInstance;
let mkdirpSpy: jest.SpyInstance;
let execSpy: jest.SpyInstance;
let authSpy: jest.SpyInstance;
@ -67,6 +68,7 @@ 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
@ -490,4 +492,48 @@ describe('setup-node', () => {
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
});
});
describe('node-version-file flag', () => {
it('Not used if node-version is provided', async () => {
// Arrange
inputs['node-version'] = '12';
// Act
await main.run();
// Assert
expect(readFileSyncSpy).toHaveBeenCalledTimes(0);
});
it('Not used if node-version-file not provided', async () => {
// Act
await main.run();
// Assert
expect(readFileSyncSpy).toHaveBeenCalledTimes(0);
});
it('Reads node-version-file if provided', async () => {
// Arrange
const versionSpec = 'v12';
const versionFile = '.nvmrc';
inputs['node-version-file'] = versionFile;
readFileSyncSpy.mockImplementation(() => versionSpec);
// Act
await main.run();
// Assert
expect(readFileSyncSpy).toHaveBeenCalledTimes(1);
expect(readFileSyncSpy).toHaveBeenCalledWith(
path.join(__dirname, '..', versionFile),
'utf8'
);
expect(logSpy).toHaveBeenCalledWith(
`Resolved ${versionFile} as ${versionSpec}`
);
});
});
});

View file

@ -7,6 +7,8 @@ inputs:
default: 'false'
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'
check-latest:
description: 'Set this option if you want the action to check for the latest available version that satisfies the version spec'
default: false

9
dist/index.js vendored
View file

@ -4692,6 +4692,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(__webpack_require__(470));
const installer = __importStar(__webpack_require__(749));
const auth = __importStar(__webpack_require__(202));
const fs = __webpack_require__(747);
const path = __importStar(__webpack_require__(622));
const url_1 = __webpack_require__(835);
function run() {
@ -4705,6 +4706,14 @@ function run() {
if (!version) {
version = core.getInput('version');
}
if (!version) {
const versionFile = core.getInput('node-version-file');
if (!!versionFile) {
const versionFilePath = path.join(__dirname, '..', versionFile);
version = fs.readFileSync(versionFilePath, 'utf8');
core.info(`Resolved ${versionFile} as ${version}`);
}
}
if (version) {
let token = core.getInput('token');
let auth = !token || isGhes() ? undefined : `token ${token}`;

View file

@ -1,6 +1,7 @@
import * as core from '@actions/core';
import * as installer from './installer';
import * as auth from './authutil';
import fs = require('fs');
import * as path from 'path';
import {URL} from 'url';
@ -15,6 +16,16 @@ export async function run() {
version = core.getInput('version');
}
if (!version) {
const versionFile = core.getInput('node-version-file');
if (!!versionFile) {
const versionFilePath = path.join(__dirname, '..', versionFile);
version = fs.readFileSync(versionFilePath, 'utf8');
core.info(`Resolved ${versionFile} as ${version}`);
}
}
if (version) {
let token = core.getInput('token');
let auth = !token || isGhes() ? undefined : `token ${token}`;