-
Notifications
You must be signed in to change notification settings - Fork 2
/
config_test.go
90 lines (82 loc) · 2.08 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package expect
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"perri.to/expect/snapshots"
"perri.to/expect/snapshots/comparabletypes"
)
func TestReadConfigNoFile(t *testing.T) {
d := t.TempDir()
c, err := readConfig(func() (string, error) { return d, nil })
if err != nil {
t.Fatal(err)
}
eq := reflect.DeepEqual(c, &Config{
Grouping: "",
SnapShotDir: "",
Replacers: map[snapshots.Kind]map[string]string{},
})
if !eq {
t.Logf("returned configuration is not empty: %#v", c)
t.FailNow()
}
}
func TestReadConfig(t *testing.T) {
d := t.TempDir()
expectedConfig := &Config{
Grouping: groupByTestFile,
SnapShotDir: "some_cool_name",
Replacers: map[snapshots.Kind]map[string]string{
comparabletypes.KindJSON: {"Date": "replaced-date"},
comparabletypes.KindString: {"Time": "replaced-time"},
},
}
m, err := json.Marshal(expectedConfig)
if err != nil {
t.Fatal(fmt.Errorf("marshaling sample config: %w", err))
}
err = os.WriteFile(filepath.Join(d, configFileName), m, snapshotFilePerm)
if err != nil {
t.Fatal(fmt.Errorf("writing sample config: %w", err))
}
c, err := readConfig(func() (string, error) { return d, nil })
if err != nil {
t.Fatal(err)
}
eq := reflect.DeepEqual(c, expectedConfig)
if !eq {
t.Logf("returned configuration is not the one we stored: %#v", c)
t.FailNow()
}
}
func TestConfig_SnapshotDir(t *testing.T) {
c := &Config{
Grouping: groupByTestFile,
SnapShotDir: "some_cool_name",
Replacers: map[snapshots.Kind]map[string]string{
comparabletypes.KindJSON: {"Date": "replaced-date"},
comparabletypes.KindString: {"Time": "replaced-time"},
},
}
f := c.SnapshotDir("config_test.go")
exp := "some_cool_name"
if f != exp {
t.Logf("expected %q but got %q", exp, f)
}
c.SnapShotDir = ""
f = c.SnapshotDir("config_test.go")
exp = "config_test.expectations"
if f != exp {
t.Logf("expected %q but got %q", exp, f)
}
c.Grouping = groupByPackage
f = c.SnapshotDir("config_test.go")
exp = "TestExpectationsSnapshots"
if f != exp {
t.Logf("expected %q but got %q", exp, f)
}
}