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..f761fe19 --- /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..15a6a664 --- /dev/null +++ b/testing/label_test.go @@ -0,0 +1,109 @@ +/* +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"}, + }, + { + name: "go test case name", + s: "TestAbc_eee", + want: []string{"Abc_eee"}, + }, + { + name: "go test case name with sub name", + s: "TestAbc_eee/case1", + want: []string{"Abc_eee"}, + }, + } + 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)) + }) + } +}