Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix creating fragments from CLI args with kind choices #676

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ linters:
- unused
- whitespace
- wsl
issues:
exclude-rules:
- path: '(.+)_test\.go'
linters:
- gosec
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
Loading