Merge branch 'pr/4032'

This commit is contained in:
Clément Fournier 2022-07-16 18:10:42 +02:00
commit a42913fb10
No known key found for this signature in database
GPG Key ID: 4D8D42402E4F47E2
5 changed files with 69 additions and 5 deletions

View File

@ -41,6 +41,8 @@ Being based on a proper Antlr grammar, CPD can:
* honor [comment-based suppressions](pmd_userdocs_cpd.html#suppression)
### Fixed Issues
* core
* [#4031](https://github.com/pmd/pmd/issues/4031): \[core] If report is written to stdout, stdout should not be closed
* java
* [#4015](https://github.com/pmd/pmd/issues/4015): \[java] Support JDK 19
* java-bestpractices

View File

@ -4,11 +4,10 @@
package net.sourceforge.pmd.cpd;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
@ -211,7 +210,7 @@ public class CPD {
// legacy writer
System.out.println(arguments.getRenderer().render(cpd.getMatches()));
} else {
arguments.getCPDRenderer().render(cpd.getMatches(), new BufferedWriter(new OutputStreamWriter(System.out)));
arguments.getCPDRenderer().render(cpd.getMatches(), IOUtil.createWriter(Charset.defaultCharset(), null));
}
if (cpd.getMatches().hasNext()) {
if (arguments.isFailOnViolation()) {

View File

@ -8,6 +8,7 @@ import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FilterInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -49,8 +50,16 @@ public final class IOUtil {
private IOUtil() {
}
/**
* Creates a writer that writes to stdout using the system default charset.
*
* @return a writer, never null
*
* @see #createWriter(String)
* @see #createWriter(Charset, String)
*/
public static Writer createWriter() {
return new OutputStreamWriter(System.out);
return createWriter(null);
}
/**
@ -103,7 +112,12 @@ public final class IOUtil {
public static Writer createWriter(Charset charset, String reportFile) {
try {
if (StringUtils.isBlank(reportFile)) {
return new OutputStreamWriter(System.out, charset);
return new OutputStreamWriter(new FilterOutputStream(System.out) {
@Override
public void close() {
// do nothing, avoid closing stdout
}
}, charset);
}
Path path = new File(reportFile).toPath().toAbsolutePath();
Files.createDirectories(path.getParent()); // ensure parent dir exists

View File

@ -12,8 +12,11 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
@ -190,6 +193,24 @@ public class CoreCliTest {
}
}
@Test
public void testReportToStdoutNotClosing() {
PrintStream originalOut = System.out;
PrintStream out = new PrintStream(new FilterOutputStream(originalOut) {
@Override
public void close() {
fail("Stream must not be closed");
}
});
try {
System.setOut(out);
startCapturingErrAndOut();
runPmd(StatusCode.VIOLATIONS_FOUND, "--no-cache", "--dir", srcDir, "--rulesets", "dummy-basic");
} finally {
System.setOut(originalOut);
}
}
@Test
public void testDeprecatedRulesetSyntaxOnCommandLine() {
startCapturingErrAndOut();

View File

@ -4,15 +4,21 @@
package net.sourceforge.pmd.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayReader;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@ -296,4 +302,26 @@ public class IOUtilTest {
InputStream stream = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8));
Assert.assertEquals(testString, IOUtil.readToString(stream, StandardCharsets.UTF_8));
}
@Test
public void testCreateWriterStdout() throws IOException {
PrintStream originalOut = System.out;
ByteArrayOutputStream data = new ByteArrayOutputStream();
PrintStream out = new PrintStream(new FilterOutputStream(data) {
@Override
public void close() {
fail("Stream must not be closed");
}
});
try {
System.setOut(out);
Writer writer = IOUtil.createWriter();
writer.write("Test");
writer.close();
assertEquals("Test", data.toString());
} finally {
System.setOut(originalOut);
}
}
}