Skip to content

Commit e5d706d

Browse files
committed
Remove as unknown casting in cliConfig.test.ts
1 parent db42d7d commit e5d706d

File tree

3 files changed

+44
-54
lines changed

3 files changed

+44
-54
lines changed

src/cliConfig.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { escapeCommandArg } from "./util";
66
/**
77
* Returns the raw global flags from user configuration.
88
*/
9-
export function getGlobalFlagsRaw(configs: WorkspaceConfiguration): string[] {
9+
export function getGlobalFlagsRaw(
10+
configs: Pick<WorkspaceConfiguration, "get">,
11+
): string[] {
1012
return configs.get<string[]>("coder.globalFlags", []);
1113
}
1214

@@ -15,7 +17,7 @@ export function getGlobalFlagsRaw(configs: WorkspaceConfiguration): string[] {
1517
* Always includes the `--global-config` argument with the specified config directory.
1618
*/
1719
export function getGlobalFlags(
18-
configs: WorkspaceConfiguration,
20+
configs: Pick<WorkspaceConfiguration, "get">,
1921
configDir: string,
2022
): string[] {
2123
// Last takes precedence/overrides previous ones
@@ -30,7 +32,9 @@ export function getGlobalFlags(
3032
/**
3133
* Returns SSH flags for the `coder ssh` command from user configuration.
3234
*/
33-
export function getSshFlags(configs: WorkspaceConfiguration): string[] {
35+
export function getSshFlags(
36+
configs: Pick<WorkspaceConfiguration, "get">,
37+
): string[] {
3438
// Make sure to match this default with the one in the package.json
3539
return configs.get<string[]>("coder.sshFlags", ["--disable-autostart"]);
3640
}

src/headers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function isExecException(err: unknown): err is ExecException {
1818
}
1919

2020
export function getHeaderCommand(
21-
config: WorkspaceConfiguration,
21+
config: Pick<WorkspaceConfiguration, "get">,
2222
): string | undefined {
2323
const cmd =
2424
config.get<string>("coder.headerCommand")?.trim() ||
@@ -27,7 +27,9 @@ export function getHeaderCommand(
2727
return cmd || undefined;
2828
}
2929

30-
export function getHeaderArgs(config: WorkspaceConfiguration): string[] {
30+
export function getHeaderArgs(
31+
config: Pick<WorkspaceConfiguration, "get">,
32+
): string[] {
3133
// Escape a command line to be executed by the Coder binary, so ssh doesn't substitute variables.
3234
const escapeSubcommand: (str: string) => string =
3335
os.platform() === "win32"

test/unit/cliConfig.test.ts

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { it, expect, describe } from "vitest";
2-
import { type WorkspaceConfiguration } from "vscode";
32

43
import { getGlobalFlags, getGlobalFlagsRaw, getSshFlags } from "@/cliConfig";
54

5+
import { MockConfigurationProvider } from "../mocks/testHelpers";
66
import { isWindows } from "../utils/platform";
77

88
describe("cliConfig", () => {
99
describe("getGlobalFlags", () => {
1010
it("should return global-config and header args when no global flags configured", () => {
11-
const config = {
12-
get: (_key: string, defaultValue: unknown) => defaultValue,
13-
} as unknown as WorkspaceConfiguration;
11+
const config = new MockConfigurationProvider();
1412

1513
expect(getGlobalFlags(config, "/config/dir")).toStrictEqual([
1614
"--global-config",
@@ -19,12 +17,11 @@ describe("cliConfig", () => {
1917
});
2018

2119
it("should return global flags from config with global-config appended", () => {
22-
const config = {
23-
get: (key: string) =>
24-
key === "coder.globalFlags"
25-
? ["--verbose", "--disable-direct-connections"]
26-
: undefined,
27-
} as unknown as WorkspaceConfiguration;
20+
const config = new MockConfigurationProvider();
21+
config.set("coder.globalFlags", [
22+
"--verbose",
23+
"--disable-direct-connections",
24+
]);
2825

2926
expect(getGlobalFlags(config, "/config/dir")).toStrictEqual([
3027
"--verbose",
@@ -35,16 +32,12 @@ describe("cliConfig", () => {
3532
});
3633

3734
it("should not filter duplicate global-config flags, last takes precedence", () => {
38-
const config = {
39-
get: (key: string) =>
40-
key === "coder.globalFlags"
41-
? [
42-
"-v",
43-
"--global-config /path/to/ignored",
44-
"--disable-direct-connections",
45-
]
46-
: undefined,
47-
} as unknown as WorkspaceConfiguration;
35+
const config = new MockConfigurationProvider();
36+
config.set("coder.globalFlags", [
37+
"-v",
38+
"--global-config /path/to/ignored",
39+
"--disable-direct-connections",
40+
]);
4841

4942
expect(getGlobalFlags(config, "/config/dir")).toStrictEqual([
5043
"-v",
@@ -57,17 +50,13 @@ describe("cliConfig", () => {
5750

5851
it("should not filter header-command flags, header args appended at end", () => {
5952
const headerCommand = "echo test";
60-
const config = {
61-
get: (key: string) => {
62-
if (key === "coder.headerCommand") {
63-
return headerCommand;
64-
}
65-
if (key === "coder.globalFlags") {
66-
return ["-v", "--header-command custom", "--no-feature-warning"];
67-
}
68-
return undefined;
69-
},
70-
} as unknown as WorkspaceConfiguration;
53+
const config = new MockConfigurationProvider();
54+
config.set("coder.headerCommand", headerCommand);
55+
config.set("coder.globalFlags", [
56+
"-v",
57+
"--header-command custom",
58+
"--no-feature-warning",
59+
]);
7160

7261
const result = getGlobalFlags(config, "/config/dir");
7362
expect(result).toStrictEqual([
@@ -84,20 +73,17 @@ describe("cliConfig", () => {
8473

8574
describe("getGlobalFlagsRaw", () => {
8675
it("returns empty array when no global flags configured", () => {
87-
const config = {
88-
get: (_key: string, defaultValue: unknown) => defaultValue,
89-
} as unknown as WorkspaceConfiguration;
76+
const config = new MockConfigurationProvider();
9077

9178
expect(getGlobalFlagsRaw(config)).toStrictEqual([]);
9279
});
9380

9481
it("returns global flags from config", () => {
95-
const config = {
96-
get: (key: string) =>
97-
key === "coder.globalFlags"
98-
? ["--verbose", "--disable-direct-connections"]
99-
: undefined,
100-
} as unknown as WorkspaceConfiguration;
82+
const config = new MockConfigurationProvider();
83+
config.set("coder.globalFlags", [
84+
"--verbose",
85+
"--disable-direct-connections",
86+
]);
10187

10288
expect(getGlobalFlagsRaw(config)).toStrictEqual([
10389
"--verbose",
@@ -108,20 +94,18 @@ describe("cliConfig", () => {
10894

10995
describe("getSshFlags", () => {
11096
it("returns default flags when no SSH flags configured", () => {
111-
const config = {
112-
get: (_key: string, defaultValue: unknown) => defaultValue,
113-
} as unknown as WorkspaceConfiguration;
97+
const config = new MockConfigurationProvider();
11498

11599
expect(getSshFlags(config)).toStrictEqual(["--disable-autostart"]);
116100
});
117101

118102
it("returns SSH flags from config", () => {
119-
const config = {
120-
get: (key: string) =>
121-
key === "coder.sshFlags"
122-
? ["--disable-autostart", "--wait=yes", "--ssh-host-prefix=custom"]
123-
: undefined,
124-
} as unknown as WorkspaceConfiguration;
103+
const config = new MockConfigurationProvider();
104+
config.set("coder.sshFlags", [
105+
"--disable-autostart",
106+
"--wait=yes",
107+
"--ssh-host-prefix=custom",
108+
]);
125109

126110
expect(getSshFlags(config)).toStrictEqual([
127111
"--disable-autostart",

0 commit comments

Comments
 (0)