Debian Codegraph @

core/config_test.go

raw
1 package core
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "github.com/miniscruff/changie/then"
10 )
11
12 func TestSaveConfig(t *testing.T) {
13 then.WithTempDir(t)
14
15 config := Config{
16 ChangesDir: "Changes",
17 UnreleasedDir: "Unrel",
18 HeaderPath: "header.tpl.md",
19 VersionHeaderPath: "header.md",
20 VersionExt: "md",
21 ChangeFormat: "chng",
22 ChangelogPath: "CHANGELOG.md",
23 }
24
25 configYaml := `changesDir: Changes
26 unreleasedDir: Unrel
27 headerPath: header.tpl.md
28 changelogPath: CHANGELOG.md
29 versionExt: md
30 versionHeaderPath: header.md
31 changeFormat: chng
32 `
33
34 err := config.Save()
35 then.Nil(t, err)
36 then.FileContents(t, configYaml, ConfigPaths[0])
37 }
38
39 func TestSaveConfigWithCustomChoicesAndOptionals(t *testing.T) {
40 then.WithTempDir(t)
41
42 config := Config{
43 ChangesDir: "Changes",
44 UnreleasedDir: "Unrel",
45 HeaderPath: "header.tpl.md",
46 VersionHeaderPath: "vheader",
47 VersionFooterPath: "vfooter",
48 VersionExt: "md",
49 ChangelogPath: "CHANGELOG.md",
50 VersionFormat: "vers",
51 ComponentFormat: "comp",
52 KindFormat: "kind",
53 ChangeFormat: "chng",
54 Components: []string{"A", "D", "G"},
55 HeaderFormat: "head",
56 FooterFormat: "foot",
57 Kinds: []KindConfig{
58 {Label: "B"},
59 {Label: "C"},
60 {Label: "E"},
61 },
62 CustomChoices: []Custom{
63 {
64 Key: "first",
65 Type: CustomString,
66 Label: "First name",
67 },
68 },
69 }
70
71 configYaml := `changesDir: Changes
72 unreleasedDir: Unrel
73 headerPath: header.tpl.md
74 changelogPath: CHANGELOG.md
75 versionExt: md
76 versionHeaderPath: vheader
77 versionFooterPath: vfooter
78 versionFormat: vers
79 componentFormat: comp
80 kindFormat: kind
81 changeFormat: chng
82 headerFormat: head
83 footerFormat: foot
84 components:
85 - A
86 - D
87 - G
88 kinds:
89 - label: B
90 - label: C
91 - label: E
92 custom:
93 - key: first
94 type: string
95 label: First name
96 `
97
98 err := config.Save()
99 then.Nil(t, err)
100 then.FileContents(t, configYaml, ConfigPaths[0])
101 }
102
103 func TestCanCheckIfFileDoesExist(t *testing.T) {
104 cfg := &Config{}
105 then.WithTempDirConfig(t, cfg)
106
107 exist, err := cfg.Exists()
108 then.True(t, exist)
109 then.Nil(t, err)
110 }
111
112 func TestCanCheckIfFileDoesNotExist(t *testing.T) {
113 cfg := &Config{}
114
115 then.WithTempDir(t)
116
117 exist, err := cfg.Exists()
118 then.False(t, exist)
119 then.Nil(t, err)
120 }
121
122 func TestLoadConfigFromAlternatePath(t *testing.T) {
123 then.WithTempDir(t)
124
125 err := os.WriteFile(ConfigPaths[1], []byte("changesDir: C\nheaderPath: header.rst\n"), CreateFileMode)
126 then.Nil(t, err)
127
128 config, err := LoadConfig()
129 then.Nil(t, err)
130 then.Equals(t, "C", config.ChangesDir)
131 then.Equals(t, "header.rst", config.HeaderPath)
132 }
133
134 func TestLoadConfigFromEnvVar(t *testing.T) {
135 then.WithTempDir(t)
136 t.Setenv("CHANGIE_CONFIG_PATH", filepath.Join("custom", "changie.yaml"))
137
138 then.WriteFile(t, []byte("changesDir: C\nheaderPath: header.rst\n"), "custom", "changie.yaml")
139
140 config, err := LoadConfig()
141 then.Nil(t, err)
142 then.Equals(t, "C", config.ChangesDir)
143 then.Equals(t, "header.rst", config.HeaderPath)
144 }
145
146 func TestLoadConfigFromEnvVarMissingFile(t *testing.T) {
147 then.WithTempDir(t)
148 t.Setenv("CHANGIE_CONFIG_PATH", filepath.Join("custom", "missing.yaml"))
149
150 _, err := LoadConfig()
151 then.NotNil(t, err)
152 }
153
154 func TestLoadConfigMultipleLayersWithoutAConfig(t *testing.T) {
155 then.WithTempDir(t)
156
157 then.Nil(t, os.MkdirAll(filepath.Join("a", "b", "c"), CreateDirMode))
158 t.Chdir("a")
159 t.Chdir("b")
160 t.Chdir("c")
161
162 _, err := LoadConfig()
163 then.Err(t, ErrConfigNotFound, err)
164 }
165
166 func TestLoadConfigMultipleLayersDown(t *testing.T) {
167 then.WithTempDir(t)
168 then.WriteFile(t, []byte("changesDir: C\nheaderPath: header.rst\n"), ".changie.yml")
169
170 then.Nil(t, os.MkdirAll(filepath.Join("a", "b", "c"), CreateDirMode))
171 t.Chdir("a")
172 t.Chdir("b")
173 t.Chdir("c")
174
175 config, err := LoadConfig()
176 then.Nil(t, err)
177 then.Equals(t, "C", config.ChangesDir)
178 then.Equals(t, "header.rst", config.HeaderPath)
179 }
180
181 func TestDefaultFragmentTemplateWithProjects(t *testing.T) {
182 then.WithTempDir(t)
183
184 configYaml := []byte(`kinds:
185 - label: B
186 - label: C
187 - label: E
188 components:
189 - A
190 - D
191 - G
192 projects:
193 - label: User Feeds
194 key: user_feeds
195 changelog: users/feeds/CHANGELOG.md
196 - label: User Management
197 key: user_management
198 changelog: users/management/CHANGELOG.md
199 `)
200 then.WriteFile(t, configYaml, ConfigPaths[0])
201
202 defaultFragmentFormat := fmt.Sprintf("{{.Project}}-{{.Component}}-{{.Kind}}-{{.Time.Format \"%v\"}}", timeFormat)
203
204 config, err := LoadConfig()
205 then.Nil(t, err)
206 then.Equals(t, defaultFragmentFormat, config.FragmentFileFormat)
207 }
208
209 func TestDefaultFragmentTemplateWithKindsAndComponents(t *testing.T) {
210 then.WithTempDir(t)
211
212 yamlBytes := []byte(`kinds:
213 - label: B
214 - label: C
215 - label: E
216 components:
217 - A
218 - D
219 - G
220 `)
221 then.WriteFile(t, yamlBytes, ConfigPaths[0])
222
223 defaultFragmentFormat := fmt.Sprintf("{{.Component}}-{{.Kind}}-{{.Time.Format \"%v\"}}", timeFormat)
224
225 config, err := LoadConfig()
226 then.Nil(t, err)
227 then.Equals(t, defaultFragmentFormat, config.FragmentFileFormat)
228 }
229
230 func TestDefaultFragmentTemplateWithKinds(t *testing.T) {
231 then.WithTempDir(t)
232
233 cfgYaml := []byte(`kinds:
234 - label: B
235 - label: C
236 - label: E
237 `)
238 then.WriteFile(t, cfgYaml, ConfigPaths[0])
239
240 defaultFragmentFormat := fmt.Sprintf("{{.Kind}}-{{.Time.Format \"%v\"}}", timeFormat)
241
242 config, err := LoadConfig()
243 then.Nil(t, err)
244 then.Equals(t, defaultFragmentFormat, config.FragmentFileFormat)
245 }
246
247 func TestDefaultFragmentTemplateWithoutKindsOrComponents(t *testing.T) {
248 then.WithTempDir(t)
249 then.WriteFile(t, []byte("unreleasedDir: unrel"), ConfigPaths[0])
250
251 defaultFragmentFormat := fmt.Sprintf("{{.Time.Format \"%v\"}}", timeFormat)
252
253 config, err := LoadConfig()
254 then.Nil(t, err)
255 then.Equals(t, defaultFragmentFormat, config.FragmentFileFormat)
256 }
257
258 func TestErrorBadYamlFile(t *testing.T) {
259 then.WithTempDir(t)
260
261 cfgYaml := []byte("not a valid yaml file---")
262 then.WriteFile(t, cfgYaml, ConfigPaths[0])
263
264 _, err := LoadConfig()
265 then.NotNil(t, err)
266 }
267
268 func TestGetHeaderFromKindLabel(t *testing.T) {
269 config := Config{
270 Kinds: []KindConfig{
271 {Label: "A", Format: "unused"},
272 {Label: "unused", Format: ""},
273 {Label: "C", Format: "KF"},
274 },
275 }
276 format := config.KindHeader("C")
277 then.Equals(t, "KF", format)
278 }
279
280 func TestGetHeaderFromKindKey(t *testing.T) {
281 config := Config{
282 Kinds: []KindConfig{
283 {Label: "A", Format: "unused"},
284 {Label: "unused", Format: ""},
285 {Key: "C", Format: "KF"},
286 },
287 }
288 format := config.KindHeader("C")
289 then.Equals(t, "KF", format)
290 }
291
292 func TestGetDefaultHeaderForKind(t *testing.T) {
293 config := Config{
294 Kinds: []KindConfig{
295 {Label: "ignored"},
296 },
297 KindFormat: "KF",
298 }
299 format := config.KindHeader("unused label")
300 then.Equals(t, "KF", format)
301 }
302
303 func TestGetChangeFormatFromKindLabel(t *testing.T) {
304 config := Config{
305 Kinds: []KindConfig{
306 {Label: "A", ChangeFormat: "unused"},
307 {Label: "unused", ChangeFormat: ""},
308 {Label: "C", ChangeFormat: "CF"},
309 },
310 }
311 format := config.ChangeFormatForKind("C")
312 then.Equals(t, "CF", format)
313 }
314
315 func TestGetChangeFormatFromKindKey(t *testing.T) {
316 config := Config{
317 Kinds: []KindConfig{
318 {Label: "A", ChangeFormat: "unused"},
319 {Label: "unused", ChangeFormat: ""},
320 {Key: "C", ChangeFormat: "CF"},
321 },
322 }
323 format := config.ChangeFormatForKind("C")
324 then.Equals(t, "CF", format)
325 }
326
327 func TestGetDefaultChangeFormatIfNoCustomOnesExist(t *testing.T) {
328 config := Config{
329 Kinds: []KindConfig{
330 {Label: "unused", ChangeFormat: "ignored"},
331 },
332 ChangeFormat: "CF",
333 }
334 format := config.ChangeFormatForKind("C")
335 then.Equals(t, "CF", format)
336 }
337
338 func TestBodyConfigCreatePrompt(t *testing.T) {
339 cust := BodyConfig{}.CreateCustom()
340
341 then.Equals(t, "Body", cust.Label)
342 then.Equals(t, CustomString, cust.Type)
343 }
344
345 func TestBodyConfigCreateBlockPrompt(t *testing.T) {
346 cust := BodyConfig{
347 UseBlock: true,
348 }.CreateCustom()
349
350 then.Equals(t, "Body", cust.Label)
351 then.Equals(t, CustomBlock, cust.Type)
352 }
353
354 func TestBodyConfigWithMinAndMax(t *testing.T) {
355 var max int64 = 10
356
357 cust := BodyConfig{MaxLength: &max}.CreateCustom()
358 then.Equals(t, 10, *cust.MaxLength)
359 }
360
361 func TestConfigProjectFindsProjectByLabel(t *testing.T) {
362 cfg := &Config{
363 Projects: []ProjectConfig{
364 {
365 Label: "Other",
366 Key: "other",
367 },
368 {
369 Label: "CLI",
370 Key: "cli",
371 },
372 },
373 }
374
375 proj, err := cfg.Project("CLI")
376 then.Nil(t, err)
377 then.Equals(t, "cli", proj.Key)
378 }
379
380 func TestConfigProjectFindsProjectByKey(t *testing.T) {
381 cfg := &Config{
382 Projects: []ProjectConfig{
383 {
384 Label: "Other",
385 Key: "other",
386 },
387 {
388 Label: "CLI",
389 Key: "cli",
390 },
391 },
392 }
393
394 proj, err := cfg.Project("cli")
395 then.Nil(t, err)
396 then.Equals(t, "CLI", proj.Label)
397 }
398
399 func TestConfigProjectNoProjectsReturnsEmptyConfig(t *testing.T) {
400 cfg := &Config{}
401
402 proj, err := cfg.Project("")
403 then.Nil(t, err)
404 then.Equals(t, "", proj.Label)
405 then.Equals(t, "", proj.Key)
406 then.Equals(t, "", proj.ChangelogPath)
407 }
408
409 func TestConfigProjectCanGetLabels(t *testing.T) {
410 cfg := &Config{
411 Projects: []ProjectConfig{
412 {
413 Label: "Labeled",
414 Key: "labeled",
415 },
416 {
417 Key: "key_only",
418 },
419 },
420 }
421
422 labels := cfg.ProjectLabels()
423 then.SliceEquals(t, []string{"Labeled", "key_only"}, labels)
424 }
425
426 func TestConfigProjectErrorNoLabelOrKeyAndRequired(t *testing.T) {
427 cfg := &Config{
428 Projects: []ProjectConfig{
429 {
430 Label: "Other",
431 Key: "other",
432 },
433 },
434 }
435
436 _, err := cfg.Project("")
437 then.Err(t, errProjectRequired, err)
438 }
439
440 func TestConfigProjectErrorNotFound(t *testing.T) {
441 cfg := &Config{
442 Projects: []ProjectConfig{
443 {
444 Label: "Other",
445 Key: "other",
446 },
447 },
448 }
449
450 _, err := cfg.Project("emailer")
451 then.Err(t, errProjectNotFound, err)
452 }
453
454 func TestAutoLevelForChangeLiteral(t *testing.T) {
455 kc := KindConfig{AutoLevel: MinorLevel}
456 level, err := kc.AutoLevelForChange(NewTemplateCache(), Change{})
457 then.Nil(t, err)
458 then.Equals(t, MinorLevel, level)
459 }
460
461 func TestAutoLevelForChangeTemplate(t *testing.T) {
462 kc := KindConfig{
463 AutoLevel: `{{if eq .Custom.Breaking "Yes"}}major{{else}}patch{{end}}`,
464 }
465 cache := NewTemplateCache()
466
467 major, err := kc.AutoLevelForChange(cache, Change{Custom: map[string]string{"Breaking": "Yes"}})
468 then.Nil(t, err)
469 then.Equals(t, MajorLevel, major)
470
471 patch, err := kc.AutoLevelForChange(cache, Change{Custom: map[string]string{"Breaking": "No"}})
472 then.Nil(t, err)
473 then.Equals(t, PatchLevel, patch)
474 }
475
476 func TestAutoLevelForChangeNoneTemplate(t *testing.T) {
477 kc := KindConfig{AutoLevel: `{{if .Custom.Skip}}none{{else}}patch{{end}}`}
478 cache := NewTemplateCache()
479
480 none, err := kc.AutoLevelForChange(cache, Change{Custom: map[string]string{"Skip": "true"}})
481 then.Nil(t, err)
482 then.Equals(t, NoneLevel, none)
483 }
484
485 func TestAutoLevelForChangeBadTemplate(t *testing.T) {
486 kc := KindConfig{AutoLevel: "{{ no_such_func }}"}
487 _, err := kc.AutoLevelForChange(NewTemplateCache(), Change{})
488 then.NotNil(t, err)
489 }
490
491 // A template that renders an unknown level (typo / wrong case) must error, not no-op.
492 func TestErrorAutoLevelForChangeInvalidRender(t *testing.T) {
493 kc := KindConfig{AutoLevel: `{{ "Major" }}`}
494 _, err := kc.AutoLevelForChange(NewTemplateCache(), Change{})
495 then.Err(t, ErrInvalidAutoLevel, err)
496 }
497
498 // A conditional with no else renders empty and must error clearly.
499 func TestErrorAutoLevelForChangeEmptyRender(t *testing.T) {
500 kc := KindConfig{AutoLevel: `{{if .Custom.Breaking}}major{{end}}`}
501 _, err := kc.AutoLevelForChange(NewTemplateCache(), Change{})
502 then.NotNil(t, err)
503 }