From 10db98bfeef21a25f245a03661c172cf16c1b216 Mon Sep 17 00:00:00 2001 From: Jan van Nunen Date: Wed, 9 Dec 2015 15:00:00 +0100 Subject: [PATCH 1/4] Removed file filter for files that are explicitly specified on the CPD command line using the '--files' command line option. --- .../net/sourceforge/pmd/cpd/CPDCommandLineInterface.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) 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 49fc2ad07f..8cc7e27440 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 @@ -105,13 +105,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) { From b9b6e13849ec6e1102579b59a97d2b528a98739c Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Wed, 20 Jan 2016 22:08:28 +0100 Subject: [PATCH 2/4] Update changelog --- .../net/sourceforge/pmd/cpd/CPDCommandLineInterface.java | 5 ++--- src/site/markdown/overview/changelog.md | 6 ++++++ 2 files changed, 8 insertions(+), 3 deletions(-) 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 8cc7e27440..3f33269854 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; @@ -77,7 +76,7 @@ public class CPDCommandLineInterface { //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 @@ -93,7 +92,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()) { diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index b813b3b3d8..0c9779dead 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -17,6 +17,7 @@ * [#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. * [#79](https://github.com/pmd/pmd/pull/79): do not flag public static void main(String[]) as UseVarargs; ignore @Override for UseVarargs * [#80](https://github.com/pmd/pmd/pull/80): Update mvn-plugin.md * [#83](https://github.com/pmd/pmd/pull/83): Adds new Code Climate-compliant JSON renderer @@ -28,3 +29,8 @@ **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. From 7a33d59a95c41b727ac9859a6be9e21d3ac5e774 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Wed, 20 Jan 2016 22:08:45 +0100 Subject: [PATCH 3/4] Add unit test for new behavior --- .../pmd/cpd/CPDCommandLineInterfaceTest.java | 56 +++++++++++++++++++ .../net/sourceforge/pmd/cpd/ts/File1.ts | 9 +++ .../net/sourceforge/pmd/cpd/ts/File2.ts | 9 +++ 3 files changed, 74 insertions(+) create mode 100644 pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java create mode 100644 pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/ts/File1.ts create mode 100644 pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/ts/File2.ts 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..38b0c8ec73 --- /dev/null +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java @@ -0,0 +1,56 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.cpd; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +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); + } + + @Test + public void shouldFindDuplicatesWithDifferentFileExtensions() throws Exception { + 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 = bufferStdout.toString("UTF-8"); + Assert.assertTrue(out.contains("Found a 9 line (30 tokens) duplication in the following files")); + } + + @Test + public void shouldFindNoDuplicatesWithDifferentFileExtensions() throws Exception { + runCPD("--minimum-tokens", "5", "--language", "js", "--files", "src/test/resources/net/sourceforge/pmd/cpd/ts/"); + + String out = bufferStdout.toString("UTF-8"); + Assert.assertTrue(out.isEmpty()); + } + + private void runCPD(String ... args) { + System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true"); + CPD.main(args); + } +} 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 From 8c4d307ddd1f271d32572672fb22493da97e7e4f Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Wed, 20 Jan 2016 22:13:25 +0100 Subject: [PATCH 4/4] Refactor command line tests for CPD and create a BaseCPDCLITest class --- .../pmd/cpd/CPDCommandLineInterfaceTest.java | 45 ++++------------- .../pmd/cpd/CPDCommandLineInterfaceTest.java | 40 +++------------- .../sourceforge/pmd/cli/BaseCPDCLITest.java | 48 +++++++++++++++++++ 3 files changed, 63 insertions(+), 70 deletions(-) create mode 100644 pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCPDCLITest.java 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 b0025deb2b..43b8b85aad 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")); } @@ -60,7 +38,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")); } @@ -83,7 +61,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()); } @@ -99,7 +77,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")); } @@ -110,7 +88,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("")); } @@ -120,12 +98,7 @@ 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")); } - - 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 index 38b0c8ec73..a36f167686 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 @@ -3,54 +3,26 @@ */ package net.sourceforge.pmd.cpd; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.io.UnsupportedEncodingException; +import net.sourceforge.pmd.cli.BaseCPDCLITest; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; -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 - public void shouldFindDuplicatesWithDifferentFileExtensions() throws Exception { + 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 = bufferStdout.toString("UTF-8"); + String out = getOutput(); Assert.assertTrue(out.contains("Found a 9 line (30 tokens) duplication in the following files")); } @Test - public void shouldFindNoDuplicatesWithDifferentFileExtensions() throws Exception { + public void shouldFindNoDuplicatesWithDifferentFileExtensions() { runCPD("--minimum-tokens", "5", "--language", "js", "--files", "src/test/resources/net/sourceforge/pmd/cpd/ts/"); - String out = bufferStdout.toString("UTF-8"); + String out = getOutput(); Assert.assertTrue(out.isEmpty()); } - - private void runCPD(String ... args) { - System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true"); - CPD.main(args); - } } 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); + } +}