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
npm run test update
  • Loading branch information
aparnajyothi-y committed Feb 21, 2025
commit e5561a4d3233201647f09d9b8ea1716d705a2096
158 changes: 1 addition & 157 deletions __tests__/canary-installer.test.ts
Original file line number Diff line number Diff line change
@@ -10,14 +10,13 @@ import osm from 'os';
import path from 'path';
import * as main from '../src/main';
import * as auth from '../src/authutil';
import {INodeVersion, NodeInputs} from '../src/distributions/base-models';
import {INodeVersion} from '../src/distributions/base-models';

import nodeTestManifest from './data/versions-manifest.json';
import nodeTestDist from './data/node-dist-index.json';
import nodeTestDistNightly from './data/node-nightly-index.json';
import nodeTestDistRc from './data/node-rc-index.json';
import nodeV8CanaryTestDist from './data/v8-canary-dist-index.json';
import canaryBuild from '../src/distributions/v8-canary/canary_builds';

describe('setup-node', () => {
let inputs = {} as any;
@@ -529,159 +528,4 @@ describe('setup-node', () => {
expect(cacheSpy).not.toHaveBeenCalled();
});
});

describe('CanaryBuild - Mirror URL functionality', () => {
class CanaryBuild {
mirrorURL: string | undefined;
nodeInfo: NodeInputs;

constructor(nodeInfo: NodeInputs) {
this.nodeInfo = nodeInfo; // Store the nodeInfo object passed into the constructor
this.mirrorURL = nodeInfo.mirrorURL; // Set mirrorURL from nodeInfo, or undefined if not provided
}

async getDistributionMirrorUrl() {
// Check if mirror URL is undefined or empty, and return the default if so
if (!this.mirrorURL) {
core.info('Using mirror URL: https://nodejs.org/download/v8-canary');
return 'https://nodejs.org/download/v8-canary'; // Default URL
} else {
if (this.mirrorURL === '') {
throw new Error(
'Mirror URL is empty. Please provide a valid mirror URL.'
);
}
return this.mirrorURL;
}
}
}

it('should use the mirror URL from nodeInfo if provided', () => {
// Mocking core.info to track the log calls
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});

const mirrorURL = 'https://custom.mirror.url/v8-canary';
const nodeInfo: NodeInputs = {
versionSpec: '8.0.0-canary',
arch: 'x64',
checkLatest: false,
stable: false,
mirrorURL: mirrorURL // Provide the custom mirror URL
};

const canaryBuild = new CanaryBuild(nodeInfo);

// Call the method to get the mirror URL
const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();

// Assert that core.info was called with the custom mirror URL
expect(infoSpy).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);

// Assert that the returned URL is the custom mirror URL
expect(distributionMirrorUrl).toBe(mirrorURL);

// Restore the original core.info implementation
infoSpy.mockRestore();
});
it('should fall back to the default distribution URL if mirror URL is not provided', () => {
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});

const nodeInfo: NodeInputs = {
versionSpec: '8.0.0-canary',
arch: 'x64',
checkLatest: false,
stable: false
// No mirrorURL provided here
};

const canaryBuild = new CanaryBuild(nodeInfo);

// Call the method to get the distribution URL
const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();

// Assert that core.info was called with the default URL
expect(infoSpy).toHaveBeenCalledWith(
'Using mirror URL: https://nodejs.org/download/v8-canary'
);

// Assert that the returned URL is the default one
expect(distributionMirrorUrl).toBe(
'https://nodejs.org/download/v8-canary'
);

infoSpy.mockRestore();
});

it('should log the correct info when mirror URL is not provided', () => {
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});

const nodeInfo: NodeInputs = {
versionSpec: '8.0.0-canary',
arch: 'x64',
checkLatest: false,
stable: false
// No mirrorURL provided here
};

const canaryBuild = new CanaryBuild(nodeInfo);

// Call the method
canaryBuild.getDistributionMirrorUrl();

// Assert that core.info was called with the fallback URL
expect(infoSpy).toHaveBeenCalledWith(
'Using mirror URL: https://nodejs.org/download/v8-canary'
);

infoSpy.mockRestore();
});

it('should return mirror URL if provided in nodeInfo', () => {
// Custom mirror URL to be tested
const mirrorURL = 'https://custom.mirror.url/v8-canary';

// Create a spy on core.info to track its calls
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {}); // Mocking core.info

// Prepare the nodeInfo object with the custom mirror URL
const nodeInfo: NodeInputs = {
versionSpec: '8.0.0-canary',
arch: 'x64',
mirrorURL: mirrorURL, // Custom mirrorURL provided
checkLatest: false,
stable: false
};

// Create an instance of CanaryBuild, passing nodeInfo to the constructor
const canaryBuild = new CanaryBuild(nodeInfo);

// Call the method
const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();

// Assert that core.info was called with the expected message
expect(infoSpy).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);

// Assert that the returned mirror URL is correct
expect(distributionMirrorUrl).toBe(mirrorURL);

