Files
magpie/internal/update/aside_test.go
T
yetone 88cccc184e update: on Windows, an exe something holds open for a moment still goes aside for the new version, and one held for good says why in full, with the release page a click away (freecss on Discord: "0.1.628 已下载 · couldn't move magpie-windows-amd64.exe aside to put the new version in: rename C:\Us…") — InstallBinary renamed the running exe to .old once and gave up on the first error, which Go prints as "rename <exe> <exe>.old: <reason>", two full paths ahead of the reason, and the version row cut it off on one line, so what failed couldn't be read. A leftover .old was already handled (RemoveOld, then .old-2…, TestInstallBinaryBesideARunningOld), and Windows renames a running image (it is mapped with FILE_SHARE_DELETE), so the likely cause is another program holding the exe open without delete sharing — an antivirus scan, OneDrive or a similar sync client — or a folder that refuses the change (access denied); which one isn't known until the full message is read. moveAside now tries again for about 3 s (0.1, 0.2, 0.4, 0.8, 1.5 s apart), clearing a read-only attribute after the first failure, with a fresh .old name each try; a missing exe isn't retried. When it still can't, the message is the reason without the paths and what to do: ERROR_SHARING_VIOLATION/ERROR_LOCK_VIOLATION says another program has it open (often an antivirus or OneDrive), access denied names the folder Windows won't let magpie change, anything else says to download the new version; the download is kept for another try, as before. The version row wraps a failed install's or download's reason (.sub.wraps) with the whole text as its tooltip, and a ready-but-failed install offers Download, which opens the release page. The Windows test box wasn't reachable from this session (ssh key refused), so nothing ran on Windows; TestInstallBinaryHeldOpen (windows-only: an exe held open without FILE_SHARE_DELETE for 0.5 s still installs; held for good, the error says another program has it open and the download is kept) was cross-compiled with GOOS=windows go test -c but not run. TestMoveAsideTriesAgain and TestMoveAsideSaysWhy (portable, with a fake rename) and the new update-check.test.cjs case (webkit, en and zh: the reason is not cut off, its tooltip is the full text, Download opens the release page, a later state is one line again) fail without the change; Chromium couldn't launch here (its Playwright cache has no ICU data). go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build and vet, go test -tags nogui ./... pass (TestFXInState failed once on a TempDir cleanup race, passes on the rerun; untouched).
2026-10-02 13:26:54 +08:00

80 lines
2.3 KiB
Go

package update
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// fakeRename has renameFile fail with err the first n times, then rename.
func fakeRename(t *testing.T, n int, err error) *int {
calls := 0
waits, rename := asideWaits, renameFile
asideWaits = []time.Duration{time.Millisecond, time.Millisecond, time.Millisecond}
renameFile = func(from, to string) error {
calls++
if calls <= n {
return &os.LinkError{Op: "rename", Old: from, New: to, Err: err}
}
return os.Rename(from, to)
}
t.Cleanup(func() { asideWaits, renameFile = waits, rename })
return &calls
}
// Something holding the running exe open for a moment (an antivirus scan,
// OneDrive) doesn't fail the update: moving it aside is tried again.
func TestMoveAsideTriesAgain(t *testing.T) {
exe := filepath.Join(t.TempDir(), "magpie-windows-amd64.exe")
os.WriteFile(exe, []byte("v1"), 0o755)
calls := fakeRename(t, 2, errSharingViolation)
if err := moveAside(exe); err != nil {
t.Fatal(err)
}
if *calls != 3 {
t.Fatalf("%d tries, want 3", *calls)
}
if b, _ := os.ReadFile(exe + ".old"); string(b) != "v1" {
t.Fatal("not moved to .old")
}
}
// One that keeps it says why and what to do, without the paths the
// version row has no room for.
func TestMoveAsideSaysWhy(t *testing.T) {
exe := filepath.Join(t.TempDir(), "magpie-windows-amd64.exe")
os.WriteFile(exe, []byte("v1"), 0o755)
calls := fakeRename(t, 100, errSharingViolation)
err := moveAside(exe)
if err == nil {
t.Fatal("moved")
}
if *calls != 4 {
t.Fatalf("%d tries, want 4", *calls)
}
msg := err.Error()
for _, want := range []string{"couldn't move magpie-windows-amd64.exe aside", strings.TrimRight(errSharingViolation.Error(), ". 。"), "another program has it open", "download the new version"} {
if !strings.Contains(msg, want) {
t.Errorf("%q lacks %q", msg, want)
}
}
if strings.Contains(msg, exe) {
t.Errorf("%q has the path", msg)
}
// a folder magpie may not change says so
fakeRename(t, 100, os.ErrPermission)
if msg := moveAside(exe).Error(); !strings.Contains(msg, "doesn't let magpie change files in "+filepath.Dir(exe)) {
t.Errorf("%q", msg)
}
// gone is not tried again
calls = fakeRename(t, 100, os.ErrNotExist)
moveAside(exe)
if *calls != 1 {
t.Fatalf("%d tries of a missing exe", *calls)
}
}