|
| 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 "./core/cliUtils"; |
| 6 | + |
| 7 | +describe("cliUtils", () => { |
| 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 | + // TODO: CI only runs on Linux but we should run it on Windows too. |
| 29 | + it("version", async () => { |
| 30 | + const binPath = path.join(tmp, "version"); |
| 31 | + await expect(cli.version(binPath)).rejects.toThrow("ENOENT"); |
| 32 | + |
| 33 | + const binTmpl = await fs.readFile( |
| 34 | + path.join(__dirname, "../fixtures/bin.bash"), |
| 35 | + "utf8", |
| 36 | + ); |
| 37 | + await fs.writeFile(binPath, binTmpl.replace("$ECHO", "hello")); |
| 38 | + await expect(cli.version(binPath)).rejects.toThrow("EACCES"); |
| 39 | + |
| 40 | + await fs.chmod(binPath, "755"); |
| 41 | + await expect(cli.version(binPath)).rejects.toThrow("Unexpected token"); |
| 42 | + |
| 43 | + await fs.writeFile(binPath, binTmpl.replace("$ECHO", "{}")); |
| 44 | + await expect(cli.version(binPath)).rejects.toThrow( |
| 45 | + "No version found in output", |
| 46 | + ); |
| 47 | + |
| 48 | + await fs.writeFile( |
| 49 | + binPath, |
| 50 | + binTmpl.replace( |
| 51 | + "$ECHO", |
| 52 | + JSON.stringify({ |
| 53 | + version: "v0.0.0", |
| 54 | + }), |
| 55 | + ), |
| 56 | + ); |
| 57 | + expect(await cli.version(binPath)).toBe("v0.0.0"); |
| 58 | + |
| 59 | + const oldTmpl = await fs.readFile( |
| 60 | + path.join(__dirname, "../fixtures/bin.old.bash"), |
| 61 | + "utf8", |
| 62 | + ); |
| 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( |
| 76 | + binPath, |
| 77 | + old("unknown flag: --output\n", "Coder v1.1.1\n"), |
| 78 | + ); |
| 79 | + expect(await cli.version(binPath)).toBe("v1.1.1"); |
| 80 | + |
| 81 | + // Error with original error if it does not begin with "Coder". |
| 82 | + await fs.writeFile(binPath, old("unknown flag: --output", "Unrelated")); |
| 83 | + await expect(cli.version(binPath)).rejects.toThrow("unknown flag"); |
| 84 | + |
| 85 | + // Error if no version. |
| 86 | + await fs.writeFile(binPath, old("unknown flag: --output", "Coder")); |
| 87 | + await expect(cli.version(binPath)).rejects.toThrow("No version found"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("rmOld", async () => { |
| 91 | + const binDir = path.join(tmp, "bins"); |
| 92 | + expect(await cli.rmOld(path.join(binDir, "bin1"))).toStrictEqual([]); |
| 93 | + |
| 94 | + await fs.mkdir(binDir, { recursive: true }); |
| 95 | + await fs.writeFile(path.join(binDir, "bin.old-1"), "echo hello"); |
| 96 | + await fs.writeFile(path.join(binDir, "bin.old-2"), "echo hello"); |
| 97 | + await fs.writeFile(path.join(binDir, "bin.temp-1"), "echo hello"); |
| 98 | + await fs.writeFile(path.join(binDir, "bin.temp-2"), "echo hello"); |
| 99 | + await fs.writeFile(path.join(binDir, "bin1"), "echo hello"); |
| 100 | + await fs.writeFile(path.join(binDir, "bin2"), "echo hello"); |
| 101 | + await fs.writeFile(path.join(binDir, "bin.asc"), "echo hello"); |
| 102 | + await fs.writeFile(path.join(binDir, "bin.old-1.asc"), "echo hello"); |
| 103 | + await fs.writeFile(path.join(binDir, "bin.temp-2.asc"), "echo hello"); |
| 104 | + |
| 105 | + expect(await cli.rmOld(path.join(binDir, "bin1"))).toStrictEqual([ |
| 106 | + { |
| 107 | + fileName: "bin.asc", |
| 108 | + error: undefined, |
| 109 | + }, |
| 110 | + { |
| 111 | + fileName: "bin.old-1", |
| 112 | + error: undefined, |
| 113 | + }, |
| 114 | + { |
| 115 | + fileName: "bin.old-1.asc", |
| 116 | + error: undefined, |
| 117 | + }, |
| 118 | + { |
| 119 | + fileName: "bin.old-2", |
| 120 | + error: undefined, |
| 121 | + }, |
| 122 | + { |
| 123 | + fileName: "bin.temp-1", |
| 124 | + error: undefined, |
| 125 | + }, |
| 126 | + { |
| 127 | + fileName: "bin.temp-2", |
| 128 | + error: undefined, |
| 129 | + }, |
| 130 | + { |
| 131 | + fileName: "bin.temp-2.asc", |
| 132 | + error: undefined, |
| 133 | + }, |
| 134 | + ]); |
| 135 | + |
| 136 | + expect(await fs.readdir(path.join(tmp, "bins"))).toStrictEqual([ |
| 137 | + "bin1", |
| 138 | + "bin2", |
| 139 | + ]); |
| 140 | + }); |
| 141 | + |
| 142 | + it("ETag", async () => { |
| 143 | + const binPath = path.join(tmp, "hash"); |
| 144 | + |
| 145 | + await fs.writeFile(binPath, "foobar"); |
| 146 | + expect(await cli.eTag(binPath)).toBe( |
| 147 | + "8843d7f92416211de9ebb963ff4ce28125932878", |
| 148 | + ); |
| 149 | + |
| 150 | + await fs.writeFile(binPath, "test"); |
| 151 | + expect(await cli.eTag(binPath)).toBe( |
| 152 | + "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", |
| 153 | + ); |
| 154 | + }); |
| 155 | +}); |
0 commit comments