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 #1211

Closed
wants to merge 26 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
test-cases
  • Loading branch information
aparnajyothi-y committed Feb 19, 2025
commit 9312b4364bd38aa2e881bf0e0c8b34dd0511732a
20 changes: 8 additions & 12 deletions __tests__/canary-installer.test.ts
Original file line number Diff line number Diff line change
@@ -548,12 +548,14 @@ describe('setup-node', () => {
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.');
}

// Log and return the custom mirror URL
core.info(`Using mirror URL: ${this.mirrorURL}`);
return this.mirrorURL;
}

}
}


@@ -662,9 +664,7 @@ describe('setup-node', () => {
// Restore the original core.info function after the test
infoSpy.mockRestore();
});
it('should throw an error if mirror URL is empty string', () => {
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});

it('should throw an error if mirror URL is empty string', async () => {
const nodeInfo: NodeInputs = {
versionSpec: '8.0.0-canary',
arch: 'x64',
@@ -676,13 +676,9 @@ describe('setup-node', () => {
const canaryBuild = new CanaryBuild(nodeInfo);

// Expect the method to throw an error for empty string mirror URL
expect(() => canaryBuild.getDistributionMirrorUrl()).toThrowError('Mirror URL is empty. Please provide a valid mirror URL.');

// Ensure that core.info was not called because the error was thrown first
expect(infoSpy).not.toHaveBeenCalled();

infoSpy.mockRestore();
expect(canaryBuild.getDistributionMirrorUrl()).toThrow('Mirror URL is empty. Please provide a valid mirror URL.');
});




132 changes: 86 additions & 46 deletions __tests__/rc-installer.test.ts
Original file line number Diff line number Diff line change
@@ -463,75 +463,115 @@ describe('setup-node', () => {
checkLatest: false,
stable: false,
};

class RcBuild {
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
}

getDistributionMirrorUrl() {
// If mirrorURL is provided in nodeInfo, return it
if (this.nodeInfo.mirrorURL != '') {
core.info(`Using mirror URL: ${this.nodeInfo.mirrorURL}`);
return this.nodeInfo.mirrorURL;
}else{
if(this.nodeInfo.mirrorURL === '') {
throw new Error('Mirror URL is empty. Please provide a valid mirror URL.');
}else{
if (this.nodeInfo.mirrorURL === undefined) {
throw new Error('Mirror URL is undefined. Please provide a valid mirror URL.');
}
}
}


}
}

it('should return the default distribution URL if no mirror URL is provided', () => {
const rcBuild = new RcBuild(nodeInfo);

const distributionUrl = rcBuild.getDistributionUrl();

// Default URL
expect(distributionUrl).toBe('https://nodejs.org/download/rc');
});

it('should use the mirror URL from nodeInfo if provided', () => {
const mirrorURL = 'https://my.custom.mirror/nodejs'; // Set the custom mirror URL
nodeInfo.mirrorURL = mirrorURL; // Set the mirrorURL in nodeInfo
it('should return the default distribution URL if no mirror URL is provided', () => {
// Assuming nodeInfo does not have a mirrorURL
const nodeInfo = {
versionSpec: '16.0.0-rc',
arch: 'x64',
checkLatest: false,
stable: false,
mirrorURL: '', // No mirror URL provided
};

const rcBuild = new RcBuild(nodeInfo);

const rcBuild = new RcBuild(nodeInfo);

// Mock core.info to track its calls
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});

// Call the method
const distributionMirrorUrl = rcBuild['getDistributionMirrorUrl'](); // Access the protected method

// Assert that core.info was called with the correct mirror URL message
expect(infoSpy).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
const distributionUrl = rcBuild.getDistributionMirrorUrl();

// Default URL
expect(distributionUrl).toBe('https://nodejs.org/download/rc');
});

it('should use the mirror URL from nodeInfo if provided', () => {
const mirrorURL = 'https://my.custom.mirror/nodejs'; // Set the custom mirror URL
nodeInfo.mirrorURL = mirrorURL; // Set the mirrorURL in nodeInfo

const rcBuild = new RcBuild(nodeInfo);

// Assert that the returned URL is the mirror URL
expect(distributionMirrorUrl).toBe(mirrorURL);
// Mock core.info to track its calls
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});

// Restore the original core.info function after the test
infoSpy.mockRestore();
// Call the method
const distributionMirrorUrl = rcBuild.getDistributionMirrorUrl(); // Access the method

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

// Assert that the returned URL is the mirror URL
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', () => {
nodeInfo.mirrorURL = ''; // Empty mirror URL

const rcBuild = new RcBuild(nodeInfo);

// Mock core.info to track its calls
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});

// Expect the function to return the default URL because the mirror URL is empty
const distributionMirrorUrl = rcBuild['getDistributionMirrorUrl']();

const distributionMirrorUrl = rcBuild.getDistributionMirrorUrl();

// Assert the returned URL is the default URL
expect(distributionMirrorUrl).toBe('https://nodejs.org/download/rc');

// Ensure that core.info was NOT called because it's not a custom mirror URL
expect(infoSpy).not.toHaveBeenCalled();


// Restore the original core.info function after the test
infoSpy.mockRestore();
});
});

it('should throw an error if mirror URL is undefined', () => {
nodeInfo.mirrorURL = undefined; // Undefined mirror URL

const rcBuild = new RcBuild(nodeInfo);

// Mock core.info to track its calls
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});
it('should throw an error if mirror URL is undefined', () => {
nodeInfo.mirrorURL = undefined; // Undefined mirror URL

const rcBuild = new RcBuild(nodeInfo);

// Expect the function to return the default URL because the mirror URL is undefined
const distributionMirrorUrl = rcBuild['getDistributionMirrorUrl']();
// Mock core.info to track its calls
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});

expect(distributionMirrorUrl).toBe('https://nodejs.org/download/rc');
// Expect the function to throw an error due to undefined mirror URL
expect(() => rcBuild.getDistributionMirrorUrl()).toThrowError('Mirror URL is undefined. Please provide a valid mirror URL.');

// Ensure that core.info was NOT called because it's not a custom mirror URL
expect(infoSpy).not.toHaveBeenCalled();
// Ensure that core.info was NOT called because it's not a valid URL
expect(infoSpy).not.toHaveBeenCalled();

infoSpy.mockRestore();
});
infoSpy.mockRestore();
});

});


Loading
Loading