Skip to content

Commit

Permalink
feat: add helper function to generate unique case name
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjingfm committed Feb 28, 2024
1 parent 48dba48 commit 54d81e0
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions testing/framework/case_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

"github.com/katanomi/pkg/testing"
. "github.com/katanomi/pkg/testing/framework/base"
. "github.com/onsi/ginkgo/v2"
)
Expand All @@ -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),
},
}
}
Expand Down
53 changes: 53 additions & 0 deletions testing/label.go
Original file line number Diff line number Diff line change
@@ -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
}
99 changes: 99 additions & 0 deletions testing/label_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}

0 comments on commit 54d81e0

Please sign in to comment.