add validation and test case

This commit is contained in:
jan.kozlowski
2024-04-29 02:05:01 +02:00
parent a83f102cc9
commit cba3688960
2 changed files with 16 additions and 0 deletions

View File

@@ -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

View File

@@ -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"}