Debian Codegraph @

cmd/diff_test.go

raw
1 package cmd
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/miniscruff/changie/core"
8 "github.com/miniscruff/changie/then"
9 )
10
11 func diffConfig() *core.Config {
12 return &core.Config{
13 ChangesDir: "chgs",
14 UnreleasedDir: "unrel",
15 HeaderPath: "head.tpl.md",
16 ChangelogPath: "changelog.md",
17 VersionExt: "md",
18 VersionFormat: "",
19 KindFormat: "",
20 ChangeFormat: "",
21 Kinds: []core.KindConfig{},
22 }
23 }
24
25 func TestDiff(t *testing.T) {
26 cfg := diffConfig()
27 then.WithTempDirConfig(t, cfg)
28
29 then.WriteFile(t, []byte("1\n"), cfg.ChangesDir, "v0.1.0.md")
30 then.WriteFile(t, []byte("2\n"), cfg.ChangesDir, "v0.2.0.md")
31 then.WriteFile(t, []byte("3\n"), cfg.ChangesDir, "v0.3.0.md")
32 then.WriteFile(t, []byte("4\n"), cfg.ChangesDir, "v1.0.0.md")
33 then.WriteFile(t, []byte("5\n"), cfg.ChangesDir, "v1.1.0.md")
34
35 t.Run("with N", func(t *testing.T) {
36 expected := "5\n4\n"
37
38 cmd := NewDiff()
39
40 builder := strings.Builder{}
41 cmd.SetOut(&builder)
42
43 err := cmd.Run(cmd.Command, []string{"2"})
44 then.Nil(t, err)
45 then.Equals(t, expected, builder.String())
46 })
47
48 t.Run("with triple dots", func(t *testing.T) {
49 expected := "4\n3\n2\n"
50
51 cmd := NewDiff()
52
53 builder := strings.Builder{}
54 cmd.SetOut(&builder)
55
56 err := cmd.Run(cmd.Command, []string{"v0.2.0...v1.0.0"})
57 then.Nil(t, err)
58 then.Equals(t, expected, builder.String())
59 })
60
61 t.Run("with constraint", func(t *testing.T) {
62 expected := "5\n4\n"
63
64 cmd := NewDiff()
65
66 builder := strings.Builder{}
67 cmd.SetOut(&builder)
68
69 err := cmd.Run(cmd.Command, []string{">=v1"})
70 then.Nil(t, err)
71 then.Equals(t, expected, builder.String())
72 })
73 }