Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
60 changes: 56 additions & 4 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import cp from 'child_process';
import osm = require('os');
import path from 'path';
import * as main from '../src/main';
import * as im from '../src/installer';
import * as nv from '../src/node-version';
import * as nvf from '../src/node-version-file';
import * as auth from '../src/authutil';
import {context} from '@actions/github';

Expand Down Expand Up @@ -36,9 +37,11 @@ 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;
let parseNodeVersionSpy: jest.SpyInstance;

beforeEach(() => {
// @actions/core
Expand All @@ -62,11 +65,13 @@ describe('setup-node', () => {
exSpy = jest.spyOn(tc, 'extractTar');
cacheSpy = jest.spyOn(tc, 'cacheDir');
getManifestSpy = jest.spyOn(tc, 'getManifestFromRepo');
getDistSpy = jest.spyOn(im, 'getVersionsFromDist');
getDistSpy = jest.spyOn(nv, 'getVersionsFromDist');
parseNodeVersionSpy = jest.spyOn(nvf, 'parseNodeVersionFile');

// 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
Expand All @@ -77,7 +82,7 @@ describe('setup-node', () => {
getManifestSpy.mockImplementation(
() => <tc.IToolRelease[]>nodeTestManifest
);
getDistSpy.mockImplementation(() => <im.INodeVersion>nodeTestDist);
getDistSpy.mockImplementation(() => <nv.INodeVersion>nodeTestDist);

// writes
cnSpy = jest.spyOn(process.stdout, 'write');
Expand Down Expand Up @@ -122,7 +127,7 @@ describe('setup-node', () => {
});

it('can mock dist versions', async () => {
let versions: im.INodeVersion[] = await im.getVersionsFromDist();
let versions: nv.INodeVersion[] = await nv.getVersionsFromDist();
expect(versions).toBeDefined();
expect(versions?.length).toBe(23);
});
Expand Down Expand Up @@ -490,4 +495,51 @@ 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';
const expectedVersionSpec = '12';

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(__dirname, '..', versionFile),
'utf8'
);
expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec);
expect(logSpy).toHaveBeenCalledWith(
`Resolved ${versionFile} as ${expectedVersionSpec}`
);
});
});
});
88 changes: 88 additions & 0 deletions __tests__/node-version-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import * as nv from '../src/node-version';
import * as nvf from '../src/node-version-file';

let nodeTestDist = require('./data/node-dist-index.json');

describe('node-version-file', () => {
let getVersionsFromDist: jest.SpyInstance;

beforeEach(() => {
// @actions/core
console.log('::stop-commands::stoptoken'); // Disable executing of runner commands when running tests in actions

getVersionsFromDist = jest.spyOn(nv, 'getVersionsFromDist');

// gets
getVersionsFromDist.mockImplementation(() => <nv.INodeVersion>nodeTestDist);
});

afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
//jest.restoreAllMocks();
});

afterAll(async () => {
console.log('::stoptoken::'); // Re-enable executing of runner commands when running tests in actions
}, 100000);

//--------------------------------------------------
// Manifest find tests
//--------------------------------------------------
describe('parseNodeVersionFile', () => {
it('without `v` prefix', async () => {
// Arrange
const versionSpec = '12';

// Act
const result = await nvf.parseNodeVersionFile(versionSpec);

// Assert
expect(result).toBe(versionSpec);
});

it('lts/*', async () => {
// Arrange
const versionSpec = 'lts/*';

// Act
const result = await nvf.parseNodeVersionFile(versionSpec);

// Assert
expect(result).toMatch(/^\d+\.\d+\.\d+$/);
});

it('lts/erbium', async () => {
// Arrange
const versionSpec = 'lts/*';

// Act
const result = await nvf.parseNodeVersionFile(versionSpec);

// Assert
expect(result).toMatch(/\d\.\d\.\d/);
});

it('partial syntax like 12', async () => {
// Arrange
const versionSpec = '12';

// Act
const result = await nvf.parseNodeVersionFile(versionSpec);

// Assert
expect(result).toBe(versionSpec);
});

it('partial syntax like 12.16', async () => {
// Arrange
const versionSpec = '12.16';

// Act
const result = await nvf.parseNodeVersionFile(versionSpec);

// Assert
expect(result).toBe(versionSpec);
});
});
});
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading