diff --git a/internal/gateway/main_test.go b/internal/gateway/main_test.go index c4b0fcf9a..cb6e2ab24 100644 --- a/internal/gateway/main_test.go +++ b/internal/gateway/main_test.go @@ -7,7 +7,6 @@ import ( "testing" "github.com/yetone/magpie/internal/agentenv" - "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/testenv" ) @@ -77,22 +76,9 @@ func isolatedTests(m *testing.M) (int, error) { // no route is written to disk behind a test's back; the history's own // tests call saveRoute themselves keepRoutes = false - // nor a CLI's identity: a request's look at Cursor's or Devin's account - // asks the stand-in behind it, and the answer was kept in the config - // folder of whichever test was running then, as it was removed (#1524) - provider.KeepCLIIdentities = false // no test asks a vendor: a provider with a real base URL and a made-up // key would, in the background (a plan key's windows, #1016), and a // plugin host's first start fetched models.dev testenv.Offline() return m.Run(), nil } - -// The package's tests keep no CLI identity on disk: an ask runs behind the -// request that started it, and one still going when its test ended wrote -// cli-identity.json into the config folder being removed (#1524). -func TestNoCLIIdentityKeptInTests(t *testing.T) { - if provider.KeepCLIIdentities { - t.Fatal("TestMain left provider.KeepCLIIdentities on") - } -} diff --git a/internal/provider/cli_identity.go b/internal/provider/cli_identity.go index eff99964d..07280819f 100644 --- a/internal/provider/cli_identity.go +++ b/internal/provider/cli_identity.go @@ -46,13 +46,22 @@ type keptIdentity struct { var identityFile sync.Mutex -// KeepCLIIdentities is whether the answers are kept on disk. Off in the -// gateway's tests: an ask runs behind the look that started it, and one -// still going when its test ended (a stand-in CLI slow under load) wrote -// cli-identity.json into the test's config folder as that was being -// removed, failing the test's TempDir cleanup (#1524). Set before the -// tests start, never while asks run. -var KeepCLIIdentities = true +// testSandbox is testenv.Marker, which provider can't import: set in a test +// binary testenv isolated. +const testSandbox = "MAGPIE_TEST_SANDBOX" + +// keepInSandbox has the answers kept on disk in a test binary too, for the +// tests of that file itself. +var keepInSandbox = false + +// keepsIdentities says whether the answers are kept on disk: always, except +// in a test binary testenv isolated. There an ask runs behind the look that +// started it, and one still going when its test ended (a stand-in CLI slow +// under load) wrote cli-identity.json into the config folder of whichever +// test was running then, as that was being removed, failing its TempDir +// cleanup: internal/gateway's (#1524), then internal/agent's (#1538), and +// any package whose tests read the accounts could. +func keepsIdentities() bool { return keepInSandbox || os.Getenv(testSandbox) == "" } func identityPath() string { return filepath.Join(filepath.Dir(Path()), "cli-identity.json") } @@ -65,7 +74,7 @@ func readIdentities() map[string]keptIdentity { } func (c *cliIdentity) keep() { - if !KeepCLIIdentities { + if !keepsIdentities() { return } identityFile.Lock() diff --git a/internal/provider/cli_identity_test.go b/internal/provider/cli_identity_test.go index 937eb185c..d90e4fd4d 100644 --- a/internal/provider/cli_identity_test.go +++ b/internal/provider/cli_identity_test.go @@ -8,8 +8,16 @@ import ( "time" ) +// keepIdentities has this test keep the answers on disk, as magpie does +// outside a test binary. +func keepIdentities(t *testing.T) { + keepInSandbox = true + t.Cleanup(func() { keepInSandbox = false }) +} + func TestCLIIdentityKeptAcrossStarts(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + keepIdentities(t) var asked atomic.Int32 answer := func() (string, string, bool, error) { asked.Add(1); return "me@example.com", "Pro", true, nil } exe := func() string { return "/bin/sh" } @@ -68,6 +76,7 @@ func TestCLIIdentityKeptAcrossStarts(t *testing.T) { func TestCLIIdentityKeptIgnoredWithoutTheCLI(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + keepIdentities(t) (&cliIdentity{name: "x", exe: func() string { return "/bin/sh" }, ask: func() (string, string, bool, error) { return "me@example.com", "", true, nil }}).get() gone := &cliIdentity{name: "x", exe: func() string { return "" }, ask: func() (string, string, bool, error) { return "", "", false, nil }} if u, _, ok := gone.get(); ok || u != "" { @@ -79,6 +88,7 @@ func TestCLIIdentityKeptIgnoredWithoutTheCLI(t *testing.T) { // nobody is signed in until it answers, and then its answer is served func TestCLIIdentityFirstAskBounded(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + keepIdentities(t) old := firstAsk firstAsk = 100 * time.Millisecond t.Cleanup(func() { firstAsk = old }) @@ -117,6 +127,7 @@ func TestCLIIdentityFirstAskBounded(t *testing.T) { // rather than waiting for it again func TestCLIIdentitySignedOutKept(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + keepIdentities(t) (&cliIdentity{name: "x", exe: func() string { return "/bin/sh" }, ask: func() (string, string, bool, error) { return "", "", false, nil }}).get() if _, found := readIdentities()["x"]; !found { t.Fatal("a signed-out answer wasn't kept") @@ -195,19 +206,20 @@ func TestCLIIdentityFailingAskedLessOften(t *testing.T) { } } -// With KeepCLIIdentities off, an answer is served but nothing is written: -// the gateway's tests turn it off, as an ask still going when a test ended -// wrote into the config folder being removed (#1524). -func TestCLIIdentityNotKeptWhenOff(t *testing.T) { +// In a test binary testenv isolated an answer is served but nothing is +// written: an ask still going when a test ended wrote into the config +// folder being removed (#1524, #1538). +func TestCLIIdentityNotKeptInATestBinary(t *testing.T) { + if os.Getenv(testSandbox) == "" { + t.Fatal("provider's tests run without testenv's sandbox") + } dir := t.TempDir() t.Setenv("XDG_CONFIG_HOME", dir) - KeepCLIIdentities = false - t.Cleanup(func() { KeepCLIIdentities = true }) c := &cliIdentity{name: "x", exe: func() string { return "/bin/sh" }, ask: func() (string, string, bool, error) { return "me@example.com", "Pro", true, nil }} if u, _, ok := c.get(); !ok || u != "me@example.com" { t.Fatalf("get: %q %v", u, ok) } if es, _ := os.ReadDir(dir); len(es) != 0 { - t.Fatalf("written with KeepCLIIdentities off: %v", es) + t.Fatalf("written in a test binary: %v", es) } } diff --git a/internal/provider/cursor_status_test.go b/internal/provider/cursor_status_test.go index 920b295e1..cfc514e11 100644 --- a/internal/provider/cursor_status_test.go +++ b/internal/provider/cursor_status_test.go @@ -15,6 +15,7 @@ import ( // sure nobody is signed in still signs it out func TestCLIIdentityUnsureKeepsAnswer(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + keepIdentities(t) c := &cliIdentity{name: "x", exe: func() string { return "/bin/sh" }, ask: func() (string, string, bool, error) { return "me@example.com", "Pro", true, nil }} if _, _, ok := c.get(); !ok { diff --git a/internal/provider/devin_test.go b/internal/provider/devin_test.go index 2ba0d6cd7..8218dfe20 100644 --- a/internal/provider/devin_test.go +++ b/internal/provider/devin_test.go @@ -440,6 +440,7 @@ func TestAskDevinStatus(t *testing.T) { // that says nobody (or keeps no key) drops it, on disk too func TestDevinStatusKeepsTheAccount(t *testing.T) { home := claudeHome(t) + keepIdentities(t) t.Setenv("XDG_DATA_HOME", filepath.Join(home, "data")) exe := filepath.Join(home, "devin") fakeDevin(t, exe)