Skip to content

Commit 8fada9f

Browse files
committed
Issue #3166: STDOUT and STDERR are reset after console test execution
1 parent 7559ea2 commit 8fada9f

File tree

5 files changed

+78
-78
lines changed

5 files changed

+78
-78
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-RC2.adoc

Lines changed: 0 additions & 71 deletions
This file was deleted.

documentation/src/docs/asciidoc/release-notes/release-notes-5.13.0-M1.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ repository on GitHub.
2626
[[release-notes-5.13.0-M1-junit-platform-new-features-and-improvements]]
2727
==== New Features and Improvements
2828

29-
* ❓
29+
* New optional CLI options `--redirect-stdout` and `--redirect-stderr` to redirect stdout
30+
and stderr outputs to a file.
3031

3132

3233
[[release-notes-5.13.0-M1-junit-jupiter]]

junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,16 @@ private TestExecutionSummary executeTests(PrintWriter out, Optional<Path> report
104104
Launcher launcher = launcherSupplier.get();
105105
SummaryGeneratingListener summaryListener = registerListeners(out, reportsDir, launcher);
106106

107-
redirectStdStreams(outputOptions.getStdoutPath(), outputOptions.getStderrPath());
108-
109-
LauncherDiscoveryRequestBuilder discoveryRequestBuilder = toDiscoveryRequestBuilder(discoveryOptions);
110-
reportsDir.ifPresent(dir -> discoveryRequestBuilder.configurationParameter(OUTPUT_DIR_PROPERTY_NAME,
111-
dir.toAbsolutePath().toString()));
112-
launcher.execute(discoveryRequestBuilder.build());
107+
PrintStream originalOut = System.out;
108+
PrintStream originalErr = System.err;
109+
try {
110+
redirectStdStreams(outputOptions.getStdoutPath(), outputOptions.getStderrPath());
111+
launchTests(launcher, reportsDir);
112+
}
113+
finally {
114+
System.setOut(originalOut);
115+
System.setErr(originalErr);
116+
}
113117

114118
TestExecutionSummary summary = summaryListener.getSummary();
115119
if (summary.getTotalFailureCount() > 0 || outputOptions.getDetails() != Details.NONE) {
@@ -119,6 +123,13 @@ private TestExecutionSummary executeTests(PrintWriter out, Optional<Path> report
119123
return summary;
120124
}
121125

126+
private void launchTests(Launcher launcher, Optional<Path> reportsDir) {
127+
LauncherDiscoveryRequestBuilder discoveryRequestBuilder = toDiscoveryRequestBuilder(discoveryOptions);
128+
reportsDir.ifPresent(dir -> discoveryRequestBuilder.configurationParameter(OUTPUT_DIR_PROPERTY_NAME,
129+
dir.toAbsolutePath().toString()));
130+
launcher.execute(discoveryRequestBuilder.build());
131+
}
132+
122133
private Optional<ClassLoader> createCustomClassLoader() {
123134
List<Path> additionalClasspathEntries = discoveryOptions.getExistingAdditionalClasspathEntries();
124135
if (!additionalClasspathEntries.isEmpty()) {

platform-tests/src/test/java/org/junit/platform/console/ConsoleLauncherIntegrationTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@
1010

1111
package org.junit.platform.console;
1212

13+
import static java.nio.file.Files.deleteIfExists;
1314
import static org.assertj.core.api.Assertions.assertThat;
1415
import static org.junit.jupiter.api.Assertions.assertAll;
1516
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1617
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
20+
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
1723

1824
import org.junit.jupiter.api.Test;
1925
import org.junit.jupiter.params.ParameterizedTest;
@@ -92,4 +98,35 @@ void executeScanModules(final String line) {
9298
assertEquals(0, new ConsoleLauncherWrapper().execute(args1).getTestsFoundCount());
9399
}
94100

101+
@ParameterizedTest
102+
@ValueSource(strings = { "--redirect-stdout", "--redirect-stderr" })
103+
void executeWithRedirectedStdStream(String redirectedStream) throws IOException {
104+
105+
Path outputFile = Path.of("foo.txt");
106+
var line = String.format(
107+
"execute -e junit-jupiter --select-method org.junit.platform.console.options.StdStreamTest#printTest "
108+
+ "%s %s",
109+
redirectedStream, outputFile);
110+
var args = line.split(" ");
111+
new ConsoleLauncherWrapper().execute(args);
112+
113+
assertTrue(Files.exists(outputFile), "File does not exist.");
114+
assertEquals(Files.size(outputFile), 20, "Invalid file size.");
115+
deleteIfExists(outputFile);
116+
}
117+
118+
@Test
119+
void executeWithRedirectedStdStreamsToSameFile() throws IOException {
120+
Path outputFile = Path.of("foo.txt");
121+
var line = String.format(
122+
"execute -e junit-jupiter --select-method org.junit.platform.console.options.StdStreamTest#printTest "
123+
+ "--redirect-stdout %s --redirect-stderr %s",
124+
outputFile, outputFile);
125+
var args = line.split(" ");
126+
new ConsoleLauncherWrapper().execute(args);
127+
128+
assertTrue(Files.exists(outputFile), "File does not exist.");
129+
assertEquals(Files.size(outputFile), 40, "Invalid file size.");
130+
deleteIfExists(outputFile);
131+
}
95132
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.console.options;
12+
13+
import org.junit.jupiter.api.Test;
14+
15+
public class StdStreamTest {
16+
17+
@Test
18+
void printTest() {
19+
System.out.print("Writing to STDOUT...");
20+
System.err.print("Writing to STDERR...");
21+
}
22+
}

0 commit comments

Comments
 (0)