Skip to content

test: isolate test suites from the developer's home directory - #5018

Open
PerryLink wants to merge 2 commits into
docker:mainfrom
PerryLink:fix/test-temp-home-isolation
Open

PerryLink wants to merge 2 commits into
docker:mainfrom
PerryLink:fix/test-temp-home-isolation

Conversation

@PerryLink

Copy link
Copy Markdown
Contributor

What

Some test packages still write Docker Agent state into the real home directory
of whoever runs go test ./.... This adds a TestMain using
testutil.RunWithTempHome to every package that was missing one, and a small
guard test so the isolation cannot silently regress.

Fixes #5013. Follow-up to the review on #4999 (the ~/.cagent path work); no
path-resolution code is touched here.

Reproduction

Run each package with HOME pointed at an empty directory and list what
survives the test run (before this change):

Package Left behind
pkg/skills <cache-dir>/skills/<hash>/remote-skill/SKILL.md + .meta
pkg/runtime <cache-dir>/models_dev.json (~7 MB)
pkg/tools/builtin/rag <data-dir>/rag/<hash>/rag_knowledge_chunked_embeddings.db
pkg/telemetry <config-dir>/user-uuid
pkg/model/provider/{base,anthropic,openai} <config-dir>/user-uuid

The issue noted the user-uuid row might be incomplete. It was: running the
whole suite this way also found

  • pkg/fake and pkg/evaluator/provider — gateway clients whose transport
    resolves the install UUID;
  • pkg/model/provider/dmr — same, via the shared provider base;
  • pkg/remote — creates an empty <data-dir>/store directory
    (content.NewStore() with the default base directory).

A full go test ./... left 8 files and 13 directories behind in a throwaway
home.

Changes

  • Add main_test.go with TestMain running
    os.Exit(testutil.RunWithTempHome(m.Run)) to pkg/telemetry,
    pkg/tools/builtin/rag, pkg/model/provider/{base,anthropic,openai,dmr},
    pkg/fake, pkg/evaluator/provider and pkg/remote.
  • Wrap the existing TestMain of pkg/skills and pkg/runtime with
    testutil.RunWithTempHome.
  • Add testutil.RequireTempHome and a TestTempHomeIsolation test to each of
    those packages. It fails when the package stops running under
    RunWithTempHome, so dropping the isolation breaks the normal test run
    instead of silently writing to the developer's home again. That replaces the
    separate "CI check against a throwaway HOME" suggested in the issue with an
    in-suite check that costs no extra test run.

pkg/runtime keeps its explicit paths.SetConfigDir because the prompt-file
guard tests write userconfig.Path(); the temporary home is layered on top.

No production code changes.

Verification

With HOME at an empty directory, go test ./... -count=1:

Result Files left Dirs left
main @ c0062b8a 266 ok, 0 failed 8 13
this branch 266 ok, 0 failed 0 0

Red/green step: the new guard tests fail on main
(tests must run with testutil.RunWithTempHome from TestMain) and pass after
the TestMain changes. Each affected package was also run individually with a
throwaway HOME before/after: residue → none.

Gates on the branch: go build ./..., go vet ./..., go run ./lint .
(3008 files, no offenses), golangci-lint run 2.14.0 (0 issues),
go mod tidy --diff clean, gofmt clean.

Note: pkg/runtime's TestDoCompactAfterHookFires needs jq in PATH
for its hook command; without jq it fails on unmodified main too
(exited with status 127), so it is a local environment gap, not a regression.

Notes

Packages that touch Docker Agent state wrote into the real home directory of
whoever ran `go test ./...`:

- pkg/skills: cached remote skills
- pkg/runtime: models.dev catalog
- pkg/tools/builtin/rag: RAG embeddings database
- pkg/telemetry and pkg/model/provider/{base,anthropic,openai,dmr}: user-uuid
- pkg/fake and pkg/evaluator/provider: user-uuid
- pkg/remote: data-dir store directory

Run those suites under testutil.RunWithTempHome from TestMain, like the
state-touching packages already do. Add testutil.RequireTempHome and a guard
test to every affected package so removing the isolated TestMain fails in CI
instead of silently writing to the developer's home directory again.

Fixes docker#5013

Signed-off-by: PerryLink <novelnexusai@outlook.com>
@PerryLink
PerryLink requested a review from a team as a code owner November 23, 2026 13:25
@aheritier aheritier added area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection area/testing Test infrastructure, CI/CD, test runners, evaluation area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/test Test-only changes labels Nov 23, 2026

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking down the extra packages that write to the developer’s home. The suite-level wrappers cover the reported paths that resolve through HOME or USERPROFILE, but the cache writes in #5013 still escape on Windows and on Linux with XDG_CACHE_HOME set.

Comment thread pkg/testutil/home.go Resolved
@aheritier

Copy link
Copy Markdown
Collaborator

Moving to draft, please mark as ready when feedback are reviewed and potentially addressed if they make sense to you.

@aheritier
aheritier marked this pull request as draft November 24, 2026 11:46
RunWithTempHome changed HOME and USERPROFILE only, but paths.GetCacheDir()
resolves through os.UserCacheDir(), which on Windows reads %LocalAppData%
and on Linux prefers an absolute $XDG_CACHE_HOME over $HOME/.cache. On
those platforms a suite kept writing the developer's cache while its home
directory already looked isolated: pkg/skills cached remote skills under
<cache>/cagent/skills and pkg/runtime could write <cache>/cagent/
models_dev.json, yet TestTempHomeIsolation still passed.

Point the effective cache root inside the temporary tree for the duration
of the run:

- Windows: LocalAppData -> <temp home>/AppData/Local
- Linux/BSD: XDG_CACHE_HOME -> <temp home>/.cache, absolute because
  os.UserCacheDir rejects a relative value (pkg/paths.GetCacheDir would
  then fall back to the shared system temporary directory)
- macOS/iOS: no extra variable; os.UserCacheDir derives the cache
  directory from $HOME, which is already redirected

RequireTempHome now also fails when the effective cache directory escapes
the temporary home, so the existing TestTempHomeIsolation guards catch a
suite that still writes the developer's cache instead of only one that
drops the isolated TestMain.

Add regression tests: the helper test presets the cache roots outside the
temporary tree and asserts the suite resolves its cache inside it and
writes nothing into the preset root; a resolver-input test pins the
per-platform variables; pkg/skills re-executes the test binary with preset
cache roots and verifies TestLoadWithRemoteSources leaves them empty.
TestLazyModelStore_GatesCustomProvider keeps its cold-cache intent by
overriding the cache variables for its own temporary home.

Signed-off-by: PerryLink <novelnexusai@outlook.com>

Verification (see EVIDENCE.md): go build ./...; go vet ./...; go test ./...
266 packages ok; go test -race ./pkg/testutil ./pkg/skills; golangci-lint
2.14.0 full repo 0 issues; go run ./lint . 3010 files no offenses;
GOOS=windows/darwin compile; PATCH.diff applies cleanly to 37c86aa.
@PerryLink
PerryLink marked this pull request as ready for review November 24, 2026 14:33

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection area/testing Test infrastructure, CI/CD, test runners, evaluation area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/test Test-only changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tests in some packages write to the developer's real home directory

2 participants