Skip to content

Commit cb9dc0e

Browse files
authored
Refactor removed_api_tests to tests against sorted strings to avoid flakiness (#245)
* Revert "fix removed_apis_test" This reverts commit beb9613. * refactor removed_apis_test to test on sorted error strings to remove flakiness Signed-off-by: perdasilva <[email protected]>
1 parent 0209b8b commit cb9dc0e

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

pkg/validation/internal/removed_apis_test.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package internal
22

33
import (
44
"reflect"
5+
"sort"
6+
"strings"
57
"testing"
68

79
"github.com/operator-framework/api/pkg/manifests"
@@ -281,20 +283,40 @@ func TestValidateDeprecatedAPIS(t *testing.T) {
281283
require.Equal(t, tt.wantWarning, len(warnsResult) > 0)
282284
if tt.wantWarning {
283285
require.Equal(t, len(tt.warnStrings), len(warnsResult))
286+
// testing against sorted strings to address flakiness on the order
287+
// of APIs listed
288+
sortedWarnStrings := sortStringSlice(tt.warnStrings)
284289
for _, w := range warnsResult {
285290
wString := w.Error()
286-
require.Contains(t, tt.warnStrings, wString)
291+
require.Contains(t, sortedWarnStrings, sortString(wString))
287292
}
288293
}
289294

290295
require.Equal(t, tt.wantError, len(errsResult) > 0)
291296
if tt.wantError {
292297
require.Equal(t, len(tt.errStrings), len(errsResult))
298+
// testing against sorted strings to address flakiness on the order
299+
// of APIs listed
300+
sortedErrStrings := sortStringSlice(tt.errStrings)
293301
for _, err := range errsResult {
294-
errString := err.Error()
295-
require.Contains(t, tt.errStrings, errString)
302+
errString := sortString(err.Error())
303+
require.Contains(t, sortedErrStrings, errString)
296304
}
297305
}
298306
})
299307
}
300308
}
309+
310+
func sortString(str string) string {
311+
split := strings.Split(str, "")
312+
sort.Strings(split)
313+
return strings.Join(split, "")
314+
}
315+
316+
func sortStringSlice(slice []string) []string {
317+
var newSlice []string
318+
for _, str := range slice {
319+
newSlice = append(newSlice, sortString(str))
320+
}
321+
return newSlice
322+
}

0 commit comments

Comments
 (0)