Skip to content

Commit

Permalink
[SUREFIRE-1934] Ability to disable system-out/system-err for successf…
Browse files Browse the repository at this point in the history
…ully passed tests

Co-authored-by: Michael Osipov <michaelo@apache.org>

This closes #670
  • Loading branch information
NissMoony authored and michael-o committed Jul 1, 2024
1 parent bce1b39 commit c670335
Show file tree
Hide file tree
Showing 24 changed files with 337 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,16 @@ public abstract class AbstractSurefireMojo extends AbstractMojo implements Suref
@Parameter(property = "enableAssertions", defaultValue = "true")
private boolean enableAssertions;

/**
* Flag for including/excluding {@code <system-out />} and {@code <system-err />} elements for
* successfully passed tests in XML reports.
* Note that the default value may change to {@code false} is a future version.
*
* @since 3.3.1
*/
@Parameter(property = "enableOutErrElements", defaultValue = "true")
private boolean enableOutErrElements;

/**
* The current build session instance.
*/
Expand Down Expand Up @@ -1474,6 +1484,10 @@ private void convertJunitCoreParameters() throws MojoExecutionException {
Double.toString(getParallelTestsTimeoutForcedInSeconds()));
getProperties()
.setProperty(ProviderParameterNames.PARALLEL_OPTIMIZE_PROP, Boolean.toString(isParallelOptimized()));
getProperties()
.setProperty(
ProviderParameterNames.ENABLE_OUT_ERR_ELEMENTS_PROP,
Boolean.toString(isEnableOutErrElements()));

String message = "parallel='" + usedParallel + '\''
+ ", perCoreThreadCount=" + getPerCoreThreadCount()
Expand All @@ -1482,7 +1496,8 @@ private void convertJunitCoreParameters() throws MojoExecutionException {
+ ", threadCountSuites=" + getThreadCountSuites()
+ ", threadCountClasses=" + getThreadCountClasses()
+ ", threadCountMethods=" + getThreadCountMethods()
+ ", parallelOptimized=" + isParallelOptimized();
+ ", parallelOptimized=" + isParallelOptimized()
+ ", enableOutErrElements=" + isEnableOutErrElements();

logDebugOrCliShowErrors(message);
}
Expand Down Expand Up @@ -1976,6 +1991,7 @@ private StartupReportConfiguration getStartupReportConfiguration(String configCh
getReportSchemaLocation(),
getEncoding(),
isForking,
isEnableOutErrElements(),
xmlReporter,
outReporter,
testsetReporter);
Expand Down Expand Up @@ -2516,6 +2532,7 @@ private String getConfigChecksum() {
checksum.add(getTempDir());
checksum.add(useModulePath());
checksum.add(getEnableProcessChecker());
checksum.add(isEnableOutErrElements());
addPluginSpecificChecksumItems(checksum);
return checksum.getSha1();
}
Expand Down Expand Up @@ -3472,6 +3489,15 @@ public void setEnableAssertions(boolean enableAssertions) {
this.enableAssertions = enableAssertions;
}

public boolean isEnableOutErrElements() {
return enableOutErrElements;
}

@SuppressWarnings("UnusedDeclaration")
public void setEnableOutErrElements(boolean enableOutErrElements) {
this.enableOutErrElements = enableOutErrElements;
}

