From b07283b296863e7ecc3db631fe548d0c06278e50 Mon Sep 17 00:00:00 2001 From: Ronnie Smith Date: Mon, 24 Jun 2024 20:39:29 -0700 Subject: [PATCH] Fix issue with creating fragments entirely from CLI args * Related to using a kind config with custom choices if you were to include the custom values in CLI args they were mistaken for invalid choices. --- .../unreleased/fixed-20240624-203927.yaml | 5 +++ core/prompt.go | 20 ++++++++- core/prompt_test.go | 45 +++++++++++++++++++ core/replacement_test.go | 8 ++-- 4 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 .changes/unreleased/fixed-20240624-203927.yaml diff --git a/.changes/unreleased/fixed-20240624-203927.yaml b/.changes/unreleased/fixed-20240624-203927.yaml new file mode 100644 index 00000000..68673248 --- /dev/null +++ b/.changes/unreleased/fixed-20240624-203927.yaml @@ -0,0 +1,5 @@ +kind: fixed +body: New command can now properly create a fragment via custom CLI arguments if using a kind with custom choices. +time: 2024-06-24T20:39:27.135679465-07:00 +custom: + Issue: "675" diff --git a/core/prompt.go b/core/prompt.go index 2db7ce4e..1f295de7 100644 --- a/core/prompt.go +++ b/core/prompt.go @@ -128,11 +128,27 @@ func (p *Prompts) validateArguments() error { return errKindProvidedWhenNotConfigured } + configuredCustoms := make([]Custom, 0) + + if len(p.Config.Kinds) > 0 && len(p.Kind) > 0 { + kc := p.Config.KindFromKeyOrLabel(p.Kind) + if kc == nil { + return fmt.Errorf("%w: %s", errInvalidKind, p.Kind) + } + + configuredCustoms = append(configuredCustoms, kc.AdditionalChoices...) + if !kc.SkipGlobalChoices { + configuredCustoms = append(configuredCustoms, p.Config.CustomChoices...) + } + } else { + configuredCustoms = append(configuredCustoms, p.Config.CustomChoices...) + } + // make sure no custom values are assigned that do not exist foundCustoms := map[string]struct{}{} for key, value := range p.Customs { - for _, choice := range p.Config.CustomChoices { + for _, choice := range configuredCustoms { if choice.Key == key { foundCustoms[key] = struct{}{} @@ -140,6 +156,8 @@ func (p *Prompts) validateArguments() error { if err != nil { return err } + + break } } } diff --git a/core/prompt_test.go b/core/prompt_test.go index 8650cc60..96659eb6 100644 --- a/core/prompt_test.go +++ b/core/prompt_test.go @@ -1,6 +1,7 @@ package core import ( + "bytes" "testing" "time" @@ -740,6 +741,50 @@ func TestSkipPromptForComponentIfSet(t *testing.T) { then.Equals(t, "skip component body", c.Body) } +func TestSkipPromptForPromptsWithCustomPromptsInKindConfig(t *testing.T) { + config := &Config{ + Kinds: []KindConfig{ + { + Key: "dependency", + AdditionalChoices: []Custom{ + { + Key: "name", + Type: CustomString, + }, + { + Key: "from", + Type: CustomString, + }, + { + Key: "to", + Type: CustomString, + }, + }, + SkipBody: true, + }, + }, + } + prompts := &Prompts{ + Config: config, + StdinReader: bytes.NewReader(nil), + TimeNow: specificTimeNow, + Kind: "dependency", + Customs: map[string]string{ + "name": "go", + "from": "1.20", + "to": "1.22", + }, + } + + changes, err := prompts.BuildChanges() + then.Nil(t, err) + + c := changes[0] + then.Equals(t, "go", c.Custom["name"]) + then.Equals(t, "1.20", c.Custom["from"]) + then.Equals(t, "1.22", c.Custom["to"]) +} + func TestSkipPromptForKindIfSet(t *testing.T) { reader, writer := then.WithReadWritePipe(t) then.DelayWrite( diff --git a/core/replacement_test.go b/core/replacement_test.go index d28d50cf..ba7fee35 100644 --- a/core/replacement_test.go +++ b/core/replacement_test.go @@ -20,7 +20,7 @@ replaced here third line ignore me` - err := os.WriteFile(filepath, []byte(startData), os.ModePerm) + err := os.WriteFile(filepath, []byte(startData), os.ModeTemporary) then.Nil(t, err) rep := Replacement{ @@ -46,7 +46,7 @@ func TestFindAndReplaceWithTemplate(t *testing.T) { "version": "1.1.0", }` - err := os.WriteFile(filepath, []byte(startData), os.ModePerm) + err := os.WriteFile(filepath, []byte(startData), os.ModeTemporary) then.Nil(t, err) rep := Replacement{ @@ -78,7 +78,7 @@ level1: version: 0.0.1 ` - err := os.WriteFile(filepath, []byte(startData), os.ModePerm) + err := os.WriteFile(filepath, []byte(startData), os.ModeTemporary) then.Nil(t, err) rep := Replacement{ @@ -110,7 +110,7 @@ level1: version: 0.0.1 ` - err := os.WriteFile(filepath, []byte(startData), os.ModePerm) + err := os.WriteFile(filepath, []byte(startData), os.ModeTemporary) then.Nil(t, err) rep := Replacement{