Add --force flag to modify annotations and labels

This change adds a new flag (`--force`) to commands `edit add annotation` and
`edit add label` so that annotations and labels are modified if they already
existed.
This commit is contained in:
Alfonso Acosta
2019-04-05 22:10:48 +02:00
committed by Alfonso Acosta
parent 53f0deec8f
commit 2cf8371add
2 changed files with 76 additions and 1 deletions

View File

@@ -187,6 +187,40 @@ func TestAddAnnotationMultipleArgs(t *testing.T) {
}
}
func TestAddAnnotationForce(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteTestKustomization()
v := validators.MakeHappyMapValidator(t)
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
args := []string{"key:foo"}
err := cmd.RunE(cmd, args)
v.VerifyCall()
if err != nil {
t.Errorf("unexpected error: %v", err.Error())
}
// trying to add the same annotation again should not work
args = []string{"key:bar"}
v = validators.MakeHappyMapValidator(t)
cmd = newCmdAddAnnotation(fakeFS, v.Validator)
err = cmd.RunE(cmd, args)
v.VerifyCall()
if err == nil {
t.Errorf("expected an error")
}
if err.Error() != "annotation key already in kustomization file" {
t.Errorf("expected an error")
}
// but trying to add it with --force should
v = validators.MakeHappyMapValidator(t)
cmd = newCmdAddAnnotation(fakeFS, v.Validator)
cmd.Flag("force").Value.Set("true")
err = cmd.RunE(cmd, args)
v.VerifyCall()
if err != nil {
t.Errorf("unexpected error: %v", err.Error())
}
}
func TestRunAddLabel(t *testing.T) {
var o addMetadataOptions
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
@@ -295,6 +329,40 @@ func TestAddLabelMultipleArgs(t *testing.T) {
}
}
func TestAddLabelForce(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteTestKustomization()
v := validators.MakeHappyMapValidator(t)
cmd := newCmdAddLabel(fakeFS, v.Validator)
args := []string{"key:foo"}
err := cmd.RunE(cmd, args)
v.VerifyCall()
if err != nil {
t.Errorf("unexpected error: %v", err.Error())
}
// trying to add the same label again should not work
args = []string{"key:bar"}
v = validators.MakeHappyMapValidator(t)
cmd = newCmdAddLabel(fakeFS, v.Validator)
err = cmd.RunE(cmd, args)
v.VerifyCall()
if err == nil {
t.Errorf("expected an error")
}
if err.Error() != "label key already in kustomization file" {
t.Errorf("expected an error")
}
// but trying to add it with --force should
v = validators.MakeHappyMapValidator(t)
cmd = newCmdAddLabel(fakeFS, v.Validator)
cmd.Flag("force").Value.Set("true")
err = cmd.RunE(cmd, args)
v.VerifyCall()
if err != nil {
t.Errorf("unexpected error: %v", err.Error())
}
}
func TestConvertToMap(t *testing.T) {
var o addMetadataOptions
args := "a:b,c:\"d\",e:\"f:g\",g:h:k"