public MavenSession getSession() {
return session;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private Object createStartupReportConfiguration(@Nonnull StartupReportConfigurat
String.class,
String.class,
boolean.class,
boolean.class,
statelessTestsetReporter,
consoleOutputReporter,
statelessTestsetInfoReporter);
Expand All @@ -103,6 +104,7 @@ private Object createStartupReportConfiguration(@Nonnull StartupReportConfigurat
reporterConfiguration.getXsdSchemaLocation(),
reporterConfiguration.getEncoding().name(),
reporterConfiguration.isForking(),
reporterConfiguration.isEnableOutErrElements(),
reporterConfiguration.getXmlReporter().clone(surefireClassLoader),
reporterConfiguration.getConsoleOutputReporter().clone(surefireClassLoader),
reporterConfiguration.getTestsetReporter().clone(surefireClassLoader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public final class StartupReportConfiguration {

private final boolean isForking;

private final boolean enableOutErrElements;

private final SurefireStatelessReporter xmlReporter;

private final SurefireConsoleOutputReporter consoleOutputReporter;
Expand All @@ -108,6 +110,7 @@ public StartupReportConfiguration(
String xsdSchemaLocation,
String encoding,
boolean isForking,
boolean enableOutErrElements,
SurefireStatelessReporter xmlReporter,
SurefireConsoleOutputReporter consoleOutputReporter,
SurefireStatelessTestsetInfoReporter testsetReporter) {
Expand All @@ -127,6 +130,7 @@ public StartupReportConfiguration(
String charset = trimToNull(encoding);
this.encoding = charset == null ? UTF_8 : Charset.forName(charset);
this.isForking = isForking;
this.enableOutErrElements = enableOutErrElements;
this.xmlReporter = xmlReporter;
this.consoleOutputReporter = consoleOutputReporter;
this.testsetReporter = testsetReporter;
Expand Down Expand Up @@ -177,6 +181,7 @@ public StatelessReportEventListener<WrappedReportEntry, TestSetStats> instantiat
trimStackTrace,
rerunFailingTestsCount,
xsdSchemaLocation,
enableOutErrElements,
testClassMethodRunHistory);

return xmlReporter.isDisable() ? null : xmlReporter.createListener(xmlReporterConfig);
Expand Down Expand Up @@ -239,6 +244,10 @@ public boolean isForking() {
return isForking;
}

public boolean isEnableOutErrElements() {
return enableOutErrElements;
}

private File resolveReportsDirectory(Integer forkNumber) {
return forkNumber == null ? reportsDirectory : replaceForkThreadsInPath(reportsDirectory, forkNumber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ public DefaultStatelessReportMojoConfiguration(
boolean trimStackTrace,
int rerunFailingTestsCount,
String xsdSchemaLocation,
boolean enableOutErrElements,
Map<String, Deque<WrappedReportEntry>> testClassMethodRunHistory) {
super(reportsDirectory, reportNameSuffix, trimStackTrace, rerunFailingTestsCount, xsdSchemaLocation);
super(
reportsDirectory,
reportNameSuffix,
trimStackTrace,
rerunFailingTestsCount,
xsdSchemaLocation,
enableOutErrElements);
this.testClassMethodRunHistory = testClassMethodRunHistory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public StatelessReportEventListener<WrappedReportEntry, TestSetStats> createList
false,
false,
false,
false);
false,
configuration.isEnableOutErrElements());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public StatelessReportEventListener<WrappedReportEntry, TestSetStats> createList
getUsePhrasedFileName(),
getUsePhrasedTestSuiteClassName(),
getUsePhrasedTestCaseClassName(),
getUsePhrasedTestCaseMethodName());
getUsePhrasedTestCaseMethodName(),
configuration.isEnableOutErrElements());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class NullStatelessXmlReporter extends StatelessXmlReporter {
static final NullStatelessXmlReporter INSTANCE = new NullStatelessXmlReporter();

private NullStatelessXmlReporter() {
super(null, null, false, 0, null, null, null, false, false, false, false);
super(null, null, false, 0, null, null, null, false, false, false, false, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public class StatelessXmlReporter implements StatelessReportEventListener<Wrappe

private final boolean phrasedMethodName;

private final boolean enableOutErrElements;

public StatelessXmlReporter(
File reportsDirectory,
String reportNameSuffix,
Expand All @@ -127,13 +129,15 @@ public StatelessXmlReporter(
boolean phrasedFileName,
boolean phrasedSuiteName,
boolean phrasedClassName,
boolean phrasedMethodName) {
boolean phrasedMethodName,
boolean enableOutErrElements) {
this.reportsDirectory = reportsDirectory;
this.reportNameSuffix = reportNameSuffix;
this.trimStackTrace = trimStackTrace;
this.rerunFailingTestsCount = rerunFailingTestsCount;
this.testClassMethodRunHistoryMap = testClassMethodRunHistoryMap;
this.xsdSchemaLocation = xsdSchemaLocation;
this.enableOutErrElements = enableOutErrElements;
this.xsdVersion = xsdVersion;
this.phrasedFileName = phrasedFileName;
this.phrasedSuiteName = phrasedSuiteName;
Expand Down Expand Up @@ -228,7 +232,9 @@ private void serializeTestClassWithoutRerun(
methodEntry.getReportEntryType().getXmlTag(),
false);
}
createOutErrElements(fw, ppw, methodEntry, outputStream);
if (methodEntry.getReportEntryType() != SUCCESS || enableOutErrElements) {
createOutErrElements(fw, ppw, methodEntry, outputStream);
}
ppw.endElement();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void setup() {
null,
null,
false,
true,
xmlReporter,
consoleOutputReporter,
infoReporter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public void processShouldExitWithoutSayingGoodBye() throws Exception {
null,
null,
true,
true,
xmlReporter,
outputReporter,
statelessTestsetInfoReporter);
Expand Down Expand Up @@ -247,6 +248,7 @@ public void processShouldWaitForAck() throws Exception {
null,
null,
true,
true,
xmlReporter,
outputReporter,
statelessTestsetInfoReporter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private static StartupReportConfiguration defaultValue() {
null,
null,
true,
true,
new SurefireStatelessReporter(),
new SurefireConsoleOutputReporter(),
new SurefireStatelessTestsetInfoReporter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void shouldCreateConsoleListener() {
String schema = "https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd";
Map<String, Deque<WrappedReportEntry>> testClassMethodRunHistory = new HashMap<>();
DefaultStatelessReportMojoConfiguration config = new DefaultStatelessReportMojoConfiguration(
reportsDirectory, reportNameSuffix, true, 5, schema, testClassMethodRunHistory);
reportsDirectory, reportNameSuffix, true, 5, schema, true, testClassMethodRunHistory);
SurefireStatelessReporter extension = new SurefireStatelessReporter();

assertThat(extension.getVersion()).isEqualTo("3.0.1");
Expand Down Expand Up @@ -141,7 +141,7 @@ public void shouldCreateJUnit5ConsoleListener() {
String schema = "https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd";
Map<String, Deque<WrappedReportEntry>> testClassMethodRunHistory = new HashMap<>();
DefaultStatelessReportMojoConfiguration config = new DefaultStatelessReportMojoConfiguration(
reportsDirectory, reportNameSuffix, true, 5, schema, testClassMethodRunHistory);
reportsDirectory, reportNameSuffix, true, 5, schema, true, testClassMethodRunHistory);
JUnit5Xml30StatelessReporter extension = new JUnit5Xml30StatelessReporter();

assertThat(extension.getVersion()).isEqualTo("3.0.1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void testMergeTestHistoryResult() throws Exception {
null,
null,
false,
true,
new SurefireStatelessReporter(),
new SurefireConsoleOutputReporter(),
new SurefireStatelessTestsetInfoReporter());
Expand Down Expand Up @@ -288,6 +289,7 @@ public void testLogger() {
null,
null,
false,
true,
new SurefireStatelessReporter(),
new SurefireConsoleOutputReporter(),
new SurefireStatelessTestsetInfoReporter());
Expand Down Expand Up @@ -352,6 +354,7 @@ public void testCreateReporterWithZeroStatistics() {
null,
null,
false,
true,
new SurefireStatelessReporter(),
new SurefireConsoleOutputReporter(),
new SurefireStatelessTestsetInfoReporter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public void testFileNameWithoutSuffix() {
false,
false,
false,
false);
false,
true);
reporter.cleanTestHistoryMap();

ReportEntry reportEntry = new SimpleReportEntry(
Expand Down Expand Up @@ -169,7 +170,8 @@ public void testAllFieldsSerialized() throws IOException {
false,
false,
false,
false);
false,
true);
reporter.testSetCompleted(testSetReportEntry, stats);

FileInputStream fileInputStream = new FileInputStream(expectedReportFile);
Expand Down Expand Up @@ -271,7 +273,8 @@ public void testOutputRerunFlakyFailure() throws IOException {
false,
false,
false,
false);
false,
true);

reporter.testSetCompleted(testSetReportEntry, stats);
reporter.testSetCompleted(testSetReportEntry, rerunStats);
Expand Down Expand Up @@ -370,7 +373,7 @@ public void testOutputRerunFlakyAssumption() throws IOException {
rerunStats.testSucceeded(testTwoSecondError);

StatelessXmlReporter reporter = new StatelessXmlReporter(
reportDir, null, false, 1, new HashMap<>(), XSD, "3.0.1", false, false, false, false);
reportDir, null, false, 1, new HashMap<>(), XSD, "3.0.1", false, false, false, false, true);

WrappedReportEntry testSetReportEntry = new WrappedReportEntry(
new SimpleReportEntry(
Expand Down Expand Up @@ -534,7 +537,7 @@ public void testReporterHandlesATestWithoutMessageAndWithEmptyStackTrace() {
null);

StatelessXmlReporter reporter = new StatelessXmlReporter(
reportDir, null, false, 1, new HashMap<>(), XSD, "3.0.1", false, false, false, false);
reportDir, null, false, 1, new HashMap<>(), XSD, "3.0.1", false, false, false, false, true);

reporter.testSetCompleted(testReport, stats);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ public class ProviderParameterNames {
public static final String PARALLEL_TIMEOUTFORCED_PROP = "paralleltimeoutforced";

public static final String PARALLEL_OPTIMIZE_PROP = "paralleloptimization";

public static final String ENABLE_OUT_ERR_ELEMENTS_PROP = "enableouterrelements";
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ public class StatelessReportMojoConfiguration {

private final String xsdSchemaLocation;

private final boolean enableOutErrElements;

public StatelessReportMojoConfiguration(
File reportsDirectory,
String reportNameSuffix,
boolean trimStackTrace,
int rerunFailingTestsCount,
String xsdSchemaLocation) {
String xsdSchemaLocation,
boolean enableOutErrElements) {
this.reportsDirectory = reportsDirectory;
this.reportNameSuffix = reportNameSuffix;
this.trimStackTrace = trimStackTrace;
this.rerunFailingTestsCount = rerunFailingTestsCount;
this.xsdSchemaLocation = xsdSchemaLocation;
this.enableOutErrElements = enableOutErrElements;
}

public File getReportsDirectory() {
Expand All @@ -68,4 +72,8 @@ public int getRerunFailingTestsCount() {
public String getXsdSchemaLocation() {
return xsdSchemaLocation;
}

public boolean isEnableOutErrElements() {
return enableOutErrElements;
}
}
Loading

0 comments on commit c670335

Please sign in to comment.