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 label check and add test #45

Merged
merged 1 commit into from
Apr 29, 2024
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: 4 additions & 1 deletion err.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ func (err *Err) HasLabel(label string) bool {
return false
}

if _, ok := err.labels[label]; ok {
// Check all labels in the error and it's stack since the stack isn't
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Check all labels in the error and it's stack since the stack isn't
// Check all labels in the error and its stack since the stack isn't

// traversed separately. If we don't check the stacked labels here we'll skip
// checking them completely.
if _, ok := err.Labels()[label]; ok {
return true
}

Expand Down
38 changes: 38 additions & 0 deletions err_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/pkg/errors"
"golang.org/x/exp/maps"

"github.com/alcionai/clues"
)
Expand Down Expand Up @@ -110,6 +111,43 @@ func TestStack(t *testing.T) {
}
}

func TestHasLabel(t *testing.T) {
const label = "some-label"

table := []struct {
name string
initial error
}{
{
name: "multiple stacked clues errors with label on first",
initial: clues.Stack(
clues.New("Labeled").Label(label),
clues.New("NotLabeled")),
},
{
name: "multiple stacked clues errors with label on second",
initial: clues.Stack(
clues.New("NotLabeled"),
clues.New("Labeled").Label(label)),
},
{
name: "single stacked clues error with label",
initial: clues.Stack(clues.New("Labeled").Label(label)),
},
}

for _, test := range table {
t.Run(test.name, func(t *testing.T) {
if !clues.HasLabel(test.initial, label) {
t.Errorf(
"expected error to have label [%s] but got %v",
label,
maps.Keys(clues.Labels(test.initial)))
}
})
}
}

func TestLabel(t *testing.T) {
table := []struct {
name string
Expand Down
Loading