Skip to content

Commit

Permalink
[doc] CPD - use non deprecated --dir cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Feb 17, 2023
1 parent a74bf96 commit 17364a9
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 34 deletions.
27 changes: 14 additions & 13 deletions docs/pages/pmd/userdocs/cpd/cpd.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ Novice as much as advanced readers may want to [read on on Refactoring Guru](htt
required="yes"
%}
{% include custom/cli_option_row.html options="--files,--dir,-d"
description="List of files and directories to process"
description="List of files and directories to process.
<p>Note: `--files` is <span class=\"label label-default\">deprecated</span> since PMD 6.52.0. Usage of `--dir` is preferred.</p>"
required="yes"
%}
{% include custom/cli_option_row.html options="--file-list"
description="Path to file containing a comma delimited list of files to analyze. If this is given, then you don't need to provide `--files`."
description="Path to file containing a comma delimited list of files to analyze. If this is given, then you don't need to provide `--dir`."
%}
{% include custom/cli_option_row.html options="--language"
description="Sources code language."
Expand Down Expand Up @@ -157,49 +158,49 @@ _Note:_ The following example use the Linux start script. For Windows, just repl

Minimum required options: Just give it the minimum duplicate size and the source directory:

$ ./run.sh cpd --minimum-tokens 100 --files /usr/local/java/src/java
$ ./run.sh cpd --minimum-tokens 100 --dir /usr/local/java/src/java

You can also specify the language:

$ ./run.sh cpd --minimum-tokens 100 --files /path/to/c/source --language cpp
$ ./run.sh cpd --minimum-tokens 100 --dir /path/to/c/source --language cpp

You may wish to check sources that are stored in different directories:

$ ./run.sh cpd --minimum-tokens 100 --files /path/to/other/source --files /path/to/other/source --files /path/to/other/source --language fortran
$ ./run.sh cpd --minimum-tokens 100 --dir /path/to/other/source --dir /path/to/other/source --dir /path/to/other/source --language fortran

<em>There should be no limit to the number of '--files', you may add... But if you stumble one, please tell us !</em>
<em>There should be no limit to the number of `--dir`, you may add... But if you stumble one, please tell us !</em>

And if you're checking a C source tree with duplicate files in different architecture directories
you can skip those using --skip-duplicate-files:

$ ./run.sh cpd --minimum-tokens 100 --files /path/to/c/source --language cpp --skip-duplicate-files
$ ./run.sh cpd --minimum-tokens 100 --dir /path/to/c/source --language cpp --skip-duplicate-files

You can also specify the encoding to use when parsing files:

$ ./run.sh cpd --minimum-tokens 100 --files /usr/local/java/src/java --encoding utf-16le
$ ./run.sh cpd --minimum-tokens 100 --dir /usr/local/java/src/java --encoding utf-16le

You can also specify a report format - here we're using the XML report:

$ ./run.sh cpd --minimum-tokens 100 --files /usr/local/java/src/java --format xml
$ ./run.sh cpd --minimum-tokens 100 --dir /usr/local/java/src/java --format xml

The default format is a text report, and there's also a `csv` report.

Note that CPD is pretty memory-hungry; you may need to give Java more memory to run it, like this:

$ export PMD_JAVA_OPTS=-Xmx512m
$ ./run.sh cpd --minimum-tokens 100 --files /usr/local/java/src/java
$ ./run.sh cpd --minimum-tokens 100 --dir /usr/local/java/src/java

In order to change the heap size under Windows, you'll need to edit the batch file `cpd.bat` or
set the environment variable `PMD_JAVA_OPTS` prior to starting CPD:

C:\ > cd C:\pmd-bin-{{site.pmd.version}}\bin
C:\...\bin > set PMD_JAVA_OPTS=-Xmx512m
C:\...\bin > .\cpd.bat --minimum-tokens 100 --files c:\temp\src
C:\...\bin > .\cpd.bat --minimum-tokens 100 --dir c:\temp\src


If you specify a source directory but don't want to scan the sub-directories, you can use the non-recursive option:

$ ./run.sh cpd --minimum-tokens 100 --non-recursive --files /usr/local/java/src/java
$ ./run.sh cpd --minimum-tokens 100 --non-recursive --dir /usr/local/java/src/java

### Exit status

Expand All @@ -209,7 +210,7 @@ This behavior has been introduced to ease CPD integration into scripts or hooks,
<table>
<tr><td>0</td><td>Everything is fine, no code duplications found</td></tr>
<tr><td>1</td><td>Couldn't understand command line parameters or CPD exited with an exception</td></tr>
<tr><td>4</td><td>At least one code duplication has been detected unless '--fail-on-violation false' is used.</td></tr>
<tr><td>4</td><td>At least one code duplication has been detected unless `--fail-on-violation false` is used.</td></tr>
</table>


Expand Down
6 changes: 3 additions & 3 deletions docs/pages/pmd/userdocs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Additionally, the following options, are specified most of the time even though
Like for PMD, CPD is started on Unix by `run.sh cpd` and on Windows by `cpd.bat`.

There are two required parameters:
* `--files <path>`: path to the sources to analyse. This can be a file name, a
* `--dir <path>`: path to the sources to analyse. This can be a file name, a
directory or a jar or zip file containing the sources.
* `--minimum-tokens <number>`: the minimum token length which should be reported as a duplicate.

Expand All @@ -138,7 +138,7 @@ There are two required parameters:
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="cpd-linux">
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">~ $ </span><span class="s2">cd</span> ~/bin/pmd-bin-{{site.pmd.version}}/bin
<span class="gp">~/.../bin $ </span><span class="s2">./run.sh</span> cpd --minimum-tokens 100 --files /home/me/src
<span class="gp">~/.../bin $ </span><span class="s2">./run.sh</span> cpd --minimum-tokens 100 --dir /home/me/src

Found a 7 line (110 tokens) duplication in the following files:
Starting at line 579 of /home/me/src/test/java/foo/FooTypeTest.java
Expand All @@ -154,7 +154,7 @@ There are two required parameters:
</div>
<div role="tabpanel" class="tab-pane" id="cpd-windows">
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">C:\ &gt; </span><span class="s2">cd</span> C:\pmd-bin-{{site.pmd.version}}\bin
<span class="gp">C:\...\bin > </span><span class="s2">.\cpd.bat</span> --minimum-tokens 100 --files c:\temp\src
<span class="gp">C:\...\bin > </span><span class="s2">.\cpd.bat</span> --minimum-tokens 100 --dir c:\temp\src

Found a 7 line (110 tokens) duplication in the following files:
Starting at line 579 of c:\temp\src\test\java\foo\FooTypeTest.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ public static String buildUsageText() {
String helpText = " For example on Windows:" + PMD.EOL;

helpText += " C:\\>" + "pmd-bin-" + PMDVersion.VERSION + "\\bin\\cpd.bat"
+ " --minimum-tokens 100 --files c:\\jdk18\\src\\java" + PMD.EOL;
+ " --minimum-tokens 100 --dir c:\\jdk18\\src\\java" + PMD.EOL;
helpText += PMD.EOL;

helpText += " For example on *nix:" + PMD.EOL;
helpText += " $ " + "pmd-bin-" + PMDVersion.VERSION + "/bin/run.sh cpd"
+ " --minimum-tokens 100 --files /path/to/java/code" + PMD.EOL;
+ " --minimum-tokens 100 --dir /path/to/java/code" + PMD.EOL;
helpText += PMD.EOL;

helpText += " Supported languages: " + Arrays.toString(LanguageFactory.supportedLanguages) + PMD.EOL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private String getExpectedFileEntriesXml(final Collection<String> filenames) {

@Test
public void testEmptyResultRendering() {
CPD.StatusCode statusCode = CPD.runCpd("--minimum-tokens", "340", "--language", "java", "--files",
CPD.StatusCode statusCode = CPD.runCpd("--minimum-tokens", "340", "--language", "java", "--dir",
SRC_DIR, "--format", "xml");
final String expectedFilesXml = getExpectedFileEntriesXml(NUMBER_OF_TOKENS.keySet());
assertEquals(CPD.StatusCode.OK, statusCode);
Expand Down Expand Up @@ -99,7 +99,7 @@ public void testDeprecatedOptionsWarning() throws IOException {

@Test
public void testDebugLogging() {
CPD.StatusCode statusCode = CPD.runCpd("--minimum-tokens", "340", "--language", "java", "--files",
CPD.StatusCode statusCode = CPD.runCpd("--minimum-tokens", "340", "--language", "java", "--dir",
SRC_DIR, "--debug");
assertEquals(CPD.StatusCode.OK, statusCode);
assertThat(errLog.getLog(), containsString("Tokenizing ")); // this is a debug logging
Expand All @@ -108,7 +108,7 @@ public void testDebugLogging() {
@Test
public void testNormalLogging() {
loggingRule.clear();
CPD.StatusCode statusCode = CPD.runCpd("--minimum-tokens", "340", "--language", "java", "--files",
CPD.StatusCode statusCode = CPD.runCpd("--minimum-tokens", "340", "--language", "java", "--dir",
SRC_DIR);
assertEquals(CPD.StatusCode.OK, statusCode);
assertThat(errLog.getLog(), not(containsString("Tokenizing "))); // this is a debug logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ public void runCPD() throws Exception {
result = CpdExecutor.runCpd(tempDir, "-h");
result.assertExecutionResult(0, SUPPORTED_LANGUAGES_CPD);

result = CpdExecutor.runCpd(tempDir, "--minimum-tokens", "10", "--format", "text", "--files", srcDir);
result = CpdExecutor.runCpd(tempDir, "--minimum-tokens", "10", "--format", "text", "--dir", srcDir);
result.assertExecutionResult(4, "Found a 10 line (55 tokens) duplication in the following files:");
result.assertExecutionResult(4, "Class1.java");
result.assertExecutionResult(4, "Class2.java");

result = CpdExecutor.runCpd(tempDir, "--minimum-tokens", "10", "--format", "xml", "--files", srcDir);
result = CpdExecutor.runCpd(tempDir, "--minimum-tokens", "10", "--format", "xml", "--dir", srcDir);
result.assertExecutionResult(4, "<duplication lines=\"10\" tokens=\"55\">");
result.assertExecutionResult(4, "Class1.java\"/>");
result.assertExecutionResult(4, "Class2.java\"/>");

result = CpdExecutor.runCpd(tempDir, "--minimum-tokens", "1000", "--format", "text", "--files", srcDir);
result = CpdExecutor.runCpd(tempDir, "--minimum-tokens", "1000", "--format", "text", "--dir", srcDir);
result.assertExecutionResult(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CPDCommandLineInterfaceTest extends BaseCPDCLITest {
*/
@Test
public void testIgnoreIdentifiers() {
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "34", "--language", "java", "--files",
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "34", "--language", "java", "--dir",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers");
Assert.assertTrue(out.contains("Found a 7 line (36 tokens) duplication"));
}
Expand All @@ -32,7 +32,7 @@ public void testIgnoreIdentifiers() {
*/
@Test
public void testIgnoreIdentifiersFailOnViolationFalse() throws Exception {
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "34", "--language", "java", "--files",
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "34", "--language", "java", "--dir",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers", "--failOnViolation",
"false");
Assert.assertTrue(out.contains("Found a 7 line (36 tokens) duplication"));
Expand All @@ -43,7 +43,7 @@ public void testIgnoreIdentifiersFailOnViolationFalse() throws Exception {
*/
@Test
public void testIgnoreIdentifiersFailOnViolationFalseLongOption() throws Exception {
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "34", "--language", "java", "--files",
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "34", "--language", "java", "--dir",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers", "--fail-on-violation",
"false");
Assert.assertTrue(out.contains("Found a 7 line (36 tokens) duplication"));
Expand All @@ -54,7 +54,7 @@ public void testIgnoreIdentifiersFailOnViolationFalseLongOption() throws Excepti
*/
@Test
public void testExcludes() throws Exception {
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "34", "--language", "java", "--ignore-identifiers", "--files",
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "34", "--language", "java", "--ignore-identifiers", "--dir",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--exclude",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/File2.java");
Assert.assertFalse(out.contains("Found a 7 line (34 tokens) duplication"));
Expand All @@ -70,7 +70,7 @@ public void testEncodingOption() throws Exception {
// set the default encoding under Windows
System.setProperty("file.encoding", "Cp1252");

String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "34", "--language", "java", "--files",
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "34", "--language", "java", "--dir",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers", "--format", "xml",
// request UTF-8 for CPD
"--encoding", "UTF-8");
Expand All @@ -89,7 +89,7 @@ public void testEncodingOption() throws Exception {
*/
@Test
public void testBrokenAndValidFile() throws IOException {
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "10", "--language", "java", "--files",
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "10", "--language", "java", "--dir",
"src/test/resources/net/sourceforge/pmd/cpd/badandgood/", "--format", "text", "--skip-lexical-errors");
Assert.assertTrue(
Pattern.compile("Skipping .*?BadFile\\.java\\. Reason: Lexical error in file").matcher(out).find());
Expand All @@ -98,14 +98,14 @@ public void testBrokenAndValidFile() throws IOException {

@Test
public void testFormatXmlWithoutEncoding() throws Exception {
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "10", "--language", "java", "--files",
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "10", "--language", "java", "--dir",
"src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--format", "xml");
Assert.assertTrue(out.contains("<duplication lines=\"3\" tokens=\"10\">"));
}

@Test
public void testCSVFormat() throws Exception {
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "100", "--files", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/",
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "100", "--dir", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/",
"--language", "c", "--format", "csv");
Assert.assertFalse(out.contains("Couldn't instantiate renderer"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class CPDCommandLineInterfaceTest extends BaseCPDCLITest {
@Test
public void shouldFindDuplicatesWithDifferentFileExtensions() {
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "5", "--language", "js", "--files",
String out = runTest(CPD.StatusCode.DUPLICATE_CODE_FOUND, "--minimum-tokens", "5", "--language", "js", "--dir",
"src/test/resources/net/sourceforge/pmd/cpd/ts/File1.ts",
"src/test/resources/net/sourceforge/pmd/cpd/ts/File2.ts");

Expand All @@ -24,7 +24,7 @@ public void shouldFindDuplicatesWithDifferentFileExtensions() {

@Test
public void shouldFindNoDuplicatesWithDifferentFileExtensions() {
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "5", "--language", "js", "--files",
String out = runTest(CPD.StatusCode.OK, "--minimum-tokens", "5", "--language", "js", "--dir",
"src/test/resources/net/sourceforge/pmd/cpd/ts/");

assertThat(out.trim(), emptyString());
Expand Down

0 comments on commit 17364a9

Please sign in to comment.