Files
magpie/internal/update/install_unknown_test.go
T
yetone 3cabaab54f update: the install-folder check removes only folders it made, and refuses a folder it can't look at
Installable (the Windows install-location dialog's check) walked up from
the chosen folder to the first one exists() said was there, and removed
the topmost one below it with os.RemoveAll. exists() was os.Stat == nil,
so a folder whose Stat failed for any other reason than "not there"
(access denied, a share that doesn't answer) counted as one the check
had made, and was removed with everything in it, up to a drive root.

Now:
- present(p) tells "there", "not there" (fs.ErrNotExist only) and
  unknown apart; statPath is os.Stat, swapped by tests.
- Installable refuses with "can't tell whether X is there" when any
  folder on the way is unknown, before making anything. It makes the
  missing folders one by one with os.Mkdir, records only those it made
  (one made by someone else meanwhile, ErrExist, isn't recorded), and
  removes them deepest first with os.Remove, so nothing it didn't make,
  and no folder that got content, is removed.
- settle's "is magpie.exe there" before installFresh (which renames
  over to) refuses too when the answer is unknown; the download runs on.
- The other exists() callers were checked: RemoveStaleNew skips a
  staged file it can't look at, writeCom moves a magpie.com aside only
  when it is seen there, keepWebView only writes a note, installed()
  and EnsureCompanion remove nothing. Unknown already meant "leave it"
  there, so they keep exists().

Tests (internal/update/install_unknown_test.go): a Stat that answers
permission denied for an existing ancestor holding a file, for the
chosen folder itself holding a file, and for an installed magpie.exe;
each asserts the folder and file survive (checked before the error).
Plus folders made are removed while existing ones with files stay, and
present's three answers.

Run at this tree (parent 80ccaba64):
- mote-run go test -count=1 -timeout 45m ./internal/update ./internal/gui:
  ok update 141s, ok gui 261s.
- gofmt -l internal/: nothing.
- mote-run GOOS=windows go vet ./...: ok. GOOS=linux go vet ./... fails
  in wails' linux files (cgo off when cross-compiling, the same at any
  tree); GOOS=linux go vet -tags nogui ./... (the code standards' form):
  ok. GOOS=windows go build: ok.
- win-run go test -count=1 ./internal/update on xiaomi-gpu: every test
  passes but TestPackagedByDpkg, red the same on origin/main 80ccaba64
  (its magpie:amd64.list fixture is an NTFS stream name), filed as #4603.

Broken by hand: Installable put back to the walk-up + RemoveAll and
settle back to !exists(to), with the injectable stat kept. All three
new tests went red: Tools/notes.txt and magpie/settings.json "is gone",
and magpie.exe held "MZ new download" instead of "MZ installed".
Restored, green.

Not tried: the real Windows dialog against an access-denied folder or a
dead network share (the permission error is injected); a folder getting
content between Mkdir and the cleanup (no hook there, os.Remove refusing
a non-empty folder is relied on); the full go test ./... suite.
2026-11-09 10:41:34 +08:00

151 lines
4.4 KiB
Go

package update
import (
"errors"
"io/fs"
"os"
"path/filepath"
"testing"
)
// denyStat has statPath answer "permission denied" for the paths given,
// as Windows does for a folder the user may not look into, and a share
// that doesn't answer does in its own words: they are there, but Stat
// can't say so.
func denyStat(t *testing.T, paths ...string) {
t.Helper()
was := statPath
t.Cleanup(func() { statPath = was })
statPath = func(p string) (fs.FileInfo, error) {
for _, d := range paths {
if samePath(p, d) {
return nil, &fs.PathError{Op: "stat", Path: p, Err: fs.ErrPermission}
}
}
return os.Stat(p)
}
}
// keeps fails the test unless file is still there holding want.
func keeps(t *testing.T, file, want string) {
t.Helper()
b, err := os.ReadFile(file)
if err != nil {
t.Fatalf("%s is gone: %v", file, err)
}
if string(b) != want {
t.Fatalf("%s holds %q, want %q", file, b, want)
}
}
// A folder on the way to the one checked that can't be looked at is
// unknown, not missing: the check refuses, and the folder and what is in
// it stay. Taken for missing, it was the folder the check had "made",
// and was removed with everything in it.
func TestInstallableKeepsAnAncestorItCantLookAt(t *testing.T) {
newInstallFixture(t)
tmp := t.TempDir()
theirs := filepath.Join(tmp, "Tools")
file := filepath.Join(theirs, "notes.txt")
if err := os.MkdirAll(theirs, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(file, []byte("mine"), 0o644); err != nil {
t.Fatal(err)
}
denyStat(t, theirs)
err := Installable(filepath.Join(theirs, "new", "magpie"))
keeps(t, file, "mine")
if err == nil || !errors.Is(err, fs.ErrPermission) {
t.Fatalf("a folder that can't be looked at: %v", err)
}
if _, err := os.Stat(filepath.Join(theirs, "new")); !errors.Is(err, fs.ErrNotExist) {
t.Fatalf("the check made a folder under one it couldn't look at: %v", err)
}
}
// The folder checked itself, there with the user's files, that can't be
// looked at: refused, and nothing in it removed.
func TestInstallableKeepsTheFolderItCantLookAt(t *testing.T) {
newInstallFixture(t)
tmp := t.TempDir()
dir := filepath.Join(tmp, "magpie")
file := filepath.Join(dir, "settings.json")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(file, []byte("{}"), 0o644); err != nil {
t.Fatal(err)
}
denyStat(t, dir)
err := Installable(dir)
keeps(t, file, "{}")
if err == nil {
t.Fatal("a folder that can't be looked at accepted")
}
}
// Only the folders the check made are removed: every folder that was
// there before stays, with what is in it.
func TestInstallableRemovesOnlyWhatItMade(t *testing.T) {
newInstallFixture(t)
tmp := t.TempDir()
keep := filepath.Join(tmp, "keep.txt")
if err := os.WriteFile(keep, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
if err := Installable(filepath.Join(tmp, "a", "b", "magpie")); err != nil {
t.Fatal(err)
}
keeps(t, keep, "x")
if _, err := os.Stat(filepath.Join(tmp, "a")); !errors.Is(err, fs.ErrNotExist) {
t.Fatalf("the folders made were left: %v", err)
}
b := filepath.Join(tmp, "a", "b")
if err := os.MkdirAll(b, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(b, "user.txt"), []byte("u"), 0o644); err != nil {
t.Fatal(err)
}
if err := Installable(filepath.Join(b, "magpie")); err != nil {
t.Fatal(err)
}
keeps(t, filepath.Join(b, "user.txt"), "u")
if _, err := os.Stat(filepath.Join(b, "magpie")); !errors.Is(err, fs.ErrNotExist) {
t.Fatalf("the folder made was left: %v", err)
}
}
// An installed magpie.exe that can't be looked at isn't moved over by a
// fresh install: settle says it can't tell, and the download runs on.
func TestSettleLeavesAnExeItCantLookAt(t *testing.T) {
f := newInstallFixture(t)
if err := os.MkdirAll(filepath.Dir(f.to), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(f.to, []byte("MZ installed"), 0o755); err != nil {
t.Fatal(err)
}
denyStat(t, f.to)
to, err := settle(f.exe)
keeps(t, f.to, "MZ installed")
if err == nil || to != "" {
t.Fatalf("settle = %q, %v; want an error", to, err)
}
}
func TestPresent(t *testing.T) {
tmp := t.TempDir()
if ok, err := present(tmp); !ok || err != nil {
t.Fatalf("present(there) = %v, %v", ok, err)
}
if ok, err := present(filepath.Join(tmp, "no")); ok || err != nil {
t.Fatalf("present(missing) = %v, %v", ok, err)
}
denyStat(t, tmp)
if ok, err := present(tmp); ok || err == nil {
t.Fatalf("present(unknown) = %v, %v", ok, err)
}
}