Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func TestDefaultConfig(t *testing.T) {
}

func TestAddFlags(t *testing.T) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use testing/synctest instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good suggestion, then we dont need to modify config/defaults.go updated 👍

// 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
Expand Down
5 changes: 4 additions & 1 deletion pkg/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
}
Expand Down