|
| 1 | +import { describe, test, expect, beforeEach, afterEach, spyOn } from "bun:test"; |
| 2 | +import { mkdtemp, rm, writeFile, readFile, stat } from "fs/promises"; |
| 3 | +import { tmpdir } from "os"; |
| 4 | +import { join } from "path"; |
| 5 | +import { Effect } from "effect"; |
| 6 | +import { replaceBinary } from "./update"; |
| 7 | + |
| 8 | +async function makeTemp() { |
| 9 | + return mkdtemp(join(tmpdir(), "polar-test-")); |
| 10 | +} |
| 11 | + |
| 12 | +describe("replaceBinary", () => { |
| 13 | + let dir: string; |
| 14 | + let newBinaryPath: string; |
| 15 | + let binaryPath: string; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + dir = await makeTemp(); |
| 19 | + newBinaryPath = join(dir, "polar-new"); |
| 20 | + binaryPath = join(dir, "polar"); |
| 21 | + await writeFile(newBinaryPath, "#!/bin/sh\necho new"); |
| 22 | + await writeFile(binaryPath, "#!/bin/sh\necho old"); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(async () => { |
| 26 | + await rm(dir, { recursive: true, force: true }); |
| 27 | + }); |
| 28 | + |
| 29 | + test("replaces target binary with new binary content", async () => { |
| 30 | + await Effect.runPromise(replaceBinary(newBinaryPath, binaryPath)); |
| 31 | + |
| 32 | + const content = await readFile(binaryPath, "utf8"); |
| 33 | + expect(content).toBe("#!/bin/sh\necho new"); |
| 34 | + }); |
| 35 | + |
| 36 | + test("sets executable permissions on target binary", async () => { |
| 37 | + await Effect.runPromise(replaceBinary(newBinaryPath, binaryPath)); |
| 38 | + |
| 39 | + const s = await stat(binaryPath); |
| 40 | + // check owner execute bit |
| 41 | + expect(s.mode & 0o111).toBeGreaterThan(0); |
| 42 | + }); |
| 43 | + |
| 44 | + test("leaves no temp file behind after success", async () => { |
| 45 | + await Effect.runPromise(replaceBinary(newBinaryPath, binaryPath)); |
| 46 | + |
| 47 | + // list files in dir — only the replaced binary should remain |
| 48 | + const { readdir } = await import("fs/promises"); |
| 49 | + const files = await readdir(dir); |
| 50 | + const tempFiles = files.filter((f) => f.startsWith(".polar-update-")); |
| 51 | + expect(tempFiles).toHaveLength(0); |
| 52 | + }); |
| 53 | + |
| 54 | + test("throws and cleans up temp file on non-EACCES write error", async () => { |
| 55 | + // Simulate a generic I/O error during Bun.write (not EACCES) |
| 56 | + const bunSpy = spyOn(Bun, "write").mockImplementationOnce(() => |
| 57 | + Promise.reject(new Error("EIO: input/output error")), |
| 58 | + ); |
| 59 | + |
| 60 | + await expect( |
| 61 | + Effect.runPromise(replaceBinary(newBinaryPath, binaryPath)), |
| 62 | + ).rejects.toThrow("EIO"); |
| 63 | + |
| 64 | + const { readdir } = await import("fs/promises"); |
| 65 | + const files = await readdir(dir); |
| 66 | + const tempFiles = files.filter((f) => f.startsWith(".polar-update-")); |
| 67 | + expect(tempFiles).toHaveLength(0); |
| 68 | + |
| 69 | + bunSpy.mockRestore(); |
| 70 | + }); |
| 71 | + |
| 72 | + test("does not throw when EACCES triggers sudo fallback", async () => { |
| 73 | + // Simulate EACCES on rename by mocking Bun.write to throw it |
| 74 | + const bunSpy = spyOn(Bun, "write").mockImplementationOnce(() => { |
| 75 | + const err: any = new Error("EACCES: permission denied"); |
| 76 | + err.code = "EACCES"; |
| 77 | + return Promise.reject(err); |
| 78 | + }); |
| 79 | + |
| 80 | + // Mock Bun.spawn so sudo mv appears to succeed |
| 81 | + const spawnSpy = spyOn(Bun, "spawn").mockImplementationOnce(() => ({ |
| 82 | + exited: Promise.resolve(0), |
| 83 | + })); |
| 84 | + |
| 85 | + await Effect.runPromise(replaceBinary(newBinaryPath, binaryPath)); |
| 86 | + |
| 87 | + // Verify sudo mv was called with the right args |
| 88 | + expect(spawnSpy).toHaveBeenCalledWith( |
| 89 | + ["sudo", "mv", newBinaryPath, binaryPath], |
| 90 | + expect.objectContaining({ stdin: "inherit" }), |
| 91 | + ); |
| 92 | + |
| 93 | + bunSpy.mockRestore(); |
| 94 | + spawnSpy.mockRestore(); |
| 95 | + }); |
| 96 | + |
| 97 | + test("throws when sudo mv exits non-zero", async () => { |
| 98 | + const bunSpy = spyOn(Bun, "write").mockImplementationOnce(() => { |
| 99 | + const err: any = new Error("EACCES: permission denied"); |
| 100 | + err.code = "EACCES"; |
| 101 | + return Promise.reject(err); |
| 102 | + }); |
| 103 | + |
| 104 | + const spawnSpy = spyOn(Bun, "spawn").mockImplementationOnce(() => ({ |
| 105 | + exited: Promise.resolve(1), |
| 106 | + })); |
| 107 | + |
| 108 | + await expect( |
| 109 | + Effect.runPromise(replaceBinary(newBinaryPath, binaryPath)), |
| 110 | + ).rejects.toThrow("sudo mv failed"); |
| 111 | + |
| 112 | + bunSpy.mockRestore(); |
| 113 | + spawnSpy.mockRestore(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments