Skip to content

test: refactor shared state DB reset into TestMain for cmd tests#458

Open
terminalchai wants to merge 6 commits into
SurgeDM:mainfrom
terminalchai:fix/cmd-state-db-test-init
Open

test: refactor shared state DB reset into TestMain for cmd tests#458
terminalchai wants to merge 6 commits into
SurgeDM:mainfrom
terminalchai:fix/cmd-state-db-test-init

Conversation

@terminalchai
Copy link
Copy Markdown
Contributor

@terminalchai terminalchai commented May 16, 2026

Problem

The \cmd\ package tests had inconsistent state DB teardown across test helpers. \setupXDGEnvIsolation\ in \ est_env_test.go\ called \state.CloseDB()\ directly in its cleanup function, but \TestMain\ only set \XDG_CONFIG_HOME\ and \APPDATA/\USERPROFILE\ — leaving \XDG_DATA_HOME, \XDG_STATE_HOME, \XDG_CACHE_HOME, \XDG_RUNTIME_DIR, and \HOME\ pointing at real user directories. This meant:

  • The shared suite DB was initialised against real XDG paths, not the isolated temp dir.
  • Tests that restored XDG isolation via \state.CloseDB()\ could leave the suite-level DB in a closed state without reopening it against the temp dir, causing subsequent tests in the same run to fail non-deterministically.

Fix

  1. **\TestMain**: set all six XDG env vars (+ \HOME) to the temp dir, then call a new
    esetSharedStateDB()\ helper that closes any existing DB, ensures config dirs exist, and reopens the DB against the isolated temp path. Also close the DB before \os.RemoveAll\ to avoid Windows file-lock errors.

  2. **
    esetSharedStateDB()**: single source of truth for suite-level DB reset — used by both \TestMain\ and the per-test cleanup in \setupXDGEnvIsolation.

  3. **\setupXDGEnvIsolation**: replace the bare \state.CloseDB()\ call with
    esetSharedStateDB()\ so the suite DB is always left in a known-open state after each per-test isolation window closes.

Testing

\
go test ./cmd/... -count=1 -race
\\

All tests pass.

Greptile Summary

This PR refactors the cmd package test setup to ensure full XDG environment isolation. TestMain now sets all six XDG env vars (plus HOME, APPDATA, USERPROFILE) to a temp directory, and a new resetSharedStateDB helper provides a single, consistent path for closing, re-initialising, and reopening the suite-level DB.

  • resetSharedStateDB is fixed to early-return on config.EnsureDirs failure, preventing state.Configure from being called against a directory that wasn't created.
  • TestMain now calls os.RemoveAll(tmpDir) before os.Exit(1) on setup failure, avoiding stale temp directories.
  • internal/config/settings.go and internal/engine/types/accuracy_test.go include cosmetic whitespace-only changes unrelated to the primary fix.

Confidence Score: 5/5

Safe to merge — all changes are confined to test infrastructure with no production logic affected.

The functional changes correctly fix error handling in resetSharedStateDB and temp-directory cleanup in TestMain. The remaining file touches are pure whitespace with no behavioral impact.

No files require special attention.

Important Files Changed

Filename Overview
cmd/main_test.go Fixes two correctness issues: resetSharedStateDB now early-returns on EnsureDirs failure (previously called state.Configure regardless), and TestMain now cleans up tmpDir before os.Exit(1) on setup failure.
internal/config/settings.go Pure whitespace/alignment reformatting of NetworkSettings struct fields — no functional changes.
internal/engine/types/accuracy_test.go Single-character alignment fix in a comment — no functional changes.
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
internal/config/settings.go:68-80
**Unrelated whitespace changes bundled into this PR**

`internal/config/settings.go` and `internal/engine/types/accuracy_test.go` contain only struct-field alignment and comment-spacing tweaks that are unrelated to the TestMain DB-reset refactor described in the PR. Bundling cosmetic changes with a functional fix makes the diff harder to read and, if a revert is ever needed, forces reverting the formatting changes too.

Reviews (3): Last reviewed commit: "chore: go fmt" | Re-trigger Greptile

Context used:

  • Rule used - What: Flag commits that bundle unnecessary changes... (source)

Comment thread cmd/main_test.go
@SuperCoolPencil
Copy link
Copy Markdown
Member

I don't understand how this PR has 0 changes but 5 commits. Am I going crazy? lol

@SuperCoolPencil SuperCoolPencil force-pushed the fix/cmd-state-db-test-init branch from 0cf34b5 to 60026c0 Compare May 18, 2026 06:48
@SuperCoolPencil
Copy link
Copy Markdown
Member

I don't understand how this PR has 0 changes but 5 commits. Am I going crazy? lol

Yep my bad. Made a wrong merge commit

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants