mirror of
https://github.com/actions/setup-node.git
synced 2026-07-12 03:25:40 +00:00
Compare commits
No commits in common. "30c33f040941864b3c6218f9d78edc967807ee90" and "d61dc50c40f4e472b93b5a1ff78eca43731ec88d" have entirely different histories.
30c33f0409
...
d61dc50c40
3
.github/workflows/versions.yml
vendored
3
.github/workflows/versions.yml
vendored
@ -82,7 +82,8 @@ jobs:
|
|||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
|
os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
|
||||||
node-version: [20-nightly, 21-nightly, 18.0.0-nightly]
|
node-version:
|
||||||
|
[20.11.0-nightly202312211a0be537da, 21-nightly, 18.0.0-nightly]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Setup Node
|
- name: Setup Node
|
||||||
|
|||||||
@ -14,6 +14,12 @@ import * as main from '../src/main';
|
|||||||
import * as util from '../src/util';
|
import * as util from '../src/util';
|
||||||
import OfficialBuilds from '../src/distributions/official_builds/official_builds';
|
import OfficialBuilds from '../src/distributions/official_builds/official_builds';
|
||||||
|
|
||||||
|
import * as installerFactory from '../src/distributions/installer-factory';
|
||||||
|
jest.mock('../src/distributions/installer-factory', () => ({
|
||||||
|
getNodejsDistribution: jest.fn()
|
||||||
|
}));
|
||||||
|
import {validateMirrorURL} from '../src/util';
|
||||||
|
|
||||||
describe('main tests', () => {
|
describe('main tests', () => {
|
||||||
let inputs = {} as any;
|
let inputs = {} as any;
|
||||||
let os = {} as any;
|
let os = {} as any;
|
||||||
@ -38,6 +44,8 @@ describe('main tests', () => {
|
|||||||
|
|
||||||
let setupNodeJsSpy: jest.SpyInstance;
|
let setupNodeJsSpy: jest.SpyInstance;
|
||||||
|
|
||||||
|
let validateMirrorUrlSpy: jest.SpyInstance;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
inputs = {};
|
inputs = {};
|
||||||
|
|
||||||
@ -165,6 +173,45 @@ describe('main tests', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getNodeVersionFromFile', () => {
|
||||||
|
each`
|
||||||
|
contents | expected
|
||||||
|
${'12'} | ${'12'}
|
||||||
|
${'12.3'} | ${'12.3'}
|
||||||
|
${'12.3.4'} | ${'12.3.4'}
|
||||||
|
${'v12.3.4'} | ${'12.3.4'}
|
||||||
|
${'lts/erbium'} | ${'lts/erbium'}
|
||||||
|
${'lts/*'} | ${'lts/*'}
|
||||||
|
${'nodejs 12.3.4'} | ${'12.3.4'}
|
||||||
|
${'ruby 2.3.4\nnodejs 12.3.4\npython 3.4.5'} | ${'12.3.4'}
|
||||||
|
${''} | ${''}
|
||||||
|
${'unknown format'} | ${'unknown format'}
|
||||||
|
${' 14.1.0 '} | ${'14.1.0'}
|
||||||
|
${'{"volta": {"node": ">=14.0.0 <=17.0.0"}}'}| ${'>=14.0.0 <=17.0.0'}
|
||||||
|
${'{"volta": {"extends": "./package.json"}}'}| ${'18.0.0'}
|
||||||
|
${'{"engines": {"node": "17.0.0"}}'} | ${'17.0.0'}
|
||||||
|
${'{}'} | ${null}
|
||||||
|
`.it('parses "$contents"', ({contents, expected}) => {
|
||||||
|
const existsSpy = jest.spyOn(fs, 'existsSync');
|
||||||
|
existsSpy.mockImplementation(() => true);
|
||||||
|
|
||||||
|
const readFileSpy = jest.spyOn(fs, 'readFileSync');
|
||||||
|
readFileSpy.mockImplementation(filePath => {
|
||||||
|
if (
|
||||||
|
typeof filePath === 'string' &&
|
||||||
|
path.basename(filePath) === 'package.json'
|
||||||
|
) {
|
||||||
|
// Special case for volta.extends
|
||||||
|
return '{"volta": {"node": "18.0.0"}}';
|
||||||
|
}
|
||||||
|
|
||||||
|
return contents;
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(util.getNodeVersionFromFile('file')).toBe(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('node-version-file flag', () => {
|
describe('node-version-file flag', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
delete inputs['node-version'];
|
delete inputs['node-version'];
|
||||||
@ -280,4 +327,41 @@ describe('main tests', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('mirror-url parameter', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
inputs['mirror-url'] = 'https://custom-mirror-url.com';
|
||||||
|
|
||||||
|
validateMirrorUrlSpy = jest.spyOn(main, 'run');
|
||||||
|
validateMirrorUrlSpy.mockImplementation(() => {});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
validateMirrorUrlSpy.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Read mirror-url if mirror-url is provided', async () => {
|
||||||
|
// Arrange
|
||||||
|
inputs['mirror-url'] = 'https://custom-mirror-url.com';
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await main.run();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(inputs['mirror-url']).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error if mirror-url is empty', async () => {
|
||||||
|
// Arrange
|
||||||
|
inputs['mirror-url'] = ' ';
|
||||||
|
|
||||||
|
// Mock log and setFailed
|
||||||
|
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); // Mock the log function
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
expect(() => validateMirrorURL(inputs['mirror-url'])).toThrow(
|
||||||
|
'Mirror URL is empty. Please provide a valid mirror URL.'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -831,7 +831,52 @@ describe('setup-node', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
describe('mirror-url parameter', () => {
|
describe('mirror-url parameter', () => {
|
||||||
it('default if mirror url is not provided', async () => {
|
it('Download mirror url if mirror-url is provided', async () => {
|
||||||
|
// Set up test inputs and environment
|
||||||
|
os.platform = 'linux';
|
||||||
|
os.arch = 'x64';
|
||||||
|
inputs['check-latest'] = 'true';
|
||||||
|
const mirrorURL = (inputs['mirror-url'] =
|
||||||
|
'https://custom-mirror-url.com');
|
||||||
|
inputs['token'] = 'faketoken';
|
||||||
|
|
||||||
|
// Mock that the version is not in cache (simulate a fresh download)
|
||||||
|
findSpy.mockImplementation(() => '');
|
||||||
|
|
||||||
|
// Mock implementations for other dependencies
|
||||||
|
const toolPath = path.normalize('/cache/node/11.11.0/x64');
|
||||||
|
exSpy.mockImplementation(async () => '/some/other/temp/path');
|
||||||
|
cacheSpy.mockImplementation(async () => toolPath);
|
||||||
|
|
||||||
|
const dlmirrorSpy = jest.fn(); // Create a spy to track the download logic
|
||||||
|
|
||||||
|
const mockDownloadNodejs = jest
|
||||||
|
.spyOn(OfficialBuilds.prototype as any, 'downloadFromMirrorURL')
|
||||||
|
.mockImplementation(async () => {
|
||||||
|
dlmirrorSpy();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Run the main method or your logic that invokes `downloadFromMirrorURL`
|
||||||
|
await main.run(); // This should internally call `downloadFromMirrorURL`
|
||||||
|
|
||||||
|
// Prepare the expected path after download
|
||||||
|
const expPath = path.join(toolPath, 'bin');
|
||||||
|
|
||||||
|
// Assert that the spy was called, meaning the download logic was triggered
|
||||||
|
expect(dlmirrorSpy).toHaveBeenCalled(); // This verifies that the download occurred
|
||||||
|
|
||||||
|
// Other assertions to verify the flow
|
||||||
|
expect(exSpy).toHaveBeenCalled();
|
||||||
|
expect(logSpy).toHaveBeenCalledWith(
|
||||||
|
`Attempting to download from ${mirrorURL}...`
|
||||||
|
);
|
||||||
|
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
|
||||||
|
|
||||||
|
// Clean up mocks after the test
|
||||||
|
mockDownloadNodejs.mockRestore(); // Ensure to restore the original method after the test
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fallback to default if mirror url is not provided', async () => {
|
||||||
os.platform = 'linux';
|
os.platform = 'linux';
|
||||||
os.arch = 'x64';
|
os.arch = 'x64';
|
||||||
|
|
||||||
|
|||||||
3
dist/setup/index.js
vendored
3
dist/setup/index.js
vendored
@ -100870,7 +100870,8 @@ function run() {
|
|||||||
if (!arch) {
|
if (!arch) {
|
||||||
arch = os_1.default.arch();
|
arch = os_1.default.arch();
|
||||||
}
|
}
|
||||||
const mirrorURL = core.getInput('mirror-url');
|
const mirrorurl = core.getInput('mirror-url');
|
||||||
|
const mirrorURL = (0, util_1.validateMirrorURL)(mirrorurl);
|
||||||
if (version) {
|
if (version) {
|
||||||
const token = core.getInput('token');
|
const token = core.getInput('token');
|
||||||
const auth = !token ? undefined : `token ${token}`;
|
const auth = !token ? undefined : `token ${token}`;
|
||||||
|
|||||||
@ -7,7 +7,11 @@ import * as path from 'path';
|
|||||||
import {restoreCache} from './cache-restore';
|
import {restoreCache} from './cache-restore';
|
||||||
import {isCacheFeatureAvailable} from './cache-utils';
|
import {isCacheFeatureAvailable} from './cache-utils';
|
||||||
import {getNodejsDistribution} from './distributions/installer-factory';
|
import {getNodejsDistribution} from './distributions/installer-factory';
|
||||||
import {getNodeVersionFromFile, printEnvDetailsAndSetOutput} from './util';
|
import {
|
||||||
|
getNodeVersionFromFile,
|
||||||
|
printEnvDetailsAndSetOutput,
|
||||||
|
validateMirrorURL
|
||||||
|
} from './util';
|
||||||
import {State} from './constants';
|
import {State} from './constants';
|
||||||
|
|
||||||
export async function run() {
|
export async function run() {
|
||||||
@ -33,7 +37,8 @@ export async function run() {
|
|||||||
arch = os.arch();
|
arch = os.arch();
|
||||||
}
|
}
|
||||||
|
|
||||||
const mirrorURL = core.getInput('mirror-url');
|
const mirrorurl = core.getInput('mirror-url');
|
||||||
|
const mirrorURL = validateMirrorURL(mirrorurl);
|
||||||
|
|
||||||
if (version) {
|
if (version) {
|
||||||
const token = core.getInput('token');
|
const token = core.getInput('token');
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user