Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mirror-url parameter to allow downloading Node.js from a custom URL #1232

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
checks update
  • Loading branch information
aparnajyothi-y committed Feb 27, 2025
commit dd2fa9d9f80ebb742a0e97f1618b663cbf181e30
3 changes: 1 addition & 2 deletions .github/workflows/versions.yml
Original file line number Diff line number Diff line change
@@ -82,8 +82,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version:
[20.11.0-nightly202312211a0be537da, 21-nightly, 18.0.0-nightly]
node-version: [20.12.0-nightly, 21-nightly, 18.0.0-nightly]
steps:
- uses: actions/checkout@v4
- name: Setup Node
84 changes: 0 additions & 84 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -14,12 +14,6 @@ import * as main from '../src/main';
import * as util from '../src/util';
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', () => {
let inputs = {} as any;
let os = {} as any;
@@ -44,8 +38,6 @@ describe('main tests', () => {

let setupNodeJsSpy: jest.SpyInstance;

let validateMirrorUrlSpy: jest.SpyInstance;

beforeEach(() => {
inputs = {};

@@ -173,45 +165,6 @@ 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', () => {
beforeEach(() => {
delete inputs['node-version'];
@@ -327,41 +280,4 @@ 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.'
);
});
});
});
47 changes: 1 addition & 46 deletions __tests__/official-installer.test.ts
Original file line number Diff line number Diff line change
@@ -831,52 +831,7 @@ describe('setup-node', () => {
);
});
describe('mirror-url parameter', () => {
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 () => {
it('default if mirror url is not provided', async () => {
os.platform = 'linux';
os.arch = 'x64';

3 changes: 1 addition & 2 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
@@ -100870,8 +100870,7 @@ function run() {
if (!arch) {
arch = os_1.default.arch();
}
const mirrorurl = core.getInput('mirror-url');
const mirrorURL = (0, util_1.validateMirrorURL)(mirrorurl);
const mirrorURL = core.getInput('mirror-url');
if (version) {
const token = core.getInput('token');
const auth = !token ? undefined : `token ${token}`;
9 changes: 2 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -7,11 +7,7 @@ import * as path from 'path';
import {restoreCache} from './cache-restore';
import {isCacheFeatureAvailable} from './cache-utils';
import {getNodejsDistribution} from './distributions/installer-factory';
import {
getNodeVersionFromFile,
printEnvDetailsAndSetOutput,
validateMirrorURL
} from './util';
import {getNodeVersionFromFile, printEnvDetailsAndSetOutput} from './util';
import {State} from './constants';

export async function run() {
@@ -37,8 +33,7 @@ export async function run() {
arch = os.arch();
}

const mirrorurl = core.getInput('mirror-url');
const mirrorURL = validateMirrorURL(mirrorurl);
const mirrorURL = core.getInput('mirror-url');

if (version) {
const token = core.getInput('token');
Loading
Oops, something went wrong.