Debian Codegraph @

cmd/next_test.go

raw
1 package cmd
2
3 import (
4 "bytes"
5 "strings"
6 "testing"
7
8 "github.com/miniscruff/changie/core"
9 "github.com/miniscruff/changie/then"
10 )
11
12 func nextTestConfig() *core.Config {
13 return &core.Config{
14 ChangesDir: "chgs",
15 UnreleasedDir: "unrel",
16 HeaderPath: "head.tpl.md",
17 ChangelogPath: "changelog.md",
18 VersionExt: "md",
19 VersionFormat: "",
20 KindFormat: "",
21 ChangeFormat: "",
22 Kinds: []core.KindConfig{},
23 }
24 }
25
26 func TestNextVersionWithPatch(t *testing.T) {
27 cfg := nextTestConfig()
28 then.WithTempDirConfig(t, cfg)
29
30 next := NewNext(core.NewTemplateCache())
31 builder := strings.Builder{}
32
33 next.SetOut(&builder)
34
35 // major and minor are not tested directly
36 // as next version is tested in utils
37 then.CreateFile(t, cfg.ChangesDir, "v0.0.1.md")
38 then.CreateFile(t, cfg.ChangesDir, "v0.1.0.md")
39 then.CreateFile(t, cfg.ChangesDir, "head.tpl.md")
40
41 err := next.Run(next.Command, []string{"patch"})
42 then.Nil(t, err)
43 then.Equals(t, "v0.1.1", builder.String())
44 }
45
46 func TestNextVersionWithProject(t *testing.T) {
47 cfg := nextTestConfig()
48 cfg.ProjectsVersionSeparator = "|"
49 cfg.Projects = []core.ProjectConfig{
50 {
51 Label: "W things",
52 Key: "w",
53 },
54 }
55 then.WithTempDirConfig(t, cfg)
56
57 builder := strings.Builder{}
58 next := NewNext(core.NewTemplateCache())
59 next.Project = "w"
60
61 next.SetOut(&builder)
62
63 // major and minor are not tested directly
64 // as next version is tested in utils
65 then.CreateFile(t, cfg.ChangesDir, "w", "v0.0.1.md")
66 then.CreateFile(t, cfg.ChangesDir, "w", "v0.1.0.md")
67 then.CreateFile(t, cfg.ChangesDir, "w", "head.tpl.md")
68
69 err := next.Run(next.Command, []string{"patch"})
70 then.Nil(t, err)
71 then.Equals(t, "w|v0.1.1", builder.String())
72 }
73
74 func TestNextVersionWithProjectBadProject(t *testing.T) {
75 cfg := nextTestConfig()
76 cfg.ProjectsVersionSeparator = "|"
77 cfg.Projects = []core.ProjectConfig{
78 {
79 Label: "W things",
80 Key: "w",
81 },
82 }
83 then.WithTempDirConfig(t, cfg)
84
85 builder := strings.Builder{}
86 next := NewNext(core.NewTemplateCache())
87 next.Project = "missing_proj"
88
89 next.SetOut(&builder)
90
91 // major and minor are not tested directly
92 // as next version is tested in utils
93 then.CreateFile(t, cfg.ChangesDir, "w", "v0.0.1.md")
94 then.CreateFile(t, cfg.ChangesDir, "w", "v0.1.0.md")
95 then.CreateFile(t, cfg.ChangesDir, "w", "head.tpl.md")
96
97 err := next.Run(next.Command, []string{"patch"})
98 then.NotNil(t, err)
99 }
100
101 func TestNextVersionWithAuto(t *testing.T) {
102 cfg := nextTestConfig()
103 cfg.Kinds = []core.KindConfig{
104 {
105 Label: "Feature",
106 AutoLevel: core.MinorLevel,
107 },
108 }
109
110 then.WithTempDirConfig(t, cfg)
111
112 builder := strings.Builder{}
113 next := NewNext(core.NewTemplateCache())
114
115 next.SetOut(&builder)
116 then.CreateFile(t, cfg.ChangesDir, "v0.0.1.md")
117 then.CreateFile(t, cfg.ChangesDir, "v0.1.0.md")
118 then.CreateFile(t, cfg.ChangesDir, "head.tpl.md")
119
120 var changeBytes bytes.Buffer
121
122 minorChange := core.Change{
123 Kind: "Feature",
124 }
125 _, err := minorChange.WriteTo(&changeBytes)
126 then.Nil(t, err)
127 then.WriteFile(t, changeBytes.Bytes(), cfg.ChangesDir, cfg.UnreleasedDir, "a.yaml")
128
129 err = next.Run(next.Command, []string{"auto"})
130 then.Nil(t, err)
131 then.Equals(t, "v0.2.0", builder.String())
132 }
133
134 func TestNextVersionWithPrereleaseAndMeta(t *testing.T) {
135 cfg := nextTestConfig()
136 then.WithTempDirConfig(t, cfg)
137
138 next := NewNext(core.NewTemplateCache())
139 builder := strings.Builder{}
140
141 next.Prerelease = []string{"b1"}
142 next.Meta = []string{"hash"}
143
144 next.SetOut(&builder)
145 then.CreateFile(t, cfg.ChangesDir, "v0.0.1.md")
146 then.CreateFile(t, cfg.ChangesDir, "v0.1.0.md")
147 then.CreateFile(t, cfg.ChangesDir, "head.tpl.md")
148
149 err := next.Run(next.Command, []string{"patch"})
150 then.Nil(t, err)
151 then.Equals(t, "v0.1.1-b1+hash", builder.String())
152 }
153
154 func TestNextVersionWithoutAnyChangesIsV1(t *testing.T) {
155 cfg := nextTestConfig()
156 then.WithTempDirConfig(t, cfg)
157
158 builder := strings.Builder{}
159
160 next := NewNext(core.NewTemplateCache())
161 next.SetOut(&builder)
162
163 err := next.Run(next.Command, []string{"major"})
164 then.Nil(t, err)
165 then.Equals(t, "v1.0.0", builder.String())
166 }
167
168 func TestErrorNextVersionBadConfig(t *testing.T) {
169 then.WithTempDir(t)
170
171 next := NewNext(core.NewTemplateCache())
172 builder := strings.Builder{}
173
174 next.SetOut(&builder)
175
176 err := next.Run(next.Command, []string{"major"})
177 then.NotNil(t, err)
178 }
179
180 func TestErrorNextPartNotSupported(t *testing.T) {
181 cfg := nextTestConfig()
182 then.WithTempDirConfig(t, cfg)
183
184 next := NewNext(core.NewTemplateCache())
185 builder := strings.Builder{}
186
187 next.SetOut(&builder)
188 then.CreateFile(t, cfg.ChangesDir, "v0.0.1.md")
189
190 err := next.Run(next.Command, []string{"notsupported"})
191 then.NotNil(t, err)
192 }
193
194 func TestErrorNextUnableToGetChanges(t *testing.T) {
195 cfg := nextTestConfig()
196 then.WithTempDirConfig(t, cfg)
197
198 next := NewNext(core.NewTemplateCache())
199 builder := strings.Builder{}
200 aVer := []byte("not a valid change")
201
202 next.SetOut(&builder)
203 then.WriteFile(t, aVer, cfg.ChangesDir, cfg.UnreleasedDir, "a.yaml")
204
205 // bad yaml will fail to load changes
206 err := next.Run(next.Command, []string{"auto"})
207 then.NotNil(t, err)
208 }