|
| 1 | +package ginkgo_wrapper |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/onsi/ginkgo/v2" |
| 8 | + |
| 9 | + "github.com/ovn-org/ovn-kubernetes/test/e2e/label" |
| 10 | +) |
| 11 | + |
| 12 | +const componentName = "ovn-kubernetes" |
| 13 | + |
| 14 | +var ( |
| 15 | + Describe = okDescribe() |
| 16 | + DescribeTableSubTree = okDescribeTableSubtree() |
| 17 | +) |
| 18 | + |
| 19 | +// okDescribe returns a wrapper function for ginkgo.Describe which prepends |
| 20 | +// the CNI name plus any additional labels. |
| 21 | +func okDescribe() func(...interface{}) bool { |
| 22 | + return func(args ...interface{}) bool { |
| 23 | + args = append([]interface{}{label.NewComponent(componentName)}, args...) |
| 24 | + return registerInSuite(ginkgo.Describe, args) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func okDescribeTableSubtree() func(...interface{}) bool { |
| 29 | + return func(args ...interface{}) bool { |
| 30 | + args = append([]interface{}{label.NewComponent(componentName)}, args...) |
| 31 | + return registerInSuite(ginkgo.DescribeTableSubtree, args) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// registerInSuite is the common implementation of all wrapper functions. It |
| 36 | +// expects to be called through one intermediate wrapper. |
| 37 | +func registerInSuite(ginkgoCall func(string, ...interface{}) bool, args []interface{}) bool { |
| 38 | + var ginkgoArgs []interface{} |
| 39 | + var offset ginkgo.Offset |
| 40 | + var texts []string |
| 41 | + |
| 42 | + for _, arg := range args { |
| 43 | + switch arg := arg.(type) { |
| 44 | + case label.Label: |
| 45 | + if !arg.IsValid() { |
| 46 | + panic(fmt.Sprintf("invalid label %q", arg)) |
| 47 | + } |
| 48 | + label := arg.String() |
| 49 | + ginkgoArgs = append(ginkgoArgs, ginkgo.Label(label)) |
| 50 | + texts = append(texts, label) |
| 51 | + case ginkgo.Offset: |
| 52 | + offset = arg |
| 53 | + case string: |
| 54 | + if arg == "" { |
| 55 | + panic("labels must not empty strings as separators are unnecessary and need to be removed") |
| 56 | + } |
| 57 | + texts = append(texts, arg) |
| 58 | + default: |
| 59 | + ginkgoArgs = append(ginkgoArgs, arg) |
| 60 | + } |
| 61 | + } |
| 62 | + offset += 2 // This function and its direct caller. |
| 63 | + |
| 64 | + // Enforce that text snippets to not start or end with spaces because |
| 65 | + // those lead to double spaces when concatenating below. |
| 66 | + for _, text := range texts { |
| 67 | + if strings.HasPrefix(text, " ") || strings.HasSuffix(text, " ") { |
| 68 | + panic("trailing or leading spaces are unnecessary and need to be removed") |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + ginkgoArgs = append(ginkgoArgs, offset) |
| 73 | + // compact labels and ensure test description contain a space between descriptions. |
| 74 | + var finalText string |
| 75 | + for _, text := range texts { |
| 76 | + if strings.HasPrefix(text, "[") { |
| 77 | + finalText += text |
| 78 | + } else { |
| 79 | + finalText += " " + text |
| 80 | + } |
| 81 | + } |
| 82 | + return ginkgoCall(finalText, ginkgoArgs...) |
| 83 | +} |
0 commit comments