Skip to content

test: isolate config dir in tests so the suite can't wipe real settings - #327

Merged
jongio merged 1 commit into
mainfrom
fix/test-config-isolation
Jul 19, 2026
Merged

test: isolate config dir in tests so the suite can't wipe real settings#327
jongio merged 1 commit into
mainfrom
fix/test-config-isolation

Conversation

@jongio

@jongio jongio commented Jul 19, 2026

Copy link
Copy Markdown
Owner

What

Adds a package-level TestMain to internal/tui, cmd/dispatch, and internal/config that points the OS config directory at a throwaway temp dir for the whole test run, so the test suite can never touch the developer's real config.json.

Why

Running go test ./... (which mage install runs after every change) was overwriting or deleting the real config file, which is why user settings weren't retained:

  • internal/tui tests build a Model and persist via config.Save (for example recordLaunch stamps a frecency launch on session launch), writing the real %APPDATA%\dispatch\config.json.
  • cmd/dispatch's TestHandleArgs_ClearCache runs the --clear-cache path, which calls config.Reset and deletes it.

dispatch update itself never touches config; the loss came from the test run.

How

config.Save/Load/Reset resolve the config dir via platform.ConfigDir() and os.UserConfigDir(). Each new TestMain sets the matching env var (APPDATA on Windows, HOME on macOS, XDG_CONFIG_HOME on Linux) to an os.MkdirTemp dir before running the package's tests, then removes it. Existing per-test helpers (withTempConfigDir, setupTempConfig) still override and restore to this baseline, so path and error-path tests are unaffected.

Test-only change; no production code is modified.

Testing

  • go build ./cmd/dispatch/ passes
  • go test ./... passes
  • go vet ./... passes
  • New tests added for new functionality (config-dir isolation TestMain in 3 packages)
  • Existing tests still pass

Verified empirically: backed up the real config, ran the full go test ./... -count=1 suite and mage install (with -race), and confirmed the real config.json hash is unchanged. Before the fix it was overwritten/deleted by the same runs. golangci-lint reports 0 issues and mage deadcode is clean.

Screenshots

N/A (no UI changes).

Config-touching tests persisted config without redirecting the OS config
directory, so running `go test ./...` - directly or via `mage install` -
overwrote or deleted the developer's real config.json
(%APPDATA%\dispatch\config.json on Windows, ~/.config/dispatch/config.json
on Linux/macOS):

- internal/tui: tests build a Model (NewModel/newTestModel) and save via
  config.Save (e.g. recordLaunch stamps a frecency launch), writing the
  real file.
- cmd/dispatch: TestHandleArgs_ClearCache runs the `--clear-cache` path,
  which calls config.Reset and deletes the real file.

`dispatch update` itself never touches config; the loss came from the
test run (which `mage install` performs after every change).

Add a package-level TestMain to internal/tui, cmd/dispatch, and
internal/config that points the config dir (APPDATA / XDG_CONFIG_HOME /
HOME per OS, mirroring os.UserConfigDir) at a throwaway temp dir for the
whole package test run. This guarantees no test - current or future -
can clobber the user's real settings. Existing per-test helpers still
override and restore to this baseline, so path/error-path tests are
unaffected.

Test-only change; no production code is modified.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a0396804-f73e-46d6-9062-02d625302301
@jongio
jongio merged commit e6fee2f into main Jul 19, 2026
2 checks passed
@jongio
jongio deleted the fix/test-config-isolation branch July 19, 2026 10:07
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.

1 participant