Copilot commented on code in PR #3647:
URL: https://github.com/apache/maven-surefire/pull/3647#discussion_r4730638304
##########
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/lastfailed/LastFailedTestsResolver.java:
##########
@@ -135,34 +140,35 @@ public LastFailedTests resolve(Collection<String>
scannedClasses, TestListResolv
logger.debug("runLastFailed ignores " +
testCase.getFullName() + ", not a test class of this run");
continue;
}
- String classFile = toClassFileName(className);
String methodName = phrasedMethodNames ? null :
toMethodName(testCase.getName(), className);
if (methodName == null) {
- if (userFilter.shouldRun(classFile, null)) {
+ if (shouldRun(userFilter, className, reportClass, null)) {
failedClasses.add(className);
}
- } else if (userFilter.shouldRun(classFile, methodName)) {
+ } else if (shouldRun(userFilter, className, reportClass,
methodName)) {
failedMethods
.computeIfAbsent(className, c -> new TreeSet<>())
.add(methodName);
+ reportClasses.put(className, reportClass);
}
}
}
Set<String> unfinishedClasses = new TreeSet<>();
if (unfinishedRunClasses != null) {
Set<String> finishedTopLevelClasses = new HashSet<>();
- for (ReportTestSuite suite : suites) {
+ for (ReportTestSuite suite : suites.keySet()) {
String className = toClassName(suite.getFullClassName());
if (className != null && suite.getLastModified() >=
unfinishedRunStart) {
finishedTopLevelClasses.add(topLevelClass(className));
}
}
for (String className : unfinishedRunClasses) {
String topLevelClass = topLevelClass(className);
+ // a stopped run leaves no report, so we go by the top-level
class
if (scannedTopLevelClasses.contains(topLevelClass)
&& !finishedTopLevelClasses.contains(topLevelClass)
- && userFilter.shouldRun(toClassFileName(className),
null)) {
+ && shouldRun(userFilter, className, topLevelClass,
null)) {
Review Comment:
If an independently selected `FooTest$SlowTest` crashes before writing a
report, a subsequent `-Dtest=FooTest -Dsurefire.runLastFailed=true` now selects
`SlowTest` through this fallback, bypassing the current test selection. The
marker stores scanned class names and does not establish that a dollar-named
class runs with its outer class. Keep direct filter matching for marker entries
unless actual nested-test ownership is established, and add a regression for a
stopped standalone dollar-named class.
##########
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/lastfailed/LastFailedTestsResolver.java:
##########
@@ -176,14 +182,25 @@ public LastFailedTests resolve(Collection<String>
scannedClasses, TestListResolv
for (String className : wholeClasses) {
tests.addAll(wholeClassPatterns(className, userFilter));
}
+ List<String> failedMethodPatterns = new ArrayList<>();
int failedMethodCount = 0;
+ boolean allInWholeClasses = true;
for (Map.Entry<String, Set<String>> entry : failedMethods.entrySet()) {
+ String className = entry.getKey();
// no need to list the failed methods of a class we run whole
- if (!wholeClasses.contains(entry.getKey())) {
- tests.add(entry.getKey() + "#" + String.join("+",
entry.getValue()));
+ if (!wholeClasses.contains(className)) {
+ failedMethodPatterns.add(className + "#" + String.join("+",
entry.getValue()));
failedMethodCount += entry.getValue().size();
+ allInWholeClasses &=
wholeClasses.contains(reportClasses.get(className));
Review Comment:
With `-Dtest=FooTest`, an `executionError` in `FooTest$Inner` plus a failure
in `FooTest$Inner$Deep#m` produces `FooTest$Inner,FooTest$Inner$Deep#m`,
because this check recognizes only the report owner `FooTest`. Naming `m`
activates method filtering and leaves out other tests in `Deep`, although
`Inner` should rerun whole. Recognize whole nested ancestors when dropping
redundant method patterns, and add a two-level nested regression.
##########
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/lastfailed/LastFailedTestsResolver.java:
##########
@@ -220,16 +237,47 @@ private static List<String> wholeClassPatterns(String
className, TestListResolve
return patterns.isEmpty() ? singletonList(className) : patterns;
}
- private List<ReportTestSuite> readReports() {
+ /**
+ * The report of {@code OrderTest} also holds the tests of its {@code
@Nested} class {@code OrderTest$Create}.
+ * {@code -Dtest=OrderTest} runs them too, as long as no method is named:
a method pattern turns on a filter
+ * in the fork that checks each test by its own class. An exclude of the
nested class still counts, the next
+ * run gets it as well.
+ *
+ * @param reportClass the class whose report holds the test
+ */
+ private static boolean shouldRun(
+ TestListResolver userFilter, String className, String reportClass,
String methodName) {
+ String classFile = toClassFileName(className);
+ if (userFilter.shouldRun(classFile, methodName)) {
+ return true;
+ }
+ return !userFilter.hasMethodPatterns()
+ && userFilter.shouldRun(toClassFileName(reportClass),
methodName)
Review Comment:
This fallback only checks the class owning the report, so it misses a
selected intermediate enclosing class. For a failure in `FooTest$Inner$Deep#m`
stored in `TEST-pkg.FooTest.xml`, a class-only `-Dtest=FooTest$Inner` normally
discovers `Deep`, but neither `className` nor `reportClass` matches here and
the resolver returns nothing to rerun. Account for intermediate enclosing
classes within the report owner as well, while retaining the method-pattern and
exclusion checks, and add a multi-level nested regression case.
##########
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/lastfailed/LastFailedTestsResolver.java:
##########
@@ -220,16 +237,47 @@ private static List<String> wholeClassPatterns(String
className, TestListResolve
return patterns.isEmpty() ? singletonList(className) : patterns;
}
- private List<ReportTestSuite> readReports() {
+ /**
+ * The report of {@code OrderTest} also holds the tests of its {@code
@Nested} class {@code OrderTest$Create}.
+ * {@code -Dtest=OrderTest} runs them too, as long as no method is named:
a method pattern turns on a filter
+ * in the fork that checks each test by its own class. An exclude of the
nested class still counts, the next
+ * run gets it as well.
+ *
+ * @param reportClass the class whose report holds the test
+ */
+ private static boolean shouldRun(
+ TestListResolver userFilter, String className, String reportClass,
String methodName) {
+ String classFile = toClassFileName(className);
+ if (userFilter.shouldRun(classFile, methodName)) {
+ return true;
+ }
+ return !userFilter.hasMethodPatterns()
+ && userFilter.shouldRun(toClassFileName(reportClass),
methodName)
+ && userFilter.getExcludedPatterns().stream()
+ .noneMatch(excluded ->
excluded.matchAsExclusive(classFile, methodName));
+ }
+
+ /**
+ * Reads the reports. Each suite comes with the class its report is named
after: {@code pkg.FooTest} for
+ * {@code TEST-pkg.FooTest.xml}, which also holds the tests of the nested
classes of {@code FooTest}.
+ */
+ private Map<ReportTestSuite, String> readReports() {
File[] reports = reportsDirectory.listFiles(
(dir, name) -> name.startsWith(REPORT_PREFIX) &&
name.endsWith(REPORT_EXTENSION));
if (reports == null) {
- return Collections.emptyList();
+ return Collections.emptyMap();
}
- List<ReportTestSuite> suites = new ArrayList<>();
+ Map<ReportTestSuite, String> suites = new IdentityHashMap<>();
for (File report : reports) {
+ String name = report.getName();
+ String reportClass = name.substring(REPORT_PREFIX.length(),
name.length() - REPORT_EXTENSION.length());
Review Comment:
With `usePhrasedFileName=true`, `@DisplayName("Orders")` can produce
`TEST-Orders.xml` while testcase class names remain `pkg.FooTest$Inner`.
Treating `Orders` as the owner makes `-Dtest=FooTest` reject the nested failure
and report nothing to rerun. Preserve the binary report owner from XML metadata
where available, and use an explicit fallback when customized names prevent
identifying it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]