Skip to content

Commit 815d932

Browse files
committed
UPSTREAM: <carry>: Fail updating annotations if any rules match 0 tests
1 parent 53fb58b commit 815d932

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

openshift-hack/e2e/annotate/annotate.go

+39-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,34 @@ 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+
if _, exists := matches[item]; exists {
142+
fmt.Fprintf(os.Stderr, "multiple entries for pattern %q\n", item)
143+
os.Exit(1)
144+
}
145+
146+
match := &matchable{pattern: item}
123147
re := regexp.MustCompile(item)
124148
if p, ok := re.LiteralPrefix(); ok {
125-
stringMatches[label] = append(stringMatches[label], p)
149+
match.literal = p
126150
} else {
127-
remain = append(remain, item)
151+
match.re = re
128152
}
129-
}
130-
if len(remain) > 0 {
131-
matches[label] = regexp.MustCompile(strings.Join(remain, `|`))
153+
matches[label] = append(matches[label], match)
132154
}
133155
}
134156
sort.Strings(allLabels)
@@ -137,7 +159,6 @@ func newGenerator(testMaps map[string][]string) *ginkgoTestRenamer {
137159

138160
return &ginkgoTestRenamer{
139161
allLabels: allLabels,
140-
stringMatches: stringMatches,
141162
matches: matches,
142163
excludedTestsFilter: excludedTestsFilter,
143164
output: make(map[string]string),
@@ -154,10 +175,8 @@ func newRenamerFromGenerated(names map[string]string) *ginkgoTestRenamer {
154175
type ginkgoTestRenamer struct {
155176
// keys defined in TestMaps in openshift-hack/e2e/annotate/rules.go
156177
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
178+
// matches to apply a particular label
179+
matches map[string][]*matchable
161180
// regular expression excluding permanently a set of tests
162181
// see ExcludedTests in openshift-hack/e2e/annotate/rules.go
163182
excludedTestsFilter *regexp.Regexp
@@ -193,17 +212,17 @@ func (r *ginkgoTestRenamer) generateRename(name string, node types.TestSpec) {
193212
}
194213

195214
var hasLabel bool
196-
for _, segment := range r.stringMatches[label] {
197-
hasLabel = strings.Contains(newName, segment)
215+
for _, match := range r.matches[label] {
216+
if match.re != nil {
217+
hasLabel = match.re.MatchString(newName)
218+
} else {
219+
hasLabel = strings.Contains(newName, match.literal)
220+
}
198221
if hasLabel {
222+
match.matched = true
199223
break
200224
}
201225
}
202-
if !hasLabel {
203-
if re := r.matches[label]; re != nil {
204-
hasLabel = r.matches[label].MatchString(newName)
205-
}
206-
}
207226

208227
if hasLabel {
209228
count++

0 commit comments

Comments
 (0)