Skip to content

Commit e06540d

Browse files
Write tests for installers and remove --passWithNoTests option from lint staged config
1 parent 99f936a commit e06540d

File tree

12 files changed

+164
-32
lines changed

12 files changed

+164
-32
lines changed

lint-staged.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const lintStagedConfig = {
33
"*.{js,json,md,ts,yaml,yml}": "npm run format:write",
44
"*.{js,ts}": ["npm run lint:fix"],
55
// run testing on all supported file types within the src/ directory
6-
"src/**/*.{js,ts}": ["npm run test -- --findRelatedTests --passWithNoTests"],
6+
"src/**/*.{js,ts}": ["npm run test -- --findRelatedTests"],
77
};
88

99
export default lintStagedConfig;

src/cli-installer/cli-installer.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import os from "os";
33
import * as core from "@actions/core";
44
import * as tc from "@actions/tool-cache";
55

6-
import { type Installer } from "./index";
7-
import { LinuxInstaller } from "./linux";
8-
import { MacOsInstaller } from "./macos";
9-
import { WindowsInstaller } from "./windows";
10-
116
export type SupportedPlatform = Extract<
127
NodeJS.Platform,
138
"linux" | "darwin" | "win32"
@@ -21,6 +16,7 @@ const archMap: Record<string, string> = {
2116
arm64: "arm64",
2217
};
2318

19+
// Builds the download URL for the 1Password CLI based on the platform and version.
2420
export const cliUrlBuilder: Record<
2521
SupportedPlatform,
2622
(version: string, arch?: string) => string
@@ -34,28 +30,14 @@ export const cliUrlBuilder: Record<
3430
};
3531

