Skip to content

Commit

Permalink
Fix issue with creating fragments entirely from CLI args
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
miniscruff committed Jun 25, 2024
1 parent 3fc28b7 commit b07283b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/fixed-20240624-203927.yaml
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 19 additions & 1 deletion core/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,36 @@ 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{}{}

err := choice.Validate(value)
if err != nil {
return err
}

break
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions core/prompt_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"bytes"
"testing"
"time"

Expand Down Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions core/replacement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit b07283b

Please sign in to comment.