Debian Codegraph @

core/change_test.go

raw
1 package core
2
3 import (
4 "fmt"
5 "os"
6 "sort"
7 "strings"
8 "testing"
9 "time"
10
11 "github.com/miniscruff/changie/then"
12 )
13
14 var (
15 orderedTimes = []time.Time{
16 time.Date(2018, 5, 24, 3, 30, 10, 5, time.Local),
17 time.Date(2017, 5, 24, 3, 30, 10, 5, time.Local),
18 time.Date(2016, 5, 24, 3, 30, 10, 5, time.Local),
19 }
20 )
21
22 func TestWriteChange(t *testing.T) {
23 mockTime := time.Date(2016, 5, 24, 3, 30, 10, 5, time.Local)
24 change := Change{
25 Body: "some body message",
26 Time: mockTime,
27 }
28
29 changesYaml := fmt.Sprintf(
30 "body: some body message\ntime: %s\n",
31 mockTime.Format(time.RFC3339Nano),
32 )
33
34 var builder strings.Builder
35
36 _, err := change.WriteTo(&builder)
37
38 then.Equals(t, changesYaml, builder.String())
39 then.Nil(t, err)
40 }
41
42 func TestLoadChangeFailsIfNoFile(t *testing.T) {
43 then.WithTempDir(t)
44
45 _, err := LoadChange("missing_file.yaml")
46
47 then.NotNil(t, err)
48 }
49
50 func TestLoadChangeFromPath(t *testing.T) {
51 then.WithTempDir(t)
52 then.Nil(t, os.WriteFile("some_file.yaml", []byte("kind: A\nbody: hey\n"), CreateFileMode))
53
54 change, err := LoadChange("some_file.yaml")
55
56 then.Nil(t, err)
57 then.Equals(t, "A", change.Kind)
58 then.Equals(t, "hey", change.Body)
59 }
60
61 func TestBadYamlFile(t *testing.T) {
62 then.WithTempDir(t)
63 then.Nil(t, os.WriteFile("some_file.yaml", []byte("not a yaml file---"), CreateFileMode))
64
65 _, err := LoadChange("some_file.yaml")
66 then.NotNil(t, err)
67 }
68
69 func TestSortByTime(t *testing.T) {
70 changes := []Change{
71 {Body: "third", Time: orderedTimes[2]},
72 {Body: "second", Time: orderedTimes[1]},
73 {Body: "first", Time: orderedTimes[0]},
74 }
75 cfg := &Config{}
76
77 sort.Slice(changes, ChangeLess(cfg, changes))
78
79 then.Equals(t, "third", changes[0].Body)
80 then.Equals(t, "second", changes[1].Body)
81 then.Equals(t, "first", changes[2].Body)
82 }
83
84 func TestSortByKindThenTime(t *testing.T) {
85 cfg := &Config{
86 Kinds: []KindConfig{
87 {Label: "A"},
88 {Label: "B"},
89 },
90 }
91 changes := []Change{
92 {Kind: "B", Body: "fourth", Time: orderedTimes[0]},
93 {Kind: "A", Body: "second", Time: orderedTimes[1]},
94 {Kind: "B", Body: "third", Time: orderedTimes[1]},
95 {Kind: "A", Body: "first", Time: orderedTimes[2]},
96 }
97 sort.Slice(changes, ChangeLess(cfg, changes))
98
99 then.Equals(t, "first", changes[0].Body)
100 then.Equals(t, "second", changes[1].Body)
101 then.Equals(t, "third", changes[2].Body)
102 then.Equals(t, "fourth", changes[3].Body)
103 }
104
105 func TestSortByTimeIfKindFormatEmpty(t *testing.T) {
106 cfg := &Config{
107 Kinds: []KindConfig{
108 {Label: "A"},
109 {Label: "B"},
110 },
111 KindFormat: "",
112 }
113 changes := []Change{
114 {Kind: "A", Body: "first", Time: orderedTimes[2]},
115 {Kind: "B", Body: "second", Time: orderedTimes[1]},
116 {Kind: "A", Body: "third", Time: orderedTimes[0]},
117 }
118 sort.Slice(changes, ChangeLess(cfg, changes))
119
120 then.Equals(t, "first", changes[0].Body)
121 then.Equals(t, "second", changes[1].Body)
122 then.Equals(t, "third", changes[2].Body)
123 }
124
125 func TestSortByComponentThenKind(t *testing.T) {
126 cfg := &Config{
127 Kinds: []KindConfig{
128 {Label: "D"},
129 {Label: "E"},
130 },
131 Components: []string{"A", "B", "C"},
132 ComponentFormat: "## {{.Component}}",
133 KindFormat: "* {{.Kind}}",
134 }
135 changes := []Change{
136 {Body: "fourth", Component: "B", Kind: "E"},
137 {Body: "second", Component: "A", Kind: "E"},
138 {Body: "third", Component: "B", Kind: "D"},
139 {Body: "first", Component: "A", Kind: "D"},
140 }
141 sort.Slice(changes, ChangeLess(cfg, changes))
142
143 then.Equals(t, "first", changes[0].Body)
144 then.Equals(t, "second", changes[1].Body)
145 then.Equals(t, "third", changes[2].Body)
146 then.Equals(t, "fourth", changes[3].Body)
147 }
148
149 func TestSortByKindWhenComponentFormatIsEmpty(t *testing.T) {
150 cfg := &Config{
151 Kinds: []KindConfig{
152 {Label: "D"},
153 {Label: "E"},
154 },
155 Components: []string{"A", "B", "C"},
156 KindFormat: "* {{.Kind}}",
157 }
158 changes := []Change{
159 {Body: "fourth", Component: "B", Kind: "E", Time: orderedTimes[0]},
160 {Body: "second", Component: "A", Kind: "E", Time: orderedTimes[1]},
161 {Body: "third", Component: "B", Kind: "D", Time: orderedTimes[1]},
162 {Body: "first", Component: "A", Kind: "D", Time: orderedTimes[2]},
163 }
164
165 sort.Slice(changes, ChangeLess(cfg, changes))
166
167 then.Equals(t, "first", changes[0].Body)
168 then.Equals(t, "third", changes[1].Body)
169 then.Equals(t, "second", changes[2].Body)
170 then.Equals(t, "fourth", changes[3].Body)
171 }
172
173 func TestLoadEnvVars(t *testing.T) {
174 config := Config{
175 EnvPrefix: "TEST_CHANGIE_",
176 }
177
178 t.Setenv("TEST_CHANGIE_ALPHA", "Beta")
179 t.Setenv("IGNORE", "Delta")
180
181 expectedEnvVars := map[string]string{
182 "ALPHA": "Beta",
183 }
184
185 then.MapEquals(t, expectedEnvVars, config.EnvVars())
186
187 // will use the cached results
188 t.Setenv("TEST_CHANGIE_UNUSED", "NotRead")
189 then.MapEquals(t, expectedEnvVars, config.EnvVars())
190 }
191
192 func TestPostProcess_NoPostConfigs(t *testing.T) {
193 cfg := &Config{}
194 change := &Change{}
195
196 then.Nil(t, change.PostProcess(cfg, nil))
197 then.MapLen(t, 0, change.Custom)
198 }
199
200 func TestPostProcess_NilKindGlobalPost(t *testing.T) {
201 cfg := &Config{
202 Post: []PostProcessConfig{
203 {
204 Key: "NilKind",
205 Value: "Yes",
206 },
207 },
208 }
209 change := &Change{
210 Custom: make(map[string]string),
211 }
212
213 then.Nil(t, change.PostProcess(cfg, nil))
214 then.MapLen(t, 1, change.Custom)
215 then.Equals(t, "Yes", change.Custom["NilKind"])
216 }
217
218 func TestPostProcess_KindPost(t *testing.T) {
219 cfg := &Config{}
220 change := &Change{
221 Custom: make(map[string]string),
222 }
223 kindCfg := &KindConfig{
224 Post: []PostProcessConfig{
225 {
226 Key: "NotNilKind",
227 Value: "AlsoYes",
228 },
229 },
230 }
231
232 then.Nil(t, change.PostProcess(cfg, kindCfg))
233 then.MapLen(t, 1, change.Custom)
234 then.Equals(t, "AlsoYes", change.Custom["NotNilKind"])
235 }
236
237 func TestPostProcess_InvalidPostTemplate(t *testing.T) {
238 cfg := &Config{}
239 change := &Change{
240 Custom: make(map[string]string),
241 }
242 kindCfg := &KindConfig{
243 Post: []PostProcessConfig{
244 {
245 Key: "BrokenTemplate",
246 Value: "{{ ..-... }}",
247 },
248 },
249 }
250
251 then.NotNil(t, change.PostProcess(cfg, kindCfg))
252 }