From 5d5d8e9b58a272270ea10eddc95f0b0f9a46b590 Mon Sep 17 00:00:00 2001 From: Aparna Jyothi Date: Fri, 28 Feb 2025 16:11:26 +0530 Subject: [PATCH] code improvements --- __tests__/main.test.ts | 37 +++++++++++++++++++ dist/setup/index.js | 15 ++++---- src/distributions/base-distribution.ts | 7 ++-- .../official_builds/official_builds.ts | 8 ++-- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 501741a6..3da27e3d 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -13,6 +13,7 @@ import each from 'jest-each'; import * as main from '../src/main'; import * as util from '../src/util'; import OfficialBuilds from '../src/distributions/official_builds/official_builds'; +import { validateMirrorURL } from '../src/util'; describe('main tests', () => { let inputs = {} as any; @@ -280,4 +281,40 @@ describe('main tests', () => { ); }); }); + describe('mirror-url parameter', () => { + beforeEach(() => { + inputs['mirror-url'] = 'https://custom-mirror-url.com'; + + + }); + + afterEach(() => { + delete inputs['mirror-url']; + }); + + it('Read mirror-url if mirror-url is provided', async () => { + // Arrange + inputs['mirror-url'] = 'https://custom-mirror-url.com'; + + // Act + await main.run(); + + // Assert + expect(inputs['mirror-url']).toBeDefined(); + }); + + it('should throw an error if mirror-url is empty', async () => { + // Arrange + inputs['mirror-url'] = ' '; + + // Mock log and setFailed + const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); // Mock the log function + + // Act & Assert + expect(() => validateMirrorURL(inputs['mirror-url'])).toThrow( + 'Mirror URL is empty. Please provide a valid mirror URL.' + ); + }); + }); + }); diff --git a/dist/setup/index.js b/dist/setup/index.js index 796b49d9..afdc69de 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -100165,16 +100165,15 @@ class BaseDistribution { catch (err) { if (err instanceof Error && err.message.includes('getaddrinfo EAI_AGAIN')) { - core.error(`Network error: Failed to resolve the server at ${dataUrl}. + core.setFailed(`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}. + core.setFailed(`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.}`); + core.setFailed(`Failed to fetch Node.js versions from ${dataUrl}.Please check the URL and try again.}`); } throw err; } @@ -100525,8 +100524,8 @@ class OfficialBuilds extends base_distribution_1.default { } } catch (err) { - core.info(err.message); - core.info('Download failed'); + core.setFailed(err.message); + core.setFailed('Download failed'); core.debug((_a = err.stack) !== null && _a !== void 0 ? _a : 'empty stack'); } } @@ -100726,13 +100725,13 @@ class OfficialBuilds extends base_distribution_1.default { } catch (error) { if (error instanceof tc.HTTPError && error.httpStatusCode === 404) { - core.error(`Node version ${this.nodeInfo.versionSpec} for platform ${this.osPlat} and architecture ${this.nodeInfo.arch} was found but failed to download. ` + + core.setFailed(`Node version ${this.nodeInfo.versionSpec} for platform ${this.osPlat} and architecture ${this.nodeInfo.arch} was found but failed to download. ` + 'This usually happens when downloadable binaries are not fully updated at https://nodejs.org/. ' + 'To resolve this issue you may either fall back to the older version or try again later.'); } else { // For any other error type, you can log the error message. - core.error(`An unexpected error occurred like url might not correct`); + core.setFailed(`An unexpected error occurred like url might not correct`); } throw error; } diff --git a/src/distributions/base-distribution.ts b/src/distributions/base-distribution.ts index 2235d28a..ddb86c9b 100644 --- a/src/distributions/base-distribution.ts +++ b/src/distributions/base-distribution.ts @@ -116,14 +116,13 @@ export default abstract class BaseDistribution { err instanceof Error && err.message.includes('getaddrinfo EAI_AGAIN') ) { - core.error(`Network error: Failed to resolve the server at ${dataUrl}. + core.setFailed(`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}. + core.setFailed(`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.}`); + core.setFailed(`Failed to fetch Node.js versions from ${dataUrl}.Please check the URL and try again.}`); } throw err; } diff --git a/src/distributions/official_builds/official_builds.ts b/src/distributions/official_builds/official_builds.ts index 3d7a2788..ba8236aa 100644 --- a/src/distributions/official_builds/official_builds.ts +++ b/src/distributions/official_builds/official_builds.ts @@ -26,8 +26,8 @@ export default class OfficialBuilds extends BaseDistribution { const toolPath = downloadPath; } } catch (err) { - core.info((err as Error).message); - core.info('Download failed'); + core.setFailed((err as Error).message); + core.setFailed('Download failed'); core.debug((err as Error).stack ?? 'empty stack'); } } else { @@ -334,14 +334,14 @@ export default class OfficialBuilds extends BaseDistribution { return toolPath; } catch (error) { if (error instanceof tc.HTTPError && error.httpStatusCode === 404) { - core.error( + core.setFailed( `Node version ${this.nodeInfo.versionSpec} for platform ${this.osPlat} and architecture ${this.nodeInfo.arch} was found but failed to download. ` + 'This usually happens when downloadable binaries are not fully updated at https://nodejs.org/. ' + 'To resolve this issue you may either fall back to the older version or try again later.' ); } else { // For any other error type, you can log the error message. - core.error(`An unexpected error occurred like url might not correct`); + core.setFailed(`An unexpected error occurred like url might not correct`); } throw error;