From 271fff9fff5e3455375cf54f26cb29f92f3a38d6 Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 4 Sep 2024 11:39:35 +0300 Subject: [PATCH] Fix tests --- lib/cleanup.js | 23 ++++++++--------------- src/cleanup.ts | 21 +++++++++------------ test/main.spec.ts | 28 ++++++++++++---------------- 3 files changed, 29 insertions(+), 43 deletions(-) diff --git a/lib/cleanup.js b/lib/cleanup.js index c44be92b8..a1c45ad8e 100644 --- a/lib/cleanup.js +++ b/lib/cleanup.js @@ -165,24 +165,17 @@ function getWorkingDirectory() { */ function supportedCliVersion(minimumVersion) { return __awaiter(this, void 0, void 0, function* () { - let cliVersion = yield getCliVersion(); - if (!cliVersion) { + let cliVersionOutput = yield utils_1.Utils.runCliAndGetOutput(['--version']); + if (!cliVersionOutput) { return false; } - return preload_1.default.gte(cliVersion, minimumVersion); - }); -} -function getCliVersion() { - return __awaiter(this, void 0, void 0, function* () { - try { - const versionOutput = yield utils_1.Utils.runCliAndGetOutput(['--version']); - const versionMatch = versionOutput.match(/jf version (\d+\.\d+\.\d+)/); - return versionMatch ? versionMatch[1] : null; - } - catch (error) { - core.warning('Failed to get JFrog CLI version: ' + error); - return null; + // Extract the version number using a regular expression + const versionMatch = cliVersionOutput.match(/jf version (\d+\.\d+\.\d+)/); + if (!versionMatch) { + return false; } + const cliVersion = versionMatch[1]; + return preload_1.default.gte(cliVersion, minimumVersion); }); } cleanup(); diff --git a/src/cleanup.ts b/src/cleanup.ts index 72c6da385..07340b337 100644 --- a/src/cleanup.ts +++ b/src/cleanup.ts @@ -127,22 +127,19 @@ function getWorkingDirectory(): string { * Check if the installed JFrog CLI version equal or greater than the minimum supplied */ export async function supportedCliVersion(minimumVersion: string): Promise { - let cliVersion: string | null = await getCliVersion(); - if (!cliVersion) { + let cliVersionOutput: string | null = await Utils.runCliAndGetOutput(['--version']); + if (!cliVersionOutput) { return false; } - return semver.gte(cliVersion, minimumVersion); -} -async function getCliVersion(): Promise { - try { - const versionOutput: string = await Utils.runCliAndGetOutput(['--version']); - const versionMatch: RegExpMatchArray | null = versionOutput.match(/jf version (\d+\.\d+\.\d+)/); - return versionMatch ? versionMatch[1] : null; - } catch (error) { - core.warning('Failed to get JFrog CLI version: ' + error); - return null; + // Extract the version number using a regular expression + const versionMatch: RegExpMatchArray | null = cliVersionOutput.match(/jf version (\d+\.\d+\.\d+)/); + if (!versionMatch) { + return false; } + + const cliVersion: string = versionMatch[1]; + return semver.gte(cliVersion, minimumVersion); } cleanup(); diff --git a/test/main.spec.ts b/test/main.spec.ts index c80e2af94..8aa756281 100644 --- a/test/main.spec.ts +++ b/test/main.spec.ts @@ -365,36 +365,32 @@ describe('Job Summaries', () => { }); }); -// Verify the Auto Build Publish feature, and the Job summaries -// are using CLI version 2.66.0 and above. describe('AutoPublishSupportedCliVersion', () => { it('should return true for CLI version greater than 2.66.0', async () => { - jest.spyOn(Utils, 'runCliAndGetOutput').mockResolvedValue('jf version 2.67.0'); - const result: boolean = await supportedCliVersion(); + let testCliVersion: string = '2.67.0'; + jest.spyOn(Utils, 'runCliAndGetOutput').mockResolvedValue(`jf version ${testCliVersion}`); + const result: boolean = await supportedCliVersion(Utils.minJobSummaryCLIVersion); expect(result).toBe(true); }); it('should return true for CLI version greater or equal to 2.66.0', async () => { - jest.spyOn(Utils, 'runCliAndGetOutput').mockResolvedValue('jf version 2.66.0'); - const result: boolean = await supportedCliVersion(); + let testCliVersion: string = '2.66.0'; + jest.spyOn(Utils, 'runCliAndGetOutput').mockResolvedValue(`jf version ${testCliVersion}`); + const result: boolean = await supportedCliVersion(Utils.minJobSummaryCLIVersion); expect(result).toBe(true); }); it('should return false for CLI version less than 2.66.0', async () => { - jest.spyOn(Utils, 'runCliAndGetOutput').mockResolvedValue('jf version 2.65.0'); - const result: boolean = await supportedCliVersion(); + let testCliVersion: string = '2.65.0'; + jest.spyOn(Utils, 'runCliAndGetOutput').mockResolvedValue(`jf version ${testCliVersion}`); + const result: boolean = await supportedCliVersion(Utils.minJobSummaryCLIVersion); expect(result).toBe(false); }); it('should return false if CLI version cannot be determined', async () => { - jest.spyOn(Utils, 'runCliAndGetOutput').mockResolvedValue('unknown version'); - const result: boolean = await supportedCliVersion(); - expect(result).toBe(false); - }); - - it('should return false if an error occurs while getting CLI version', async () => { - jest.spyOn(Utils, 'runCliAndGetOutput').mockRejectedValue(new Error('Failed to get version')); - const result: boolean = await supportedCliVersion(); + let testCliVersion: string = 'unknown version'; + jest.spyOn(Utils, 'runCliAndGetOutput').mockResolvedValue(`jf version ${testCliVersion}`); + const result: boolean = await supportedCliVersion(Utils.minJobSummaryCLIVersion); expect(result).toBe(false); }); });