3632
export class CliInstaller {
37-
protected readonly version: string;
38-
protected readonly arch: string;
33+
public readonly version: string;
34+
public readonly arch: string;
3935

4036
public constructor(version: string) {
4137
this.version = version;
4238
this.arch = this.getArch();
4339
}
4440

45-
public static create(version: string): Installer {
46-
const platform = os.platform();
47-
switch (platform) {
48-
case "linux":
49-
return new LinuxInstaller(version);
50-
case "darwin":
51-
return new MacOsInstaller(version);
52-
case "win32":
53-
return new WindowsInstaller(version);
54-
default:
55-
throw new Error(`Unsupported platform: ${platform}`);
56-
}
57-
}
58-
5941
public async install(url: string): Promise<void> {
6042
console.info(`Downloading 1Password CLI from: ${url}`);
6143
const downloadPath = await tc.downloadTool(url);

src/cli-installer/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
export { CliInstaller } from "./cli-installer";
2-
3-
export interface Installer {
4-
installCli(): Promise<void>;
5-
}
1+
export { type Installer, newCliInstaller } from "./installer";
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os from "os";
2+
3+
import { newCliInstaller } from "./installer";
4+
import { LinuxInstaller } from "./linux";
5+
import { MacOsInstaller } from "./macos";
6+
import { WindowsInstaller } from "./windows";
7+
8+
jest.mock("os");
9+
jest.mock("./linux");
10+
jest.mock("./macos");
11+
jest.mock("./windows");
12+
13+
describe("newCliInstaller", () => {
14+
const version = "1.0.0";
15+
16+
afterEach(() => {
17+
jest.resetAllMocks();
18+
});
19+
20+
it("should return LinuxInstaller for linux platform", () => {
21+
(os.platform as jest.Mock).mockReturnValue("linux");
22+
const installer = newCliInstaller(version);
23+
expect(installer).toBeInstanceOf(LinuxInstaller);
24+
});
25+
26+
it("should return MacOsInstaller for darwin platform", () => {
27+
(os.platform as jest.Mock).mockReturnValue("darwin");
28+
const installer = newCliInstaller(version);
29+
expect(installer).toBeInstanceOf(MacOsInstaller);
30+
});
31+
32+
it("should return WindowsInstaller for win32 platform", () => {
33+
(os.platform as jest.Mock).mockReturnValue("win32");
34+
const installer = newCliInstaller(version);
35+
expect(installer).toBeInstanceOf(WindowsInstaller);
36+
});
37+
38+
it("should throw error for unsupported platform", () => {
39+
(os.platform as jest.Mock).mockReturnValue("sunos");
40+
expect(() => newCliInstaller(version)).toThrow(
41+
"Unsupported platform: sunos",
42+
);
43+
});
44+
});

src/cli-installer/installer.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os from "os";
2+
3+
import { LinuxInstaller } from "./linux";
4+
import { MacOsInstaller } from "./macos";
5+
import { WindowsInstaller } from "./windows";
6+
7+
export interface Installer {
8+
installCli(): Promise<void>;
9+
}
10+
11+
export const newCliInstaller = (version: string): Installer => {
12+
const platform = os.platform();
13+
switch (platform) {
14+
case "linux":
15+
return new LinuxInstaller(version);
16+
case "darwin":
17+
return new MacOsInstaller(version);
18+
case "win32":
19+
return new WindowsInstaller(version);
20+
default:
21+
throw new Error(`Unsupported platform: ${platform}`);
22+
}
23+
};

src/cli-installer/linux.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os from "os";
2+
3+
import {
4+
CliInstaller,
5+
cliUrlBuilder,
6+
type SupportedPlatform,
7+
} from "./cli-installer";
8+
import { LinuxInstaller } from "./linux";
9+
10+
describe("LinuxInstaller", () => {
11+
const version = "1.2.3";
12+
13+
it("should construct with given version and architecture", () => {
14+
const installer = new LinuxInstaller(version);
15+
expect(installer.version).toEqual(version);
16+
expect(installer.arch).toEqual(os.arch());
17+
});
18+
19+
it("should call install with correct URL", async () => {
20+
const installer = new LinuxInstaller(version);
21+
const installMock = jest
22+
.spyOn(CliInstaller.prototype, "install")
23+
.mockResolvedValue();
24+
25+
await installer.installCli();
26+
27+
const builder = cliUrlBuilder["linux" as SupportedPlatform];
28+
const url = builder(version, installer.arch);
29+
expect(installMock).toHaveBeenCalledWith(url);
30+
});
31+
});

src/cli-installer/linux.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
cliUrlBuilder,
44
type SupportedPlatform,
55
} from "./cli-installer";
6-
import type { Installer } from "./index";
6+
import type { Installer } from "./installer";
77

88
export class LinuxInstaller extends CliInstaller implements Installer {
99
private readonly platform: SupportedPlatform = "linux"; // Node.js platform identifier for Linux

src/cli-installer/macos.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os from "os";
2+
3+
import { cliUrlBuilder, type SupportedPlatform } from "./cli-installer";
4+
import { MacOsInstaller } from "./macos";
5+
6+
describe("MacOsInstaller", () => {
7+
const version = "1.2.3";
8+
9+
it("should construct with given version and architecture", () => {
10+
const installer = new MacOsInstaller(version);
11+
expect(installer.version).toEqual(version);
12+
expect(installer.arch).toEqual(os.arch());
13+
});
14+
15+
it("should call install with correct URL", async () => {
16+
const installer = new MacOsInstaller(version);
17+
const installMock = jest.spyOn(installer, "install").mockResolvedValue();
18+
19+
await installer.installCli();
20+
21+
const builder = cliUrlBuilder["darwin" as SupportedPlatform];
22+
const url = builder(version, installer.arch);
23+
expect(installMock).toHaveBeenCalledWith(url);
24+
});
25+
});

src/cli-installer/macos.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
cliUrlBuilder,
1212
type SupportedPlatform,
1313
} from "./cli-installer";
14-
import { type Installer } from "./index";
14+
import { type Installer } from "./installer";
1515

1616
const execAsync = promisify(exec);
1717

src/cli-installer/windows.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os from "os";
2+
3+
import {
4+
CliInstaller,
5+
cliUrlBuilder,
6+
type SupportedPlatform,
7+
} from "./cli-installer";
8+
import { WindowsInstaller } from "./windows";
9+
10+
describe("WindowsInstaller", () => {
11+
const version = "1.2.3";
12+
13+
it("should construct with given version and architecture", () => {
14+
const installer = new WindowsInstaller(version);
15+
expect(installer.version).toEqual(version);
16+
expect(installer.arch).toEqual(os.arch());
17+
});
18+
19+
it("should call install with correct URL", async () => {
20+
const installer = new WindowsInstaller(version);
21+
const installMock = jest
22+
.spyOn(CliInstaller.prototype, "install")
23+
.mockResolvedValue();
24+
25+
await installer.installCli();
26+
27+
const builder = cliUrlBuilder["win32" as SupportedPlatform];
28+
const url = builder(version, installer.arch);
29+
expect(installMock).toHaveBeenCalledWith(url);
30+
});
31+
});

0 commit comments

Comments
 (0)