Skip to content

Commit 0163621

Browse files
committed
Added unit tests
1 parent 39b7de9 commit 0163621

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

src/main/osinstaller/CxInstaller.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class CxInstaller {
2525
private readonly client: Client;
2626

2727
private static readonly PLATFORMS: Record<SupportedPlatforms, PlatformData> = {
28-
win32: { platform: winOS, extension: 'zip' },
28+
win32: { platform: 'windows', extension: 'zip' },
2929
darwin: { platform: macOS, extension: 'tar.gz' },
3030
linux: { platform: linuxOS, extension: 'tar.gz' }
3131
};
@@ -36,7 +36,7 @@ export class CxInstaller {
3636
this.client = client;
3737
}
3838

39-
private async getDownloadURL(): Promise<string> {
39+
async getDownloadURL(): Promise<string> {
4040
const cliVersion = await this.readASTCLIVersion();
4141
const platformData = CxInstaller.PLATFORMS[this.platform];
4242

@@ -65,7 +65,7 @@ export class CxInstaller {
6565
}
6666

6767
public getExecutablePath(): string {
68-
const executableName = this.platform === 'win32' ? 'cx.exe' : 'cx';
68+
const executableName = this.platform === winOS ? 'cx.exe' : 'cx';
6969
return path.join(this.resourceDirPath, executableName);
7070
}
7171

@@ -170,7 +170,7 @@ export class CxInstaller {
170170
}
171171
}
172172

173-
private checkExecutableExists(): boolean {
173+
public checkExecutableExists(): boolean {
174174
return fs.existsSync(this.getExecutablePath());
175175
}
176176

@@ -195,4 +195,8 @@ export class CxInstaller {
195195
private getCompressFolderName(): string {
196196
return `ast-cli.${this.platform === winOS ? 'zip' : 'tar.gz'}`;
197197
}
198+
199+
public getPlatform(): SupportedPlatforms {
200+
return this.platform;
201+
}
198202
}

src/tests/CxInstallerTest.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { CxInstaller } from "../main/osinstaller/CxInstaller";
2+
import { anyString, mock, instance, when, verify } from "ts-mockito";
3+
import { HttpClient } from "../main/client/HttpClient";
4+
5+
const clientMock = mock(HttpClient);
6+
const clientMockInstance = instance(clientMock);
7+
8+
const cxInstallerLinux = new CxInstaller("linux", clientMockInstance);
9+
const cxInstallerMac = new CxInstaller("darwin", clientMockInstance);
10+
const cxInstallerWindows = new CxInstaller("win32", clientMockInstance);
11+
12+
describe("CxInstaller cases", () => {
13+
it('CxInstaller getDownloadURL Linux Successful case', async () => {
14+
const url = await cxInstallerLinux.getDownloadURL();
15+
const architecture = getArchitecture(cxInstallerLinux.getPlatform());
16+
expect(url).toBe(`https://download.checkmarx.com/CxOne/CLI/2.2.5/ast-cli_2.2.5_linux_${architecture}.tar.gz`);
17+
});
18+
19+
it('CxInstaller getDownloadURL Mac Successful case', async () => {
20+
const url = await cxInstallerMac.getDownloadURL();
21+
const architecture = getArchitecture(cxInstallerMac.getPlatform());
22+
expect(url).toBe(`https://download.checkmarx.com/CxOne/CLI/2.2.5/ast-cli_2.2.5_darwin_${architecture}.tar.gz`);
23+
});
24+
25+
it('CxInstaller getDownloadURL Windows Successful case', async () => {
26+
const url = await cxInstallerWindows.getDownloadURL();
27+
const architecture = getArchitecture(cxInstallerWindows.getPlatform());
28+
expect(url).toBe(`https://download.checkmarx.com/CxOne/CLI/2.2.5/ast-cli_2.2.5_windows_${architecture}.zip`);
29+
});
30+
});
31+
32+
describe("CxInstaller getExecutablePath cases", () => {
33+
it('CxInstaller getExecutablePath Linux Successful case', () => {
34+
const executablePath = cxInstallerLinux.getExecutablePath();
35+
expect(executablePath).toContain(`src/main/wrapper/resources/cx`);
36+
});
37+
38+
it('CxInstaller getExecutablePath Mac Successful case', () => {
39+
const executablePath = cxInstallerMac.getExecutablePath();
40+
expect(executablePath).toContain(`src/main/wrapper/resources/cx`);
41+
});
42+
43+
it('CxInstaller getExecutablePath Windows Successful case', () => {
44+
const executablePath = cxInstallerWindows.getExecutablePath();
45+
expect(executablePath).toContain(`src/main/wrapper/resources/cx.exe`);
46+
});
47+
});
48+
49+
describe("CxInstaller checkExecutableExists cases", () => {
50+
beforeAll(async () => {
51+
// Set up mock behavior
52+
when(clientMock.downloadFile(anyString(), anyString())).thenResolve();
53+
54+
// Trigger the download
55+
await cxInstallerWindows.downloadIfNotInstalledCLI();
56+
});
57+
58+
it('CxInstaller checkExecutableExists Windows Successful case', () => {
59+
const exists = cxInstallerWindows.checkExecutableExists();
60+
expect(exists).toBe(false);
61+
62+
// Verify if downloadFile was called with the expected arguments
63+
verify(clientMock.downloadFile(anyString(), anyString())).called();
64+
});
65+
});
66+
67+
function getArchitecture(platform: string): string {
68+
if (platform !== 'linux') {
69+
return 'x64';
70+
}
71+
72+
const archMap: Record<string, string> = {
73+
'arm64': 'arm64',
74+
'arm': 'armv6'
75+
};
76+
77+
return archMap[process.arch] || 'x64';
78+
}

0 commit comments

Comments
 (0)