From bb54b20223235d1195564548183aafe164d53823 Mon Sep 17 00:00:00 2001 From: Tatiana Bradley Date: Wed, 8 Nov 2023 17:20:49 -0500 Subject: [PATCH] internal/report: remove use of CutPrefix and add test for labels Using new(er) function CutPrefix caused deploy to fail because it expected to run using older version of Go. Change-Id: I335ca27870caf0b9d6a57bf98fb3e2be1e205743 Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/540855 Auto-Submit: Tatiana Bradley LUCI-TryBot-Result: Go LUCI Reviewed-by: Damien Neil --- internal/report/report.go | 5 ++++- internal/report/report_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/internal/report/report.go b/internal/report/report.go index 76e53853..9fa1ed9f 100644 --- a/internal/report/report.go +++ b/internal/report/report.go @@ -104,7 +104,10 @@ func (er ExcludedReason) ToLabel() string { } func FromLabel(label string) (ExcludedReason, bool) { - er, ok := strings.CutPrefix(label, excludedLabelPrefix) + pre, er, ok := strings.Cut(label, excludedLabelPrefix) + if pre != "" { + return "", false + } return ExcludedReason(er), ok } diff --git a/internal/report/report_test.go b/internal/report/report_test.go index 161ec584..6ac12a96 100644 --- a/internal/report/report_test.go +++ b/internal/report/report_test.go @@ -75,6 +75,19 @@ func TestYAMLFilename(t *testing.T) { } } +func TestToFromLabel(t *testing.T) { + str := "EFFECTIVELY_PRIVATE" + label := "excluded: EFFECTIVELY_PRIVATE" + er := ExcludedReason(str) + if got, want := er.ToLabel(), label; got != want { + t.Errorf("(%s).ToLabel = %s, want %s", er, got, want) + } + got, ok := FromLabel(label) + if want := er; !ok || got != want { + t.Errorf("FromLabel(%s) = (%s, %t), want (%s, true)", label, got, ok, want) + } +} + func TestParseFilepath(t *testing.T) { filepath := "data/reports/GO-1999-0023.yaml" wantFolder := "data/reports"