Files
magpie/internal/update/com_test.go
T
yetone 482e883142 update: a Windows magpie on the Desktop leaves no magpie.exe.new or magpie.com there
nianshou5 on Discord (#help, Windows): magpie.com and magpie.exe.new
showed on the Desktop and came back after being deleted. Both were
written beside magpie.exe:

- magpie.exe.new is the update the GUI downloads as soon as it is out
  and swaps in at the next restart. StageBinary put it beside the
  program whenever that folder was writable, so it sat on the Desktop
  until a restart, and a deleted one was downloaded again by the next
  check.
- magpie.com (#3737) is written at every start while the app's folder
  is on PATH, and Settings > Command line's Add puts that folder on
  PATH, so deleting it on the Desktop lasted until the next start.

Now:

- StageBinary downloads into magpie's cache, in a folder of this
  program's own (update/<hash of the exe path>), when a rename from
  there reaches the program's folder (renamesInto, a real probe: the
  swap must stay one rename on one volume). With the cache on another
  volume it goes into a hidden .magpie-update folder beside the program
  (FILE_ATTRIBUTE_HIDDEN on Windows). A folder magpie may not write is
  unchanged (cache, moved in as administrator). The stage folder is
  removed after the swap or a failed download.
- At start RemoveStaleNew looks in every place a download may be,
  magpie.exe.new beside the program included, and removes one no newer
  than the running version (same bytes, or an older file version); a
  newer one stays, as before.
- Settled (the installed copy opened from a Desktop/Downloads one)
  also removes the update downloaded for that one, which nothing will
  put in now.
- In a download folder (Desktop, Downloads, a temp folder, as
  isDownloadFolder already says) a start only keeps an existing
  magpie.com up to date and never writes a missing one; Add still
  writes it. Any other folder on PATH is as before. A Desktop
  magpie.exe that opens the installed one (Settle) already wrote none.

Run:
- Windows (win-run, xiaomi-gpu, sandbox HOME from testenv), a repro
  test with the running test binary standing for magpie.exe on a
  Desktop on PATH: at origin/main 72ed121fd StageBinary staged
  test.test.exe.new beside it, and magpie.com came back after delete +
  start; with this change the download was in
  AppData\Local\magpie\update\<hash>\ and magpie.com stayed gone.
- Windows: a real program (whoami.exe) staged into the cache passed the
  real trial start (canStart) and was swapped in for a Desktop
  magpie.exe with the real moveAside/moveIn; the stage folder was gone
  after.
- At the pushed head (rebased on d6beb9975): go test ./internal/update
  on mote and on Windows: ok. Before the rebase also on Linux (wsl-run). gofmt, go vet ./internal/update (darwin, linux, windows),
  GOOS=windows go vet ./internal/gui ./internal/cli: clean.
- go test ./internal/gui ./internal/cli on Windows: TestUpdateThatWontStart,
  TestLodyImportHandoff, TestImportStatusUnreadableIsUnknown,
  TestAionUiImportRow (#4115), TestGatewayHeldByAContainer (#2107,
  #3649) and TestLibraryMCPHub (#4140) red, the same 6 when run at
  origin/main 72ed121fd.
- Full suite on mote (-timeout 45m): started at the pre-rebase tree
  (same update changes, base 72ed121fd) and still running at push; its
  result and any red go in a follow-up.

Broken by hand: staging back beside the exe, the download-folder rule
in EnsureCompanion off, removeStaged out of Settled: the new tests
(TestUpdateStagedOutOfTheProgramsFolder, TestUpdateStagedHiddenWithoutTheCache,
TestStartClearsAnOlderDownloadBeside, TestNoCompanionPutBackOnTheDesktop,
TestSettledClearsOnlyMagpiesFiles, TestNoCompanionBesideAForwardingDownload)
went red.

Not tried: a real GUI self-update of a release build on the Windows
desktop, a cache and Desktop on separate real volumes (the hidden
fallback was tested with no cache), the reporter's own machine, and a
real terminal's `magpie` (unchanged code outside download folders).
2026-10-31 00:21:26 +08:00

143 lines
4.8 KiB
Go

package update
import (
"bytes"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
// The launcher reads the name comText writes with its own copy of the
// marker (launcher/args.go): the two are one.
func TestComMagicIsTheLaunchers(t *testing.T) {
b, err := os.ReadFile(filepath.Join("launcher", "args.go"))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(b), `const magic = "\x00magpie-com-target"`) || comMagic != "\x00magpie-com-target" {
t.Fatal("magpie.com's marker differs between the app and the launcher")
}
}
// #3737: a shell doesn't wait for the GUI-linked magpie.exe, so magpie
// writes magpie.com (run first by PATHEXT) beside it, naming the app it
// runs; written once, again only when its bytes differ (an update).
func TestWriteCom(t *testing.T) {
dir := t.TempDir()
exe := filepath.Join(dir, "magpie-windows-amd64.exe")
if err := os.WriteFile(exe, []byte("MZ"), 0o755); err != nil {
t.Fatal(err)
}
launcher := []byte("MZ launcher v1")
p, err := writeCom(launcher, exe)
if err != nil || p != filepath.Join(dir, "magpie.com") {
t.Fatalf("writeCom: %q, %v", p, err)
}
b, _ := os.ReadFile(p)
if !bytes.HasPrefix(b, launcher) || comTarget(b) != "magpie-windows-amd64.exe" {
t.Fatalf("magpie.com: %q", b)
}
if !comRuns(p, exe) || comRuns(p, filepath.Join(dir, "magpie.exe")) {
t.Fatal("magpie.com not read as running this app alone")
}
st1, _ := os.Stat(p)
if _, err := writeCom(launcher, exe); err != nil {
t.Fatal(err)
}
if st2, _ := os.Stat(p); !os.SameFile(st1, st2) {
t.Fatal("an up-to-date magpie.com written again")
}
// the next version's launcher
if _, err := writeCom([]byte("MZ launcher v2"), exe); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(p); !bytes.HasPrefix(b, []byte("MZ launcher v2")) {
t.Fatalf("magpie.com not brought up to date: %q", b)
}
// one moved aside while it ran goes on the next run
if err := os.WriteFile(p+".old", []byte("MZ launcher v1"), 0o755); err != nil {
t.Fatal(err)
}
if _, err := writeCom([]byte("MZ launcher v2"), exe); err != nil || exists(p+".old") {
t.Fatalf("magpie.com.old left: %v", err)
}
if left, _ := filepath.Glob(filepath.Join(dir, ".magpie-com-*")); len(left) > 0 {
t.Fatalf("temp files left: %q", left)
}
}
func TestComOwner(t *testing.T) {
dir := t.TempDir()
dl := filepath.Join(dir, "magpie-windows-amd64.exe")
if !comOwner(dl) {
t.Fatal("a lone download doesn't own magpie.com")
}
if err := os.WriteFile(filepath.Join(dir, "magpie.exe"), []byte("MZ"), 0o755); err != nil {
t.Fatal(err)
}
if comOwner(dl) {
t.Fatal("a copy beside magpie.exe takes `magpie` from it")
}
if !comOwner(filepath.Join(dir, "Magpie.exe")) {
t.Fatal("magpie.exe doesn't own its magpie.com")
}
}
// EnsureCompanion writes where a terminal runs magpie from, and nowhere
// else: a downloads folder off PATH gets nothing.
func TestEnsureCompanion(t *testing.T) {
old, wasDL := comLauncher, downloadFolder
t.Cleanup(func() { comLauncher, downloadFolder = old, wasDL })
// on Windows a temporary folder counts as a download folder, which
// gets one only from Add (TestNoCompanionPutBackOnTheDesktop)
downloadFolder = func(string) bool { return false }
sep := string(os.PathListSeparator)
comLauncher = nil
dir := t.TempDir()
exe := filepath.Join(dir, "magpie.exe")
t.Setenv("PATH", dir)
if err := EnsureCompanion(exe); err != nil || exists(filepath.Join(dir, comName)) {
t.Fatalf("a build with no launcher wrote magpie.com: %v", err)
}
comLauncher = []byte("MZ launcher")
off := t.TempDir()
t.Setenv("PATH", "/x"+sep+dir+string(filepath.Separator))
if err := EnsureCompanion(filepath.Join(off, "magpie.exe")); err != nil || exists(filepath.Join(off, comName)) {
t.Fatalf("magpie.com written off PATH: %v", err)
}
if err := EnsureCompanion(exe); err != nil || !comRuns(filepath.Join(dir, comName), exe) {
t.Fatalf("magpie.com not written in a folder on PATH: %v", err)
}
// off PATH, but a magpie.cmd says a terminal runs it from there
shim := t.TempDir()
dl := filepath.Join(shim, "magpie-windows-arm64.exe")
if err := os.WriteFile(filepath.Join(shim, shimName), []byte("@echo off\r\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := EnsureCompanion(dl); err != nil || !comRuns(filepath.Join(shim, comName), dl) {
t.Fatalf("magpie.com not written beside a magpie.cmd: %v", err)
}
}
// findCLI names the program a shell runs: magpie.com, before magpie.exe
// in the same folder.
func TestFindCLIPrefersCom(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("PATHEXT is Windows'")
}
dir := t.TempDir()
for _, n := range []string{"magpie.exe", "magpie.com"} {
if err := os.WriteFile(filepath.Join(dir, n), []byte("MZ"), 0o755); err != nil {
t.Fatal(err)
}
}
if got := findCLI([]string{dir}); !strings.EqualFold(got, filepath.Join(dir, "magpie.com")) {
t.Fatalf("findCLI: %q", got)
}
}