Skip to content

Commit d751912

Browse files
committed
Turn config tests into table test
1 parent 679b18d commit d751912

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

cmd/zb/config_test.go

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
package main
55

66
import (
7+
"fmt"
78
"os"
89
"path/filepath"
10+
"slices"
911
"testing"
1012

11-
"zb.256lights.llc/pkg/zbstore"
13+
"github.com/google/go-cmp/cmp"
1214
)
1315

1416
func TestDefaultGlobalConfig(t *testing.T) {
@@ -22,32 +24,44 @@ func TestDefaultGlobalConfig(t *testing.T) {
2224
}
2325

2426
func TestGlobalConfigMergeFiles(t *testing.T) {
25-
dir := t.TempDir()
26-
var paths [2]string
27-
paths[0] = filepath.Join(dir, "config1.jwcc")
28-
if err := os.WriteFile(paths[0], []byte(`{"debug": true, "storeDirectory": "/foo"}`+"\n"), 0o666); err != nil {
29-
t.Fatal(err)
30-
}
31-
paths[1] = filepath.Join(dir, "config2.jwcc")
32-
if err := os.WriteFile(paths[1], []byte(`{"storeDirectory": "/bar"}`+"\n"), 0o666); err != nil {
33-
t.Fatal(err)
27+
tests := []struct {
28+
name string
29+
files []string
30+
want globalConfig
31+
}{
32+
{
33+
name: "MergeScalar",
34+
files: []string{
35+
`{"debug": true, "storeDirectory": "/foo"}` + "\n",
36+
`{"storeDirectory": "/bar"}` + "\n",
37+
},
38+
want: globalConfig{
39+
Debug: true,
40+
Directory: "/bar",
41+
},
42+
},
3443
}
3544

36-
g := new(globalConfig)
37-
err := g.mergeFiles(func(yield func(string) bool) {
38-
for _, path := range paths {
39-
if !yield(path) {
40-
return
45+
for _, test := range tests {
46+
t.Run(test.name, func(t *testing.T) {
47+
dir := t.TempDir()
48+
paths := make([]string, len(test.files))
49+
for i, content := range test.files {
50+
path := filepath.Join(dir, fmt.Sprintf("config%d.jwcc", i+1))
51+
if err := os.WriteFile(path, []byte(content), 0o666); err != nil {
52+
t.Fatal(err)
53+
}
54+
paths[i] = path
4155
}
42-
}
43-
})
44-
if err != nil {
45-
t.Error("mergeFiles:", err)
46-
}
47-
if !g.Debug {
48-
t.Error("g.Debug = false; want true (config1.jwcc ignored)")
49-
}
50-
if got, want := g.Directory, zbstore.Directory("/bar"); got != want {
51-
t.Errorf("g.Directory = %q; want %q", got, want)
56+
57+
got := new(globalConfig)
58+
err := got.mergeFiles(slices.Values(paths))
59+
if err != nil {
60+
t.Error("mergeFiles:", err)
61+
}
62+
if diff := cmp.Diff(&test.want, got, cmp.AllowUnexported(stringAllowList{})); diff != "" {
63+
t.Errorf("-want +got:\n%s", diff)
64+
}
65+
})
5266
}
5367
}

0 commit comments

Comments
 (0)