Files
magpie/internal/testenv/testenv_test.go
T
JoeyWu-tech 8cf9dbe6bd testenv: the test home holds XDG_CONFIG_DIRS too (#3372)
Isolate pointed HOME, USERPROFILE, the XDG_*_HOME folders, APPDATA and
LOCALAPPDATA into the temporary home, and left XDG_CONFIG_DIRS as the
machine has it. The next commit has Open at login on Linux read a
package's autostart entry in $XDG_CONFIG_DIRS/autostart, /etc/xdg when
it is unset or empty (#3372), and the gui's settingsState reads Open at
login (api.go's loginState) in many tests. On a machine with a package's
magpie.desktop (NixOS, or a distro's package some day) their s.Login
would follow that entry, and a test switching it would write a hiding
entry where it expects none. LESSONS has the rule: give the sandbox
every variable the code reads (ee7a9b24a).

Now Isolate points XDG_CONFIG_DIRS at <home>/etc/xdg, which nothing
makes, so a test reads no system entry unless it writes one there. Every
test package that reaches internal/autostart (autostart, cli, daemon,
gui and tui) isolates through testenv. It isn't one of folderVars:
checkChild checks each of those as one path in the temporary folder, and
XDG_CONFIG_DIRS is a list.

Rebased onto origin/main 58fb3a77 with the next commit; this one is
as it was.

What was run (tree 2da4f92a, on origin/main 58fb3a77, go1.26.8,
linux/amd64, Linux 5.15), outside graftcp, umask 077:
- This commit's tree alone: not run. At the next commit's tree
  (59fb33a8), go test ./internal/testenv -v: TestIsolate ok, and all
  but TestProgram (umask 077, #3640); the full suite there is in the
  next commit.

The rest is what was run for 2d17d5e8, at tree efb86cbc on origin/main
6e6aba51, the same code (not this tree):
- TestIsolate sets XDG_CONFIG_DIRS to /etc/xdg before Isolate, and now
  checks that it lies in the test home after. With the new line taken
  out of Isolate by hand it went red, testenv_test.go:65:
  XDG_CONFIG_DIRS is "/etc/xdg", outside
  "/tmp/magpie-test-home-1706789611"; put back, it passed.
- The testenv package through pkgtest.sh, outside graftcp: all pass but
  TestProgram, red under this box's umask 077 (#3640), as it is when run
  alone at origin/main 6e6aba51. No -race: the change sets one more
  variable before any test starts.
- Full suite, -tags nogui (CI's Linux job tags gtk3), outside graftcp
  (systemd-run, umask 077, Bun 1.4.2 first on PATH as on CI, -timeout
  45m, log kept): 64 packages ok and 8 packages red, each filed: #3640
  (umask 077: testenv's TestProgram, as above, and edit's
  TestWriteAtomicReadOnlyHardLink), #3141 (TestInstalledVersion,
  TestCursorFolderFromTheEditorIsNotTheCLI,
  TestCopilotFolderFromAnIDEPluginIsNotTheCLI,
  TestQoderSettingsFromAnIDEPluginIsNotTheCLI,
  TestOmpDetectedWithPiDir), #3398 (TestCodexDriftReached), #3641 (the
  two Reasonix tests, this box's /tmp/.git), #3420 (davsync's TestSync),
  #3675 (TestPluginToldWhereTheAgentWorks, this box's /tmp/.git), #3649
  (TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie, this
  box's open fds), #3741, and provider's synctest fatal (#3632) in
  TestUnreadLoginsWriteErrorsKeepCredentials, after which that package's
  tests didn't run.
- gofmt -l: clean. go vet -tags nogui for linux, windows and darwin,
  GOOS=windows go vet ./..., GOOS=freebsd go vet ./internal/testenv/, go
  build -tags nogui for linux and darwin and GOOS=windows go build
  ./...: all clean. Untagged linux vet and build stop at pkg-config here
  (no GTK headers on this box).

Not changed: internal/gateway isolates itself in its own TestMain, not
through testenv, and doesn't set XDG_CONFIG_DIRS; none of its tests
reach internal/autostart.

Not tried: a machine with a package's magpie.desktop in
/etc/xdg/autostart (this box has none), where without this the gui's
tests would read it once the next commit is in.

Batch run, at tree 104d75f0a (the twelve coffemon ports together, on
origin/main 7d022bdb6), mote, macOS, go1.26.8:
- go test -tags nogui -count=1 -timeout 45m on the packages the batch
  changes (agent, autostart, daemon, gateway, library, proc, provider,
  sessions, testenv, tui, gui): ok each.
- GUI, node --test with Playwright 1.62.0, Chromium and WebKit:
  library-*.test.cjs and i18n-keys.test.cjs, 1044 of 1044 pass.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... and go build ./...: ok at cdd7c8e46, the same
  batch on origin/main 00098f12b one rebase before.
- The whole go test ./... at cdd7c8e46: 86 packages ok, one red,
  internal/cli TestEveryRouteHasACommandOrIsNamed (GET
  /api/context/reply has no command), red on main too (#5026).
- Linux (wsl-run, Ubuntu 24.04, at 339055896, the batch on f4c664a09):
  proc, autostart, testenv, tui ok; agent: only TestGrokInstalled red
  (#3548); #4033's proc.Look reds (Zed, Copilot, Qoder, Cursor, Omp)
  pass.
- Windows (win-run, Windows 11, at 339055896): proc, autostart, testenv,
  tui ok; library and agent red in the same tests as on origin/main
  f4c664a09 (#4115, #1827, #4786; #5068, #5069, #5070 filed now).
- Rebased onto origin/main fb1f4856a (three commits, no file in
  common with the batch) and run again at the pushed tree: gofmt -l
  nothing, go build ./... and go vet ./... ok; go test -tags nogui
  sessions, agent, autostart, tui, proc, testenv ok; i18n-keys and
  library-* 1044 of 1044 pass.
- Not run before the push: the whole GUI suite (a run at cdd7c8e46 was
  cut off at 1170 of ~1174 tests when mote rebooted; it runs again after
  the tag and any red is filed).
2026-11-03 20:38:11 +08:00

132 lines
4.3 KiB
Go

package testenv
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
)
// A stand-in is the program a lookup finds, before an installed one of the
// same name further down PATH, on Windows too, where a file with no
// extension isn't a program: the gateway's tests make theirs with
// StandIns, and read the user's own Codex's version through it (#1827).
func TestStandInsComeBeforeTheInstalled(t *testing.T) {
stand, installed := t.TempDir(), t.TempDir()
if err := StandIns(stand, []string{"codex"}); err != nil {
t.Fatal(err)
}
real := filepath.Join(installed, "codex")
body := "#!/bin/sh\necho codex-cli 0.162.0\n"
if runtime.GOOS == "windows" {
real, body = real+".cmd", "@echo codex-cli 0.162.0\r\n"
}
if err := os.WriteFile(real, []byte(body), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", stand+string(os.PathListSeparator)+installed)
got, err := exec.LookPath("codex")
if err != nil {
t.Fatal(err)
}
if filepath.Dir(got) != stand {
t.Fatalf("codex is %s, not the stand-in in %s", got, stand)
}
if out, err := exec.Command(got).Output(); err == nil || len(out) > 0 {
t.Fatalf("the stand-in ran: %q, %v", out, err)
}
}
func TestIsolate(t *testing.T) {
for _, k := range append(folderVars, "XDG_CONFIG_DIRS", "CODEX_HOME", "PATH", Marker) {
t.Setenv(k, os.Getenv(k)) // restored after the test
}
os.Setenv("CODEX_HOME", filepath.Join(t.TempDir(), "codex"))
// the machine's system folders, where a package keeps its autostart entry
os.Setenv("XDG_CONFIG_DIRS", "/etc/xdg")
t.Setenv("HTTPS_PROXY", "http://127.0.0.1:9") // the machine's own, which net/http would freeze into its first read
home, err := Isolate()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { remove(home) })
for _, k := range proxyVars {
if _, ok := os.LookupEnv(k); ok {
t.Errorf("%s is still set", k)
}
}
for _, f := range []func() (string, error){os.UserHomeDir, os.UserConfigDir, os.UserCacheDir} {
if p, err := f(); err != nil || !within(p, home) {
t.Errorf("%q (%v) is outside %q", p, err, home)
}
}
if p := os.Getenv("XDG_CONFIG_DIRS"); !within(p, home) {
t.Errorf("XDG_CONFIG_DIRS is %q, outside %q", p, home)
}
if _, ok := os.LookupEnv("CODEX_HOME"); ok {
t.Error("CODEX_HOME is still set")
}
remove(home)
if _, err := os.Stat(home); !os.IsNotExist(err) {
t.Errorf("the test home is still there: %v", err)
}
}
// wsl.exe, which magpie runs by that name to list and open the WSL
// distros, is a failing stand-in under Isolate: the distros' homes lie
// outside the test home, and a Windows test read the real one's Codex
// sessions (#4177).
func TestIsolateStandsInForWSL(t *testing.T) {
for _, k := range append(folderVars, "PATH", Marker) {
t.Setenv(k, os.Getenv(k)) // restored after the test
}
home, err := Isolate()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { remove(home) })
got, err := exec.LookPath("wsl.exe")
if err != nil || !within(got, home) {
t.Fatalf("wsl.exe is %q (%v), not a stand-in in %s", got, err, home)
}
if out, err := exec.Command(got, "-l", "-q").Output(); err == nil || len(out) > 0 {
t.Fatalf("the stand-in listed distros: %q, %v", out, err)
}
}
// A binary started again by a test keeps that test's homes, all in the
// temporary folder; one pointing anywhere else is refused.
func TestCheckChild(t *testing.T) {
sandbox, fixture := t.TempDir(), t.TempDir()
for _, k := range append(folderVars, "CODEX_HOME") {
t.Setenv(k, "")
}
SetHome(t, fixture)
t.Setenv("CODEX_HOME", filepath.Join(fixture, ".codex"))
if err := checkChild(sandbox); err != nil {
t.Errorf("a fixture home in the temporary folder: %v", err)
}
outside, _ := filepath.Abs(filepath.Join(filepath.VolumeName(sandbox)+string(filepath.Separator), "not-temp", "home"))
t.Setenv("CODEX_HOME", outside)
if err := checkChild(sandbox); err == nil {
t.Error("CODEX_HOME outside the temporary folder passed")
}
t.Setenv("CODEX_HOME", "")
if err := checkChild(outside); err == nil {
t.Error("a sandbox outside the temporary folder passed")
}
}
func TestRemoveTouchesOnlyItsOwn(t *testing.T) {
dir := t.TempDir()
other := filepath.Join(dir, prefix+"x")
os.MkdirAll(other, 0o755)
remove(other) // not in os.TempDir() itself
remove("")
remove(".")
if _, err := os.Stat(other); err != nil {
t.Errorf("remove deleted a folder it didn't make: %v", err)
}
}