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 7edabf1c00..f42dae63ae 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 @@ -5,7 +5,6 @@ package net.sourceforge.pmd.cpd; import java.io.File; import java.io.FileNotFoundException; -import java.io.FilenameFilter; import java.io.IOException; import java.net.URISyntaxException; import java.util.Arrays; @@ -78,7 +77,7 @@ public class CPDCommandLineInterface { try { // Add files if (null != arguments.getFiles() && !arguments.getFiles().isEmpty()) { - addSourcesFilesToCPD(arguments.getFiles(), arguments.filenameFilter(), cpd, !arguments.isNonRecursive()); + addSourcesFilesToCPD(arguments.getFiles(), cpd, !arguments.isNonRecursive()); } // Add Database URIS @@ -103,7 +102,7 @@ public class CPDCommandLineInterface { } } - private static void addSourcesFilesToCPD(List files, FilenameFilter filter, CPD cpd, boolean recursive) { + private static void addSourcesFilesToCPD(List files, CPD cpd, boolean recursive) { try { for (File file : files) { if (!file.exists()) { @@ -115,13 +114,7 @@ public class CPDCommandLineInterface { cpd.addAllInDirectory(file); } } else { - //Add a single file if it is accepted by the file filter - File directory = file.getAbsoluteFile().getParentFile(); - String filename = file.getName(); - - if (filter.accept(directory, filename)) { - cpd.add(file); - } + cpd.add(file); } } } catch (IOException e) { diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java index 857bfbbc83..9509bed5cb 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java @@ -3,41 +3,19 @@ */ package net.sourceforge.pmd.cpd; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.PrintStream; -import java.io.UnsupportedEncodingException; import java.util.regex.Pattern; -import org.junit.After; +import net.sourceforge.pmd.cli.BaseCPDCLITest; + import org.junit.Assert; -import org.junit.Before; import org.junit.Test; /** * Unit test for {@link CPDCommandLineInterface}. * */ -public class CPDCommandLineInterfaceTest { - private ByteArrayOutputStream bufferStdout; - private PrintStream originalStdout; - private PrintStream originalStderr; - - @Before - public void setup() throws UnsupportedEncodingException { - originalStdout = System.out; - originalStderr = System.err; - bufferStdout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bufferStdout, false, "UTF-8")); - System.setErr(System.out); - } - - @After - public void teardown() { - System.setOut(originalStdout); - System.setErr(originalStderr); - } - +public class CPDCommandLineInterfaceTest extends BaseCPDCLITest { /** * Test ignore identifiers argument. */ @@ -45,7 +23,7 @@ public class CPDCommandLineInterfaceTest { public void testIgnoreIdentifiers() throws Exception { runCPD("--minimum-tokens", "34", "--language", "java", "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers"); - String out = bufferStdout.toString("UTF-8"); + String out = getOutput(); Assert.assertTrue(out.contains("Found a 7 line (36 tokens) duplication")); Assert.assertEquals(4, Integer.parseInt(System.getProperty(CPDCommandLineInterface.STATUS_CODE_PROPERTY))); } @@ -76,7 +54,7 @@ public class CPDCommandLineInterfaceTest { "--exclude", "src/test/resources/net/sourceforge/pmd/cpd/clitest/File2.java" ); - String out = bufferStdout.toString("UTF-8"); + String out = getOutput(); Assert.assertFalse(out.contains("Found a 7 line (34 tokens) duplication")); Assert.assertEquals(0, Integer.parseInt(System.getProperty(CPDCommandLineInterface.STATUS_CODE_PROPERTY))); } @@ -100,7 +78,7 @@ public class CPDCommandLineInterfaceTest { // reset default encoding System.setProperty("file.encoding", origEncoding); - String out = bufferStdout.toString("UTF-8"); + String out = getOutput(); Assert.assertTrue(out.startsWith("")); Assert.assertTrue(Pattern.compile("System\\.out\\.println\\([ij] \\+ \"รค\"\\);").matcher(out).find()); Assert.assertEquals(4, Integer.parseInt(System.getProperty(CPDCommandLineInterface.STATUS_CODE_PROPERTY))); @@ -117,7 +95,7 @@ public class CPDCommandLineInterfaceTest { "--files", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/", "--format", "text", "--skip-lexical-errors"); - String out = bufferStdout.toString("UTF-8"); + String out = getOutput(); Assert.assertTrue(Pattern.compile("Skipping .*?BadFile\\.java\\. Reason: Lexical error in file").matcher(out).find()); Assert.assertTrue(out.contains("Found a 5 line (13 tokens) duplication")); Assert.assertEquals(4, Integer.parseInt(System.getProperty(CPDCommandLineInterface.STATUS_CODE_PROPERTY))); @@ -129,7 +107,7 @@ public class CPDCommandLineInterfaceTest { "--language", "java", "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--format", "xml"); - String out = bufferStdout.toString("UTF-8"); + String out = getOutput(); Assert.assertTrue(out.contains("")); Assert.assertEquals(4, Integer.parseInt(System.getProperty(CPDCommandLineInterface.STATUS_CODE_PROPERTY))); } @@ -140,13 +118,8 @@ public class CPDCommandLineInterfaceTest { "--files", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/", "--language", "c", "--format", "csv"); - String out = bufferStdout.toString("UTF-8"); + String out = getOutput(); Assert.assertFalse(out.contains("Couldn't instantiate renderer")); Assert.assertEquals(0, Integer.parseInt(System.getProperty(CPDCommandLineInterface.STATUS_CODE_PROPERTY))); } - - private void runCPD(String... args) { - System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true"); - CPD.main(args); - } } 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 new file mode 100644 index 0000000000..a36f167686 --- /dev/null +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java @@ -0,0 +1,28 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.cpd; + +import net.sourceforge.pmd.cli.BaseCPDCLITest; + +import org.junit.Assert; +import org.junit.Test; + +public class CPDCommandLineInterfaceTest extends BaseCPDCLITest { + @Test + public void shouldFindDuplicatesWithDifferentFileExtensions() { + runCPD("--minimum-tokens", "5", "--language", "js", "--files", "src/test/resources/net/sourceforge/pmd/cpd/ts/File1.ts", + "src/test/resources/net/sourceforge/pmd/cpd/ts/File2.ts"); + + String out = getOutput(); + Assert.assertTrue(out.contains("Found a 9 line (30 tokens) duplication in the following files")); + } + + @Test + public void shouldFindNoDuplicatesWithDifferentFileExtensions() { + runCPD("--minimum-tokens", "5", "--language", "js", "--files", "src/test/resources/net/sourceforge/pmd/cpd/ts/"); + + String out = getOutput(); + Assert.assertTrue(out.isEmpty()); + } +} diff --git a/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/ts/File1.ts b/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/ts/File1.ts new file mode 100644 index 0000000000..ba8f911c8a --- /dev/null +++ b/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/ts/File1.ts @@ -0,0 +1,9 @@ +(function(){ + +var x = 1; +var y = 2; +var z = 3; +window.alert('Test'); + + +})(); \ No newline at end of file diff --git a/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/ts/File2.ts b/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/ts/File2.ts new file mode 100644 index 0000000000..ba8f911c8a --- /dev/null +++ b/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/ts/File2.ts @@ -0,0 +1,9 @@ +(function(){ + +var x = 1; +var y = 2; +var z = 3; +window.alert('Test'); + + +})(); \ No newline at end of file diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCPDCLITest.java b/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCPDCLITest.java new file mode 100644 index 0000000000..2ab113fe97 --- /dev/null +++ b/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCPDCLITest.java @@ -0,0 +1,48 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.cli; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; + +import net.sourceforge.pmd.cpd.CPD; +import net.sourceforge.pmd.cpd.CPDCommandLineInterface; + +import org.junit.After; +import org.junit.Before; + +public abstract class BaseCPDCLITest { + private ByteArrayOutputStream bufferStdout; + private PrintStream originalStdout; + private PrintStream originalStderr; + + @Before + public void setup() throws UnsupportedEncodingException { + originalStdout = System.out; + originalStderr = System.err; + bufferStdout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bufferStdout, false, "UTF-8")); + System.setErr(System.out); + } + + @After + public void teardown() { + System.setOut(originalStdout); + System.setErr(originalStderr); + } + + public final String getOutput() { + try { + return bufferStdout.toString("UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + protected void runCPD(String ... args) { + System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true"); + CPD.main(args); + } +} diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index eafd877048..4ccdc1059c 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -26,6 +26,7 @@ * [#25](https://github.com/adangel/pmd/pull/25): Added option to exclude C# using directives from CPD analysis * [#27](https://github.com/adangel/pmd/pull/27): Added support for Raw String Literals (C++11). * [#29)(https://github.com/adangel/pmd/pull/29): Added support for files with UTF-8 BOM to JSP tokenizer. +* [#30](https://github.com/adangel/pmd/pull/30): Removed file filter for files that are explicitly specified on the CPD command line using the '--files' command line option. * [#72](https://github.com/pmd/pmd/pull/72): Added capability in Java and JSP parser for tracking tokens. * [#73](https://github.com/pmd/pmd/pull/73): Add rule to look for invalid message format in slf4j loggers * [#74](https://github.com/pmd/pmd/pull/74): Fix rendering CommentDefaultAccessModifier description as code @@ -73,3 +74,9 @@ * [#1442](https://sourceforge.net/p/pmd/bugs/1442/): Java 9 Jigsaw readiness **API Changes:** + +**CLI Changes:** + +* 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.