|
1 | 1 | import * as core from '@actions/core';
|
2 | 2 | import * as io from '@actions/io';
|
3 | 3 | import * as tc from '@actions/tool-cache';
|
| 4 | +import * as exec from '@actions/exec'; |
4 | 5 | import * as im from '../src/installer';
|
5 | 6 | import * as cache from '@actions/cache';
|
6 | 7 | import fs from 'fs';
|
7 | 8 | import cp from 'child_process';
|
8 | 9 | import osm = require('os');
|
9 | 10 | import path from 'path';
|
| 11 | +import each from 'jest-each'; |
10 | 12 | import * as main from '../src/main';
|
11 | 13 | import * as auth from '../src/authutil';
|
12 | 14 |
|
@@ -38,6 +40,7 @@ describe('setup-node', () => {
|
38 | 40 | let authSpy: jest.SpyInstance;
|
39 | 41 | let parseNodeVersionSpy: jest.SpyInstance;
|
40 | 42 | let isCacheActionAvailable: jest.SpyInstance;
|
| 43 | + let getExecOutputSpy: jest.SpyInstance; |
41 | 44 |
|
42 | 45 | beforeEach(() => {
|
43 | 46 | // @actions/core
|
@@ -103,6 +106,10 @@ describe('setup-node', () => {
|
103 | 106 | // uncomment to debug
|
104 | 107 | // process.stderr.write('log:' + line + '\n');
|
105 | 108 | });
|
| 109 | + |
| 110 | + // @actions/exec |
| 111 | + getExecOutputSpy = jest.spyOn(exec, 'getExecOutput'); |
| 112 | + getExecOutputSpy.mockImplementation(() => 'v16.15.0'); |
106 | 113 | });
|
107 | 114 |
|
108 | 115 | afterEach(() => {
|
@@ -613,6 +620,33 @@ describe('setup-node', () => {
|
613 | 620 | );
|
614 | 621 | });
|
615 | 622 |
|
| 623 | + it('reads package.json as node-version-file if provided', async () => { |
| 624 | + // Arrange |
| 625 | + const versionSpec = fs.readFileSync( |
| 626 | + path.join(__dirname, 'data/package.json'), |
| 627 | + 'utf-8' |
| 628 | + ); |
| 629 | + const versionFile = 'package.json'; |
| 630 | + const expectedVersionSpec = '14'; |
| 631 | + process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); |
| 632 | + inputs['node-version-file'] = versionFile; |
| 633 | + |
| 634 | + parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec); |
| 635 | + existsSpy.mockImplementationOnce( |
| 636 | + input => input === path.join(__dirname, 'data', versionFile) |
| 637 | + ); |
| 638 | + // Act |
| 639 | + await main.run(); |
| 640 | + |
| 641 | + // Assert |
| 642 | + expect(existsSpy).toHaveBeenCalledTimes(1); |
| 643 | + expect(existsSpy).toHaveReturnedWith(true); |
| 644 | + expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec); |
| 645 | + expect(logSpy).toHaveBeenCalledWith( |
| 646 | + `Resolved ${versionFile} as ${expectedVersionSpec}` |
| 647 | + ); |
| 648 | + }); |
| 649 | + |
616 | 650 | it('both node-version-file and node-version are provided', async () => {
|
617 | 651 | inputs['node-version'] = '12';
|
618 | 652 | const versionSpec = 'v14';
|
@@ -927,3 +961,23 @@ describe('setup-node', () => {
|
927 | 961 | );
|
928 | 962 | });
|
929 | 963 | });
|
| 964 | + |
| 965 | +describe('helper methods', () => { |
| 966 | + describe('parseNodeVersionFile', () => { |
| 967 | + each` |
| 968 | + contents | expected |
| 969 | + ${'12'} | ${'12'} |
| 970 | + ${'12.3'} | ${'12.3'} |
| 971 | + ${'12.3.4'} | ${'12.3.4'} |
| 972 | + ${'v12.3.4'} | ${'12.3.4'} |
| 973 | + ${'lts/erbium'} | ${'lts/erbium'} |
| 974 | + ${'lts/*'} | ${'lts/*'} |
| 975 | + ${'nodejs 12.3.4'} | ${'12.3.4'} |
| 976 | + ${'ruby 2.3.4\nnodejs 12.3.4\npython 3.4.5'} | ${'12.3.4'} |
| 977 | + ${''} | ${''} |
| 978 | + ${'unknown format'} | ${'unknown format'} |
| 979 | + `.it('parses "$contents"', ({contents, expected}) => { |
| 980 | + expect(im.parseNodeVersionFile(contents)).toBe(expected); |
| 981 | + }); |
| 982 | + }); |
| 983 | +}); |
0 commit comments