test: isolate config dir in tests so the suite can't wipe real settings - #327
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a package-level
TestMaintointernal/tui,cmd/dispatch, andinternal/configthat 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 realconfig.json.Why
Running
go test ./...(whichmage installruns after every change) was overwriting or deleting the real config file, which is why user settings weren't retained:internal/tuitests build a Model and persist viaconfig.Save(for examplerecordLaunchstamps a frecency launch on session launch), writing the real%APPDATA%\dispatch\config.json.cmd/dispatch'sTestHandleArgs_ClearCacheruns the--clear-cachepath, which callsconfig.Resetand deletes it.dispatch updateitself never touches config; the loss came from the test run.How
config.Save/Load/Resetresolve the config dir viaplatform.ConfigDir()andos.UserConfigDir(). Each newTestMainsets the matching env var (APPDATAon Windows,HOMEon macOS,XDG_CONFIG_HOMEon Linux) to anos.MkdirTempdir 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/passesgo test ./...passesgo vet ./...passesTestMainin 3 packages)Verified empirically: backed up the real config, ran the full
go test ./... -count=1suite andmage install(with-race), and confirmed the realconfig.jsonhash is unchanged. Before the fix it was overwritten/deleted by the same runs. golangci-lint reports 0 issues andmage deadcodeis clean.Screenshots
N/A (no UI changes).