Skip to content

Commit

Permalink
add validation and test case
Browse files Browse the repository at this point in the history
  • Loading branch information
kozjan committed Jun 6, 2024
1 parent a83f102 commit cba3688
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions kustomize/commands/edit/add/addmetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func (o *addMetadataOptions) validateAndParse(args []string) error {
if len(args) < 1 {
return fmt.Errorf("must specify %s", o.kind)
}
if !o.labelsWithoutSelector && o.includeTemplates {
return fmt.Errorf("--without-selector flag must be specified for --include-templates to work")
}
m, err := util.ConvertSliceToMap(args, o.kind.String())
if err != nil {
return err
Expand Down
13 changes: 13 additions & 0 deletions kustomize/commands/edit/add/addmetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,19 @@ func TestAddLabelWithoutSelectorIncludeTemplates(t *testing.T) {
assert.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"new": "label"}, IncludeTemplates: true})
}

func TestAddLabelIncludeTemplatesWithoutRequiredFlag(t *testing.T) {
fSys := filesys.MakeFsInMemory()
v := valtest_test.MakeHappyMapValidator(t)
cmd := newCmdAddLabel(fSys, v.Validator)
args := []string{"new:label"}
_ = cmd.Flag("include-templates").Value.Set("true")
_ = cmd.Flag("without-selector").Value.Set("false")
err := cmd.RunE(cmd, args)
v.VerifyNoCall()
require.Error(t, err)
require.Containsf(t, err.Error(), "--without-selector flag must be specified for --include-templates to work", "incorrect error: %s", err.Error())
}

func TestAddLabelWithoutSelectorAddLabel(t *testing.T) {
var o addMetadataOptions
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
Expand Down

0 comments on commit cba3688

Please sign in to comment.