-
Notifications
You must be signed in to change notification settings - Fork 14
fix: harden updater stale pending handling #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { readFileSync } from "node:fs" | ||
| import { describe, expect, test } from "bun:test" | ||
|
|
||
| const layout = readFileSync(new URL("./layout.tsx", import.meta.url), "utf8") | ||
| const errorPage = readFileSync(new URL("./error.tsx", import.meta.url), "utf8") | ||
| const settings = readFileSync(new URL("../components/settings-general.tsx", import.meta.url), "utf8") | ||
| const platform = readFileSync(new URL("../context/platform.tsx", import.meta.url), "utf8") | ||
|
|
||
| describe("update install renderer contracts", () => { | ||
| test("renderer install actions do not relaunch after platform update", () => { | ||
| expect(layout).not.toMatch(/await\s+platform\.restart!\(\)/) | ||
| expect(settings).not.toMatch(/await\s+platform\.restart!\(\)/) | ||
| expect(errorPage).not.toMatch(/await\s+platform\.restart!\(\)/) | ||
| expect(layout).not.toMatch(/\.then\(\(\)\s*=>\s*platform\.restart!\(\)\)/) | ||
| expect(settings).not.toMatch(/\.then\(\(\)\s*=>\s*platform\.restart!\(\)\)/) | ||
| expect(errorPage).not.toMatch(/\.then\(\(\)\s*=>\s*platform\.restart!\(\)\)/) | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test("renderer update prompts only require platform update", () => { | ||
| expect(layout).toContain("if (!platform.checkUpdate || !platform.update) return") | ||
| expect(layout).not.toContain("if (!platform.checkUpdate || !platform.update || !platform.restart) return") | ||
| expect(settings).toContain("platform.update") | ||
| expect(settings).not.toContain("platform.update && platform.restart") | ||
| }) | ||
|
|
||
| test("cache update failures are part of the renderer-facing type", () => { | ||
| expect(platform).toContain('"check" | "download" | "metadata" | "cache"') | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/desktop-electron/src/main/index-updater-source.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { readFileSync } from "node:fs" | ||
| import { describe, expect, test } from "bun:test" | ||
|
|
||
| const source = readFileSync(new URL("./index.ts", import.meta.url), "utf8") | ||
|
|
||
| describe("main updater source contracts", () => { | ||
| test("disables stable downgrades after assigning latest channel", () => { | ||
| const channelIndex = source.search(/autoUpdater\.channel\s*=\s*"latest"/) | ||
| const downgradeIndex = source.search(/autoUpdater\.allowDowngrade\s*=\s*false/) | ||
| expect(channelIndex).toBeGreaterThanOrEqual(0) | ||
| expect(downgradeIndex).toBeGreaterThan(channelIndex) | ||
| expect(source).not.toContain("autoUpdater.allowDowngrade = true") | ||
| }) | ||
|
|
||
| test("disables auto install on quit only on macOS", () => { | ||
| expect(source).toContain('autoUpdater.autoInstallOnAppQuit = process.platform !== "darwin"') | ||
| expect(source).not.toContain("autoUpdater.autoInstallOnAppQuit = false") | ||
| }) | ||
|
|
||
| test("strict pending cleanup uses shared updater cache helper and propagates rm errors", () => { | ||
| expect(source).toContain('import { pendingUpdateCacheDir } from "./updater-cache"') | ||
| expect(source).toContain("await rm(pendingUpdateCacheDir(), { recursive: true, force: true })") | ||
| expect(source).not.toMatch( | ||
| /rm\(pendingUpdateCacheDir\(\),\s*\{\s*recursive:\s*true,\s*force:\s*true\s*\}\)\s*\.catch\(\(\)\s*=>/, | ||
| ) | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { getAppCacheDir, pendingUpdateCacheDir, UPDATER_CACHE_DIR_NAME } from "./updater-cache" | ||
|
|
||
| describe("updater cache path", () => { | ||
| test("uses the same updater cache dir name as app-update.yml", () => { | ||
| expect(UPDATER_CACHE_DIR_NAME).toBe("pawwork-updater") | ||
| }) | ||
|
|
||
| test("resolves macOS pending cache from the user library cache root", () => { | ||
| expect(getAppCacheDir({ platform: "darwin", homedir: "/Users/demo", env: {} })).toBe("/Users/demo/Library/Caches") | ||
| expect(pendingUpdateCacheDir({ platform: "darwin", homedir: "/Users/demo", env: {} })).toBe( | ||
| "/Users/demo/Library/Caches/pawwork-updater/pending", | ||
| ) | ||
| }) | ||
|
|
||
| test("resolves Windows pending cache from LOCALAPPDATA when present", () => { | ||
| expect( | ||
| pendingUpdateCacheDir({ | ||
| platform: "win32", | ||
| homedir: "C:\\Users\\demo", | ||
| env: { LOCALAPPDATA: "C:\\Users\\demo\\AppData\\Local" }, | ||
| }), | ||
| ).toBe("C:\\Users\\demo\\AppData\\Local\\pawwork-updater\\pending") | ||
| }) | ||
|
|
||
| test("resolves Windows pending cache from localappdata casing when present", () => { | ||
| expect( | ||
| pendingUpdateCacheDir({ | ||
| platform: "win32", | ||
| homedir: "C:\\Users\\demo", | ||
| env: { localappdata: "D:\\Cache" }, | ||
| }), | ||
| ).toBe("D:\\Cache\\pawwork-updater\\pending") | ||
| }) | ||
|
|
||
| test("falls back to AppData\\Local on Windows when LOCALAPPDATA is missing", () => { | ||
| expect(pendingUpdateCacheDir({ platform: "win32", homedir: "C:\\Users\\demo", env: {} })).toBe( | ||
| "C:\\Users\\demo\\AppData\\Local\\pawwork-updater\\pending", | ||
| ) | ||
| }) | ||
|
|
||
| test("ignores relative Windows cache roots from env", () => { | ||
| expect( | ||
| pendingUpdateCacheDir({ | ||
| platform: "win32", | ||
| homedir: "C:\\Users\\demo", | ||
| env: { LOCALAPPDATA: "relative-cache" }, | ||
| }), | ||
| ).toBe("C:\\Users\\demo\\AppData\\Local\\pawwork-updater\\pending") | ||
| }) | ||
|
|
||
| test("uses lowercase Windows cache root when uppercase env is relative", () => { | ||
| expect( | ||
| pendingUpdateCacheDir({ | ||
| platform: "win32", | ||
| homedir: "C:\\Users\\demo", | ||
| env: { LOCALAPPDATA: "relative-cache", localappdata: "D:\\Cache" }, | ||
| }), | ||
| ).toBe("D:\\Cache\\pawwork-updater\\pending") | ||
| }) | ||
|
|
||
| test("resolves Linux pending cache from XDG_CACHE_HOME when present", () => { | ||
| expect( | ||
| pendingUpdateCacheDir({ platform: "linux", homedir: "/home/demo", env: { XDG_CACHE_HOME: "/tmp/cache" } }), | ||
| ).toBe("/tmp/cache/pawwork-updater/pending") | ||
| }) | ||
|
|
||
| test("falls back to ~/.cache on Linux when XDG_CACHE_HOME is missing", () => { | ||
| expect(pendingUpdateCacheDir({ platform: "linux", homedir: "/home/demo", env: {} })).toBe( | ||
| "/home/demo/.cache/pawwork-updater/pending", | ||
| ) | ||
| }) | ||
|
Astro-Han marked this conversation as resolved.
|
||
|
|
||
| test("ignores relative Linux cache roots from env", () => { | ||
| expect( | ||
| pendingUpdateCacheDir({ platform: "linux", homedir: "/home/demo", env: { XDG_CACHE_HOME: "relative-cache" } }), | ||
| ).toBe("/home/demo/.cache/pawwork-updater/pending") | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { homedir as currentHomedir } from "node:os" | ||
| import path from "node:path" | ||
|
|
||
| export const UPDATER_CACHE_DIR_NAME = "pawwork-updater" | ||
|
|
||
| type CacheInput = { | ||
| platform?: NodeJS.Platform | ||
| homedir?: string | ||
| env?: NodeJS.ProcessEnv | ||
| } | ||
|
|
||
| function pathForPlatform(platform: NodeJS.Platform) { | ||
| return platform === "win32" ? path.win32 : path.posix | ||
| } | ||
|
|
||
| function firstAbsolutePath( | ||
| platformPath: typeof path.posix | typeof path.win32, | ||
| fallback: string, | ||
| ...values: Array<string | undefined> | ||
| ) { | ||
| return values.find((value) => value && platformPath.isAbsolute(value)) ?? fallback | ||
| } | ||
|
|
||
| export function getAppCacheDir(input: CacheInput = {}) { | ||
| const platform = input.platform ?? process.platform | ||
| const homedir = input.homedir ?? currentHomedir() | ||
| const env = input.env ?? process.env | ||
| const platformPath = pathForPlatform(platform) | ||
|
|
||
| if (platform === "win32") | ||
| return firstAbsolutePath( | ||
| platformPath, | ||
| platformPath.join(homedir, "AppData", "Local"), | ||
| env.LOCALAPPDATA, | ||
| env.localappdata, | ||
| ) | ||
| if (platform === "darwin") return platformPath.join(homedir, "Library", "Caches") | ||
| return firstAbsolutePath(platformPath, platformPath.join(homedir, ".cache"), env.XDG_CACHE_HOME) | ||
| } | ||
|
|
||
| export function pendingUpdateCacheDir(input: CacheInput = {}) { | ||
| const platform = input.platform ?? process.platform | ||
| return pathForPlatform(platform).join(getAppCacheDir(input), UPDATER_CACHE_DIR_NAME, "pending") | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.