diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-13 09:39:15 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-13 09:39:15 +0300 |
| commit | 50d5220ed5a2187dbf548d70f3d795a39f7bfd00 (patch) | |
| tree | 44647a70d126d789a539ddd38cc3986fcf7e61c9 /internal/flags/flags_test.go | |
| parent | e9c747c607e14d8b8f69547aa3c0f3b079c20796 (diff) | |
replace global flags singleton with Parse() returning Config value
Remove the global atomic.Pointer[Config], sync.Once, and parseErr fields
from the flags package. Parse() now returns (Config, error) directly,
eliminating all global-state accessors (Get, SetPidFilter, SetTidFilter,
SetTUIExportEnable, setCurrent, updateCurrent). The internal parse logic
is factored into parseFromFlagSet so tests can inject a fresh FlagSet
without touching os.Args or package-level state.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/flags/flags_test.go')
| -rw-r--r-- | internal/flags/flags_test.go | 32 |
1 files changed, 6 insertions, 26 deletions
diff --git a/internal/flags/flags_test.go b/internal/flags/flags_test.go index 77c167c..1630554 100644 --- a/internal/flags/flags_test.go +++ b/internal/flags/flags_test.go @@ -3,39 +3,19 @@ package flags import ( "flag" "io" - "os" "strings" "testing" "time" ) +// parseForTest builds a fresh FlagSet and parses the given args, returning +// the resulting Config. It avoids touching any global state so tests can run +// in parallel without interfering with each other. func parseForTest(t *testing.T, args ...string) (Config, error) { t.Helper() - - oldCommandLine := flag.CommandLine - oldArgs := os.Args - oldCurrent := Get() - oldParseErr := parseErr - fs := flag.NewFlagSet("ior-test", flag.ContinueOnError) fs.SetOutput(io.Discard) - flag.CommandLine = fs - os.Args = append([]string{"ior"}, args...) - - setCurrent(NewFlags()) - parseErr = nil - - err := parse() - cfg := Get() - - t.Cleanup(func() { - flag.CommandLine = oldCommandLine - os.Args = oldArgs - setCurrent(oldCurrent) - parseErr = oldParseErr - }) - - return cfg, err + return parseFromFlagSet(fs, args) } func TestParseLiveIntervalAndPID(t *testing.T) { @@ -50,8 +30,8 @@ func TestParseLiveIntervalAndPID(t *testing.T) { if cfg.PidFilter != 1234 { t.Fatalf("pid filter = %d, want 1234", cfg.PidFilter) } - if got := Get().GetPidFilter(); got != 1234 { - t.Fatalf("Get().GetPidFilter() = %d, want 1234", got) + if got := cfg.GetPidFilter(); got != 1234 { + t.Fatalf("cfg.GetPidFilter() = %d, want 1234", got) } } |
