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

feat: add helper function to generate unique case name #539

Merged
merged 1 commit into from
Feb 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
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
}
109 changes: 109 additions & 0 deletions testing/label_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}