|
| 1 | +import { lstatSync, mkdirSync, mkdtempSync, readlinkSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 5 | +import { repairLegacyMacAppPath } from "./macAppPathMigration"; |
| 6 | + |
| 7 | +describe("repairLegacyMacAppPath", () => { |
| 8 | + let root: string; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + root = mkdtempSync(join(tmpdir(), "poracode-mac-app-path-")); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + rmSync(root, { recursive: true, force: true }); |
| 16 | + }); |
| 17 | + |
| 18 | + function packagedExecutable(appName: string): string { |
| 19 | + const executablePath = join(root, appName, "Contents", "MacOS", appName.replace(/\.app$/u, "")); |
| 20 | + mkdirSync(join(root, appName, "Contents", "MacOS"), { recursive: true }); |
| 21 | + writeFileSync(executablePath, "executable"); |
| 22 | + return executablePath; |
| 23 | + } |
| 24 | + |
| 25 | + it("restores the legacy Nightly path as a relative symlink", () => { |
| 26 | + const executablePath = packagedExecutable("Poracode Nightly.app"); |
| 27 | + |
| 28 | + expect( |
| 29 | + repairLegacyMacAppPath("nightly", { |
| 30 | + platform: "darwin", |
| 31 | + isPackaged: true, |
| 32 | + executablePath, |
| 33 | + }), |
| 34 | + ).toBe("created"); |
| 35 | + |
| 36 | + const legacyPath = join(root, "Lightcode Nightly.app"); |
| 37 | + expect(lstatSync(legacyPath).isSymbolicLink()).toBe(true); |
| 38 | + expect(readlinkSync(legacyPath)).toBe("Poracode Nightly.app"); |
| 39 | + }); |
| 40 | + |
| 41 | + it("restores the legacy Stable path", () => { |
| 42 | + const executablePath = packagedExecutable("Poracode.app"); |
| 43 | + |
| 44 | + expect( |
| 45 | + repairLegacyMacAppPath("stable", { |
| 46 | + platform: "darwin", |
| 47 | + isPackaged: true, |
| 48 | + executablePath, |
| 49 | + }), |
| 50 | + ).toBe("created"); |
| 51 | + expect(readlinkSync(join(root, "Lightcode.app"))).toBe("Poracode.app"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("never replaces an existing legacy app", () => { |
| 55 | + const executablePath = packagedExecutable("Poracode Nightly.app"); |
| 56 | + const legacyPath = join(root, "Lightcode Nightly.app"); |
| 57 | + mkdirSync(legacyPath); |
| 58 | + |
| 59 | + expect( |
| 60 | + repairLegacyMacAppPath("nightly", { |
| 61 | + platform: "darwin", |
| 62 | + isPackaged: true, |
| 63 | + executablePath, |
| 64 | + }), |
| 65 | + ).toBe("skipped"); |
| 66 | + expect(lstatSync(legacyPath).isDirectory()).toBe(true); |
| 67 | + }); |
| 68 | + |
| 69 | + it("skips unpackaged, non-macOS, and unexpectedly named bundles", () => { |
| 70 | + const executablePath = packagedExecutable("Poracode Nightly.app"); |
| 71 | + const otherExecutablePath = packagedExecutable("Renamed.app"); |
| 72 | + |
| 73 | + expect( |
| 74 | + repairLegacyMacAppPath("nightly", { |
| 75 | + platform: "linux", |
| 76 | + isPackaged: true, |
| 77 | + executablePath, |
| 78 | + }), |
| 79 | + ).toBe("skipped"); |
| 80 | + expect( |
| 81 | + repairLegacyMacAppPath("nightly", { |
| 82 | + platform: "darwin", |
| 83 | + isPackaged: false, |
| 84 | + executablePath, |
| 85 | + }), |
| 86 | + ).toBe("skipped"); |
| 87 | + expect( |
| 88 | + repairLegacyMacAppPath("nightly", { |
| 89 | + platform: "darwin", |
| 90 | + isPackaged: true, |
| 91 | + executablePath: otherExecutablePath, |
| 92 | + }), |
| 93 | + ).toBe("skipped"); |
| 94 | + }); |
| 95 | +}); |
0 commit comments