|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off -- Real files exercise the CommonJS updater installation boundary. |
| 2 | +import * as NodeCrypto from "node:crypto"; |
| 3 | +import * as NodeModule from "node:module"; |
| 4 | +import * as NodeOS from "node:os"; |
| 5 | +import * as NodePath from "node:path"; |
| 6 | + |
| 7 | +import { AppImageUpdater } from "electron-updater/out/AppImageUpdater.js"; |
| 8 | +import { DownloadedUpdateHelper } from "electron-updater/out/DownloadedUpdateHelper.js"; |
| 9 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 10 | + |
| 11 | +// The dependency uses CommonJS; spy on the same Node modules it loads. |
| 12 | +const require = NodeModule.createRequire(import.meta.url); |
| 13 | +const fs = require("node:fs") as typeof import("node:fs"); |
| 14 | +const childProcess = require("node:child_process") as typeof import("node:child_process"); |
| 15 | +const roots: string[] = []; |
| 16 | +const oldBinary = "working AppImage"; |
| 17 | +const newBinary = "verified replacement AppImage"; |
| 18 | + |
| 19 | +class TestUpdater extends AppImageUpdater { |
| 20 | + override spawnLog = vi.fn(async () => true); |
| 21 | + |
| 22 | + async prepare(installer: string, sha512: string) { |
| 23 | + this.downloadedUpdateHelper = new DownloadedUpdateHelper(NodePath.dirname(installer)); |
| 24 | + await this.downloadedUpdateHelper.setDownloadedFile( |
| 25 | + installer, |
| 26 | + null, |
| 27 | + { version: "1.1.0", files: [], path: "", sha512, releaseDate: "2026-09-08" }, |
| 28 | + { |
| 29 | + url: new URL("https://example.com/update.AppImage"), |
| 30 | + info: { url: "update.AppImage", sha512 }, |
| 31 | + }, |
| 32 | + NodePath.basename(installer), |
| 33 | + false, |
| 34 | + ); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +async function fixture(name = "t3code.AppImage") { |
| 39 | + const root = fs.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-appimage-")); |
| 40 | + roots.push(root); |
| 41 | + const installed = NodePath.join(root, name); |
| 42 | + const cache = NodePath.join(root, "cache"); |
| 43 | + fs.mkdirSync(cache); |
| 44 | + const installer = NodePath.join(cache, "T3-Code-1.1.0.AppImage"); |
| 45 | + fs.writeFileSync(installed, oldBinary, { mode: 0o755 }); |
| 46 | + fs.writeFileSync(installer, newBinary, { mode: 0o755 }); |
| 47 | + vi.stubEnv("APPIMAGE", installed); |
| 48 | + const updater = new TestUpdater(null, { version: "1.0.0" }); |
| 49 | + updater.logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() }; |
| 50 | + const errors = vi.fn(); |
| 51 | + updater.on("error", errors); |
| 52 | + await updater.prepare( |
| 53 | + installer, |
| 54 | + NodeCrypto.createHash("sha512").update(newBinary).digest("base64"), |
| 55 | + ); |
| 56 | + return { root, installed, installer, updater, errors }; |
| 57 | +} |
| 58 | + |
| 59 | +beforeEach(() => vi.useFakeTimers({ toFake: ["setImmediate"] })); |
| 60 | + |
| 61 | +afterEach(() => { |
| 62 | + vi.useRealTimers(); |
| 63 | + vi.restoreAllMocks(); |
| 64 | + vi.unstubAllEnvs(); |
| 65 | + for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe("AppImage installation", () => { |
| 69 | + it.each(["empty", "corrupt", "ENOSPC"])( |
| 70 | + "preserves the old executable after a %s copy", |
| 71 | + async (fault) => { |
| 72 | + const { installed, installer, updater, errors, root } = await fixture(); |
| 73 | + const badCopy = (destination: import("node:fs").PathLike) => { |
| 74 | + fs.writeFileSync(destination, fault === "corrupt" ? "x".repeat(newBinary.length) : ""); |
| 75 | + if (fault === "ENOSPC") |
| 76 | + throw Object.assign(new Error("No space left on device"), { code: "ENOSPC" }); |
| 77 | + }; |
| 78 | + vi.spyOn(fs, "copyFileSync").mockImplementation((_source, destination) => |
| 79 | + badCopy(destination), |
| 80 | + ); |
| 81 | + // Reproduce the reported successful-but-empty cross-filesystem mv on the unpatched updater. |
| 82 | + vi.spyOn(childProcess, "execFileSync").mockImplementation((command, args) => { |
| 83 | + if (command !== "mv" || !Array.isArray(args)) throw new Error("Unexpected process launch"); |
| 84 | + badCopy(String(args[2])); |
| 85 | + fs.unlinkSync(installer); |
| 86 | + return Buffer.alloc(0); |
| 87 | + }); |
| 88 | + |
| 89 | + updater.quitAndInstall(true, true); |
| 90 | + |
| 91 | + expect(fs.readFileSync(installed, "utf8")).toBe(oldBinary); |
| 92 | + expect(fs.readFileSync(installer, "utf8")).toBe(newBinary); |
| 93 | + expect(updater.spawnLog).not.toHaveBeenCalled(); |
| 94 | + expect(vi.getTimerCount()).toBe(0); |
| 95 | + expect(errors).toHaveBeenCalledOnce(); |
| 96 | + expect(fs.readdirSync(root).sort()).toEqual(["cache", "t3code.AppImage"]); |
| 97 | + }, |
| 98 | + ); |
| 99 | + |
| 100 | + it.each(["t3code.AppImage", "T3-Code-1.0.0.AppImage"])( |
| 101 | + "atomically installs and relaunches %s", |
| 102 | + async (name) => { |
| 103 | + const { installed, installer, updater } = await fixture(name); |
| 104 | + const destination = |
| 105 | + name === "t3code.AppImage" |
| 106 | + ? installed |
| 107 | + : NodePath.join(NodePath.dirname(installed), NodePath.basename(installer)); |
| 108 | + const rename = fs.renameSync; |
| 109 | + const swapped = vi.spyOn(fs, "renameSync").mockImplementation((source, target) => { |
| 110 | + expect(fs.readFileSync(installed, "utf8")).toBe(oldBinary); |
| 111 | + expect(fs.readFileSync(source, "utf8")).toBe(newBinary); |
| 112 | + expect(fs.statSync(source).dev).toBe(fs.statSync(NodePath.dirname(String(target))).dev); |
| 113 | + rename(source, target); |
| 114 | + }); |
| 115 | + const renamed = vi.fn(); |
| 116 | + updater.on("appimage-filename-updated", renamed); |
| 117 | + |
| 118 | + expect(updater.install(true, true)).toBe(true); |
| 119 | + |
| 120 | + expect(swapped).toHaveBeenCalledOnce(); |
| 121 | + expect(fs.readFileSync(destination, "utf8")).toBe(newBinary); |
| 122 | + // eslint-disable-next-line t3code/no-global-process-runtime -- Check permissions on the real fixture filesystem. |
| 123 | + if (NodeOS.platform() !== "win32") expect(fs.statSync(destination).mode & 0o777).toBe(0o755); |
| 124 | + expect(updater.spawnLog).toHaveBeenCalledWith( |
| 125 | + destination, |
| 126 | + [], |
| 127 | + expect.objectContaining({ APPIMAGE_SILENT_INSTALL: "true" }), |
| 128 | + ); |
| 129 | + expect(renamed.mock.calls).toEqual(destination === installed ? [] : [[destination]]); |
| 130 | + }, |
| 131 | + ); |
| 132 | + |
| 133 | + it("keeps the executable and download retryable when the atomic rename fails", async () => { |
| 134 | + const { installed, installer, updater, errors } = await fixture(); |
| 135 | + const rename = vi.spyOn(fs, "renameSync").mockImplementationOnce(() => { |
| 136 | + throw Object.assign(new Error("Permission denied"), { code: "EACCES" }); |
| 137 | + }); |
| 138 | + |
| 139 | + updater.quitAndInstall(true, true); |
| 140 | + |
| 141 | + expect(fs.readFileSync(installed, "utf8")).toBe(oldBinary); |
| 142 | + expect(fs.readFileSync(installer, "utf8")).toBe(newBinary); |
| 143 | + expect(errors).toHaveBeenCalledOnce(); |
| 144 | + expect(updater.spawnLog).not.toHaveBeenCalled(); |
| 145 | + expect(vi.getTimerCount()).toBe(0); |
| 146 | + rename.mockRestore(); |
| 147 | + expect(updater.install(true, true)).toBe(true); |
| 148 | + expect(fs.readFileSync(installed, "utf8")).toBe(newBinary); |
| 149 | + }); |
| 150 | +}); |
0 commit comments