|
| 1 | +import { it, expect, describe } from "vitest"; |
| 2 | +import { type WorkspaceConfiguration } from "vscode"; |
| 3 | + |
| 4 | +import { getGlobalFlags, shouldDisableAutostart } from "@/cliConfig"; |
| 5 | + |
| 6 | +import { isWindows } from "../utils/platform"; |
| 7 | + |
| 8 | +describe("cliConfig", () => { |
| 9 | + describe("getGlobalFlags", () => { |
| 10 | + it("should return global-config and header args when no global flags configured", () => { |
| 11 | + const config = { |
| 12 | + get: () => undefined, |
| 13 | + } as unknown as WorkspaceConfiguration; |
| 14 | + |
| 15 | + expect(getGlobalFlags(config, "/config/dir")).toStrictEqual([ |
| 16 | + "--global-config", |
| 17 | + '"/config/dir"', |
| 18 | + ]); |
| 19 | + }); |
| 20 | + |
| 21 | + 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; |
| 28 | + |
| 29 | + expect(getGlobalFlags(config, "/config/dir")).toStrictEqual([ |
| 30 | + "--verbose", |
| 31 | + "--disable-direct-connections", |
| 32 | + "--global-config", |
| 33 | + '"/config/dir"', |
| 34 | + ]); |
| 35 | + }); |
| 36 | + |
| 37 | + 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; |
| 48 | + |
| 49 | + expect(getGlobalFlags(config, "/config/dir")).toStrictEqual([ |
| 50 | + "-v", |
| 51 | + "--global-config /path/to/ignored", |
| 52 | + "--disable-direct-connections", |
| 53 | + "--global-config", |
| 54 | + '"/config/dir"', |
| 55 | + ]); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should not filter header-command flags, header args appended at end", () => { |
| 59 | + 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; |
| 71 | + |
| 72 | + const result = getGlobalFlags(config, "/config/dir"); |
| 73 | + expect(result).toStrictEqual([ |
| 74 | + "-v", |
| 75 | + "--header-command custom", // ignored by CLI |
| 76 | + "--no-feature-warning", |
| 77 | + "--global-config", |
| 78 | + '"/config/dir"', |
| 79 | + "--header-command", |
| 80 | + quoteCommand(headerCommand), |
| 81 | + ]); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe("shouldDisableAutostart", () => { |
| 86 | + const mockConfig = (setting: string) => |
| 87 | + ({ |
| 88 | + get: (key: string) => |
| 89 | + key === "coder.disableAutostart" ? setting : undefined, |
| 90 | + }) as unknown as WorkspaceConfiguration; |
| 91 | + |
| 92 | + it("returns true when setting is 'always' regardless of platform", () => { |
| 93 | + const config = mockConfig("always"); |
| 94 | + expect(shouldDisableAutostart(config, "darwin")).toBe(true); |
| 95 | + expect(shouldDisableAutostart(config, "linux")).toBe(true); |
| 96 | + expect(shouldDisableAutostart(config, "win32")).toBe(true); |
| 97 | + }); |
| 98 | + |
| 99 | + it("returns false when setting is 'never' regardless of platform", () => { |
| 100 | + const config = mockConfig("never"); |
| 101 | + expect(shouldDisableAutostart(config, "darwin")).toBe(false); |
| 102 | + expect(shouldDisableAutostart(config, "linux")).toBe(false); |
| 103 | + expect(shouldDisableAutostart(config, "win32")).toBe(false); |
| 104 | + }); |
| 105 | + |
| 106 | + it("returns true when setting is 'auto' and platform is darwin", () => { |
| 107 | + const config = mockConfig("auto"); |
| 108 | + expect(shouldDisableAutostart(config, "darwin")).toBe(true); |
| 109 | + }); |
| 110 | + |
| 111 | + it("returns false when setting is 'auto' and platform is not darwin", () => { |
| 112 | + const config = mockConfig("auto"); |
| 113 | + expect(shouldDisableAutostart(config, "linux")).toBe(false); |
| 114 | + expect(shouldDisableAutostart(config, "win32")).toBe(false); |
| 115 | + expect(shouldDisableAutostart(config, "freebsd")).toBe(false); |
| 116 | + }); |
| 117 | + |
| 118 | + it("defaults to 'auto' when setting is not configured", () => { |
| 119 | + const config = { |
| 120 | + get: (_key: string, defaultValue: unknown) => defaultValue, |
| 121 | + } as unknown as WorkspaceConfiguration; |
| 122 | + expect(shouldDisableAutostart(config, "darwin")).toBe(true); |
| 123 | + expect(shouldDisableAutostart(config, "linux")).toBe(false); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +function quoteCommand(value: string): string { |
| 129 | + // Used to escape environment variables in commands. See `getHeaderArgs` in src/headers.ts |
| 130 | + const quote = isWindows() ? '"' : "'"; |
| 131 | + return `${quote}${value}${quote}`; |
| 132 | +} |
0 commit comments