From adad07cfd12f44c28903f449b111588a45a2efbc Mon Sep 17 00:00:00 2001 From: Vasanthdev2004 Date: Fri, 20 Nov 2026 20:02:27 +0530 Subject: [PATCH 1/2] test(cli): isolate the doctor tests from the real per-user state On Windows `zero doctor` validates the sandbox setup marker for the directory it runs in, and building the expected marker saves a capability SID for a directory the sandbox home has not seen. Six doctor tests ran it from a fresh temp directory with the per-user roots left alone, so every run of the suite appended six entries to the developer's real windows-cap-sids.json. Call isolateCLIUserState in the seven tests that run the top-level doctor command, and add a guard that parses the package's test files and fails when a function passes a doctor argument list without it. Part of #1233. Outside tests the doctor command still saves an entry for a new directory; that half is left for a follow-up. --- internal/cli/doctor_isolation_test.go | 99 +++++++++++++++++++++++++++ internal/cli/observability_test.go | 7 ++ 2 files changed, 106 insertions(+) create mode 100644 internal/cli/doctor_isolation_test.go diff --git a/internal/cli/doctor_isolation_test.go b/internal/cli/doctor_isolation_test.go new file mode 100644 index 000000000..a083805ef --- /dev/null +++ b/internal/cli/doctor_isolation_test.go @@ -0,0 +1,99 @@ +package cli + +import ( + "go/ast" + "go/parser" + "go/token" + "os" + "sort" + "strconv" + "strings" + "testing" +) + +// THE DOCTOR COMMAND WRITES PER-USER STATE, SO EVERY TEST THAT RUNS IT ISOLATES. +// +// On Windows `zero doctor` resolves the sandbox home from the process +// environment and validates the setup marker for the directory it runs in. +// Building the expected marker asks for that directory's capability SID, and a +// directory the sandbox home has not seen gets one generated and saved. A test +// that runs doctor from a fresh temp directory without redirecting the +// per-user roots therefore appended one entry to the developer's real +// windows-cap-sids.json on every run: six tests here did, and a machine that +// ran this suite a few hundred times carried thousands of them. +// +// isolateCLIUserState moves every per-user root under the test's own temp +// directory, which is where that file then lands. This guard reads the test +// sources instead of watching the real file, because a test must not depend +// on, or look at, the developer's home to pass. +func TestDoctorTestsIsolateUserState(t *testing.T) { + entries, err := os.ReadDir(".") + if err != nil { + t.Fatal(err) + } + fileSet := token.NewFileSet() + runsDoctor := 0 + var missing []string + for _, entry := range entries { + name := entry.Name() + if entry.IsDir() || !strings.HasSuffix(name, "_test.go") { + continue + } + file, err := parser.ParseFile(fileSet, name, nil, 0) + if err != nil { + t.Fatalf("parse %s: %v", name, err) + } + for _, declaration := range file.Decls { + function, ok := declaration.(*ast.FuncDecl) + if !ok || function.Body == nil { + continue + } + doctor, isolated := false, false + ast.Inspect(function.Body, func(node ast.Node) bool { + switch typed := node.(type) { + case *ast.CompositeLit: + if startsWithDoctor(typed) { + doctor = true + } + case *ast.CallExpr: + if callee, ok := typed.Fun.(*ast.Ident); ok && callee.Name == "isolateCLIUserState" { + isolated = true + } + } + return true + }) + if !doctor { + continue + } + runsDoctor++ + if !isolated { + missing = append(missing, name+": "+function.Name.Name) + } + } + } + // A guard that finds nothing to check proves nothing: the argument shape it + // looks for may have changed. + if runsDoctor == 0 { + t.Fatal("SETUP INVALID: found no test that passes a doctor argument list; update startsWithDoctor") + } + if len(missing) > 0 { + sort.Strings(missing) + t.Fatalf("%d of %d functions run `zero doctor` without isolateCLIUserState, so they write the developer's real per-user state:\n %s", + len(missing), runsDoctor, strings.Join(missing, "\n ")) + } +} + +// startsWithDoctor reports an argument list whose first word is the top-level +// doctor command: []string{"doctor", ...}, or a row of a [][]string table. +// "backends doctor" is a different command and starts with another word. +func startsWithDoctor(literal *ast.CompositeLit) bool { + if len(literal.Elts) == 0 { + return false + } + first, ok := literal.Elts[0].(*ast.BasicLit) + if !ok || first.Kind != token.STRING { + return false + } + value, err := strconv.Unquote(first.Value) + return err == nil && value == "doctor" +} diff --git a/internal/cli/observability_test.go b/internal/cli/observability_test.go index 24bdeff92..5167a599a 100644 --- a/internal/cli/observability_test.go +++ b/internal/cli/observability_test.go @@ -20,6 +20,7 @@ import ( ) func TestRunDoctorFormatsRedactedProviderDiagnostics(t *testing.T) { + isolateCLIUserState(t) var stdout bytes.Buffer var stderr bytes.Buffer cwd := t.TempDir() @@ -63,6 +64,7 @@ func TestRunDoctorFormatsRedactedProviderDiagnostics(t *testing.T) { } func TestRunDoctorConnectivityProbesProvider(t *testing.T) { + isolateCLIUserState(t) var stdout bytes.Buffer var stderr bytes.Buffer cwd := t.TempDir() @@ -113,6 +115,7 @@ func TestRunDoctorConnectivityProbesProvider(t *testing.T) { } func TestRunDoctorConnectivityUsesOAuthLogin(t *testing.T) { + isolateCLIUserState(t) tokenPath := filepath.Join(t.TempDir(), "oauth-tokens.json") t.Setenv("XDG_CONFIG_HOME", t.TempDir()) t.Setenv("ZERO_OAUTH_STORAGE", "file") @@ -172,6 +175,7 @@ func TestRunDoctorConnectivityUsesOAuthLogin(t *testing.T) { } func TestRunDoctorConnectivityReportsProviderHealthFailure(t *testing.T) { + isolateCLIUserState(t) var stdout bytes.Buffer var stderr bytes.Buffer cwd := t.TempDir() @@ -211,6 +215,7 @@ func TestRunDoctorConnectivityReportsProviderHealthFailure(t *testing.T) { } func TestRunDoctorConnectivityDoesNotMaskConfigHealthFailure(t *testing.T) { + isolateCLIUserState(t) var stdout bytes.Buffer var stderr bytes.Buffer cwd := t.TempDir() @@ -243,6 +248,7 @@ func TestRunDoctorConnectivityDoesNotMaskConfigHealthFailure(t *testing.T) { } func TestRunDoctorJSONReturnsFailureForMissingProvider(t *testing.T) { + isolateCLIUserState(t) var stdout bytes.Buffer var stderr bytes.Buffer @@ -429,6 +435,7 @@ func TestRunSearchJSONRedactsQueryAndSessionMetadata(t *testing.T) { } func TestRunDoctorReportsConfigValidationForMalformedFile(t *testing.T) { + isolateCLIUserState(t) cwd := t.TempDir() zeroDir := filepath.Join(cwd, ".zero") if err := os.MkdirAll(zeroDir, 0o755); err != nil { From 334f43ab26be969ac78b7ec9a37f15527b49bf65 Mon Sep 17 00:00:00 2001 From: Vasanthdev2004 Date: Mon, 23 Nov 2026 17:15:50 +0530 Subject: [PATCH 2/2] test(cli): clear the sandbox overrides in the isolation helper ZERO_SANDBOX_GRANTS_PATH and ZERO_WINDOWS_SANDBOX_HOME outrank the base directories isolateCLIUserState redirects, so an inherited value still sent the doctor tests to the developer's real sandbox home. Clear both in the helper, and pin that the two resolvers land under the fixture root when both were set beforehand. --- internal/cli/doctor_isolation_test.go | 37 +++++++++++++++++++++++ internal/cli/user_state_isolation_test.go | 6 ++++ 2 files changed, 43 insertions(+) diff --git a/internal/cli/doctor_isolation_test.go b/internal/cli/doctor_isolation_test.go index a083805ef..5379250b9 100644 --- a/internal/cli/doctor_isolation_test.go +++ b/internal/cli/doctor_isolation_test.go @@ -5,12 +5,49 @@ import ( "go/parser" "go/token" "os" + "path/filepath" "sort" "strconv" "strings" "testing" + + "github.com/Twigpine/zero/internal/sandbox" ) +// AN INHERITED OVERRIDE DOES NOT REACH AROUND THE ISOLATION. +// +// ZERO_SANDBOX_GRANTS_PATH and ZERO_WINDOWS_SANDBOX_HOME outrank the base +// directories the helper redirects. A developer who has either set would +// otherwise have the doctor tests read the setup marker from, and save +// capability entries into, their real sandbox home, and get results that +// depend on that machine. Both resolvers must land under the fixture root +// whatever the test process inherited. +func TestIsolateCLIUserStateNeutralizesSandboxOverrides(t *testing.T) { + outside := t.TempDir() + t.Setenv("ZERO_SANDBOX_GRANTS_PATH", filepath.Join(outside, "grants.json")) + t.Setenv("ZERO_WINDOWS_SANDBOX_HOME", filepath.Join(outside, "sandbox-home")) + + isolateCLIUserState(t) + + fixtureRoot := filepath.Clean(os.Getenv("XDG_CONFIG_HOME")) + if fixtureRoot == "." || fixtureRoot == filepath.Clean(outside) { + t.Fatalf("SETUP INVALID: the helper left XDG_CONFIG_HOME at %q", fixtureRoot) + } + grants, err := sandbox.ResolveGrantPath(nil) + if err != nil { + t.Fatal(err) + } + home, err := sandbox.ResolveWindowsSandboxHome(nil) + if err != nil { + t.Fatal(err) + } + for name, resolved := range map[string]string{"sandbox grants path": grants, "Windows sandbox home": home} { + if !strings.HasPrefix(filepath.Clean(resolved), fixtureRoot+string(os.PathSeparator)) { + t.Errorf("%s = %s, want it under the fixture root %s; an inherited override still decides it", name, resolved, fixtureRoot) + } + } +} + // THE DOCTOR COMMAND WRITES PER-USER STATE, SO EVERY TEST THAT RUNS IT ISOLATES. // // On Windows `zero doctor` resolves the sandbox home from the process diff --git a/internal/cli/user_state_isolation_test.go b/internal/cli/user_state_isolation_test.go index 39cd3ea44..38a873809 100644 --- a/internal/cli/user_state_isolation_test.go +++ b/internal/cli/user_state_isolation_test.go @@ -42,6 +42,12 @@ func isolateCLIUserState(t *testing.T) { // Credentials resolve keyring-first. This keeps a migrated key in a file // under the fixture root rather than in the developer's OS keychain. t.Setenv("ZERO_CRED_STORAGE", "encrypted-file") + // Two overrides outrank every base directory above. An inherited value + // would send the sandbox grants file and, on Windows, the sandbox home + // straight back to the developer's real ones. Cleared, both resolve under + // the fixture root like everything else. + t.Setenv("ZERO_SANDBOX_GRANTS_PATH", "") + t.Setenv("ZERO_WINDOWS_SANDBOX_HOME", "") } // AND THE ISOLATION IS ITSELF PINNED, ONE HELPER AT A TIME. A seeded config