Compare commits

..

1 Commits

Author SHA1 Message Date
aparnajyothi-y
eefc13894f
Merge 07434d7eed into 802632921f 2025-01-31 14:52:25 +00:00
8 changed files with 284 additions and 428 deletions

View File

@ -19,7 +19,6 @@ import nodeTestDistRc from './data/node-rc-index.json';
import nodeV8CanaryTestDist from './data/v8-canary-dist-index.json'; import nodeV8CanaryTestDist from './data/v8-canary-dist-index.json';
import canaryBuild from '../src/distributions/v8-canary/canary_builds'; import canaryBuild from '../src/distributions/v8-canary/canary_builds';
describe('setup-node', () => { describe('setup-node', () => {
let inputs = {} as any; let inputs = {} as any;
let os = {} as any; let os = {} as any;
@ -530,161 +529,85 @@ describe('setup-node', () => {
expect(cacheSpy).not.toHaveBeenCalled(); expect(cacheSpy).not.toHaveBeenCalled();
}); });
}); });
});
describe('CanaryBuild - Mirror URL functionality', () => { describe('CanaryBuild - Mirror URL functionality', () => {
const nodeInfo: NodeInputs = {
versionSpec: '18.0.0-rc', arch: 'x64', mirrorURL: '',
class CanaryBuild { checkLatest: false,
mirrorURL: string | undefined; stable: false
nodeInfo: NodeInputs; };
it('should return the default distribution URL if no mirror URL is provided', () => {
constructor(nodeInfo: NodeInputs) { const canaryBuild = new CanaryBuild(nodeInfo);
this.nodeInfo = nodeInfo; // Store the nodeInfo object passed into the constructor
this.mirrorURL = nodeInfo.mirrorURL; // Set mirrorURL from nodeInfo, or undefined if not provided // @ts-ignore: Accessing protected method for testing purposes
} const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();
expect(distributionMirrorUrl).toBe('https://nodejs.org/download/v8-canary');
async getDistributionMirrorUrl() { });
// Check if mirror URL is undefined or empty, and return the default if so
if (!this.mirrorURL) { it('should use the mirror URL from nodeInfo if provided', () => {
core.info('Using mirror URL: https://nodejs.org/download/v8-canary'); const mirrorURL = 'https://custom.mirror.url/v8-canary';
return 'https://nodejs.org/download/v8-canary'; // Default URL nodeInfo.mirrorURL = mirrorURL;
} const canaryBuild = new CanaryBuild(nodeInfo);
// Log and return the custom mirror URL // @ts-ignore: Accessing protected method for testing purposes
core.info(`Using mirror URL: ${this.mirrorURL}`); const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();
return this.mirrorURL; expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
} expect(distributionMirrorUrl).toBe(mirrorURL);
} });
it('should fall back to the default distribution URL if mirror URL is not provided', () => {
it('should use the mirror URL from nodeInfo if provided', () => { nodeInfo.mirrorURL = ''; // No mirror URL
// Mocking core.info to track the log calls const canaryBuild = new CanaryBuild(nodeInfo);
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});
// @ts-ignore: Accessing protected method for testing purposes
const mirrorURL = 'https://custom.mirror.url/v8-canary'; const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();
const nodeInfo: NodeInputs = { expect(core.info).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/v8-canary');
versionSpec: '8.0.0-canary', expect(distributionMirrorUrl).toBe('https://nodejs.org/download/v8-canary');
arch: 'x64', });
checkLatest: false,
stable: false, it('should log the correct info when mirror URL is not provided', () => {
mirrorURL: mirrorURL // Provide the custom mirror URL nodeInfo.mirrorURL = ''; // No mirror URL
}; const canaryBuild = new CanaryBuild(nodeInfo);
const canaryBuild = new CanaryBuild(nodeInfo); // @ts-ignore: Accessing protected method for testing purposes
canaryBuild.getDistributionMirrorUrl();
// Call the method to get the mirror URL expect(core.info).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/v8-canary');
const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl(); });
// Assert that core.info was called with the custom mirror URL it('should return mirror URL if provided in nodeInfo', () => {
expect(infoSpy).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`); const mirrorURL = 'https://custom.mirror.url/v8-canary';
nodeInfo.mirrorURL = mirrorURL;
// Assert that the returned URL is the custom mirror URL
expect(distributionMirrorUrl).toBe(mirrorURL); const canaryBuild = new CanaryBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
// Restore the original core.info implementation const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();
infoSpy.mockRestore(); expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
}); expect(distributionMirrorUrl).toBe(mirrorURL);
it('should fall back to the default distribution URL if mirror URL is not provided', () => { });
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});
it('should use the default URL if mirror URL is empty string', () => {
const nodeInfo: NodeInputs = { nodeInfo.mirrorURL = ''; // Empty string for mirror URL
versionSpec: '8.0.0-canary', const canaryBuild = new CanaryBuild(nodeInfo);
arch: 'x64',
checkLatest: false, // @ts-ignore: Accessing protected method for testing purposes
stable: false const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();
// No mirrorURL provided here expect(core.info).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/v8-canary');
}; expect(distributionMirrorUrl).toBe('https://nodejs.org/download/v8-canary');
});
const canaryBuild = new CanaryBuild(nodeInfo);
it('should return the default URL if mirror URL is undefined', async () => {
// Call the method to get the distribution URL // Create a spy on core.info
const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl(); const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {}); // Mocking with empty implementation
// Assert that core.info was called with the default URL // @ts-ignore: Accessing protected method for testing purposes
expect(infoSpy).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/v8-canary'); const distributionMirrorUrl = await canaryBuild.getDistributionMirrorUrl(); // Use await here
// Assert that the returned URL is the default one // Assert that core.info was called with the expected message
expect(distributionMirrorUrl).toBe('https://nodejs.org/download/v8-canary'); expect(infoSpy).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/v8-canary');
expect(distributionMirrorUrl).toBe('https://nodejs.org/download/v8-canary');
infoSpy.mockRestore();
}); // Optional: Restore the original function after the test
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', () => {
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {});
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()).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();
});
});
}); });

View File

@ -286,9 +286,9 @@ describe('main tests', () => {
); );
}); });
}); });
});
// Create a mock object that satisfies the BaseDistribution interface
// Create a mock object that satisfies the BaseDistribution interface
const createMockNodejsDistribution = () => ({ const createMockNodejsDistribution = () => ({
setupNodeJs: jest.fn(), setupNodeJs: jest.fn(),
httpClient: {}, // Mocking the httpClient (you can replace this with more detailed mocks if needed) httpClient: {}, // Mocking the httpClient (you can replace this with more detailed mocks if needed)
@ -328,13 +328,13 @@ describe('Mirror URL Tests', () => {
auth: undefined, auth: undefined,
stable: true, stable: true,
arch: 'x64', arch: 'x64',
mirrorURL: 'https://custom-mirror-url.com', // Ensure this matches mirrorURL: 'https://custom-mirror-url.com',
}); });
}); });
it('should use default mirror URL when no mirror URL is provided', async () => { it('should use default mirror URL when no mirror URL is provided', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => { jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'mirror-url') return ''; // Simulating no mirror URL provided if (name === 'mirror-url') return '';
if (name === 'node-version') return '14.x'; if (name === 'node-version') return '14.x';
return ''; return '';
}); });
@ -344,7 +344,7 @@ describe('Mirror URL Tests', () => {
await main.run(); await main.run();
// Expect that setupNodeJs is called with an empty mirror URL (default behavior) // Expect that setupNodeJs is called with an empty mirror URL
expect(mockNodejsDistribution.setupNodeJs).toHaveBeenCalledWith(expect.objectContaining({ expect(mockNodejsDistribution.setupNodeJs).toHaveBeenCalledWith(expect.objectContaining({
mirrorURL: '', // Default URL is expected to be handled internally mirrorURL: '', // Default URL is expected to be handled internally
})); }));
@ -360,17 +360,42 @@ describe('Mirror URL Tests', () => {
}; };
// Simulate calling the main function that will trigger setupNodeJs // Simulate calling the main function that will trigger setupNodeJs
await main.run(); await main.run({ mirrorURL });
// Debugging: Log the arguments that setupNodeJs was called with
console.log('setupNodeJs calls:', mockNodejsDistribution.setupNodeJs.mock.calls);
// Assert that setupNodeJs was called with the correct trimmed mirrorURL // Assert that setupNodeJs was called with the correct trimmed mirrorURL
expect(mockNodejsDistribution.setupNodeJs).toHaveBeenCalledWith( expect(mockNodejsDistribution.setupNodeJs).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
mirrorURL: expectedTrimmedURL, // Ensure the URL is trimmed properly mirrorURL: expectedTrimmedURL,
}) })
); );
}); });
});
it('should warn if architecture is provided but node-version is missing', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'architecture') return 'x64';
if (name === 'node-version') return '';
return '';
});
const mockWarning = jest.spyOn(core, 'warning');
const mockNodejsDistribution = createMockNodejsDistribution();
(installerFactory.getNodejsDistribution as jest.Mock).mockReturnValue(mockNodejsDistribution);
await main.run();
expect(mockWarning).toHaveBeenCalledWith(
"`architecture` is provided but `node-version` is missing. In this configuration, the version/architecture of Node will not be changed. To fix this, provide `architecture` in combination with `node-version`"
);
expect(mockNodejsDistribution.setupNodeJs).not.toHaveBeenCalled(); // Setup Node should not be called
});
}); });
function someFunctionThatCallsSetupNodeJs(arg0: { mirrorURL: string; }) {
throw new Error('Function not implemented.');
}

View File

@ -613,48 +613,28 @@ jest.mock('@actions/core', () => ({
// Create a subclass to access the protected method for testing purposes // Create a subclass to access the protected method for testing purposes
class TestNightlyNodejs extends NightlyNodejs { class TestNightlyNodejs extends NightlyNodejs {
nodeInputs: NodeInputs; public getDistributionUrlPublic() {
return this.getDistributionUrl(); // This allows us to call the protected method
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', () => { describe('NightlyNodejs', () => {
it('uses mirror URL when provided', async () => { it('uses mirror URL when provided', async () => {
const mirrorURL = 'https://my.custom.mirror/nodejs/nightly'; const mirrorURL = 'https://my.custom.mirror/nodejs/nightly';
const nodeInfo: NodeInputs = { const nodeInfo: NodeInputs = {
mirrorURL: mirrorURL, // Use the custom mirror URL here mirrorURL: '', versionSpec: '18.0.0-nightly', arch: 'x64',
versionSpec: '18.0.0-nightly',
arch: 'x64',
checkLatest: false, checkLatest: false,
stable: false stable: false
}; };
const nightlyNode = new TestNightlyNodejs(nodeInfo); const nightlyNode = new TestNightlyNodejs(nodeInfo);
const distributionUrl = nightlyNode.getDistributionUrlPublic(); const distributionUrl = nightlyNode.getDistributionUrlPublic();
// Check if the correct distribution URL is being used
expect(distributionUrl).toBe(mirrorURL); expect(distributionUrl).toBe(mirrorURL);
// Verify if the core.info was called with the correct message
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`); expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
}); });
it('falls back to default distribution URL when no mirror URL is provided', async () => { it('falls back to default distribution URL when no mirror URL is provided', async () => {
const nodeInfo: NodeInputs = { const nodeInfo: NodeInputs = {
versionSpec: '18.0.0-nightly', arch: 'x64', versionSpec: '18.0.0-nightly', arch: 'x64',
@ -668,26 +648,19 @@ describe('NightlyNodejs', () => {
expect(core.info).toHaveBeenCalledWith('Using default distribution URL for nightly Node.js.'); expect(core.info).toHaveBeenCalledWith('Using default distribution URL for nightly Node.js.');
}); });
const core = require('@actions/core'); // Mock core it('logs mirror URL when provided', async () => {
jest.spyOn(core, 'info').mockImplementation(() => {}); // Mock core.info function const mirrorURL = 'https://custom.mirror/nodejs/nightly';
const nodeInfo: NodeInputs = {
mirrorURL: '', versionSpec: '18.0.0-nightly', arch: 'x64',
checkLatest: false,
stable: false
};
const nightlyNode = new TestNightlyNodejs(nodeInfo);
it('logs mirror URL when provided', async () => { nightlyNode.getDistributionUrlPublic();
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}`);
});
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
});
it('logs default URL when no mirror URL is provided', async () => { it('logs default URL when no mirror URL is provided', async () => {
const nodeInfo: NodeInputs = { const nodeInfo: NodeInputs = {

View File

@ -828,122 +828,108 @@ describe('setup-node', () => {
} }
); );
}); });
});
import { OfficialBuilds } from './path-to-your-official-builds-file'; // Adjust path
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
jest.mock('@actions/core');
jest.mock('@actions/tool-cache');
describe('OfficialBuilds - Mirror URL functionality', () => { describe('OfficialBuilds - Mirror URL functionality', () => {
const nodeInfo: NodeInputs = {
let officialBuilds: OfficialBuilds; mirrorURL: '', versionSpec: '18.0.0-nightly', arch: 'x64',
beforeEach(() => {
const mockNodeInfo = {
versionSpec: '16.x',
mirrorURL: 'https://my.custom.mirror/nodejs',
arch: 'x64',
stable: true,
checkLatest: false, checkLatest: false,
osPlat: 'linux', // Mock OS platform to avoid "undefined" error stable: false
auth: 'someAuthToken',
}; };
officialBuilds = new OfficialBuilds(mockNodeInfo);
});
it('should download using the mirror URL when provided', async () => { it('should download using the mirror URL when provided', async () => {
const mirrorURL = 'https://my.custom.mirror/nodejs';
nodeInfo.mirrorURL = mirrorURL;
const officialBuilds = new OfficialBuilds(nodeInfo);
// Mock download from mirror URL
const mockDownloadPath = '/some/temp/path'; const mockDownloadPath = '/some/temp/path';
const mockDownloadTool = jest.spyOn(tc, 'downloadTool').mockResolvedValue(mockDownloadPath); jest.spyOn(tc, 'downloadTool').mockResolvedValue(mockDownloadPath);
const mockAddPath = jest.spyOn(core, 'addPath').mockImplementation(() => {});
await officialBuilds.setupNodeJs(); await officialBuilds.setupNodeJs();
// Check if the mirror URL was used
expect(core.info).toHaveBeenCalledWith('Attempting to download using mirror URL...'); expect(core.info).toHaveBeenCalledWith('Attempting to download using mirror URL...');
expect(core.info).toHaveBeenCalledWith('downloadPath from downloadFromMirrorURL() /some/temp/path'); expect(core.info).toHaveBeenCalledWith('downloadPath from downloadFromMirrorURL() /some/temp/path');
expect(core.addPath).toHaveBeenCalledWith(mockDownloadPath); expect(core.addPath).toHaveBeenCalledWith(mockDownloadPath);
}); });
it('should log a message when mirror URL is used', async () => { it('should log a message when mirror URL is used', async () => {
const mockInfo = jest.spyOn(core, 'info').mockImplementation(() => {}); const mirrorURL = 'https://my.custom.mirror/nodejs';
nodeInfo.mirrorURL = mirrorURL;
const officialBuilds = new OfficialBuilds(nodeInfo);
const mockDownloadPath = '/some/temp/path';
jest.spyOn(tc, 'downloadTool').mockResolvedValue(mockDownloadPath);
await officialBuilds.setupNodeJs(); await officialBuilds.setupNodeJs();
// Check if the appropriate message is logged for mirror URL expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: https://my.custom.mirror/nodejs`);
}); });
it('should fall back to default URL if mirror URL is not provided', async () => { it('should fall back to default URL if mirror URL is not provided', async () => {
// Mock a scenario where mirror URL is not provided nodeInfo.mirrorURL = ''; // No mirror URL provided
officialBuilds.nodeInfo.mirrorURL = undefined; const officialBuilds = new OfficialBuilds(nodeInfo);
const mockInfo = jest.spyOn(core, 'info').mockImplementation(() => {}); const mockDownloadPath = '/some/temp/path';
jest.spyOn(tc, 'downloadTool').mockResolvedValue(mockDownloadPath);
await officialBuilds.setupNodeJs(); await officialBuilds.setupNodeJs();
// Check if fallback logic was triggered expect(core.info).toHaveBeenCalledWith('Attempting to download from default Node.js URL...');
expect(core.info).toHaveBeenCalledWith('Falling back to download directly from Node'); expect(core.addPath).toHaveBeenCalledWith(mockDownloadPath);
}); });
it('should log an error and handle failure during mirror URL download', async () => { it('should log an error and handle failure during mirror URL download', async () => {
const mirrorURL = 'https://my.custom.mirror/nodejs';
nodeInfo.mirrorURL = mirrorURL;
const officialBuilds = new OfficialBuilds(nodeInfo);
// Simulate an error during the download process
const errorMessage = 'Network error'; const errorMessage = 'Network error';
const mockError = jest.spyOn(core, 'error').mockImplementation(() => {}); jest.spyOn(tc, 'downloadTool').mockRejectedValue(new Error(errorMessage));
const mockDebug = jest.spyOn(core, 'debug').mockImplementation(() => {});
await officialBuilds.setupNodeJs();
const mockDownloadTool = jest.spyOn(tc, 'downloadTool').mockRejectedValue(new Error(errorMessage));
expect(core.info).toHaveBeenCalledWith('Attempting to download using mirror URL...');
try { expect(core.error).toHaveBeenCalledWith(errorMessage);
await officialBuilds.setupNodeJs(); expect(core.debug).toHaveBeenCalledWith(expect.stringContaining('empty stack'));
} catch (error) { });
// Expect core.error to be called with the error message
expect(core.error).toHaveBeenCalledWith(errorMessage); it('should log a fallback message if downloading from the mirror URL fails', async () => {
expect(core.debug).toHaveBeenCalledWith(expect.stringContaining('empty stack')); const mirrorURL = 'https://my.custom.mirror/nodejs';
} nodeInfo.mirrorURL = mirrorURL;
}); const officialBuilds = new OfficialBuilds(nodeInfo);
it('should log a fallback message if downloading from the mirror URL fails', async () => { // Simulate download failure and fallback to default URL
const mockInfo = jest.spyOn(core, 'info').mockImplementation(() => {}); const errorMessage = 'Network error';
const mockDownloadTool = jest.spyOn(tc, 'downloadTool').mockRejectedValue(new Error('Download failed')); jest.spyOn(tc, 'downloadTool').mockRejectedValue(new Error(errorMessage));
const mockDownloadPath = '/some/temp/path';
jest.spyOn(tc, 'downloadTool').mockResolvedValue(mockDownloadPath);
await officialBuilds.setupNodeJs(); await officialBuilds.setupNodeJs();
// Check if fallback log message was triggered
expect(core.info).toHaveBeenCalledWith('Failed to download from mirror URL. Falling back to default Node.js URL...'); expect(core.info).toHaveBeenCalledWith('Failed to download from mirror URL. Falling back to default Node.js URL...');
expect(core.addPath).toHaveBeenCalledWith(mockDownloadPath);
}); });
it('should throw an error if mirror URL is not provided and downloading from both mirror and default fails', async () => { it('should throw an error if mirror URL is not provided and downloading from both mirror and default fails', async () => {
const errorMessage = `Unable to find Node version for platform linux and architecture x64.`; nodeInfo.mirrorURL = ''; // No mirror URL
const officialBuilds = new OfficialBuilds(nodeInfo);
const mockDownloadTool = jest.spyOn(tc, 'downloadTool').mockRejectedValue(new Error('Download failed')); // Simulate failure in both mirror and default download
const mockGetNodeJsVersions = jest.spyOn(officialBuilds, 'getNodeJsVersions').mockResolvedValue([]); const errorMessage = 'Network error';
jest.spyOn(tc, 'downloadTool').mockRejectedValue(new Error(errorMessage));
// Simulating failure in getting versions and download await expect(officialBuilds.setupNodeJs()).rejects.toThrowError(new Error('Unable to find Node version for platform linux and architecture x64.'));
try {
await officialBuilds.setupNodeJs();
} catch (error) {
expect(error.message).toContain(errorMessage);
}
}); });
it('should throw an error if mirror URL is undefined and not provided', async () => { it('should throw an error if mirror URL is undefined and not provided', async () => {
const errorMessage = `Unable to find Node version for platform linux and architecture x64.`; // Mock missing mirror URL
officialBuilds.nodeInfo.mirrorURL = undefined; // Simulate missing mirror URL process.env.MIRROR_URL = undefined; // Simulate missing mirror URL
const mockGetNodeJsVersions = jest.spyOn(officialBuilds, 'getNodeJsVersions').mockResolvedValue([]); // Mock the version lookup method to avoid triggering version errors
const mockDownloadTool = jest.spyOn(tc, 'downloadTool').mockRejectedValue(new Error('Download failed')); jest.spyOn(OfficialBuilds, 'findVersionInHostedToolCacheDirectory').mockResolvedValue(null); // Simulate "not found"
try { // Now we expect the function to throw the "Mirror URL is undefined" error
await officialBuilds.setupNodeJs(); await expect(officialBuilds.setupNodeJs()).rejects.toThrowError('Mirror URL is undefined');
} catch (error) {
expect(error.message).toContain(errorMessage);
}
}); });
}); });
});

