[PR #10389] fix(tests): prevent tests from overwriting user auth.json - Issue #10370 #13433

Closed
opened 2026-02-16 18:18:17 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/10389

State: closed
Merged: No


Summary

Fixes #10370 - Tests were overwriting the user's real auth.json file during execution.

Root Cause

The Global object was imported at the top of test/preload.ts before XDG environment variables were set. Since xdg-basedir reads environment variables at import time, this caused tests to resolve to the user's actual home directory instead of the test directory.

Changes

File: test/preload.ts

  • Moved Global import to execute AFTER all XDG_* environment variables are set
  • This ensures xdg-basedir resolves to the test directory instead of the real home directory

Before:

const { Global } = await import("../src/global")  // ← WRONG: imports before env vars
process.env["XDG_DATA_HOME"] = path.join(dir, "share")
// ... other env vars

After:

process.env["XDG_DATA_HOME"] = path.join(dir, "share")
process.env["XDG_CACHE_HOME"] = path.join(dir, "cache")
process.env["XDG_CONFIG_HOME"] = path.join(dir, "config")
process.env["XDG_STATE_HOME"] = path.join(dir, "state")
// NOW safe to import Global
const { Global } = await import("../src/global")

Impact

  • Before: Running npm test would overwrite ~/.local/share/opencode/auth.json with test data
  • After: Tests correctly use isolated test directory at /tmp/opencode-test-data-<pid>/home

Testing

  • Verified tests run without affecting user's real auth.json
  • All 321 tests still pass
  • Test isolation now properly maintained

Checklist

  • Code follows project style guidelines
  • All tests pass (321/321)
  • Changes manually tested
  • Documentation updated (if applicable)
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/10389 **State:** closed **Merged:** No --- ## Summary Fixes #10370 - Tests were overwriting the user's real auth.json file during execution. ## Root Cause The Global object was imported at the top of `test/preload.ts` before XDG environment variables were set. Since `xdg-basedir` reads environment variables at import time, this caused tests to resolve to the user's actual home directory instead of the test directory. ## Changes **File: test/preload.ts** - Moved `Global` import to execute AFTER all `XDG_*` environment variables are set - This ensures `xdg-basedir` resolves to the test directory instead of the real home directory **Before:** ```typescript const { Global } = await import("../src/global") // ← WRONG: imports before env vars process.env["XDG_DATA_HOME"] = path.join(dir, "share") // ... other env vars ``` **After:** ```typescript process.env["XDG_DATA_HOME"] = path.join(dir, "share") process.env["XDG_CACHE_HOME"] = path.join(dir, "cache") process.env["XDG_CONFIG_HOME"] = path.join(dir, "config") process.env["XDG_STATE_HOME"] = path.join(dir, "state") // NOW safe to import Global const { Global } = await import("../src/global") ``` ## Impact - **Before**: Running `npm test` would overwrite `~/.local/share/opencode/auth.json` with test data - **After**: Tests correctly use isolated test directory at `/tmp/opencode-test-data-<pid>/home` ## Testing - Verified tests run without affecting user's real auth.json - All 321 tests still pass - Test isolation now properly maintained ## Checklist - [x] Code follows project style guidelines - [x] All tests pass (321/321) - [x] Changes manually tested - [x] Documentation updated (if applicable)
yindo added the pull-request label 2026-02-16 18:18:17 -05:00
yindo closed this issue 2026-02-16 18:18:17 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13433