mirror of
https://github.com/actions/setup-node.git
synced 2025-06-29 14:13:49 +00:00
mirror-url -implementation
This commit is contained in:
parent
802632921f
commit
1b0ef2d227
13 changed files with 1223 additions and 184 deletions
|
@ -11,7 +11,7 @@ import path from 'path';
|
|||
import * as main from '../src/main';
|
||||
import * as auth from '../src/authutil';
|
||||
import OfficialBuilds from '../src/distributions/official_builds/official_builds';
|
||||
import {INodeVersion} from '../src/distributions/base-models';
|
||||
import {INodeVersion, NodeInputs} from '../src/distributions/base-models';
|
||||
|
||||
import nodeTestManifest from './data/versions-manifest.json';
|
||||
import nodeTestDist from './data/node-dist-index.json';
|
||||
|
@ -828,4 +828,144 @@ 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', () => {
|
||||
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';
|
||||
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 mockInfo = jest.spyOn(core, 'info').mockImplementation(() => {});
|
||||
|
||||
await officialBuilds.setupNodeJs();
|
||||
|
||||
// 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 () => {
|
||||
// Mock a scenario where mirror URL is not provided
|
||||
officialBuilds.nodeInfo.mirrorURL = undefined;
|
||||
|
||||
const mockInfo = jest.spyOn(core, 'info').mockImplementation(() => {});
|
||||
|
||||
await officialBuilds.setupNodeJs();
|
||||
|
||||
// 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 errorMessage = 'Network error';
|
||||
const mockError = jest.spyOn(core, 'error').mockImplementation(() => {});
|
||||
const mockDebug = jest.spyOn(core, 'debug').mockImplementation(() => {});
|
||||
|
||||
const mockDownloadTool = jest
|
||||
.spyOn(tc, 'downloadTool')
|
||||
.mockRejectedValue(new Error(errorMessage));
|
||||
|
||||
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 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...'
|
||||
);
|
||||
});
|
||||
|
||||
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.`;
|
||||
|
||||
const mockDownloadTool = jest
|
||||
.spyOn(tc, 'downloadTool')
|
||||
.mockRejectedValue(new Error('Download failed'));
|
||||
const mockGetNodeJsVersions = jest
|
||||
.spyOn(officialBuilds, 'getNodeJsVersions')
|
||||
.mockResolvedValue([]);
|
||||
|
||||
// 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 () => {
|
||||
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);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue