From 54d81e0ac78898f7f3e198d43c50e455928103df Mon Sep 17 00:00:00 2001 From: mingfu Date: Wed, 28 Feb 2024 22:24:11 +0800 Subject: [PATCH] feat: add helper function to generate unique case name --- testing/framework/case_builder.go | 3 + testing/label.go | 53 +++++++++++++++++ testing/label_test.go | 99 +++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 testing/label.go create mode 100644 testing/label_test.go diff --git a/testing/framework/case_builder.go b/testing/framework/case_builder.go index 7e9d00ef..d9036299 100644 --- a/testing/framework/case_builder.go +++ b/testing/framework/case_builder.go @@ -20,6 +20,7 @@ import ( "context" "fmt" + "github.com/katanomi/pkg/testing" . "github.com/katanomi/pkg/testing/framework/base" . "github.com/onsi/ginkgo/v2" ) @@ -35,6 +36,8 @@ func newCase(name string, priority TestCasePriority) *CaseBuilder { SugaredLogger: fmw.SugaredLogger.Named(name), } }), + // set the unique case name label + Labels: testing.Case(name), }, } } diff --git a/testing/label.go b/testing/label.go new file mode 100644 index 00000000..d3806bbd --- /dev/null +++ b/testing/label.go @@ -0,0 +1,53 @@ +/* +Copyright 2024 The Katanomi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package testing + +import ( + "fmt" + "regexp" + + . "github.com/onsi/ginkgo/v2" +) + +var caseNameReg = regexp.MustCompile(`#CASE_(\w+)#`) +var unitTestCaseNameReg = regexp.MustCompile(`^Test(\w+)$`) + +// Case the unique case name label +func Case(caseName string) Labels { + if caseName == "" { + return Labels{} + } + return Labels{fmt.Sprintf("#CASE_%s#", caseName)} +} + +// GetCaseNames Resolve the case identifier from the testcase name in the junit report +// For go test junit report, the case name may be started with `Test`, e.g: TestGetProject +// For ginkgo test junit report, the case name may contain the #CASE_%s# string, e.g: [It] when xxxx [#CASE_GetProject#] +func GetCaseNames(name string) []string { + parts := caseNameReg.FindAllStringSubmatch(name, -1) + + if len(parts) == 0 { + parts = unitTestCaseNameReg.FindAllStringSubmatch(name, -1) + } + + matchedNames := make([]string, 0, len(parts)) + for _, item := range parts { + matchedNames = append(matchedNames, item[1]) + } + + return matchedNames +} diff --git a/testing/label_test.go b/testing/label_test.go new file mode 100644 index 00000000..630120c0 --- /dev/null +++ b/testing/label_test.go @@ -0,0 +1,99 @@ +/* +Copyright 2024 The Katanomi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package testing + +import ( + "testing" + + "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestGetCaseName(t *testing.T) { + g := NewWithT(t) + tests := []struct { + name string + s string + want []string + }{ + { + name: "without case name", + s: "test", + want: []string{}, + }, + { + name: "with case name", + s: "#CASE_aaaa#", + want: []string{"aaaa"}, + }, + { + name: "case name contains underscore", + s: "#CASE_aaaa_bbbb#", + want: []string{"aaaa_bbbb"}, + }, + { + name: "case name contains special characters", + s: "#CASE_aaaa@bbbb#", + want: []string{}, + }, + { + name: "empty string", + s: "", + want: []string{}, + }, + { + name: "Multiple case name", + s: "#CASE_aaaa# #CASE_bbbb#", + want: []string{"aaaa", "bbbb"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g.Expect(GetCaseNames(tt.s)).To(Equal(tt.want)) + }) + } +} + +func TestCase(t *testing.T) { + g := NewWithT(t) + tests := []struct { + name string + caseName string + want ginkgo.Labels + }{ + { + name: "empty case name", + caseName: "", + want: ginkgo.Labels{}, + }, + { + name: "case name", + caseName: "abc", + want: ginkgo.Labels{"#CASE_abc#"}, + }, + { + name: "case name contains underscore", + caseName: "abc_eee", + want: ginkgo.Labels{"#CASE_abc_eee#"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g.Expect(Case(tt.caseName)).To(Equal(tt.want)) + }) + } +}