test: sandbox mcp query logs - #520
Conversation
|
Hey @aryansk, thanks for picking this up — and for the careful validation notes in the description. The diagnosis is right, the mechanism is right, and this does close the exact path #518 named: I see, the PR is still not ready for review, however I took a look and I'd like one change before merging, because three files survive. The package still writes to the real home
The reason is that
Repro that doesn't depend on what's already in your home — point go test -c -o /tmp/mcp.test ./internal/mcp/
FAKE=$(mktemp -d)
( cd internal/mcp && env -u XDG_CACHE_HOME -u XDG_DATA_HOME -u XDG_CONFIG_HOME \
-u GORTEX_QUERY_LOG HOME="$FAKE" /tmp/mcp.test -test.count=1 >/dev/null 2>&1 )
find "$FAKE" -type fOn This matters more than "one leftover path", because Suggested change
func TestMain(m *testing.M) {
os.Setenv(profiles.ActiveEnv, profiles.DefaultName)
// NewServer builds the query logger and mounts the global memory store
// eagerly, so the package needs a sandboxed home — redirecting the cache
// alone leaves <DataDir>/memories on the developer's real ~/.gortex.
restore, err := testenv.SandboxProcess()
if err != nil {
fmt.Fprintf(os.Stderr, "internal/mcp: cannot sandbox the test environment: %v\n", err)
os.Exit(1)
}
// query_log.go lets an ambient GORTEX_QUERY_LOG beat CacheDir, so clear it
// and let the logger resolve the sandboxed cache directory.
os.Unsetenv("GORTEX_QUERY_LOG")
code := m.Run()
restore()
os.Exit(code)
}Imports become Worth keeping the explicit Not this PRFlagging these so the scope stays where you put it — please don't feel obliged to pick them up, I'll file them separately:
One nit
Also note CI hasn't run on this branch yet (no checks reported — fork PRs need a maintainer to approve the workflow), so the description's validation is the only signal we have so far. I'll get the workflows approved so the matrix has a look before this goes in. |
|
Thanks for the detailed review. I updated the package TestMain to use the existing internal/testenv.SandboxProcess(), which redirects HOME/USERPROFILE and all XDG roots so the DataDir-backed global memory store is sandboxed too. I also unset GORTEX_QUERY_LOG so an ambient override cannot bypass the sandbox. The full suite now passes with |
|
@aryansk thank you for your contribution! |
|
Thank you for the kind note and for the careful review. I appreciate the guidance on sandboxing both the cache- and data-backed paths, and I’m glad the final test isolation now covers the full surface. |
Summary
test cache.
values around the package test run.
This prevents concurrent package tests from writing to a developer's real
~/.gortex/cache/query-log.jsonlwhile keeping individual tests free tooverride the logger path with
t.Setenv.Validation
gofmtpassed.git diff --checkpassed.go test ./internal/mcp -run 'Test(QueryLogger|CountFromResultText|FirstStringArg)' -count=1passed.go test ./internal/mcppassed (the first dependency build took about 17minutes; the cached test run completed in about 102 seconds).
Closes #518