Skip to content

Commit 18a60d6

Browse files
authored
[CHANGE] removing a tag that doesn't exist results in an error (#228)
1 parent de1f16b commit 18a60d6

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

v2/types.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,18 @@ func (u *TagList) Add(p ...string) {
465465
}
466466

467467
// Remove removes 1 or more tags from a list
468-
func (u *TagList) Remove(p ...string) {
468+
func (u *TagList) Remove(p ...string) error {
469469
for _, v := range p {
470470
v = strings.TrimSpace(v)
471471
idx := u.find(v)
472472
if idx != -1 {
473473
a := *u
474474
*u = append(a[:idx], a[idx+1:]...)
475+
} else {
476+
return fmt.Errorf("unable to remove tag: %q - not found", v)
475477
}
476478
}
479+
return nil
477480
}
478481

479482
type CIDRList []string

v2/types_test.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ func TestTagList(t *testing.T) {
126126
AssertEquals(true, tags.Contains("TWO"), t)
127127
AssertEquals("TWO", tags[1], t)
128128

129-
tags.Remove("ONE")
129+
err := tags.Remove("ONE")
130+
if err == nil {
131+
t.Fatal("removing tag that doesn't exist should have failed")
132+
}
130133
AssertEquals("one", tags[0], t)
131134
AssertEquals(true, tags.Contains("TWO"), t)
132135
}
@@ -476,23 +479,27 @@ func TestTagList_Add(t *testing.T) {
476479

477480
func TestTagList_Delete(t *testing.T) {
478481
type test struct {
479-
v string
480-
a TagList
481-
shouldBe TagList
482+
v string
483+
a TagList
484+
shouldBe TagList
485+
shouldFail bool
482486
}
483487

484488
tests := []test{
485-
{v: "A", a: TagList{}, shouldBe: TagList{}},
489+
{v: "A", a: TagList{}, shouldBe: TagList{}, shouldFail: true},
486490
{v: "A", a: TagList{"A"}, shouldBe: TagList{}},
487-
{v: "a", a: TagList{"A"}, shouldBe: TagList{"A"}},
488-
{v: "a:Hello", a: TagList{"a:hello"}, shouldBe: TagList{"a:hello"}},
489-
{v: "a:a", a: TagList{"a:A"}, shouldBe: TagList{"a:A"}},
491+
{v: "a", a: TagList{"A"}, shouldBe: TagList{"A"}, shouldFail: true},
492+
{v: "a:Hello", a: TagList{"a:hello"}, shouldBe: TagList{"a:hello"}, shouldFail: true},
493+
{v: "a:a", a: TagList{"a:A"}, shouldBe: TagList{"a:A"}, shouldFail: true},
490494
}
491495

492496
for idx, test := range tests {
493-
test.a.Remove(test.v)
497+
err := test.a.Remove(test.v)
498+
if test.shouldFail && err == nil {
499+
t.Fatalf("[%d] expected delete to fail: %v", idx, test.a)
500+
}
494501
if !test.a.Equals(&test.shouldBe) {
495-
t.Errorf("[%d] expected lists to be equal: %v", idx, test.a)
502+
t.Fatalf("[%d] expected lists to be equal: %v", idx, test.a)
496503
}
497504
}
498505
}

0 commit comments

Comments
 (0)