Debian Codegraph @

core/prompt_test.go

raw
1 package core
2
3 import (
4 "bytes"
5 "testing"
6 "time"
7
8 "github.com/miniscruff/changie/then"
9 )
10
11 func specificTimeNow() time.Time {
12 return time.Date(2023, time.November, 31, 18, 30, 0, 0, time.UTC)
13 }
14
15 func TestAskPromptsForBody(t *testing.T) {
16 reader, writer := then.WithReadWritePipe(t)
17 then.DelayWrite(
18 t, writer,
19 []byte("body stuff"),
20 []byte{13},
21 )
22
23 config := &Config{}
24 prompts := &Prompts{
25 Config: config,
26 StdinReader: reader,
27 TimeNow: specificTimeNow,
28 Enabled: true,
29 }
30 changes, err := prompts.BuildChanges()
31 then.Nil(t, err)
32
33 then.SliceLen(t, 1, changes)
34 c := changes[0]
35 then.Equals(t, "", c.Component)
36 then.Equals(t, "", c.Kind)
37 then.MapLen(t, 0, c.Custom)
38 then.Equals(t, "body stuff", c.Body)
39 }
40
41 func TestAskPromptsForBodyWithProject(t *testing.T) {
42 reader, writer := then.WithReadWritePipe(t)
43 then.DelayWrite(
44 t, writer,
45 []byte{32},
46 []byte{13},
47 []byte("body stuff"),
48 []byte{13},
49 )
50
51 config := &Config{
52 Projects: []ProjectConfig{
53 {Label: "Client", Key: "client"},
54 {Label: "Other", Key: "other"},
55 },
56 }
57 prompts := &Prompts{
58 Config: config,
59 StdinReader: reader,
60 TimeNow: specificTimeNow,
61 Enabled: true,
62 }
63
64 changes, err := prompts.BuildChanges()
65 then.Nil(t, err)
66
67 then.SliceLen(t, 1, changes)
68 c := changes[0]
69 then.Equals(t, "client", c.Project)
70 then.Equals(t, "", c.Component)
71 then.Equals(t, "", c.Kind)
72 then.Equals(t, 0, len(c.Custom))
73 then.Equals(t, "body stuff", c.Body)
74 }
75
76 func TestAskPromptsForBodyWithProjectErrBadProject(t *testing.T) {
77 reader, _ := then.WithReadWritePipe(t)
78
79 config := &Config{
80 Projects: []ProjectConfig{
81 {Label: "Client", Key: "client"},
82 {Label: "Other", Key: "other"},
83 },
84 }
85
86 prompts := &Prompts{
87 Config: config,
88 StdinReader: reader,
89 TimeNow: specificTimeNow,
90 Projects: []string{"missing"},
91 Enabled: true,
92 }
93
94 _, err := prompts.BuildChanges()
95 then.Err(t, errProjectNotFound, err)
96 }
97
98 func TestAskPromptsFailIfDisabled(t *testing.T) {
99 config := &Config{
100 Projects: []ProjectConfig{
101 {Label: "Client", Key: "client"},
102 {Label: "Other", Key: "other"},
103 },
104 Components: []string{"cli", "tests", "utils"},
105 Kinds: []KindConfig{
106 {Label: "added"},
107 {Label: "changed"},
108 {Label: "removed"},
109 },
110 CustomChoices: []Custom{
111 {Key: "Issue", Type: CustomInt},
112 },
113 }
114
115 t.Run("project", func(t *testing.T) {
116 prompts := &Prompts{
117 Config: config,
118 StdinReader: nil,
119 TimeNow: specificTimeNow,
120 Enabled: false,
121 }
122
123 _, err := prompts.BuildChanges()
124 then.Err(t, errProjectMissingPromptDisabled, err)
125 })
126
127 t.Run("component", func(t *testing.T) {
128 prompts := &Prompts{
129 Config: config,
130 StdinReader: nil,
131 TimeNow: specificTimeNow,
132 Enabled: false,
133 }
134
135 prompts.Projects = []string{"other"}
136 _, err := prompts.BuildChanges()
137 then.Err(t, errComponentMissingPromptDisabled, err)
138 })
139
140 t.Run("kind", func(t *testing.T) {
141 prompts := &Prompts{
142 Config: config,
143 StdinReader: nil,
144 TimeNow: specificTimeNow,
145 Enabled: false,
146 }
147
148 prompts.Projects = []string{"other"}
149 prompts.Component = "cli"
150
151 _, err := prompts.BuildChanges()
152 then.Err(t, errKindMissingPromptDisabled, err)
153 })
154
155 t.Run("body", func(t *testing.T) {
156 prompts := &Prompts{
157 Config: config,
158 StdinReader: nil,
159 TimeNow: specificTimeNow,
160 Enabled: false,
161 }
162
163 prompts.Projects = []string{"other"}
164 prompts.Component = "cli"
165 prompts.Kind = "added"
166
167 _, err := prompts.BuildChanges()
168 then.Err(t, errBodyMissingPromptDisabled, err)
169 })
170
171 t.Run("custom", func(t *testing.T) {
172 prompts := &Prompts{
173 Config: config,
174 StdinReader: nil,
175 TimeNow: specificTimeNow,
176 Enabled: false,
177 }
178
179 prompts.Projects = []string{"other"}
180 prompts.Component = "cli"
181 prompts.Kind = "added"
182 prompts.Body = "some body"
183
184 _, err := prompts.BuildChanges()
185 then.Err(t, errCustomMissingPromptDisabled, err)
186 })
187 }
188
189 func TestAskPromptsForBodyWithProjectErrBadInput(t *testing.T) {
190 reader, writer := then.WithReadWritePipe(t)
191 then.DelayWrite(
192 t, writer,
193 []byte{3},
194 )
195
196 config := &Config{
197 Projects: []ProjectConfig{
198 {Label: "Client", Key: "client"},
199 {Label: "Other", Key: "other"},
200 },
201 }
202 prompts := &Prompts{
203 Config: config,
204 StdinReader: reader,
205 TimeNow: specificTimeNow,
206 Enabled: true,
207 }
208
209 _, err := prompts.BuildChanges()
210 then.NotNil(t, err)
211 }
212
213 func TestAskPromptsForBodyWithProjectErrNoProject(t *testing.T) {
214 reader, writer := then.WithReadWritePipe(t)
215 then.DelayWrite(
216 t, writer,
217 []byte{13},
218 )
219
220 config := &Config{
221 Projects: []ProjectConfig{
222 {Label: "Client", Key: "client"},
223 {Label: "Other", Key: "other"},
224 },
225 }
226 prompts := &Prompts{
227 Config: config,
228 StdinReader: reader,
229 TimeNow: specificTimeNow,
230 Enabled: true,
231 }
232
233 _, err := prompts.BuildChanges()
234 then.Err(t, errProjectRequired, err)
235 }
236
237 func TestAskComponentKindBody(t *testing.T) {
238 reader, writer := then.WithReadWritePipe(t)
239 then.DelayWrite(
240 t, writer,
241 []byte{106, 13},
242 []byte{106, 106, 13},
243 []byte("body here"),
244 []byte{13},
245 )
246
247 config := &Config{
248 Components: []string{"cli", "tests", "utils"},
249 Kinds: []KindConfig{
250 {Label: "added"},
251 {Label: "changed"},
252 {Label: "removed"},
253 },
254 }
255 prompts := &Prompts{
256 Config: config,
257 StdinReader: reader,
258 TimeNow: specificTimeNow,
259 Enabled: true,
260 }
261 changes, err := prompts.BuildChanges()
262 then.Nil(t, err)
263
264 c := changes[0]
265 then.Equals(t, "tests", c.Component)
266 then.Equals(t, "removed", c.Kind)
267 then.Equals(t, len(c.Custom), 0)
268 then.Equals(t, "body here", c.Body)
269 }
270
271 func TestBodyKindPostProcess(t *testing.T) {
272 reader, _ := then.WithReadWritePipe(t)
273
274 config := &Config{
275 Kinds: []KindConfig{
276 {
277 Label: "added",
278 Post: []PostProcessConfig{
279 {Key: "Post", Value: "{{.Body}}+{{.Custom.Issue}}"},
280 },
281 },
282 },
283 CustomChoices: []Custom{
284 {Key: "Issue", Type: CustomInt},
285 },
286 }
287 prompts := &Prompts{
288 Config: config,
289 StdinReader: reader,
290 TimeNow: specificTimeNow,
291 Body: "our body",
292 Kind: "added",
293 Customs: map[string]string{
294 "Issue": "25",
295 },
296 Enabled: true,
297 }
298 changes, err := prompts.BuildChanges()
299 then.Nil(t, err)
300
301 c := changes[0]
302 then.Equals(t, "", c.Component)
303 then.Equals(t, "added", c.Kind)
304 then.Equals(t, "our body", c.Body)
305 then.Equals(t, "25", c.Custom["Issue"])
306 then.Equals(t, "our body+25", c.Custom["Post"])
307 }
308
309 func TestBodyCustom(t *testing.T) {
310 reader, writer := then.WithReadWritePipe(t)
311 then.DelayWrite(
312 t, writer,
313 []byte("body again"),
314 []byte{13},
315 []byte("custom check value"),
316 []byte{13},
317 )
318
319 config := &Config{
320 CustomChoices: []Custom{
321 {Key: "check", Type: CustomString},
322 },
323 }
324
325 prompts := &Prompts{
326 Config: config,
327 StdinReader: reader,
328 TimeNow: specificTimeNow,
329 Enabled: true,
330 }
331 changes, err := prompts.BuildChanges()
332 then.Nil(t, err)
333
334 c := changes[0]
335
336 then.Equals(t, "", c.Component)
337 then.Equals(t, "", c.Kind)
338 then.Equals(t, "custom check value", c.Custom["check"])
339 then.Equals(t, "body again", c.Body)
340 }
341
342 func TestBodyCustomWithExistingCustomValue(t *testing.T) {
343 reader, writer := then.WithReadWritePipe(t)
344 then.DelayWrite(
345 t, writer,
346 []byte("body again"),
347 []byte{13},
348 []byte("256"),
349 []byte{13},
350 )
351
352 config := &Config{
353 CustomChoices: []Custom{
354 {Key: "Issue", Type: CustomString},
355 {Key: "Project", Label: "Custom Project Prompt", Type: CustomString},
356 },
357 }
358 prompts := &Prompts{
359 Config: config,
360 StdinReader: reader,
361 TimeNow: specificTimeNow,
362 Customs: map[string]string{
363 "Project": "Changie",
364 },
365 Enabled: true,
366 }
367 changes, err := prompts.BuildChanges()
368 then.Nil(t, err)
369
370 c := changes[0]
371
372 then.Equals(t, "", c.Component)
373 then.Equals(t, "", c.Kind)
374 then.Equals(t, "256", c.Custom["Issue"])
375 then.Equals(t, "Changie", c.Custom["Project"])
376 then.Equals(t, "body again", c.Body)
377 }
378
379 func TestSkippedBodyGlobalChoicesKindWithAdditional(t *testing.T) {
380 reader, writer := then.WithReadWritePipe(t)
381 then.DelayWrite(
382 t, writer,
383 []byte{13},
384 []byte("breaking value"),
385 []byte{13},
386 )
387
388 config := &Config{
389 CustomChoices: []Custom{
390 {Key: "skipped", Type: CustomString, Label: "a"},
391 },
392 Kinds: []KindConfig{
393 {
394 Label: "added",
395 SkipBody: true,
396 SkipGlobalChoices: true,
397 AdditionalChoices: []Custom{
398 {Key: "break", Type: CustomString, Label: "b"},
399 },
400 },
401 {Label: "not used"},
402 },
403 }
404
405 prompts := &Prompts{
406 Config: config,
407 StdinReader: reader,
408 TimeNow: specificTimeNow,
409 Enabled: true,
410 }
411 changes, err := prompts.BuildChanges()
412 then.Nil(t, err)
413
414 c := changes[0]
415
416 then.Equals(t, "", c.Component)
417 then.Equals(t, "", c.Custom["skipped"])
418 then.Equals(t, "breaking value", c.Custom["break"])
419 then.Equals(t, "", c.Body)
420 }
421
422 func TestBodyAndPostProcess(t *testing.T) {
423 reader, _ := then.WithReadWritePipe(t)
424
425 config := &Config{
426 Post: []PostProcessConfig{
427 {Key: "Post", Value: "Body again {{.Body}}"},
428 },
429 }
430 prompts := &Prompts{
431 Config: config,
432 StdinReader: reader,
433 TimeNow: specificTimeNow,
434 Body: "our body",
435 Customs: make(map[string]string),
436 Enabled: true,
437 }
438
439 changes, err := prompts.BuildChanges()
440 then.Nil(t, err)
441
442 c := changes[0]
443
444 then.Equals(t, "", c.Component)
445 then.Equals(t, "", c.Kind)
446 then.Equals(t, "Body again our body", c.Custom["Post"])
447 then.Equals(t, "our body", c.Body)
448 }
449
450 func TestBodyAndPostProcessSkipGlobalPost(t *testing.T) {
451 reader, _ := then.WithReadWritePipe(t)
452 config := &Config{
453 Kinds: []KindConfig{
454 {
455 Label: "added",
456 SkipGlobalPost: true,
457 Post: []PostProcessConfig{
458 {Key: "Post", Value: "{{.Body}}+{{.Custom.Issue}}"},
459 },
460 },
461 },
462 CustomChoices: []Custom{
463 {Key: "Issue", Type: CustomInt},
464 },
465 Post: []PostProcessConfig{
466 {Key: "GlobalPost", Value: "should be skipped"},
467 },
468 }
469
470 prompts := &Prompts{
471 Config: config,
472 StdinReader: reader,
473 TimeNow: specificTimeNow,
474 Body: "our body",
475 Kind: "added",
476 Customs: map[string]string{
477 "Issue": "30",
478 },
479 Enabled: true,
480 }
481
482 changes, err := prompts.BuildChanges()
483 then.Nil(t, err)
484
485 c := changes[0]
486
487 then.Equals(t, "", c.Component)
488 then.Equals(t, "added", c.Kind)
489 then.Equals(t, "our body", c.Body)
490 then.Equals(t, "our body+30", c.Custom["Post"])
491 then.Equals(t, "", c.Custom["GlobalPost"])
492 }
493
494 func TestErrorInvalidBody(t *testing.T) {
495 reader, writer := then.WithReadWritePipe(t)
496 then.DelayWrite(
497 t, writer,
498 []byte("abc"),
499 []byte{13},
500 )
501
502 var min int64 = 5
503
504 config := &Config{
505 Body: BodyConfig{
506 MinLength: &min,
507 },
508 }
509
510 prompts := &Prompts{
511 Config: config,
512 StdinReader: reader,
513 TimeNow: specificTimeNow,
514 Enabled: true,
515 }
516
517 _, err := prompts.BuildChanges()
518 then.Err(t, errInputTooShort, err)
519 }
520
521 func TestErrorInvalidPost(t *testing.T) {
522 reader, _ := then.WithReadWritePipe(t)
523 config := &Config{
524 Post: []PostProcessConfig{
525 {Key: "Post", Value: "invalid {{++...thing}}"},
526 },
527 }
528 prompts := &Prompts{
529 Config: config,
530 StdinReader: reader,
531 TimeNow: specificTimeNow,
532 Body: "our body",
533 Enabled: true,
534 }
535
536 _, err := prompts.BuildChanges()
537 then.NotNil(t, err)
538 }
539
540 func TestErrorInvalidPostWithProjects(t *testing.T) {
541 reader, _ := then.WithReadWritePipe(t)
542 config := &Config{
543 Post: []PostProcessConfig{
544 {Key: "Post", Value: "invalid {{++...thing}}"},
545 },
546 Projects: []ProjectConfig{
547 {Label: "Client", Key: "client"},
548 {Label: "Other", Key: "other"},
549 },
550 }
551 prompts := &Prompts{
552 Config: config,
553 StdinReader: reader,
554 TimeNow: specificTimeNow,
555 Body: "our body",
556 Projects: []string{"client"},
557 Enabled: true,
558 }
559
560 _, err := prompts.BuildChanges()
561 then.NotNil(t, err)
562 }
563
564 func TestErrorInvalidCustomType(t *testing.T) {
565 reader, writer := then.WithReadWritePipe(t)
566 then.DelayWrite(
567 t, writer,
568 []byte("invalid custom type"),
569 []byte{13},
570 )
571
572 config := &Config{
573 CustomChoices: []Custom{
574 {Key: "check", Type: "bad type", Label: "a"},
575 },
576 }
577
578 prompts := &Prompts{
579 Config: config,
580 StdinReader: reader,
581 TimeNow: specificTimeNow,
582 Enabled: true,
583 }
584
585 _, err := prompts.BuildChanges()
586 then.NotNil(t, err)
587 }
588
589 func TestErrorInvalidComponent(t *testing.T) {
590 reader, writer := then.WithReadWritePipe(t)
591 then.DelayWrite(
592 t, writer,
593 []byte{3},
594 )
595
596 config := &Config{
597 Components: []string{"a", "b"},
598 }
599 prompts := &Prompts{
600 Config: config,
601 StdinReader: reader,
602 TimeNow: specificTimeNow,
603 Enabled: true,
604 }
605
606 _, err := prompts.BuildChanges()
607 then.NotNil(t, err)
608 }
609
610 func TestErrorInvalidKind(t *testing.T) {
611 reader, writer := then.WithReadWritePipe(t)
612 then.DelayWrite(
613 t, writer,
614 []byte{3},
615 )
616
617 config := &Config{
618 Kinds: []KindConfig{
619 {Label: "a"},
620 {Label: "b"},
621 },
622 }
623 prompts := &Prompts{
624 Config: config,
625 StdinReader: reader,
626 TimeNow: specificTimeNow,
627 Enabled: true,
628 }
629
630 _, err := prompts.BuildChanges()
631 then.NotNil(t, err)
632 }
633
634 func TestErrorFaultBody(t *testing.T) {
635 reader, writer := then.WithReadWritePipe(t)
636 then.DelayWrite(
637 t, writer,
638 []byte{3},
639 )
640
641 config := &Config{}
642 prompts := &Prompts{
643 Config: config,
644 StdinReader: reader,
645 TimeNow: specificTimeNow,
646 Enabled: true,
647 }
648
649 _, err := prompts.BuildChanges()
650 then.NotNil(t, err)
651 }
652
653 func TestErrorInvalidCustomValue(t *testing.T) {
654 reader, writer := then.WithReadWritePipe(t)
655 then.DelayWrite(
656 t, writer,
657 []byte("custom body"),
658 []byte{13},
659 []byte("control C out"),
660 []byte{3},
661 )
662
663 config := &Config{
664 CustomChoices: []Custom{
665 {Key: "check", Type: CustomString, Label: "a"},
666 },
667 }
668 prompts := &Prompts{
669 Config: config,
670 StdinReader: reader,
671 TimeNow: specificTimeNow,
672 Enabled: true,
673 }
674
675 _, err := prompts.BuildChanges()
676 then.NotNil(t, err)
677 }
678
679 func TestErrorBadKindLabel(t *testing.T) {
680 reader, _ := then.WithReadWritePipe(t)
681 config := &Config{
682 Kinds: []KindConfig{
683 {Label: "kind"},
684 },
685 }
686 prompts := &Prompts{
687 Config: config,
688 StdinReader: reader,
689 TimeNow: specificTimeNow,
690 Kind: "not kind",
691 Body: "body",
692 Enabled: true,
693 }
694
695 _, err := prompts.BuildChanges()
696 then.Err(t, errInvalidKind, err)
697 }
698
699 func TestErrorBadComponentInput(t *testing.T) {
700 reader, _ := then.WithReadWritePipe(t)
701 config := &Config{
702 Components: []string{"a", "b", "c"},
703 }
704 prompts := &Prompts{
705 Config: config,
706 StdinReader: reader,
707 TimeNow: specificTimeNow,
708 Component: "d",
709 Body: "body",
710 Enabled: true,
711 }
712
713 _, err := prompts.BuildChanges()
714 then.Err(t, errInvalidComponent, err)
715 }
716
717 func TestErrorComponentGivenWithNoConfiguration(t *testing.T) {
718 reader, _ := then.WithReadWritePipe(t)
719 config := &Config{}
720 prompts := &Prompts{
721 Config: config,
722 StdinReader: reader,
723 TimeNow: specificTimeNow,
724 Component: "we shouldn't have a component",
725 Body: "body",
726 Enabled: true,
727 }
728
729 _, err := prompts.BuildChanges()
730 then.Err(t, errComponentProvidedWhenNotConfigured, err)
731 }
732
733 func TestErrorKindGivenWithNoConfiguration(t *testing.T) {
734 reader, _ := then.WithReadWritePipe(t)
735 config := &Config{}
736 prompts := &Prompts{
737 Config: config,
738 StdinReader: reader,
739 TimeNow: specificTimeNow,
740 Kind: "we shouldn't have a kind",
741 Body: "body",
742 Enabled: true,
743 }
744
745 _, err := prompts.BuildChanges()
746 then.Err(t, errKindProvidedWhenNotConfigured, err)
747 }
748
749 func TestErrorCustomGivenWithNoConfiguration(t *testing.T) {
750 reader, _ := then.WithReadWritePipe(t)
751 config := &Config{
752 CustomChoices: []Custom{
753 {Key: "Issue", Type: CustomInt},
754 },
755 }
756 prompts := &Prompts{
757 Config: config,
758 StdinReader: reader,
759 TimeNow: specificTimeNow,
760 Customs: map[string]string{
761 "MissingKey": "40",
762 },
763 Enabled: true,
764 }
765
766 _, err := prompts.BuildChanges()
767 then.Err(t, errCustomProvidedNotConfigured, err)
768 }
769
770 func TestErrorCustomGivenDoesNotPassValidation(t *testing.T) {
771 reader, _ := then.WithReadWritePipe(t)
772 minValue := int64(50)
773 config := &Config{
774 CustomChoices: []Custom{
775 {Key: "Issue", Type: CustomInt, MinInt: &minValue},
776 },
777 }
778 prompts := &Prompts{
779 Config: config,
780 StdinReader: reader,
781 TimeNow: specificTimeNow,
782 Customs: map[string]string{
783 "Issue": "40",
784 },
785 Enabled: true,
786 }
787
788 _, err := prompts.BuildChanges()
789 then.Err(t, errIntTooLow, err)
790 }
791
792 func TestErrorBodyGivenWithNoConfiguration(t *testing.T) {
793 reader, _ := then.WithReadWritePipe(t)
794 config := &Config{
795 Kinds: []KindConfig{
796 {Label: "kind", SkipBody: true},
797 },
798 }
799 prompts := &Prompts{
800 Config: config,
801 StdinReader: reader,
802 TimeNow: specificTimeNow,
803 Body: "body",
804 Kind: "kind",
805 Enabled: true,
806 }
807
808 _, err := prompts.BuildChanges()
809 then.Err(t, errKindDoesNotAcceptBody, err)
810 }
811
812 func TestErrorBodyGivenDoesNotPassValidation(t *testing.T) {
813 var min int64 = 10
814
815 reader, _ := then.WithReadWritePipe(t)
816 config := &Config{
817 Kinds: []KindConfig{
818 {Label: "kind"},
819 },
820 Body: BodyConfig{
821 MinLength: &min,
822 },
823 }
824 prompts := &Prompts{
825 Config: config,
826 StdinReader: reader,
827 TimeNow: specificTimeNow,
828 Kind: "kind",
829 Body: "body",
830 Enabled: true,
831 }
832
833 _, err := prompts.BuildChanges()
834 then.Err(t, errInputTooShort, err)
835 }
836
837 func TestSkipPromptForComponentIfSet(t *testing.T) {
838 reader, writer := then.WithReadWritePipe(t)
839 then.DelayWrite(
840 t, writer,
841 []byte("skip component body"),
842 []byte{13},
843 )
844
845 config := &Config{
846 Components: []string{"a", "b"},
847 }
848 prompts := &Prompts{
849 Config: config,
850 StdinReader: reader,
851 TimeNow: specificTimeNow,
852 Component: "a",
853 Enabled: true,
854 }
855
856 changes, err := prompts.BuildChanges()
857 then.Nil(t, err)
858
859 c := changes[0]
860 then.Equals(t, "a", c.Component)
861 then.Equals(t, "skip component body", c.Body)
862 }
863
864 func TestSkipPromptForCustomIfOptionalAndDisabled(t *testing.T) {
865 config := &Config{
866 CustomChoices: []Custom{
867 {
868 Key: "cust",
869 Optional: true,
870 Type: "string",
871 },
872 },
873 }
874 prompts := &Prompts{
875 Config: config,
876 TimeNow: specificTimeNow,
877 Enabled: false,
878 Body: "skip custom input",
879 }
880
881 changes, err := prompts.BuildChanges()
882 then.Nil(t, err)
883
884 c := changes[0]
885 then.Equals(t, "skip custom input", c.Body)
886 then.MapLen(t, 0, c.Custom)
887 then.Equals(t, "", c.Custom["cust"])
888 }
889
890 func TestSkipPromptForPromptsWithCustomPromptsInKindConfig(t *testing.T) {
891 config := &Config{
892 Kinds: []KindConfig{
893 {
894 Key: "dependency",
895 AdditionalChoices: []Custom{
896 {
897 Key: "name",
898 Type: CustomString,
899 },
900 {
901 Key: "from",
902 Type: CustomString,
903 },
904 {
905 Key: "to",
906 Type: CustomString,
907 },
908 },
909 SkipBody: true,
910 },
911 },
912 }
913 prompts := &Prompts{
914 Config: config,
915 StdinReader: bytes.NewReader(nil),
916 TimeNow: specificTimeNow,
917 Kind: "dependency",
918 Customs: map[string]string{
919 "name": "go",
920 "from": "1.20",
921 "to": "1.22",
922 },
923 Enabled: true,
924 }
925
926 changes, err := prompts.BuildChanges()
927 then.Nil(t, err)
928
929 c := changes[0]
930 then.Equals(t, "go", c.Custom["name"])
931 then.Equals(t, "1.20", c.Custom["from"])
932 then.Equals(t, "1.22", c.Custom["to"])
933 }
934
935 func TestSkipPromptForKindIfSet(t *testing.T) {
936 reader, writer := then.WithReadWritePipe(t)
937 then.DelayWrite(
938 t, writer,
939 []byte("skip kind body"),
940 []byte{13},
941 )
942
943 config := &Config{
944 Kinds: []KindConfig{
945 {Label: "kind"},
946 },
947 }
948 prompts := &Prompts{
949 Config: config,
950 StdinReader: reader,
951 TimeNow: specificTimeNow,
952 Kind: "kind",
953 Enabled: true,
954 }
955
956 changes, err := prompts.BuildChanges()
957 then.Nil(t, err)
958
959 c := changes[0]
960 then.Equals(t, "kind", c.Kind)
961 then.Equals(t, "skip kind body", c.Body)
962 }
963
964 func TestSkipPromptForBodyIfSet(t *testing.T) {
965 reader, writer := then.WithReadWritePipe(t)
966 then.DelayWrite(
967 t, writer,
968 []byte("kind"),
969 []byte{13},
970 )
971
972 config := &Config{
973 Kinds: []KindConfig{
974 {Label: "kind"},
975 },
976 }
977 prompts := &Prompts{
978 Config: config,
979 StdinReader: reader,
980 TimeNow: specificTimeNow,
981 Body: "skip body body",
982 Enabled: true,
983 }
984
985 changes, err := prompts.BuildChanges()
986 then.Nil(t, err)
987
988 c := changes[0]
989 then.Equals(t, "kind", c.Kind)
990 then.Equals(t, "skip body body", c.Body)
991 }
992
993 func TestGetBodyTxtWithEditor(t *testing.T) {
994 then.WithTempDir(t)
995 t.Setenv("EDITOR", "vim")
996
997 expectedBodyMsg := "some body msg"
998 config := &Config{}
999
1000 prompts := &Prompts{
1001 Config: config,
1002 BodyEditor: true,
1003 TimeNow: specificTimeNow,
1004 EditorCmdBuilder: func(filename string) (EditorRunner, error) {
1005 return &dummyEditorRunner{
1006 filename: filename,
1007 body: []byte(expectedBodyMsg),
1008 t: t,
1009 }, nil
1010 },
1011 Enabled: true,
1012 }
1013
1014 changes, err := prompts.BuildChanges()
1015 then.Nil(t, err)
1016
1017 c := changes[0]
1018
1019 then.Equals(t, "", c.Component)
1020 then.Equals(t, "", c.Kind)
1021 then.MapLen(t, 0, c.Custom)
1022 then.Equals(t, expectedBodyMsg, c.Body)
1023 }
1024
1025 func TestGetBodyTxtWithEditorUnableToCreateCmd(t *testing.T) {
1026 t.Setenv("EDITOR", "")
1027 then.WithTempDir(t)
1028
1029 config := &Config{}
1030 prompts := &Prompts{
1031 Config: config,
1032 BodyEditor: true,
1033 TimeNow: specificTimeNow,
1034 EditorCmdBuilder: func(s string) (EditorRunner, error) {
1035 return BuildCommand(s)
1036 },
1037 Enabled: true,
1038 }
1039
1040 _, err := prompts.BuildChanges()
1041 then.NotNil(t, err)
1042 }