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
…uly passed tests

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

This closes apache#670
  • Loading branch information
NissMoony authored and michael-o committed Jun 29, 2024
1 parent 3c49ebd commit 30696af
Show file tree
Hide file tree
Showing 24 changed files with 315 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,15 @@ 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 +1483,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 +1495,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 +1990,7 @@ private StartupReportConfiguration getStartupReportConfiguration(String configCh
getReportSchemaLocation(),
getEncoding(),
isForking,
isEnableOutErrElements(),
xmlReporter,
outReporter,
testsetReporter);
Expand Down Expand Up @@ -2516,6 +2531,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 +3488,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 @@ -64,6 +64,7 @@ public StatelessReportEventListener<WrappedReportEntry, TestSetStats> createList
configuration.getRerunFailingTestsCount(),
configuration.getTestClassMethodRunHistory(),
configuration.getXsdSchemaLocation(),
configuration.isEnableOutErrElements(),
getVersion(),
false,
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public StatelessReportEventListener<WrappedReportEntry, TestSetStats> createList
configuration.getRerunFailingTestsCount(),
configuration.getTestClassMethodRunHistory(),
configuration.getXsdSchemaLocation(),
configuration.isEnableOutErrElements(),
getVersion(),
getUsePhrasedFileName(),
getUsePhrasedTestSuiteClassName(),
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, true, null, false, false, false, false);
}

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

private final String xsdSchemaLocation;

private final boolean enableOutErrElements;

private final String xsdVersion;

// Map between test class name and a map between test method name
Expand All @@ -123,6 +125,7 @@ public StatelessXmlReporter(
int rerunFailingTestsCount,
Map<String, Deque<WrappedReportEntry>> testClassMethodRunHistoryMap,
String xsdSchemaLocation,
boolean enableOutErrElements,
String xsdVersion,
boolean phrasedFileName,
boolean phrasedSuiteName,
Expand All @@ -134,6 +137,7 @@ public StatelessXmlReporter(
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 @@ -105,6 +105,7 @@ public void testFileNameWithoutSuffix() {
0,
new ConcurrentHashMap<String, Deque<WrappedReportEntry>>(),
XSD,
true,
"3.0.1",
false,
false,
Expand Down Expand Up @@ -165,6 +166,7 @@ public void testAllFieldsSerialized() throws IOException {
0,
new ConcurrentHashMap<String, Deque<WrappedReportEntry>>(),
XSD,
true,
"3.0.1",
false,
false,
Expand Down Expand Up @@ -267,6 +269,7 @@ public void testOutputRerunFlakyFailure() throws IOException {
1,
new HashMap<String, Deque<WrappedReportEntry>>(),
XSD,
true,
"3.0.1",
false,
false,
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, true, "3.0.1", false, false, false, false);

WrappedReportEntry testSetReportEntry = new WrappedReportEntry(
new SimpleReportEntry(
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 30696af

Please sign in to comment.