|
| 1 | +import fs from "fs/promises" |
| 2 | +import os from "os" |
| 3 | +import path from "path" |
| 4 | +import { beforeAll, describe, expect, it } from "vitest" |
| 5 | +import * as cli from "./cliManager" |
| 6 | + |
| 7 | +describe("cliManager", () => { |
| 8 | + const tmp = path.join(os.tmpdir(), "vscode-coder-tests") |
| 9 | + |
| 10 | + beforeAll(async () => { |
| 11 | + // Clean up from previous tests, if any. |
| 12 | + await fs.rm(tmp, { recursive: true, force: true }) |
| 13 | + await fs.mkdir(tmp, { recursive: true }) |
| 14 | + }) |
| 15 | + |
| 16 | + it("name", () => { |
| 17 | + expect(cli.name().startsWith("coder-")).toBeTruthy() |
| 18 | + }) |
| 19 | + |
| 20 | + it("stat", async () => { |
| 21 | + const binPath = path.join(tmp, "stat") |
| 22 | + expect(await cli.stat(binPath)).toBeUndefined() |
| 23 | + |
| 24 | + await fs.writeFile(binPath, "test") |
| 25 | + expect((await cli.stat(binPath))?.size).toBe(4) |
| 26 | + }) |
| 27 | + |
| 28 | + it("rm", async () => { |
| 29 | + const binPath = path.join(tmp, "rm") |
| 30 | + await cli.rm(binPath) |
| 31 | + |
| 32 | + await fs.writeFile(binPath, "test") |
| 33 | + await cli.rm(binPath) |
| 34 | + }) |
| 35 | + |
| 36 | + // TODO: CI only runs on Linux but we should run it on Windows too. |
| 37 | + it("version", async () => { |
| 38 | + const binPath = path.join(tmp, "version") |
| 39 | + await expect(cli.version(binPath)).rejects.toThrow("ENOENT") |
| 40 | + |
| 41 | + const binTmpl = await fs.readFile(path.join(__dirname, "../fixtures/bin.bash"), "utf8") |
| 42 | + await fs.writeFile(binPath, binTmpl.replace("$ECHO", "hello")) |
| 43 | + await expect(cli.version(binPath)).rejects.toThrow("EACCES") |
| 44 | + |
| 45 | + await fs.chmod(binPath, "755") |
| 46 | + await expect(cli.version(binPath)).rejects.toThrow("Unexpected token") |
| 47 | + |
| 48 | + await fs.writeFile(binPath, binTmpl.replace("$ECHO", "{}")) |
| 49 | + await expect(cli.version(binPath)).rejects.toThrow("No version found in output") |
| 50 | + |
| 51 | + await fs.writeFile( |
| 52 | + binPath, |
| 53 | + binTmpl.replace( |
| 54 | + "$ECHO", |
| 55 | + JSON.stringify({ |
| 56 | + version: "v0.0.0", |
| 57 | + }), |
| 58 | + ), |
| 59 | + ) |
| 60 | + expect(await cli.version(binPath)).toBe("v0.0.0") |
| 61 | + |
| 62 | + const oldTmpl = await fs.readFile(path.join(__dirname, "../fixtures/bin.old.bash"), "utf8") |
| 63 | + const old = (stderr: string, stdout: string): string => { |
| 64 | + return oldTmpl.replace("$STDERR", stderr).replace("$STDOUT", stdout) |
| 65 | + } |
| 66 | + |
| 67 | + // Should fall back only if it says "unknown flag". |
| 68 | + await fs.writeFile(binPath, old("foobar", "Coder v1.1.1")) |
| 69 | + await expect(cli.version(binPath)).rejects.toThrow("foobar") |
| 70 | + |
| 71 | + await fs.writeFile(binPath, old("unknown flag: --output", "Coder v1.1.1")) |
| 72 | + expect(await cli.version(binPath)).toBe("v1.1.1") |
| 73 | + |
| 74 | + // Should trim off the newline if necessary. |
| 75 | + await fs.writeFile(binPath, old("unknown flag: --output\n", "Coder v1.1.1\n")) |
| 76 | + expect(await cli.version(binPath)).toBe("v1.1.1") |
| 77 | + |
| 78 | + // Error with original error if it does not begin with "Coder". |
| 79 | + await fs.writeFile(binPath, old("unknown flag: --output", "Unrelated")) |
| 80 | + await expect(cli.version(binPath)).rejects.toThrow("unknown flag") |
| 81 | + |
| 82 | + // Error if no version. |
| 83 | + await fs.writeFile(binPath, old("unknown flag: --output", "Coder")) |
| 84 | + await expect(cli.version(binPath)).rejects.toThrow("No version found") |
| 85 | + }) |
| 86 | + |
| 87 | + it("rmOld", async () => { |
| 88 | + const binDir = path.join(tmp, "bins") |
| 89 | + expect(await cli.rmOld(path.join(binDir, "bin1"))).toStrictEqual([]) |
| 90 | + |
| 91 | + await fs.mkdir(binDir, { recursive: true }) |
| 92 | + await fs.writeFile(path.join(binDir, "bin.old-1"), "echo hello") |
| 93 | + await fs.writeFile(path.join(binDir, "bin.old-2"), "echo hello") |
| 94 | + await fs.writeFile(path.join(binDir, "bin.temp-1"), "echo hello") |
| 95 | + await fs.writeFile(path.join(binDir, "bin.temp-2"), "echo hello") |
| 96 | + await fs.writeFile(path.join(binDir, "bin1"), "echo hello") |
| 97 | + await fs.writeFile(path.join(binDir, "bin2"), "echo hello") |
| 98 | + |
| 99 | + expect(await cli.rmOld(path.join(binDir, "bin1"))).toStrictEqual([ |
| 100 | + { |
| 101 | + fileName: "bin.old-1", |
| 102 | + error: undefined, |
| 103 | + }, |
| 104 | + { |
| 105 | + fileName: "bin.old-2", |
| 106 | + error: undefined, |
| 107 | + }, |
| 108 | + { |
| 109 | + fileName: "bin.temp-1", |
| 110 | + error: undefined, |
| 111 | + }, |
| 112 | + { |
| 113 | + fileName: "bin.temp-2", |
| 114 | + error: undefined, |
| 115 | + }, |
| 116 | + ]) |
| 117 | + |
| 118 | + expect(await fs.readdir(path.join(tmp, "bins"))).toStrictEqual(["bin1", "bin2"]) |
| 119 | + }) |
| 120 | + |
| 121 | + it("ETag", async () => { |
| 122 | + const binPath = path.join(tmp, "hash") |
| 123 | + |
| 124 | + await fs.writeFile(binPath, "foobar") |
| 125 | + expect(await cli.eTag(binPath)).toBe("8843d7f92416211de9ebb963ff4ce28125932878") |
| 126 | + |
| 127 | + await fs.writeFile(binPath, "test") |
| 128 | + expect(await cli.eTag(binPath)).toBe("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") |
| 129 | + }) |
| 130 | +}) |
0 commit comments