From be48b91ae4f3136c2ed05d2ee5551df42434cfb0 Mon Sep 17 00:00:00 2001 From: Cael Rowley Date: Wed, 6 May 2026 10:47:27 +0200 Subject: [PATCH] fix(config): de-flake TestAddFlags by stubbing time source --- pkg/config/config_test.go | 6 ++++++ pkg/config/defaults.go | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 02040ebba9..6e93314d27 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -41,6 +41,12 @@ func TestDefaultConfig(t *testing.T) { } func TestAddFlags(t *testing.T) { + // Freeze the time source feeding randString so AddFlags' internal DefaultConfig() + // and the per-assertion DefaultConfig() calls below produce the same DA.Namespace seed. + origNowUnix := nowUnix + nowUnix = func() int64 { return 2_000_000_000 } + t.Cleanup(func() { nowUnix = origNowUnix }) + // Create a command with flags cmd := &cobra.Command{Use: "test"} AddGlobalFlags(cmd, "test") // Add basic flags first diff --git a/pkg/config/defaults.go b/pkg/config/defaults.go index 233585e0c5..5ea895a48f 100644 --- a/pkg/config/defaults.go +++ b/pkg/config/defaults.go @@ -133,10 +133,13 @@ func DefaultConfig() Config { } } +// nowUnix returns the current Unix timestamp; package-level so tests can stub it. +var nowUnix = func() int64 { return time.Now().Unix() } + func randString(length int) string { const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" result := make([]byte, length) - rng := rand.New(rand.NewSource(time.Now().Unix())) //nolint:gosec // even half random is good enough here. + rng := rand.New(rand.NewSource(nowUnix())) //nolint:gosec // even half random is good enough here. for i := range result { result[i] = charset[rng.Intn(len(charset))] }