forked from junit-team/junit5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI options to redirect stderr and stdout.
Add new CLI options --redirect-stdout and --redirect-stderr to redirect stdout and stderr to a file, if both are set to the same file, stdout and stderr will be merged so we can keep the relative order of messages. This makes it easier to correlate error messages with the corresponding output. Issue: junit-team#3166
- Loading branch information
mobounya
committed
Jan 13, 2024
1 parent
10cb52b
commit af11682
Showing
5 changed files
with
245 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...nsole/src/main/java/org/junit/platform/console/tasks/RedirectStdoutAndStderrListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.console.tasks; | ||
|
||
import static org.apiguardian.api.API.Status.INTERNAL; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.apiguardian.api.API; | ||
import org.junit.platform.engine.reporting.ReportEntry; | ||
import org.junit.platform.launcher.LauncherConstants; | ||
import org.junit.platform.launcher.TestExecutionListener; | ||
import org.junit.platform.launcher.TestIdentifier; | ||
import org.junit.platform.launcher.TestPlan; | ||
|
||
@API(status = INTERNAL, since = "5.11") | ||
public class RedirectStdoutAndStderrListener implements TestExecutionListener { | ||
private final Path stdoutOutputPath; | ||
private final Path stderrOutputPath; | ||
private final StringWriter stdoutBuffer; | ||
private final StringWriter stderrBuffer; | ||
private final PrintWriter out; | ||
|
||
public RedirectStdoutAndStderrListener(Path stdoutOutputPath, Path stderrOutputPath, PrintWriter out) { | ||
this.stdoutOutputPath = stdoutOutputPath; | ||
this.stderrOutputPath = stderrOutputPath; | ||
this.stdoutBuffer = new StringWriter(); | ||
this.stderrBuffer = new StringWriter(); | ||
this.out = out; | ||
} | ||
|
||
public void reportingEntryPublished(TestIdentifier testIdentifier, ReportEntry entry) { | ||
if (testIdentifier.isTest()) { | ||
String redirectedStdoutContent = entry.getKeyValuePairs().get(LauncherConstants.STDOUT_REPORT_ENTRY_KEY); | ||
String redirectedStderrContent = entry.getKeyValuePairs().get(LauncherConstants.STDERR_REPORT_ENTRY_KEY); | ||
|
||
if (redirectedStdoutContent != null && !redirectedStdoutContent.isEmpty()) { | ||
this.stdoutBuffer.append(redirectedStdoutContent); | ||
} | ||
if (redirectedStderrContent != null && !redirectedStderrContent.isEmpty()) { | ||
this.stderrBuffer.append(redirectedStderrContent); | ||
} | ||
} | ||
} | ||
|
||
public void testPlanExecutionFinished(TestPlan testPlan) { | ||
if (stdoutBuffer.getBuffer().length() > 0) { | ||
flushBufferedOutputToFile(this.stdoutOutputPath, this.stdoutBuffer); | ||
} | ||
if (stderrBuffer.getBuffer().length() > 0) { | ||
flushBufferedOutputToFile(this.stderrOutputPath, this.stderrBuffer); | ||
} | ||
} | ||
|
||
private void flushBufferedOutputToFile(Path file, StringWriter buffer) { | ||
deleteFile(file); | ||
createFile(file); | ||
writeContentToFile(file, buffer.toString()); | ||
} | ||
|
||
private void writeContentToFile(Path file, String buffer) { | ||
try (Writer fileWriter = Files.newBufferedWriter(file)) { | ||
fileWriter.write(buffer); | ||
} | ||
catch (IOException e) { | ||
printException("Failed to write content to file: " + file, e); | ||
} | ||
} | ||
|
||
private void deleteFile(Path file) { | ||
try { | ||
Files.deleteIfExists(file); | ||
} | ||
catch (IOException e) { | ||
printException("Failed to delete file: " + file, e); | ||
} | ||
} | ||
|
||
private void createFile(Path file) { | ||
try { | ||
Files.createFile(file); | ||
} | ||
catch (IOException e) { | ||
printException("Failed to create file: " + file, e); | ||
} | ||
} | ||
|
||
private void printException(String message, Exception exception) { | ||
out.println(message); | ||
exception.printStackTrace(out); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters