diff --git a/src/test/java/com/google/devtools/build/lib/testutil/MoreAsserts.java b/src/test/java/com/google/devtools/build/lib/testutil/MoreAsserts.java index 990a4177bb6ef9..bda92607c8b0f9 100644 --- a/src/test/java/com/google/devtools/build/lib/testutil/MoreAsserts.java +++ b/src/test/java/com/google/devtools/build/lib/testutil/MoreAsserts.java @@ -302,6 +302,25 @@ public static Event assertContainsEvent(Iterable eventCollector, return null; // unreachable } + /** + * If {@code eventCollector} does not contain an event which matches {@code expectedEventRegex}, + * fails with an informative assertion. + */ + public static void assertContainsEventRegex( + Iterable eventCollector, String expectedEventRegex) { + for (Event event : eventCollector) { + if (event.toString().matches(expectedEventRegex)) { + return; + } + } + String eventsString = eventsToString(eventCollector); + String failureMessage = "Event matching '" + expectedEventRegex + "' not found"; + if (!eventsString.isEmpty()) { + failureMessage += "; found these though: " + eventsString; + } + fail(failureMessage); + } + /** * If the specified EventCollector contains an event which has * 'expectedEvent' as a substring, an informative assertion fails.