test files

This commit is contained in:
Aparna Jyothi 2025-02-06 17:46:54 +05:30
parent fb3b65568f
commit 6a4f7710a5
6 changed files with 372 additions and 270 deletions

View file

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