Skip to content

Commit 2c3f544

Browse files
committed
UPSTREAM: <carry>: Fail updating annotations if any rules match 0 tests
1 parent 10ff6f4 commit 2c3f544

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

openshift-hack/e2e/annotate/annotate.go

+34-20
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ func Run(testMaps map[string][]string, filter func(name string) bool) {
4848
os.Exit(1)
4949
}
5050

51+
unusedPatterns := false
52+
for _, label := range generator.allLabels {
53+
for _, match := range generator.matches[label] {
54+
if !match.matched {
55+
unusedPatterns = true
56+
fmt.Fprintf(os.Stderr, "Unused pattern: %s => %s\n", label, match.pattern)
57+
}
58+
}
59+
}
60+
if unusedPatterns {
61+
os.Exit(1)
62+
}
63+
5164
// All tests must be associated with a sig (either upstream), or downstream
5265
// If you get this error, you should add the [sig-X] tag to your test (if its
5366
// in origin) or if it is upstream add a new rule to rules.go that assigns
@@ -110,25 +123,29 @@ func init() {
110123
}
111124
}
112125

126+
type matchable struct {
127+
pattern string
128+
literal string
129+
re *regexp.Regexp
130+
matched bool
131+
}
132+
113133
func newGenerator(testMaps map[string][]string) *ginkgoTestRenamer {
114134
var allLabels []string
115-
matches := make(map[string]*regexp.Regexp)
116-
stringMatches := make(map[string][]string)
135+
matches := make(map[string][]*matchable)
117136

118137
for label, items := range testMaps {
119138
sort.Strings(items)
120139
allLabels = append(allLabels, label)
121-
var remain []string
122140
for _, item := range items {
141+
match := &matchable{pattern: item}
123142
re := regexp.MustCompile(item)
124143
if p, ok := re.LiteralPrefix(); ok {
125-
stringMatches[label] = append(stringMatches[label], p)
144+
match.literal = p
126145
} else {
127-
remain = append(remain, item)
146+
match.re = re
128147
}
129-
}
130-
if len(remain) > 0 {
131-
matches[label] = regexp.MustCompile(strings.Join(remain, `|`))
148+
matches[label] = append(matches[label], match)
132149
}
133150
}
134151
sort.Strings(allLabels)
@@ -137,7 +154,6 @@ func newGenerator(testMaps map[string][]string) *ginkgoTestRenamer {
137154

138155
return &ginkgoTestRenamer{
139156
allLabels: allLabels,
140-
stringMatches: stringMatches,
141157
matches: matches,
142158
excludedTestsFilter: excludedTestsFilter,
143159
output: make(map[string]string),
@@ -154,10 +170,8 @@ func newRenamerFromGenerated(names map[string]string) *ginkgoTestRenamer {
154170
type ginkgoTestRenamer struct {
155171
// keys defined in TestMaps in openshift-hack/e2e/annotate/rules.go
156172
allLabels []string
157-
// exact substrings to match to apply a particular label
158-
stringMatches map[string][]string
159-
// regular expressions to match to apply a particular label
160-
matches map[string]*regexp.Regexp
173+
// matches to apply a particular label
174+
matches map[string][]*matchable
161175
// regular expression excluding permanently a set of tests
162176
// see ExcludedTests in openshift-hack/e2e/annotate/rules.go
163177
excludedTestsFilter *regexp.Regexp
@@ -193,17 +207,17 @@ func (r *ginkgoTestRenamer) generateRename(name string, node types.TestSpec) {
193207
}
194208

195209
var hasLabel bool
196-
for _, segment := range r.stringMatches[label] {
197-
hasLabel = strings.Contains(newName, segment)
210+
for _, match := range r.matches[label] {
211+
if match.re != nil {
212+
hasLabel = match.re.MatchString(newName)
213+
} else {
214+
hasLabel = strings.Contains(newName, match.literal)
215+
}
198216
if hasLabel {
217+
match.matched = true
199218
break
200219
}
201220
}
202-
if !hasLabel {
203-
if re := r.matches[label]; re != nil {
204-
hasLabel = r.matches[label].MatchString(newName)
205-
}
206-
}
207221

208222
if hasLabel {
209223
count++

0 commit comments

Comments
 (0)