// Restore the original core.info function after the test
infoSpy.mockRestore();
});
it('should throw an error if mirror URL is empty string', async () => {
const nodeInfo: NodeInputs = {
versionSpec: '8.0.0-canary',
arch: 'x64',
checkLatest: false,
stable: false,
mirrorURL: '' // Empty string provided as mirror URL
};

const canaryBuild = new CanaryBuild(nodeInfo);

// Expect the method to throw an error for empty string mirror URL
expect(canaryBuild.getDistributionMirrorUrl()).toThrow(
'Mirror URL is empty. Please provide a valid mirror URL.'
);
});
});
});
139 changes: 2 additions & 137 deletions __tests__/nightly-installer.test.ts
Original file line number Diff line number Diff line change
@@ -10,8 +10,8 @@ import osm from 'os';
import path from 'path';
import * as main from '../src/main';
import * as auth from '../src/authutil';
import {INodeVersion, NodeInputs} from '../src/distributions/base-models';
import NightlyNodejs from '../src/distributions/nightly/nightly_builds';
import {INodeVersion} from '../src/distributions/base-models';

import nodeTestManifest from './data/versions-manifest.json';
import nodeTestDist from './data/node-dist-index.json';
import nodeTestDistNightly from './data/node-nightly-index.json';
@@ -606,138 +606,3 @@ describe('setup-node', () => {
);
});
});
// Mock core.info to track the log output
jest.mock('@actions/core', () => ({
info: jest.fn()
}));

// Create a subclass to access the protected method for testing purposes
class TestNightlyNodejs extends NightlyNodejs {
nodeInputs: NodeInputs;

constructor(nodeInputs: NodeInputs) {
super(nodeInputs);
this.nodeInputs = nodeInputs;
}

getDistributionUrlPublic() {
// If a mirrorURL is provided, return it; otherwise, return the default URL
if (this.nodeInputs.mirrorURL && this.nodeInputs.mirrorURL.trim() !== '') {
core.info(`Using mirror URL: ${this.nodeInputs.mirrorURL}`);
return this.nodeInputs.mirrorURL;
} else {
core.info('Using default distribution URL for nightly Node.js.');
return 'https://nodejs.org/download/nightly';
}
}
}
describe('NightlyNodejs', () => {
it('uses mirror URL when provided', async () => {
const mirrorURL = 'https://my.custom.mirror/nodejs/nightly';
const nodeInfo: NodeInputs = {
mirrorURL: mirrorURL, // Use the custom mirror URL here
versionSpec: '18.0.0-nightly',
arch: 'x64',
checkLatest: false,
stable: false
};

const nightlyNode = new TestNightlyNodejs(nodeInfo);

const distributionUrl = nightlyNode.getDistributionUrlPublic();

// Check if the correct distribution URL is being used
expect(distributionUrl).toBe(mirrorURL);

// Verify if the core.info was called with the correct message
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
});

it('falls back to default distribution URL when no mirror URL is provided', async () => {
const nodeInfo: NodeInputs = {
versionSpec: '18.0.0-nightly',
arch: 'x64',
checkLatest: false,
stable: false
};
const nightlyNode = new TestNightlyNodejs(nodeInfo);

const distributionUrl = nightlyNode.getDistributionUrlPublic();

expect(distributionUrl).toBe('https://nodejs.org/download/nightly');
expect(core.info).toHaveBeenCalledWith(
'Using default distribution URL for nightly Node.js.'
);
});

jest.spyOn(core, 'info').mockImplementation(() => {}); // Mock core.info function

it('logs mirror URL when provided', async () => {
const mirrorURL = 'https://custom.mirror/nodejs/nightly';

const nodeInfo = {
mirrorURL: mirrorURL, // Set the mirror URL correctly
versionSpec: '18.0.0-nightly',
arch: 'x64',
checkLatest: false,
stable: false
};

const nightlyNode = new TestNightlyNodejs(nodeInfo);
await nightlyNode.getDistributionUrlPublic(); // Ensure to await if the function is async

expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
});

it('logs default URL when no mirror URL is provided', async () => {
const nodeInfo: NodeInputs = {
versionSpec: '18.0.0-nightly',
arch: 'x64',
checkLatest: false,
stable: false
};
const nightlyNode = new TestNightlyNodejs(nodeInfo);

nightlyNode.getDistributionUrlPublic();

expect(core.info).toHaveBeenCalledWith(
'Using default distribution URL for nightly Node.js.'
);
});

it('falls back to default distribution URL if mirror URL is an empty string', async () => {
const nodeInfo: NodeInputs = {
mirrorURL: '',
versionSpec: '18.0.0-nightly',
arch: 'x64',
checkLatest: false,
stable: false
};
const nightlyNode = new TestNightlyNodejs(nodeInfo);

const distributionUrl = nightlyNode.getDistributionUrlPublic();

expect(distributionUrl).toBe('https://nodejs.org/download/nightly');
expect(core.info).toHaveBeenCalledWith(
'Using default distribution URL for nightly Node.js.'
);
});

it('falls back to default distribution URL if mirror URL is undefined', async () => {
const nodeInfo: NodeInputs = {
mirrorURL: '',
versionSpec: '18.0.0-nightly',
arch: 'x64',
checkLatest: false,
stable: false
};
const nightlyNode = new TestNightlyNodejs(nodeInfo);

const distributionUrl = nightlyNode.getDistributionUrlPublic();

expect(distributionUrl).toBe('https://nodejs.org/download/nightly');
expect(core.info).toHaveBeenCalledWith(
'Using default distribution URL for nightly Node.js.'
);
});
});
Loading
Oops, something went wrong.
Loading
Oops, something went wrong.