Skip to content

Commit 5fb09f2

Browse files
committed
Make it possible to run app tests with -count=n where n > 1
Clearly I didn't think through the effects of mutating the original test case's store when I wrote this the first time.
1 parent 56645a2 commit 5fb09f2

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

internal/randomizer/app_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,14 @@ var testCases = []struct {
232232
func TestMain(t *testing.T) {
233233
for _, tc := range testCases {
234234
t.Run(tc.description, func(t *testing.T) {
235-
app := NewApp("randomizer", tc.store)
235+
store := tc.store.Clone()
236+
app := NewApp("randomizer", store)
236237
app.shuffle = slices.Sort
237238

238239
res, err := app.Main(context.Background(), tc.args)
239240
tc.check(t, res, err)
240241

241-
if tc.expectedStore != nil && !reflect.DeepEqual(tc.store, tc.expectedStore) {
242+
if tc.expectedStore != nil && !reflect.DeepEqual(store, tc.expectedStore) {
242243
t.Errorf("unexpected store state\ngot: %v\nwant: %v", tc.store, tc.expectedStore)
243244
}
244245
})

internal/randomizer/rndtest/rndtest.go

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import (
1212
// strings. A nil Store will return errors for every operation.
1313
type Store map[string][]string
1414

15+
// Clone returns a deep copy of the original store.
16+
func (s Store) Clone() Store {
17+
if s == nil {
18+
return nil
19+
}
20+
out := make(Store, len(s))
21+
for k, v := range s {
22+
out[k] = slices.Clone(v)
23+
}
24+
return out
25+
}
26+
1527
// List implements randomizer.Store.
1628
func (s Store) List(_ context.Context) ([]string, error) {
1729
if s == nil {

0 commit comments

Comments
 (0)