Debian Codegraph @

cmd/batch_test.go

raw
1 package cmd
2
3 import (
4 "fmt"
5 "io/fs"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10 "time"
11
12 "gopkg.in/yaml.v3"
13
14 "github.com/miniscruff/changie/core"
15 "github.com/miniscruff/changie/then"
16 )
17
18 var changeIncrementer = 0
19
20 func batchTestConfig() *core.Config {
21 return &core.Config{
22 ChangesDir: "news",
23 UnreleasedDir: "future",
24 HeaderPath: "",
25 ChangelogPath: "news.md",
26 VersionFileFormat: "{{.Version}}.md",
27 VersionExt: "md",
28 VersionFormat: "## {{.Version}}",
29 KindFormat: "### {{.Kind}}",
30 ChangeFormat: "* {{.Body}}",
31 FragmentFileFormat: "",
32 Kinds: []core.KindConfig{
33 {Label: "added"},
34 {Label: "removed"},
35 {Label: "other"},
36 },
37 }
38 }
39
40 // writeChangeFile will write a change file with an auto-incrementing index to prevent
41 // same second clobbering
42 func writeChangeFile(t *testing.T, cfg *core.Config, change *core.Change) {
43 // set our time as an arbitrary amount from jan 1 2000 so
44 // each change is 1 hour later then the last
45 if change.Time.Year() == 0 {
46 diff := time.Duration(changeIncrementer) * time.Hour
47 change.Time = time.Date(2000, 0, 0, 0, 0, 0, 0, time.UTC).Add(diff)
48 }
49
50 bs, _ := yaml.Marshal(&change)
51 name := fmt.Sprintf("change-%d.yaml", changeIncrementer)
52 changeIncrementer++
53
54 if change.Filename == "" {
55 change.Filename = filepath.Join(cfg.ChangesDir, cfg.UnreleasedDir, name)
56 }
57
58 then.WriteFile(t, bs, change.Filename)
59 }
60
61 func TestBatchCanBatch(t *testing.T) {
62 cfg := batchTestConfig()
63 // declared path but missing is accepted
64 cfg.VersionHeaderPath = "header.md"
65
66 then.WithTempDirConfig(t, cfg)
67
68 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "A"})
69 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "B"})
70 writeChangeFile(t, cfg, &core.Change{Kind: "removed", Body: "C"})
71
72 batch := NewBatch(time.Now, core.NewTemplateCache())
73 err := batch.Run(batch.Command, []string{"v0.2.0"})
74 then.Nil(t, err)
75
76 verContents := `## v0.2.0
77 ### added
78 * A
79 * B
80 ### removed
81 * C`
82
83 then.FileContents(t, verContents, cfg.ChangesDir, "v0.2.0.md")
84 then.DirectoryFileCount(t, 0, cfg.ChangesDir, cfg.UnreleasedDir)
85 }
86
87 func TestBatchCanBatchWithProject(t *testing.T) {
88 cfg := batchTestConfig()
89 cfg.VersionFileFormat = "{{.Version}}-custom-format.md"
90 cfg.Projects = []core.ProjectConfig{
91 {
92 Label: "A",
93 Key: "a",
94 },
95 }
96 // declared path but missing is accepted
97 cfg.VersionHeaderPath = "header.md"
98
99 then.WithTempDirConfig(t, cfg)
100
101 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "A", Project: "a"})
102 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "B", Project: "b"})
103 writeChangeFile(t, cfg, &core.Change{Kind: "removed", Body: "C", Project: "a"})
104
105 batch := NewBatch(time.Now, core.NewTemplateCache())
106 batch.Project = "a"
107 err := batch.Run(batch.Command, []string{"v0.2.0"})
108 then.Nil(t, err)
109
110 verContents := `## v0.2.0
111 ### added
112 * A
113 ### removed
114 * C`
115
116 then.FileContents(t, verContents, cfg.ChangesDir, "a", "v0.2.0-custom-format.md")
117 then.DirectoryFileCount(t, 1, cfg.ChangesDir, cfg.UnreleasedDir)
118 }
119
120 func TestBatchProjectFailsIfUnableToFindProject(t *testing.T) {
121 cfg := batchTestConfig()
122 cfg.Projects = []core.ProjectConfig{
123 {
124 Label: "A",
125 Key: "a",
126 },
127 }
128
129 then.WithTempDirConfig(t, cfg)
130
131 batch := NewBatch(time.Now, core.NewTemplateCache())
132 batch.Project = "not found"
133
134 err := batch.Run(batch.Command, []string{"v0.2.0"})
135 then.NotNil(t, err)
136 }
137
138 func TestBatchCanAddNewLinesBeforeAndAfterKindHeader(t *testing.T) {
139 cfg := batchTestConfig()
140 // declared path but missing is accepted
141 cfg.VersionHeaderPath = "header.md"
142 cfg.Newlines.BeforeKind = 2
143 cfg.Newlines.AfterKind = 2
144
145 then.WithTempDirConfig(t, cfg)
146
147 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "A"})
148 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "B"})
149 writeChangeFile(t, cfg, &core.Change{Kind: "removed", Body: "C"})
150
151 batch := NewBatch(time.Now, core.NewTemplateCache())
152 err := batch.Run(batch.Command, []string{"v0.2.0"})
153 then.Nil(t, err)
154
155 verContents := `## v0.2.0
156
157
158 ### added
159
160
161 * A
162 * B
163
164
165 ### removed
166
167
168 * C`
169
170 then.FileContents(t, verContents, cfg.ChangesDir, "v0.2.0.md")
171 then.DirectoryFileCount(t, 0, cfg.ChangesDir, cfg.UnreleasedDir)
172 }
173
174 func TestBatchErrorStandardBadWriter(t *testing.T) {
175 cfg := batchTestConfig()
176 then.WithTempDirConfig(t, cfg)
177
178 w := then.NewErrWriter()
179
180 batch := NewBatch(time.Now, core.NewTemplateCache())
181 batch.writer = w
182 err := batch.WriteTemplate(
183 cfg.VersionFormat,
184 2, // tries to write some before lines and fails
185 cfg.Newlines.AfterVersion,
186 "v0.2.0",
187 )
188 w.Raised(t, err)
189 }
190
191 func TestBatchComplexVersion(t *testing.T) {
192 cfg := batchTestConfig()
193 cfg.EnvPrefix = "TEST_CHANGIE_"
194 cfg.HeaderFormat = "{{ bodies .Changes | len }} changes this release"
195 cfg.ChangeFormat = "* {{.Body}} by {{.Custom.Author}}"
196 cfg.FooterFormat = `### contributors
197 {{- range (customs .Changes "Author" | uniq) }}
198 * [{{.}}](https://github.com/{{.}})
199 {{- end}}
200 env: {{.Env.ENV_KEY}}`
201 cfg.VersionFormat = "## {{.VersionNoPrefix}}"
202
203 then.WithTempDirConfig(t, cfg)
204 t.Setenv("TEST_CHANGIE_ENV_KEY", "env value")
205
206 batch := NewBatch(time.Now, core.NewTemplateCache())
207 batch.IncludeDirs = []string{"beta"}
208 batch.Prerelease = []string{"b1"}
209 batch.Meta = []string{"hash"}
210
211 writeChangeFile(t, cfg,
212 &core.Change{
213 Kind: "added",
214 Body: "D",
215 Custom: map[string]string{
216 "Author": "miniscruff",
217 },
218 },
219 )
220
221 betaChange := core.Change{
222 Kind: "added",
223 Body: "E",
224 Custom: map[string]string{
225 "Author": "otherAuthor",
226 },
227 Time: time.Now(), // make sure our beta change is more recent then our other changes
228 }
229
230 then.WriteFileTo(t, betaChange, cfg.ChangesDir, "beta", "change.yaml")
231
232 err := batch.Run(batch.Command, []string{"v0.2.0"})
233 then.Nil(t, err)
234
235 verContents := `## 0.2.0-b1+hash
236 2 changes this release
237 ### added
238 * D by miniscruff
239 * E by otherAuthor
240 ### contributors
241 * [miniscruff](https://github.com/miniscruff)
242 * [otherAuthor](https://github.com/otherAuthor)
243 env: env value`
244 then.FileContents(t, verContents, cfg.ChangesDir, "v0.2.0-b1+hash.md")
245 then.DirectoryFileCount(t, 0, cfg.ChangesDir, cfg.UnreleasedDir)
246 then.FileNotExists(t, cfg.ChangesDir, "beta", "change.yaml")
247 }
248
249 func TestBatchDryRun(t *testing.T) {
250 cfg := batchTestConfig()
251 then.WithTempDirConfig(t, cfg)
252
253 batch := NewBatch(time.Now, core.NewTemplateCache())
254 batch.DryRun = true
255
256 var builder strings.Builder
257
258 batch.SetOut(&builder)
259 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "D"})
260 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "E"})
261 writeChangeFile(t, cfg, &core.Change{Kind: "removed", Body: "F"})
262
263 err := batch.Run(batch.Command, []string{"v0.2.0"})
264 then.Nil(t, err)
265
266 verContents := `## v0.2.0
267 ### added
268 * D
269 * E
270 ### removed
271 * F`
272 then.Equals(t, verContents, builder.String())
273 then.DirectoryFileCount(t, 3, cfg.ChangesDir, cfg.UnreleasedDir)
274 }
275
276 func TestBatchDryRunWithKeys(t *testing.T) {
277 cfg := batchTestConfig()
278 cfg.Kinds[0].Label = ":fire: Added"
279 cfg.Kinds[0].Key = "added"
280 then.WithTempDirConfig(t, cfg)
281
282 batch := NewBatch(time.Now, core.NewTemplateCache())
283 batch.DryRun = true
284
285 var builder strings.Builder
286
287 batch.SetOut(&builder)
288 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "D"})
289 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "E"})
290 writeChangeFile(t, cfg, &core.Change{Kind: "removed", Body: "F"})
291
292 err := batch.Run(batch.Command, []string{"v0.2.0"})
293 then.Nil(t, err)
294
295 verContents := `## v0.2.0
296 ### :fire: Added
297 * D
298 * E
299 ### removed
300 * F`
301 then.Equals(t, verContents, builder.String())
302 then.DirectoryFileCount(t, 3, cfg.ChangesDir, cfg.UnreleasedDir)
303 }
304
305 func TestBatchRemovePrereleases(t *testing.T) {
306 cfg := batchTestConfig()
307 then.WithTempDirConfig(t, cfg)
308
309 batch := NewBatch(time.Now, core.NewTemplateCache())
310 batch.RemovePrereleases = true
311
312 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "A"})
313 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "B"})
314 writeChangeFile(t, cfg, &core.Change{Kind: "removed", Body: "C"})
315
316 then.CreateFile(t, cfg.ChangesDir, "v0.1.2-a1.md")
317 then.CreateFile(t, cfg.ChangesDir, "v0.1.2-a2.md")
318
319 err := batch.Run(batch.Command, []string{"v0.2.0"})
320 then.Nil(t, err)
321
322 infos, err := os.ReadDir(cfg.ChangesDir)
323 then.Nil(t, err)
324 then.Equals(t, cfg.UnreleasedDir, infos[0].Name())
325 then.Equals(t, "v0.2.0.md", infos[1].Name())
326 then.Equals(t, 2, len(infos))
327 }
328
329 func TestBatchKeepChangeFiles(t *testing.T) {
330 cfg := batchTestConfig()
331 then.WithTempDirConfig(t, cfg)
332
333 batch := NewBatch(time.Now, core.NewTemplateCache())
334 batch.KeepFragments = true
335
336 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "A"})
337 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "B"})
338 writeChangeFile(t, cfg, &core.Change{Kind: "removed", Body: "C"})
339
340 err := batch.Run(batch.Command, []string{"v0.2.0"})
341 then.Nil(t, err)
342 then.DirectoryFileCount(t, 3, cfg.ChangesDir, cfg.UnreleasedDir)
343 }
344
345 func TestBatchWithHeadersAndFooters(t *testing.T) {
346 cfg := batchTestConfig()
347 cfg.VersionHeaderPath = "h2.md"
348 cfg.VersionFooterPath = "f2.md"
349 cfg.HeaderFormat = "header format"
350 cfg.FooterFormat = "footer format"
351 then.WithTempDirConfig(t, cfg)
352
353 batch := NewBatch(time.Now, core.NewTemplateCache())
354 batch.VersionHeaderPath = "h1.md"
355 batch.VersionFooterPath = "f1.md"
356
357 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "A"})
358 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "B"})
359 writeChangeFile(t, cfg, &core.Change{Kind: "removed", Body: "C"})
360
361 then.WriteFile(t, []byte("first header\n"), cfg.ChangesDir, cfg.UnreleasedDir, "h1.md")
362 then.WriteFile(t, []byte("second header\n"), cfg.ChangesDir, cfg.UnreleasedDir, "h2.md")
363 then.WriteFile(t, []byte("first footer\n"), cfg.ChangesDir, cfg.UnreleasedDir, "f1.md")
364 then.WriteFile(t, []byte("second footer\n"), cfg.ChangesDir, cfg.UnreleasedDir, "f2.md")
365
366 err := batch.Run(batch.Command, []string{"v0.2.0"})
367 then.Nil(t, err)
368
369 verContents := `## v0.2.0
370 first header
371
372 second header
373
374 header format
375 ### added
376 * A
377 * B
378 ### removed
379 * C
380 footer format
381 first footer
382
383 second footer
384 `
385 then.FileContents(t, verContents, cfg.ChangesDir, "v0.2.0.md")
386 }
387
388 func TestBatchErrorBadChanges(t *testing.T) {
389 cfg := batchTestConfig()
390 then.WithTempDirConfig(t, cfg)
391
392 batch := NewBatch(time.Now, core.NewTemplateCache())
393 aVer := []byte("not a valid change")
394 then.WriteFile(t, aVer, cfg.ChangesDir, cfg.UnreleasedDir, "a.yaml")
395
396 err := batch.Run(batch.Command, []string{"v0.1.1"})
397 then.NotNil(t, err)
398 }
399
400 func TestBatchErrorNoChangesAndNotAllowed(t *testing.T) {
401 cfg := batchTestConfig()
402 then.WithTempDirConfig(t, cfg)
403
404 err := os.MkdirAll(filepath.Join("news", "future"), 0777)
405 then.Nil(t, err)
406
407 batch := NewBatch(time.Now, core.NewTemplateCache())
408 batch.AllowNoChanges = false
409
410 err = batch.Run(batch.Command, []string{"v0.1.1"})
411 then.Err(t, errNoChangesNotAllowed, err)
412 }
413
414 func TestBatchOverrideIfForced(t *testing.T) {
415 cfg := batchTestConfig()
416 then.WithTempDirConfig(t, cfg)
417
418 batch := NewBatch(time.Now, core.NewTemplateCache())
419 batch.Force = true
420
421 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "A"})
422
423 err := batch.Run(batch.Command, []string{"v0.2.0"})
424 then.Nil(t, err)
425
426 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "B"})
427
428 err = batch.Run(batch.Command, []string{"v0.2.0"})
429 then.Nil(t, err)
430
431 verContents := `## v0.2.0
432 ### added
433 * B`
434 then.FileContents(t, verContents, cfg.ChangesDir, "v0.2.0.md")
435 }
436
437 func TestBatchErrorIfBatchExists(t *testing.T) {
438 cfg := batchTestConfig()
439 then.WithTempDirConfig(t, cfg)
440
441 batch := NewBatch(time.Now, core.NewTemplateCache())
442
443 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "A"})
444
445 err := batch.Run(batch.Command, []string{"v0.2.0"})
446 then.Nil(t, err)
447
448 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "B"})
449
450 err = batch.Run(batch.Command, []string{"v0.2.0"})
451 then.Err(t, errVersionExists, err)
452 }
453
454 func TestBatchErrorBadVersion(t *testing.T) {
455 cfg := batchTestConfig()
456 then.WithTempDirConfig(t, cfg)
457
458 batch := NewBatch(time.Now, core.NewTemplateCache())
459
460 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "A"})
461
462 err := batch.Run(batch.Command, []string{"---asdfasdf---"})
463 then.Err(t, core.ErrBadVersionOrPart, err)
464 }
465
466 func TestBatchErrorBadLatestVersion(t *testing.T) {
467 cfg := batchTestConfig()
468 cfg.ChangesDir = "../../opt/apt/not/real"
469 then.WithTempDirConfig(t, cfg)
470
471 batch := NewBatch(time.Now, core.NewTemplateCache())
472
473 // fail trying to read a bad directory
474 err := batch.Run(batch.Command, []string{"v0.2.0"})
475 then.NotNil(t, err)
476 }
477
478 func TestBatchErrorBadConfig(t *testing.T) {
479 then.WithTempDir(t)
480 then.WriteFile(t, []byte("not a proper config"), core.ConfigPaths[0])
481
482 batch := NewBatch(time.Now, core.NewTemplateCache())
483 err := batch.Run(batch.Command, []string{"v0.1.0"})
484 then.NotNil(t, err)
485 }
486
487 func TestBatchErrorBadVersionFormat(t *testing.T) {
488 cfg := batchTestConfig()
489 cfg.VersionFormat = "{{bad.format}"
490 then.WithTempDirConfig(t, cfg)
491
492 batch := NewBatch(time.Now, core.NewTemplateCache())
493
494 writeChangeFile(t, cfg, &core.Change{Kind: "removed", Body: "C"})
495
496 err := batch.Run(batch.Command, []string{"v0.11.0"})
497 then.NotNil(t, err)
498 }
499
500 func TestBatchErrorBadKindFormat(t *testing.T) {
501 cfg := batchTestConfig()
502 cfg.KindFormat = "bad {{...buh}}"
503 batch := NewBatch(time.Now, core.NewTemplateCache())
504
505 then.WithTempDirConfig(t, cfg)
506 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "C"})
507
508 err := batch.Run(batch.Command, []string{"v0.2.3"})
509 then.NotNil(t, err)
510
511 _, err = os.Stat(filepath.Join(cfg.ChangesDir, "v0.2.3.md"))
512 then.Err(t, fs.ErrNotExist, err)
513 }
514
515 func TestBatchErrorBadComponentFormat(t *testing.T) {
516 cfg := batchTestConfig()
517 cfg.ComponentFormat = "bad {{...buh}}"
518 batch := NewBatch(time.Now, core.NewTemplateCache())
519
520 then.WithTempDirConfig(t, cfg)
521 writeChangeFile(t, cfg, &core.Change{Component: "ui", Body: "C"})
522
523 err := batch.Run(batch.Command, []string{"v0.2.3"})
524 then.NotNil(t, err)
525 }
526
527 func TestBatchErrorBadChangeFormat(t *testing.T) {
528 cfg := batchTestConfig()
529 cfg.ChangeFormat = "bad {{...buh}}"
530 batch := NewBatch(time.Now, core.NewTemplateCache())
531
532 then.WithTempDirConfig(t, cfg)
533 writeChangeFile(t, cfg, &core.Change{Component: "ui", Body: "C"})
534
535 err := batch.Run(batch.Command, []string{"v0.2.3"})
536 then.NotNil(t, err)
537 }
538
539 func TestBatchWriteChanges(t *testing.T) {
540 cfg := batchTestConfig()
541 then.WithTempDirConfig(t, cfg)
542
543 var builder strings.Builder
544
545 batch := NewBatch(time.Now, core.NewTemplateCache())
546 batch.config = cfg
547 batch.writer = &builder
548
549 changes := []core.Change{
550 {Kind: "added", Body: "w"},
551 {Kind: "added", Body: "x"},
552 {Kind: "removed", Body: "y"},
553 {Kind: "removed", Body: "z"},
554 }
555
556 err := batch.WriteChanges(changes)
557 then.Nil(t, err)
558
559 expected := `
560 ### added
561 * w
562 * x
563 ### removed
564 * y
565 * z`
566 then.Equals(t, expected, builder.String())
567 }
568
569 func TestBatchCreateVersionsWithoutKindHeaders(t *testing.T) {
570 cfg := batchTestConfig()
571 cfg.KindFormat = ""
572 cfg.ChangeFormat = "* {{.Body}} ({{.Kind}})"
573
574 var builder strings.Builder
575
576 batch := NewBatch(time.Now, core.NewTemplateCache())
577 batch.config = cfg
578 batch.writer = &builder
579
580 changes := []core.Change{
581 {Body: "w", Kind: "added"},
582 {Body: "x", Kind: "added"},
583 {Body: "y", Kind: "removed"},
584 {Body: "z", Kind: "removed"},
585 }
586
587 err := batch.WriteChanges(changes)
588 then.Nil(t, err)
589
590 expected := `
591 * w (added)
592 * x (added)
593 * y (removed)
594 * z (removed)`
595 then.Equals(t, expected, builder.String())
596 }
597
598 func TestBatchVersionFileWithComponentHeaders(t *testing.T) {
599 cfg := batchTestConfig()
600 cfg.Components = []string{"linker", "compiler"}
601 cfg.ComponentFormat = "## {{.Component}}"
602 cfg.KindFormat = "### {{.Kind}}"
603 cfg.ChangeFormat = "* {{.Body}}"
604
605 var builder strings.Builder
606
607 batch := NewBatch(time.Now, core.NewTemplateCache())
608 batch.config = cfg
609 batch.writer = &builder
610
611 changes := []core.Change{
612 {Body: "w", Kind: "added", Component: "linker"},
613 {Body: "x", Kind: "added", Component: "linker"},
614 {Body: "y", Kind: "removed", Component: "linker"},
615 {Body: "z", Kind: "removed", Component: "compiler"},
616 }
617
618 err := batch.WriteChanges(changes)
619 then.Nil(t, err)
620
621 expected := `
622 ## linker
623 ### added
624 * w
625 * x
626 ### removed
627 * y
628 ## compiler
629 ### removed
630 * z`
631 then.Equals(t, expected, builder.String())
632 }
633
634 func TestBatchVersionFileWithoutComponentHeadersGroupsByKind(t *testing.T) {
635 cfg := batchTestConfig()
636 cfg.Components = []string{"cli", "web"}
637 cfg.ComponentFormat = ""
638 cfg.KindFormat = "### {{.Kind}}"
639 cfg.ChangeFormat = "* {{.Body}} ({{.Component}})"
640
641 then.WithTempDirConfig(t, cfg)
642
643 writeChangeFile(t, cfg, &core.Change{Body: "cli added", Kind: "added", Component: "cli"})
644 writeChangeFile(t, cfg, &core.Change{Body: "cli removed", Kind: "removed", Component: "cli"})
645 writeChangeFile(t, cfg, &core.Change{Body: "web added", Kind: "added", Component: "web"})
646
647 batch := NewBatch(time.Now, core.NewTemplateCache())
648 err := batch.Run(batch.Command, []string{"v0.2.0"})
649 then.Nil(t, err)
650
651 verContents := `## v0.2.0
652 ### added
653 * cli added (cli)
654 * web added (web)
655 ### removed
656 * cli removed (cli)`
657
658 then.FileContents(t, verContents, cfg.ChangesDir, "v0.2.0.md")
659 }
660
661 func TestBatchClearUnreleasedRemovesUnreleasedFilesIncludingHeader(t *testing.T) {
662 then.WithTempDir(t)
663
664 cfg := batchTestConfig()
665 batch := NewBatch(time.Now, core.NewTemplateCache())
666 batch.config = cfg
667
668 alphaPath := filepath.Join(cfg.ChangesDir, "alpha")
669 betaPath := filepath.Join(cfg.ChangesDir, "beta")
670 batch.IncludeDirs = []string{"alpha", "beta"}
671
672 then.CreateFile(t, cfg.ChangesDir, cfg.UnreleasedDir, ".gitkeep")
673 then.CreateFile(t, cfg.ChangesDir, cfg.UnreleasedDir, "header.md")
674 then.CreateFile(t, alphaPath, ".gitkeep")
675
676 changes := []core.Change{
677 {Kind: "added", Body: "A"},
678 {Kind: "added", Body: "B"},
679 {Kind: "added", Body: "alpha b", Filename: filepath.Join(alphaPath, "b.yaml")},
680 {Kind: "added", Body: "beta c", Filename: filepath.Join(betaPath, "c.yaml")},
681 }
682 for i := range changes {
683 writeChangeFile(t, cfg, &changes[i])
684 }
685
686 err := batch.ClearUnreleased(
687 changes,
688 "",
689 "header.md",
690 "does-not-exist.md",
691 )
692 then.Nil(t, err)
693
694 then.DirectoryFileCount(t, 1, cfg.ChangesDir, cfg.UnreleasedDir)
695 then.DirectoryFileCount(t, 1, alphaPath)
696
697 then.FileNotExists(t, betaPath)
698 }
699
700 func TestBatchClearUnreleasedMovesFilesIncludingHeaderIfSpecified(t *testing.T) {
701 then.WithTempDir(t)
702
703 cfg := batchTestConfig()
704 batch := NewBatch(time.Now, core.NewTemplateCache())
705 batch.config = cfg
706 batch.MoveDir = "beta"
707
708 for _, name := range []string{".gitkeep", "header.md"} {
709 then.CreateFile(t, cfg.ChangesDir, cfg.UnreleasedDir, name)
710 }
711
712 changes := []core.Change{
713 {Kind: "added", Body: "A"},
714 {Kind: "added", Body: "B"},
715 {Kind: "added", Body: "C"},
716 }
717 for i := range changes {
718 writeChangeFile(t, cfg, &changes[i])
719 }
720
721 err := batch.ClearUnreleased(
722 changes,
723 "header.md",
724 "",
725 "does-not-exist.md",
726 )
727 then.Nil(t, err)
728
729 // should of moved the unreleased and header file to beta
730 then.DirectoryFileCount(t, 4, cfg.ChangesDir, "beta")
731 // .gitkeep should remain
732 then.DirectoryFileCount(t, 1, cfg.ChangesDir, cfg.UnreleasedDir)
733 }
734
735 func TestBatchCanAddNewLinesAfterReleaseNotes(t *testing.T) {
736 cfg := batchTestConfig()
737 cfg.Newlines.AfterReleaseNotes = 2
738
739 then.WithTempDirConfig(t, cfg)
740
741 writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "A"})
742 writeChangeFile(t, cfg, &core.Change{Kind: "removed", Body: "B"})
743
744 batch := NewBatch(time.Now, core.NewTemplateCache())
745 err := batch.Run(batch.Command, []string{"v0.2.0"})
746 then.Nil(t, err)
747
748 verContents := `## v0.2.0
749 ### added
750 * A
751 ### removed
752 * B
753
754 `
755 then.FileContents(t, verContents, cfg.ChangesDir, "v0.2.0.md")
756 then.DirectoryFileCount(t, 0, cfg.ChangesDir, cfg.UnreleasedDir)
757 }