View File

@ -453,93 +453,87 @@ describe('setup-node', () => {
); );
}); });
}); });
describe('RcBuild - Mirror URL functionality', () => {
const nodeInfo: NodeInputs = {
versionSpec: '18.0.0-rc',
arch: 'x64',
mirrorURL: '',
checkLatest: false,
stable: false,
};
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
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}`);
// 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']();
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();
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(() => {});
// Expect the function to return the default URL because the mirror URL is undefined
const distributionMirrorUrl = rcBuild['getDistributionMirrorUrl']();
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();
infoSpy.mockRestore();
});
});
}); });
describe('RcBuild - Mirror URL functionality', () => {
const nodeInfo: NodeInputs = {
versionSpec: '18.0.0-rc', arch: 'x64', mirrorURL: '',
checkLatest: false,
stable: false
};
it('should return the default distribution URL if no mirror URL is provided', () => {
const rcBuild = new RcBuild(nodeInfo);
const distributionUrl = rcBuild.getDistributionUrl();
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';
nodeInfo.mirrorURL = mirrorURL;
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = rcBuild.getDistributionMirrorUrl();
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
expect(distributionMirrorUrl).toBe(mirrorURL);
});
it('should fall back to the default distribution URL if mirror URL is not provided', () => {
nodeInfo.mirrorURL = ''; // No mirror URL
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = rcBuild.getDistributionMirrorUrl();
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: https://nodejs.org/download/rc`);
expect(distributionMirrorUrl).toBe('https://nodejs.org/download/rc');
});
it('should log the correct info when mirror URL is not provided', () => {
nodeInfo.mirrorURL = ''; // No mirror URL
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = rcBuild.getDistributionMirrorUrl();
expect(core.info).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/rc');
});
it('should throw an error if mirror URL is undefined and not provided', async () => {
nodeInfo.mirrorURL = undefined; // Undefined mirror URL
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
await expect(rcBuild['getDistributionMirrorUrl']()).resolves.toBe('https://nodejs.org/download/rc');
});
it('should return mirror URL if provided in nodeInfo', () => {
const mirrorURL = 'https://custom.mirror.url';
nodeInfo.mirrorURL = mirrorURL;
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
// @ts-ignore: Accessing protected method for testing purposes
const url = rcBuild['getDistributionMirrorUrl']();
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
expect(url).toBe(mirrorURL);
});
it('should use the default URL if mirror URL is empty string', () => {
nodeInfo.mirrorURL = ''; // Empty string for mirror URL
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const url = rcBuild['getDistributionMirrorUrl']();
expect(core.info).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/rc');
expect(url).toBe('https://nodejs.org/download/rc');
});
});

