diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java index 0c43ccb176..9f4c2f3ce2 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java @@ -86,8 +86,8 @@ public class CPDCommandLineInterface { } cpd.go(); + System.out.println(arguments.getRenderer().render(cpd.getMatches())); if (cpd.getMatches().hasNext()) { - System.out.println(arguments.getRenderer().render(cpd.getMatches())); if (arguments.isFailOnViolation()) { setStatusCodeOrExit(DUPLICATE_CODE_FOUND); } else { diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java new file mode 100644 index 0000000000..513cf8c1ea --- /dev/null +++ b/pmd-core/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java @@ -0,0 +1,33 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.cpd; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.LogMode; +import org.junit.contrib.java.lang.system.RestoreSystemProperties; +import org.junit.contrib.java.lang.system.StandardOutputStreamLog; +import org.junit.rules.TestRule; + +public class CPDCommandLineInterfaceTest { + @Rule + public final TestRule restoreSystemProperties = new RestoreSystemProperties(); + @Rule + public final StandardOutputStreamLog log = new StandardOutputStreamLog(LogMode.LOG_ONLY); + + @Test + public void testEmptyResultRendering() { + System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true"); + CPDCommandLineInterface.main(new String[] { + "--minimum-tokens", "340", + "--language", "java", + "--files", "src/test/resources/net/sourceforge/pmd/cpd/files/", + "--format", "xml" + }); + Assert.assertEquals("\n\n", log.getLog()); + } + +} diff --git a/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java b/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java index a36f167686..6a2ad260d6 100644 --- a/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java @@ -23,6 +23,6 @@ public class CPDCommandLineInterfaceTest extends BaseCPDCLITest { runCPD("--minimum-tokens", "5", "--language", "js", "--files", "src/test/resources/net/sourceforge/pmd/cpd/ts/"); String out = getOutput(); - Assert.assertTrue(out.isEmpty()); + Assert.assertTrue(out.trim().isEmpty()); } } diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 156a70d6e7..b4d11aaf11 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -144,6 +144,7 @@ you'll need a java8 runtime environment. * [#1470](https://sourceforge.net/p/pmd/bugs/1470/): Error with type-bound lambda * [#1478](https://sourceforge.net/p/pmd/bugs/1478/): PMD CLI - Use first language as default if Java is not available * [#1485](https://sourceforge.net/p/pmd/bugs/1485/): Analysis of some apex classes cause a stackoverflow error + * [#1481](https://sourceforge.net/p/pmd/bugs/1481/): no problems found results in blank file instead of empty xml **API Changes:** @@ -152,6 +153,7 @@ you'll need a java8 runtime environment. * CPD: If a complete filename is specified, the language dependent filename filter is not applied. This allows to scan files, that are not using the standard file extension. If a directory is specified, the filename filter is still applied and only those files with the correct file extension of the language are scanned. +* CPD: If no problems found, an empty report will be output instead of nothing. See also [#1481](https://sourceforge.net/p/pmd/bugs/1481/) * New command line parameter for PMD: `-norulesetcompatibility` - this disables the ruleset factory compatibility filter and fails, if e.g. an old rule name is used in the ruleset. See also [#1360](https://sourceforge.net/p/pmd/bugs/1360/).