Files
yetone 0361189f9e test: every package's tests pass on Windows, and none writes outside its sandbox (the user: 把 Windows 上那几个失败的测试修掉) — on a Windows box the suite failed in most packages, and the root package hung for 25 minutes. Tests set HOME, which os.UserHomeDir ignores on Windows (it reads USERPROFILE), so they read and wrote the runner's own home; each now sets USERPROFILE beside HOME, and Devin's, Goose's, Crush's and Claude Desktop's tests set APPDATA/LOCALAPPDATA, where those keep their files there. said() read the command's output only once it returned, and a pipe holds a few KB on Windows, so magpie model --help blocked forever; it reads while the command writes. Unix permission bits (the gateway key store's 0600 too), exec bits and symlinks Windows lacks are checked only off Windows; TOML strings hold Windows paths as literal strings, JSON as marshalled ones; a URL's path is compared as one. The provider and usage packages run in a home of their own (TestMain, as the gateway's already does), since a CLI's identity kept by a refresh still going when its test ended landed in the runner's ~/.config/magpie — and on a Mac in the user's own. Two product fixes came out of it: imagemcp took a file:///C:/… root or image for the path /C:/…, which doesn't exist on Windows, and now makes a Windows path of it (a host for a UNC share); and session discovery could keep a new Cowork session hidden on Linux, where a folder's mtime is a kernel tick and its size doesn't change, so an entry added within the tick of the last listing left both as they were — a listing made within 2s of its folder's last change is read again, as git does for racy files. TestFilePath and TestRacyDirectoryListedAgain fail without the change. On Windows 11 every package passes (32 of 32) and a run of each package in a fresh home leaves nothing in home, APPDATA or LOCALAPPDATA; on Linux (WSL) and macOS go test -tags nogui ./... passes. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build pass.
2026-10-01 23:15:07 +08:00

127 lines
3.9 KiB
Go

package update
import (
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
// app makes a stand-in .app holding the text v.
func app(t *testing.T, path, v string) {
t.Helper()
if err := os.MkdirAll(filepath.Join(path, "Contents"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(path, "Contents", "v"), []byte(v), 0o644); err != nil {
t.Fatal(err)
}
}
func version(t *testing.T, path string) string {
t.Helper()
b, err := os.ReadFile(filepath.Join(path, "Contents", "v"))
if err != nil {
t.Fatal(err)
}
return string(b)
}
// A folder magpie may not write to: Install fails with a permission error,
// leaves the app as it was, and keeps what it staged.
func TestInstallNeedsAdmin(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("a read-only folder and the administrator's password are a Mac's and Linux's")
}
if os.Getuid() == 0 {
t.Skip("root writes anywhere")
}
root := t.TempDir()
apps := filepath.Join(root, "Applications")
bundle := filepath.Join(apps, "magpie.app")
app(t, bundle, "old")
staged := filepath.Join(root, "cache", "app", "magpie.app")
app(t, staged, "new")
os.Chmod(apps, 0o555)
defer os.Chmod(apps, 0o755)
if stageDir(apps) == filepath.Join(apps, ".magpie-update") {
t.Error("stageDir picked a folder it can't write to")
}
err := Install(staged, bundle)
if !NeedsAdmin(err) {
t.Fatalf("Install = %v, want a permission error", err)
}
if version(t, bundle) != "old" || version(t, staged) != "new" {
t.Error("a failed Install changed something")
}
}
// The script InstallAsAdmin runs as root swaps the apps, awkward names and
// all; run here as the user in a folder they own.
func TestSwapScript(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the swap runs as root under /bin/sh, on a Mac or Linux")
}
dir := filepath.Join(t.TempDir(), `it's "a" \ $(dir) `)
bundle, staged, old := filepath.Join(dir, "magpie.app"), filepath.Join(dir, "s", "magpie.app"), filepath.Join(dir, "s", "old.app")
app(t, bundle, "old")
app(t, staged, "new")
if b, err := exec.Command("/bin/sh", "-c", swapScript(staged, bundle, old)).CombinedOutput(); err != nil {
t.Fatalf("%v: %s", err, b)
}
if version(t, bundle) != "new" {
t.Error("the new app isn't in place")
}
if _, err := os.Stat(old); !os.IsNotExist(err) {
t.Error("the old app was left behind")
}
// a failed second move puts the app back
os.RemoveAll(staged)
if exec.Command("/bin/sh", "-c", swapScript(staged, bundle, old)).Run() == nil {
t.Error("the swap succeeded without a staged app")
}
if version(t, bundle) != "new" {
t.Error("the app wasn't put back")
}
}
// On a Mac the script goes to osascript as an AppleScript string, and a
// dismissed prompt is ErrCanceled.
func TestAsAdmin(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("osascript")
}
defer func(r func(string, ...string) ([]byte, error)) { run = r }(run)
var got []string
run = func(name string, args ...string) ([]byte, error) {
got = append([]string{name}, args...)
return []byte("execution error: User canceled. (-128)"), errors.New("exit status 1")
}
script := swapScript(`/a "b"/s.app`, `/x\y.app`, "/o.app")
if err := asAdmin(script); !errors.Is(err, ErrCanceled) {
t.Errorf("asAdmin = %v, want ErrCanceled", err)
}
if len(got) != 3 || got[0] != "osascript" || !strings.Contains(got[2], `"`+appleString(script)+`"`) || !strings.HasSuffix(got[2], "with administrator privileges") {
t.Errorf("ran %q", got)
}
// AppleScript unescapes it back to the script
out, err := exec.Command("osascript", "-e", `"`+appleString(script)+`"`).Output()
if err != nil || strings.TrimSuffix(string(out), "\n") != script {
t.Errorf("AppleScript read %q (%v), want %q", out, err, script)
}
}
func TestStuck(t *testing.T) {
if Stuck("/private/var/folders/xy/T/AppTranslocation/1234/d/magpie.app") != "translocated" {
t.Error("a translocated app isn't stuck")
}
if Stuck(t.TempDir()) != "" {
t.Error("a writable folder is stuck")
}
}