31
dist/setup/index.js vendored
View File

@ -100158,25 +100158,8 @@ class BaseDistribution {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const initialUrl = this.getDistributionUrl(); const initialUrl = this.getDistributionUrl();
const dataUrl = `${initialUrl}/index.json`; const dataUrl = `${initialUrl}/index.json`;
try { const response = yield this.httpClient.getJson(dataUrl);
const response = yield this.httpClient.getJson(dataUrl); return response.result || [];
return response.result || [];
}
catch (err) {
if (err instanceof Error && err.message.includes('getaddrinfo EAI_AGAIN')) {
core.error(`Network error: Failed to resolve the server at ${dataUrl}.
Please check your DNS settings or verify that the URL is correct.`);
}
else if (err instanceof hc.HttpClientError && err.statusCode === 404) {
core.error(`404 Error: Unable to find versions at ${dataUrl}.
Please verify that the mirror URL is valid.`);
}
else {
core.error(`Failed to fetch Node.js versions from ${dataUrl}.
Please check the URL and try again.}`);
}
throw err;
}
}); });
} }
getNodejsDistInfo(version) { getNodejsDistInfo(version) {
@ -100232,11 +100215,6 @@ class BaseDistribution {
this.osPlat == 'win32') { this.osPlat == 'win32') {
return yield this.acquireWindowsNodeFromFallbackLocation(info.resolvedVersion, info.arch, info.downloadUrl); return yield this.acquireWindowsNodeFromFallbackLocation(info.resolvedVersion, info.arch, info.downloadUrl);
} }
// Handle network-related issues (e.g., DNS resolution failures)
if (err instanceof Error && err.message.includes('getaddrinfo EAI_AGAIN')) {
core.error(`Network error: Failed to resolve the server at ${info.downloadUrl}.
This could be due to a DNS resolution issue. Please verify the URL or check your network connection.`);
}
core.error(`Download failed from ${info.downloadUrl}. Please check the URl and try again.`); core.error(`Download failed from ${info.downloadUrl}. Please check the URl and try again.`);
throw err; throw err;
} }
@ -100745,7 +100723,7 @@ class OfficialBuilds extends base_distribution_1.default {
const versions = this.filterVersions(nodeJsVersions); const versions = this.filterVersions(nodeJsVersions);
const evaluatedVersion = this.evaluateVersions(versions); const evaluatedVersion = this.evaluateVersions(versions);
if (!evaluatedVersion) { if (!evaluatedVersion) {
throw new Error(`Unable to find Node version '${this.nodeInfo.versionSpec}' for platform ${this.osPlat} and architecture ${this.nodeInfo.arch} from the provided mirror-url ${this.nodeInfo.mirrorURL}. Please check the mirror-url`); throw new Error(`Unable to find Node version '${this.nodeInfo.versionSpec}' for platform ${this.osPlat} and architecture ${this.nodeInfo.arch}.`);
} }
const toolName = this.getNodejsMirrorURLInfo(evaluatedVersion); const toolName = this.getNodejsMirrorURLInfo(evaluatedVersion);
try { try {
@ -100864,9 +100842,6 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
const base_distribution_prerelease_1 = __importDefault(__nccwpck_require__(957)); const base_distribution_prerelease_1 = __importDefault(__nccwpck_require__(957));
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
class CanaryBuild extends base_distribution_prerelease_1.default { class CanaryBuild extends base_distribution_prerelease_1.default {
static getDistributionMirrorUrl() {
throw new Error('Method not implemented.');
}
constructor(nodeInfo) { constructor(nodeInfo) {
super(nodeInfo); super(nodeInfo);
this.distribution = 'v8-canary'; this.distribution = 'v8-canary';

View File

@ -108,22 +108,9 @@ export default abstract class BaseDistribution {
const initialUrl = this.getDistributionUrl(); const initialUrl = this.getDistributionUrl();
const dataUrl = `${initialUrl}/index.json`; const dataUrl = `${initialUrl}/index.json`;
try {
const response = await this.httpClient.getJson<INodeVersion[]>(dataUrl); const response = await this.httpClient.getJson<INodeVersion[]>(dataUrl);
return response.result || []; return response.result || [];
}catch (err) {
if (err instanceof Error && err.message.includes('getaddrinfo EAI_AGAIN')) {
core.error(`Network error: Failed to resolve the server at ${dataUrl}.
Please check your DNS settings or verify that the URL is correct.`);
} else if (err instanceof hc.HttpClientError && err.statusCode === 404) {
core.error(`404 Error: Unable to find versions at ${dataUrl}.
Please verify that the mirror URL is valid.`);
} else {
core.error(`Failed to fetch Node.js versions from ${dataUrl}.
Please check the URL and try again.}`);
}
throw err;
}
} }
protected getNodejsDistInfo(version: string) { protected getNodejsDistInfo(version: string) {
@ -196,13 +183,6 @@ export default abstract class BaseDistribution {
info.downloadUrl info.downloadUrl
); );
} }
// Handle network-related issues (e.g., DNS resolution failures)
if (err instanceof Error && err.message.includes('getaddrinfo EAI_AGAIN')) {
core.error(
`Network error: Failed to resolve the server at ${info.downloadUrl}.
This could be due to a DNS resolution issue. Please verify the URL or check your network connection.`
);
}
core.error( core.error(
`Download failed from ${info.downloadUrl}. Please check the URl and try again.` `Download failed from ${info.downloadUrl}. Please check the URl and try again.`
); );

View File

@ -323,7 +323,7 @@ export default class OfficialBuilds extends BaseDistribution {
if (!evaluatedVersion) { if (!evaluatedVersion) {
throw new Error( throw new Error(
`Unable to find Node version '${this.nodeInfo.versionSpec}' for platform ${this.osPlat} and architecture ${this.nodeInfo.arch} from the provided mirror-url ${this.nodeInfo.mirrorURL}. Please check the mirror-url` `Unable to find Node version '${this.nodeInfo.versionSpec}' for platform ${this.osPlat} and architecture ${this.nodeInfo.arch}.`
); );
} }