| 1 | import { createHash } from "node:crypto"; |
| 2 | import { spawnSync } from "node:child_process"; |
| 3 | import { |
| 4 | chmodSync, |
| 5 | existsSync, |
| 6 | mkdirSync, |
| 7 | mkdtempSync, |
| 8 | readdirSync, |
| 9 | readFileSync, |
| 10 | realpathSync, |
| 11 | rmSync, |
| 12 | writeFileSync, |
| 13 | } from "node:fs"; |
| 14 | import { tmpdir } from "node:os"; |
| 15 | import path from "node:path"; |
| 16 | import { fileURLToPath } from "node:url"; |
| 17 | import { afterEach, describe, expect, it } from "vitest"; |
| 18 | |
| 19 | const installer = fileURLToPath(new URL("../public/install.sh", import.meta.url)); |
| 20 | const fixtureRoots: string[] = []; |
| 21 | |
| 22 | function executable(contents: string): Buffer { |
| 23 | return Buffer.from(`#!/bin/sh\nprintf '%s\\n' '${contents}'\n`, "utf8"); |
| 24 | } |
| 25 | |
| 26 | function sha256(contents: Buffer): string { |
| 27 | return createHash("sha256").update(contents).digest("hex"); |
| 28 | } |
| 29 | |
| 30 | function installFixture( |
| 31 | withLegacyTui: boolean, |
| 32 | options: { |
| 33 | os?: "Darwin" | "Linux"; |
| 34 | arch?: "x86_64" | "aarch64"; |
| 35 | version?: string; |
| 36 | glibc?: string; |
| 37 | expectedStatus?: number; |
| 38 | shell?: string; |
| 39 | installUnderHome?: boolean; |
| 40 | installDirName?: string; |
| 41 | homeFiles?: string[]; |
| 42 | onPath?: "first" | "shadowed"; |
| 43 | args?: string[]; |
| 44 | } = {}, |
| 45 | ) { |
| 46 | const { |
| 47 | os = "Darwin", |
| 48 | arch = "x86_64", |
| 49 | version, |
| 50 | glibc = "2.39", |
| 51 | expectedStatus = 0, |
| 52 | shell = "/bin/zsh", |
| 53 | installUnderHome = false, |
| 54 | installDirName = "install", |
| 55 | homeFiles = [], |
| 56 | onPath, |
| 57 | args = [], |
| 58 | } = options; |
| 59 | const root = mkdtempSync(path.join(tmpdir(), "codewhale-web-installer-")); |
| 60 | fixtureRoots.push(root); |
| 61 | const releaseDir = path.join(root, "release"); |
| 62 | const home = path.join(root, "home"); |
| 63 | const installDir = installUnderHome |
| 64 | ? path.join(home, ".local", "bin") |
| 65 | : path.join(root, installDirName); |
| 66 | const fakeBin = path.join(root, "fake-bin"); |
| 67 | mkdirSync(releaseDir, { recursive: true }); |
| 68 | mkdirSync(installDir, { recursive: true, mode: 0o700 }); |
| 69 | mkdirSync(fakeBin, { recursive: true }); |
| 70 | |
| 71 | const runtime = executable("codewhale 0.9.5 (fixture)"); |
| 72 | const target = `${os === "Darwin" ? "macos" : "linux"}-${arch === "aarch64" ? "arm64" : "x64"}`; |
| 73 | const assets = [`codewhale-${target}`, `codew-${target}`]; |
| 74 | for (const asset of assets) { |
| 75 | writeFileSync(path.join(releaseDir, asset), runtime); |
| 76 | } |
| 77 | writeFileSync( |
| 78 | path.join(releaseDir, "codewhale-artifacts-sha256.txt"), |
| 79 | assets.map((asset) => `${sha256(runtime)} ${asset}`).join("\n") + "\n", |
| 80 | ); |
| 81 | |
| 82 | const fakeUname = [ |
| 83 | "#!/bin/sh", |
| 84 | 'case "${1:-}" in', |
| 85 | ` -s) printf '%s\\n' ${os} ;;`, |
| 86 | ` -m) printf '%s\\n' ${arch} ;;`, |
| 87 | ` *) printf '%s\\n' ${os} ;;`, |
| 88 | "esac", |
| 89 | "", |
| 90 | ].join("\n"); |
| 91 | const fakeCurl = [ |
| 92 | "#!/bin/sh", |
| 93 | 'out=""', |
| 94 | 'url=""', |
| 95 | 'while [ "$#" -gt 0 ]; do', |
| 96 | ' case "$1" in', |
| 97 | ' -o) shift; out="$1" ;;', |
| 98 | " -*) ;;", |
| 99 | ' *) url="$1" ;;', |
| 100 | " esac", |
| 101 | " shift", |
| 102 | "done", |
| 103 | 'cp "$FAKE_RELEASE_DIR/${url##*/}" "$out"', |
| 104 | "", |
| 105 | ].join("\n"); |
| 106 | const fakeGetconf = [ |
| 107 | "#!/bin/sh", |
| 108 | `printf '%s\\n' 'glibc ${glibc}'`, |
| 109 | "", |
| 110 | ].join("\n"); |
| 111 | for (const [name, contents] of [ |
| 112 | ["uname", fakeUname], |
| 113 | ["curl", fakeCurl], |
| 114 | ["getconf", fakeGetconf], |
| 115 | ]) { |
| 116 | const destination = path.join(fakeBin, name); |
| 117 | writeFileSync(destination, contents); |
| 118 | chmodSync(destination, 0o755); |
| 119 | } |
| 120 | |
| 121 | mkdirSync(home, { recursive: true, mode: 0o700 }); |
| 122 | for (const file of homeFiles) { |
| 123 | writeFileSync(path.join(home, file), "# existing profile\n"); |
| 124 | } |
| 125 | // The installer resolves its directory with `cd -P`, so PATH must carry the |
| 126 | // real path (macOS tmpdir sits behind a /var -> /private/var symlink). |
| 127 | const pathPrefix: string[] = []; |
| 128 | if (onPath === "shadowed") { |
| 129 | const shadow = path.join(root, "shadow"); |
| 130 | mkdirSync(shadow, { recursive: true }); |
| 131 | for (const name of ["codewhale", "codew"]) { |
| 132 | writeFileSync(path.join(shadow, name), executable(`${name} other`)); |
| 133 | chmodSync(path.join(shadow, name), 0o755); |
| 134 | } |
| 135 | pathPrefix.push(realpathSync(shadow)); |
| 136 | } |
| 137 | if (onPath) pathPrefix.push(realpathSync(installDir)); |
| 138 | |
| 139 | const legacyPath = path.join(installDir, "codewhale-tui"); |
| 140 | if (withLegacyTui) { |
| 141 | writeFileSync(legacyPath, executable("codewhale-tui 0.9.4 (legacy fixture)")); |
| 142 | chmodSync(legacyPath, 0o755); |
| 143 | } |
| 144 | |
| 145 | const env = { |
| 146 | ...process.env, |
| 147 | CODEWHALE_INSTALL_DIR: installDir, |
| 148 | CODEWHALE_RELEASE_BASE_URL: "https://fixtures.invalid/download", |
| 149 | ...(version ? { CODEWHALE_VERSION: version } : {}), |
| 150 | FAKE_RELEASE_DIR: releaseDir, |
| 151 | HOME: home, |
| 152 | SHELL: shell, |
| 153 | PATH: [...pathPrefix, fakeBin, process.env.PATH ?? "/usr/bin:/bin"].join(":"), |
| 154 | }; |
| 155 | const result = spawnSync("/bin/sh", [installer, ...args], { encoding: "utf8", env }); |
| 156 | expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(expectedStatus); |
| 157 | |
| 158 | return { env, installDir, legacyPath, result, runtime }; |
| 159 | } |
| 160 | |
| 161 | afterEach(() => { |
| 162 | for (const root of fixtureRoots.splice(0)) { |
| 163 | rmSync(root, { recursive: true, force: true }); |
| 164 | } |
| 165 | }); |
| 166 | |
| 167 | describe.skipIf(process.platform === "win32")("public installer compatibility contract", () => { |
| 168 | it("installs beside a leftover legacy TUI command and leaves it untouched", () => { |
| 169 | const { installDir, legacyPath, runtime } = installFixture(true); |
| 170 | |
| 171 | expect(readFileSync(path.join(installDir, "codewhale"))).toEqual(runtime); |
| 172 | expect(readFileSync(path.join(installDir, "codew"))).toEqual(runtime); |
| 173 | expect(readFileSync(legacyPath)).toEqual(executable("codewhale-tui 0.9.4 (legacy fixture)")); |
| 174 | }); |
| 175 | |
| 176 | it("does not create the retired TUI command for a clean v0.9.5 install", () => { |
| 177 | const { installDir, legacyPath, result, runtime } = installFixture(false); |
| 178 | |
| 179 | expect(readFileSync(path.join(installDir, "codewhale"))).toEqual(runtime); |
| 180 | expect(readFileSync(path.join(installDir, "codew"))).toEqual(runtime); |
| 181 | expect(existsSync(legacyPath)).toBe(false); |
| 182 | expect(result.stdout).not.toContain("Refreshed legacy compatibility command:"); |
| 183 | }); |
| 184 | |
| 185 | const block = (dir: string) => `# >>> codewhale PATH >>>\nexport PATH="${dir}:$PATH"\n# <<< codewhale PATH <<<\n`; |
| 186 | |
| 187 | it.each([ |
| 188 | ["/bin/zsh", "Linux", ".zshrc"], |
| 189 | ["/bin/bash", "Linux", ".bashrc"], |
| 190 | ["/bin/bash", "Darwin", ".bash_profile"], |
| 191 | ["/bin/dash", "Linux", ".profile"], |
| 192 | ] as const)( |
| 193 | "adds the marked PATH block to the profile for %s on %s and says so", |
| 194 | (shell, os, profile) => { |
| 195 | const { installDir, result } = installFixture(false, { shell, os }); |
| 196 | const home = path.join(installDir, "..", "home"); |
| 197 | |
| 198 | expect(readFileSync(path.join(home, profile), "utf8")).toBe(block(realpathSync(installDir))); |
| 199 | expect(result.stdout).toContain(`Added to ~/${profile}`); |
| 200 | expect(result.stdout).toContain("--no-modify-path"); |
| 201 | expect(result.stdout).toContain("To undo, run:"); |
| 202 | expect(result.stdout).toContain("export PATH="); |
| 203 | expect(result.stdout).toContain("docs/INSTALL.md#put-it-on-your-path"); |
| 204 | }, |
| 205 | ); |
| 206 | |
| 207 | it("prints the fish command and edits no file", () => { |
| 208 | const { installDir, result } = installFixture(false, { shell: "/usr/bin/fish", os: "Linux" }); |
| 209 | |
| 210 | expect(result.stdout).toContain(`fish_add_path "${realpathSync(installDir)}"`); |
| 211 | expect(result.stdout).toContain("this fish shell"); |
| 212 | expect(readdirSync(path.join(installDir, "..", "home"))).toEqual([]); |
| 213 | }); |
| 214 | |
| 215 | it("skips the edit with --no-modify-path and prints the command instead", () => { |
| 216 | const { installDir, result } = installFixture(false, { shell: "/bin/zsh", os: "Linux", args: ["--no-modify-path"] }); |
| 217 | |
| 218 | expect(readdirSync(path.join(installDir, "..", "home"))).toEqual([]); |
| 219 | expect(result.stdout).toContain("Not editing your shell profile"); |
| 220 | expect(result.stdout).toContain(`'# <<< codewhale PATH <<<' >> ~/.zshrc`); |
| 221 | }); |
| 222 | |
| 223 | it("never duplicates the block when the installer runs again", () => { |
| 224 | const first = installFixture(false, { shell: "/bin/zsh", os: "Linux" }); |
| 225 | const home = path.join(first.installDir, "..", "home"); |
| 226 | const before = readFileSync(path.join(home, ".zshrc"), "utf8"); |
| 227 | const again = spawnSync("/bin/sh", [installer], { |
| 228 | encoding: "utf8", |
| 229 | env: first.env, |
| 230 | }); |
| 231 | |
| 232 | expect(again.status, again.stderr).toBe(0); |
| 233 | expect(readFileSync(path.join(home, ".zshrc"), "utf8")).toBe(before); |
| 234 | expect(again.stdout).toContain("already has the codewhale PATH block"); |
| 235 | }); |
| 236 | |
| 237 | it.skipIf(!existsSync("/bin/dash"))("a new dash shell finds codewhale through the added block and the undo command removes it", () => { |
| 238 | const { installDir, result } = installFixture(false, { shell: "/bin/dash", os: "Linux" }); |
| 239 | const home = path.join(installDir, "..", "home"); |
| 240 | const found = spawnSync("/bin/dash", ["-c", ". \"$HOME/.profile\"; command -v codewhale"], { |
| 241 | encoding: "utf8", |
| 242 | env: { ...process.env, HOME: home, PATH: "/usr/bin:/bin" }, |
| 243 | }); |
| 244 | expect(found.status, found.stderr).toBe(0); |
| 245 | expect(found.stdout.trim()).toBe(path.join(realpathSync(installDir), "codewhale")); |
| 246 | |
| 247 | const undo = result.stdout.split("\n").map((line) => line.trim()).find((line) => line.startsWith("cw_tmp=")); |
| 248 | expect(undo).toBeDefined(); |
| 249 | const undone = spawnSync("/bin/sh", ["-c", undo as string], { encoding: "utf8", env: { ...process.env, HOME: home } }); |
| 250 | expect(undone.status, undone.stderr).toBe(0); |
| 251 | expect(readFileSync(path.join(home, ".profile"), "utf8")).toBe(""); |
| 252 | }); |
| 253 | |
| 254 | it.each([ |
| 255 | ["/bin/zsh", "Linux"], |
| 256 | ["/bin/bash", "Linux"], |
| 257 | ["/usr/bin/fish", "Linux"], |
| 258 | ["/bin/dash", "Darwin"], |
| 259 | ] as const)("ends with the PATH command and the verify command for %s on %s", (shell, os) => { |
| 260 | const { result } = installFixture(false, { shell, os, installUnderHome: true }); |
| 261 | const lines = result.stdout.split("\n").map((line) => line.trim()).filter(Boolean); |
| 262 | |
| 263 | expect(lines.at(-1)).toBe("codewhale --version"); |
| 264 | if (shell.endsWith("fish")) { |
| 265 | expect(lines.at(-5)).toContain("fish_add_path"); |
| 266 | } else { |
| 267 | expect(lines.at(-3)).toBe('export PATH="$HOME/.local/bin:$PATH"; hash -r'); |
| 268 | } |
| 269 | }); |
| 270 | |
| 271 | it("ends with the full-path verify command for a shell it has no PATH line for", () => { |
| 272 | const { installDir, result } = installFixture(false, { shell: "/bin/tcsh" }); |
| 273 | const lines = result.stdout.split("\n").map((line) => line.trim()).filter(Boolean); |
| 274 | |
| 275 | expect(lines.at(-1)).toBe(`"${realpathSync(installDir)}/codewhale" --version`); |
| 276 | }); |
| 277 | |
| 278 | it("ends with the next step when PATH already selects this install", () => { |
| 279 | const { result } = installFixture(false, { onPath: "first" }); |
| 280 | const lines = result.stdout.split("\n").map((line) => line.trim()).filter(Boolean); |
| 281 | |
| 282 | expect(lines.at(-1)).toBe("Next: codewhale --version"); |
| 283 | }); |
| 284 | |
| 285 | it("writes the default directory as $HOME/.local/bin in the added block", () => { |
| 286 | const { installDir } = installFixture(false, { installUnderHome: true }); |
| 287 | |
| 288 | expect(readFileSync(path.join(installDir, "..", "..", ".zshrc"), "utf8")).toBe(block("$HOME/.local/bin")); |
| 289 | }); |
| 290 | |
| 291 | it.each([ |
| 292 | [[".profile"], "~/.profile"], |
| 293 | [[".bash_login", ".profile"], "~/.bash_login"], |
| 294 | [[".bash_profile", ".profile"], "~/.bash_profile"], |
| 295 | ] as const)("keeps macOS login bash reading %j by naming %s", (homeFiles, profile) => { |
| 296 | const { installDir, result } = installFixture(false, { shell: "/bin/bash", os: "Darwin", homeFiles: [...homeFiles] }); |
| 297 | const file = path.join(installDir, "..", "home", profile.slice(2)); |
| 298 | |
| 299 | expect(result.stdout).toContain(`Added to ${profile}`); |
| 300 | expect(readFileSync(file, "utf8")).toBe(`# existing profile\n${block(realpathSync(installDir))}`); |
| 301 | }); |
| 302 | |
| 303 | it.each(["/bin/tcsh", "/usr/bin/nu"])("prints no POSIX line for the unrecognised shell %s", (shell) => { |
| 304 | const { result } = installFixture(false, { shell }); |
| 305 | |
| 306 | expect(result.stdout).toContain(`this installer has no PATH line for ${path.basename(shell)}`); |
| 307 | expect(result.stdout).not.toContain("export PATH="); |
| 308 | expect(result.stdout).toContain("docs/INSTALL.md#put-it-on-your-path"); |
| 309 | }); |
| 310 | |
| 311 | it.each(["it's", "$(id)", "a`b`", 'q"x', "back\\slash"])( |
| 312 | "prints no pasteable PATH line for a directory named %s", |
| 313 | (installDirName) => { |
| 314 | const { result } = installFixture(false, { installDirName }); |
| 315 | |
| 316 | expect(result.stdout).toContain("contains shell-special characters"); |
| 317 | expect(result.stdout).not.toContain("export PATH="); |
| 318 | expect(result.stdout).not.toContain(">> ~/"); |
| 319 | }, |
| 320 | ); |
| 321 | |
| 322 | it("prints no PATH hint when PATH already selects this install", () => { |
| 323 | const { result } = installFixture(false, { onPath: "first" }); |
| 324 | |
| 325 | expect(result.stdout).not.toContain("PATH selects"); |
| 326 | expect(result.stdout).not.toContain("first on PATH in future shells"); |
| 327 | expect(result.stdout).not.toContain("export PATH="); |
| 328 | }); |
| 329 | |
| 330 | it("prints the PATH hint when an earlier PATH entry shadows this install", () => { |
| 331 | const { installDir, result } = installFixture(false, { onPath: "shadowed" }); |
| 332 | |
| 333 | expect(result.stdout).toMatch(/PATH selects .*shadow\/codewhale; this install is/); |
| 334 | expect(result.stdout).toContain("is on PATH but not first"); |
| 335 | expect(readdirSync(path.join(installDir, "..", "home"))).toEqual([]); |
| 336 | }); |
| 337 | |
| 338 | it.each([undefined, "v0.9.6", "v0.9.11"])( |
| 339 | "does not apply a glibc floor to static Linux arm64 version %s", |
| 340 | (version) => { |
| 341 | const { installDir } = installFixture(false, { |
| 342 | os: "Linux", |
| 343 | arch: "aarch64", |
| 344 | version, |
| 345 | glibc: "2.17", |
| 346 | }); |
| 347 | |
| 348 | expect(existsSync(path.join(installDir, "codewhale"))).toBe(true); |
| 349 | expect(existsSync(path.join(installDir, "codew"))).toBe(true); |
| 350 | }, |
| 351 | ); |
| 352 | |
| 353 | it("keeps the truthful glibc preflight for an explicitly requested older Linux arm64 release", () => { |
| 354 | const { result } = installFixture(false, { |
| 355 | os: "Linux", |
| 356 | arch: "aarch64", |
| 357 | version: "v0.9.5", |
| 358 | glibc: "2.35", |
| 359 | expectedStatus: 1, |
| 360 | }); |
| 361 | |
| 362 | expect(result.stderr).toContain( |
| 363 | "Codewhale v0.9.5 linux-arm64 assets require glibc 2.39 or newer", |
| 364 | ); |
| 365 | expect(result.stderr).toContain( |
| 366 | "Current v0.9.6+ assets are static musl builds", |
| 367 | ); |
| 368 | }); |
| 369 | }); |
| 370 |