Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea committed Sep 4, 2024
1 parent c8b40cb commit 271fff9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 43 deletions.
23 changes: 8 additions & 15 deletions lib/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
21 changes: 9 additions & 12 deletions src/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
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<string | null> {
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();
28 changes: 12 additions & 16 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 271fff9

Please sign in to comment.