Debian Codegraph @

cmd/new_test.go

raw
1 package cmd
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9 "time"
10
11 "github.com/miniscruff/changie/core"
12 "github.com/miniscruff/changie/then"
13 )
14
15 func newTestConfig() *core.Config {
16 return &core.Config{
17 ChangesDir: "news",
18 UnreleasedDir: "future",
19 HeaderPath: "header.rst",
20 ChangelogPath: "news.md",
21 VersionExt: "md",
22 VersionFormat: "## {{.Version}}",
23 KindFormat: "### {{.Kind}}",
24 ChangeFormat: "* {{.Body}}",
25 EnvPrefix: "ENVPREFIX_",
26 Kinds: []core.KindConfig{
27 {Label: "added"},
28 {Label: "removed"},
29 {Label: "other"},
30 },
31 }
32 }
33
34 func newMockTime() time.Time {
35 return time.Date(2021, 5, 22, 13, 30, 10, 5, time.UTC)
36 }
37
38 func TestNewWithEnvVars(t *testing.T) {
39 cfg := newTestConfig()
40 cfg.Post = []core.PostProcessConfig{
41 {Key: "TestPost", Value: "{{.Env.TESTCONTENT}}"},
42 }
43 then.WithTempDirConfig(t, cfg)
44 reader, writer := then.WithReadWritePipe(t)
45
46 t.Setenv("ENVPREFIX_TESTCONTENT", "Test content")
47 // we need to override this value as it would fail in CI with the interactive system
48 // but is ok here as we override stdin and stdout anyway
49 t.Setenv("CI", "false")
50
51 then.DelayWrite(
52 t, writer,
53 []byte{106, 13},
54 []byte("a message with testcontent"),
55 []byte{13},
56 )
57
58 cmd := NewNew(
59 newMockTime,
60 core.NewTemplateCache(),
61 )
62 cmd.SetIn(reader)
63
64 then.Nil(t, os.MkdirAll(filepath.Join(cfg.ChangesDir, cfg.UnreleasedDir), 0755))
65
66 err := cmd.Run(cmd.Command, nil)
67 then.Nil(t, err)
68
69 futurePath := filepath.Join(cfg.ChangesDir, cfg.UnreleasedDir)
70 fileInfos, err := os.ReadDir(futurePath)
71 then.Nil(t, err)
72 then.Equals(t, 1, len(fileInfos))
73 then.Equals(t, ".yaml", filepath.Ext(fileInfos[0].Name()))
74
75 changeContent := fmt.Sprintf(
76 "kind: removed\nbody: a message with testcontent\ntime: %s\ncustom:\n TestPost: Test content\n",
77 newMockTime().Format(time.RFC3339Nano),
78 )
79
80 then.FileExists(t, futurePath, fileInfos[0].Name())
81 then.FileContents(t, changeContent, futurePath, fileInfos[0].Name())
82 }
83
84 func TestNewWithCustomEnvVars(t *testing.T) {
85 cfg := newTestConfig()
86 cfg.CustomChoices = []core.Custom{
87 {
88 Key: "Author",
89 Type: core.CustomString,
90 Optional: false,
91 Label: "author",
92 },
93 }
94
95 then.WithTempDirConfig(t, cfg)
96 reader, writer := then.WithReadWritePipe(t)
97
98 t.Setenv("ENVPREFIX_CUSTOM_Author", "me")
99 // we need to override this value as it would fail in CI with the interactive system
100 // but is ok here as we override stdin and stdout anyway
101 t.Setenv("CI", "false")
102
103 then.DelayWrite(
104 t, writer,
105 []byte{106, 13},
106 []byte("a message with testcontent"),
107 []byte{13},
108 )
109
110 cmd := NewNew(
111 newMockTime,
112 core.NewTemplateCache(),
113 )
114 cmd.SetIn(reader)
115
116 then.Nil(t, os.MkdirAll(filepath.Join(cfg.ChangesDir, cfg.UnreleasedDir), 0755))
117
118 err := cmd.Run(cmd.Command, nil)
119 then.Nil(t, err)
120
121 futurePath := filepath.Join(cfg.ChangesDir, cfg.UnreleasedDir)
122 fileInfos, err := os.ReadDir(futurePath)
123 then.Nil(t, err)
124 then.Equals(t, 1, len(fileInfos))
125 then.Equals(t, ".yaml", filepath.Ext(fileInfos[0].Name()))
126
127 changeContent := fmt.Sprintf(
128 "kind: removed\nbody: a message with testcontent\ntime: %s\ncustom:\n Author: me\n",
129 newMockTime().Format(time.RFC3339Nano),
130 )
131
132 then.FileExists(t, futurePath, fileInfos[0].Name())
133 then.FileContents(t, changeContent, futurePath, fileInfos[0].Name())
134 }
135
136 func TestNewCreatesNewFileAfterPrompts(t *testing.T) {
137 // we need to override this value as it would fail in CI with the interactive system
138 // but is ok here as we override stdin and stdout anyway
139 t.Setenv("CI", "false")
140
141 cfg := newTestConfig()
142 then.WithTempDirConfig(t, cfg)
143
144 reader, writer := then.WithReadWritePipe(t)
145 then.DelayWrite(
146 t, writer,
147 []byte{106, 13},
148 []byte("a message"),
149 []byte{13},
150 )
151
152 cmd := NewNew(
153 newMockTime,
154 core.NewTemplateCache(),
155 )
156 cmd.SetIn(reader)
157
158 // Do not create the unreleased dir, let new do it for us.
159
160 err := cmd.Run(cmd.Command, nil)
161 then.Nil(t, err)
162
163 futurePath := filepath.Join(cfg.ChangesDir, cfg.UnreleasedDir)
164 fileInfos, err := os.ReadDir(futurePath)
165 then.Nil(t, err)
166 then.Equals(t, 1, len(fileInfos))
167 then.Equals(t, ".yaml", filepath.Ext(fileInfos[0].Name()))
168
169 changeContent := fmt.Sprintf(
170 "kind: removed\nbody: a message\ntime: %s\n",
171 newMockTime().Format(time.RFC3339Nano),
172 )
173
174 then.FileExists(t, futurePath, fileInfos[0].Name())
175 then.FileContents(t, changeContent, futurePath, fileInfos[0].Name())
176 }
177
178 func TestErrorNewBadCustomValues(t *testing.T) {
179 cfg := newTestConfig()
180 then.WithTempDirConfig(t, cfg)
181
182 cmd := NewNew(
183 newMockTime,
184 core.NewTemplateCache(),
185 )
186 cmd.Custom = []string{"bad-format"}
187
188 err := cmd.Run(cmd.Command, nil)
189 then.NotNil(t, err)
190 }
191
192 func TestErrorOnBadConfig(t *testing.T) {
193 then.WithTempDir(t)
194
195 cmd := NewNew(
196 newMockTime,
197 core.NewTemplateCache(),
198 )
199
200 err := cmd.Run(cmd.Command, nil)
201 then.NotNil(t, err)
202 }
203
204 func TestErrorNewOnBadPrompts(t *testing.T) {
205 cfg := newTestConfig()
206 then.WithTempDirConfig(t, cfg)
207 reader, writer := then.WithReadWritePipe(t)
208
209 cmd := NewNew(
210 newMockTime,
211 core.NewTemplateCache(),
212 )
213 cmd.SetIn(reader)
214
215 then.DelayWrite(
216 t, writer,
217 []byte{106, 13},
218 []byte("a message"),
219 []byte{3}, // 3=ctrl+c to quit
220 )
221
222 err := cmd.Run(cmd.Command, nil)
223 then.NotNil(t, err)
224 }
225
226 func TestErrorNewFragmentTemplate(t *testing.T) {
227 cfg := newTestConfig()
228 cfg.FragmentFileFormat = "{{...asdf}}"
229 then.WithTempDirConfig(t, cfg)
230 reader, writer := then.WithReadWritePipe(t)
231
232 cmd := NewNew(
233 newMockTime,
234 core.NewTemplateCache(),
235 )
236 cmd.SetIn(reader)
237
238 then.DelayWrite(
239 t, writer,
240 []byte{106, 13},
241 []byte("a message"),
242 []byte{13},
243 )
244
245 err := cmd.Run(cmd.Command, nil)
246 then.NotNil(t, err)
247 }
248
249 func TestNewOutputsToCmdOutWhenDry(t *testing.T) {
250 // we need to override this value as it would fail in CI with the interactive system
251 // but is ok here as we override stdin and stdout anyway
252 t.Setenv("CI", "false")
253
254 cfg := newTestConfig()
255 cfg.Kinds = []core.KindConfig{}
256 then.WithTempDirConfig(t, cfg)
257 reader, writer := then.WithReadWritePipe(t)
258
259 outWriter := strings.Builder{}
260 cmd := NewNew(
261 newMockTime,
262 core.NewTemplateCache(),
263 )
264 cmd.DryRun = true
265 cmd.BodyEditor = false
266 cmd.SetIn(reader)
267 cmd.SetOut(&outWriter)
268
269 then.DelayWrite(
270 t, writer,
271 []byte("another body"),
272 []byte{13},
273 )
274
275 changeContent := fmt.Sprintf(
276 "body: another body\ntime: %s\n",
277 newMockTime().Format(time.RFC3339Nano),
278 )
279
280 err := cmd.Run(cmd.Command, nil)
281 then.Nil(t, err)
282 then.Equals(t, changeContent, outWriter.String())
283 }
284
285 func TestNewFragmentTemplateSlash(t *testing.T) {
286 // we need to override this value as it would fail in CI with the interactive system
287 // but is ok here as we override stdin and stdout anyway
288 t.Setenv("CI", "false")
289
290 cfg := newTestConfig()
291 cfg.Components = []string{"test/component"}
292 then.WithTempDirConfig(t, cfg)
293 reader, writer := then.WithReadWritePipe(t)
294
295 then.DelayWrite(
296 t, writer,
297 []byte{106, 13},
298 []byte{106, 13},
299 []byte("a message"),
300 []byte{13},
301 )
302
303 cmd := NewNew(
304 newMockTime,
305 core.NewTemplateCache(),
306 )
307 cmd.SetIn(reader)
308
309 then.Nil(t, os.MkdirAll(filepath.Join(cfg.ChangesDir, cfg.UnreleasedDir), 0755))
310
311 err := cmd.Run(cmd.Command, nil)
312 then.Nil(t, err)
313
314 futurePath := filepath.Join(cfg.ChangesDir, cfg.UnreleasedDir)
315 fileInfos, err := os.ReadDir(futurePath)
316 then.Nil(t, err)
317 then.Equals(t, 1, len(fileInfos))
318 then.Equals(t, ".yaml", filepath.Ext(fileInfos[0].Name()))
319
320 changeContent := fmt.Sprintf(
321 "component: test/component\nkind: removed\nbody: a message\ntime: %s\n",
322 newMockTime().Format(time.RFC3339Nano),
323 )
324
325 then.FileExists(t, futurePath, fileInfos[0].Name())
326 then.FileContents(t, changeContent, futurePath, fileInfos[0].Name())
327 }
328
329 func TestPromptEnabled(t *testing.T) {
330 t.Run("prompts enabled by default", func(t *testing.T) {
331 n := NewNew(nil, nil)
332
333 t.Setenv("CI", "false")
334 then.True(t, n.parsePromptEnabled())
335 })
336
337 t.Run("prompts disabled with flag", func(t *testing.T) {
338 n := NewNew(nil, nil)
339 n.Interactive = false
340
341 t.Setenv("CI", "false")
342 then.False(t, n.parsePromptEnabled())
343 })
344
345 t.Run("prompts disabled with CI env var", func(t *testing.T) {
346 n := NewNew(nil, nil)
347
348 t.Setenv("CI", "true")
349 then.False(t, n.parsePromptEnabled())
350 })
351 }