-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3417 from aidan-harding:language-detection
[core] Support forcing a specific language from the command-line #3417
- Loading branch information
Showing
14 changed files
with
262 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
pmd-core/src/test/java/net/sourceforge/pmd/lang/LanguageParameterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
*/ | ||
|
||
package net.sourceforge.pmd.lang; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import net.sourceforge.pmd.cli.PMDCommandLineInterface; | ||
import net.sourceforge.pmd.cli.PMDParameters; | ||
|
||
public class LanguageParameterTest { | ||
|
||
/** Test that language parameters from the CLI are correctly passed through to the PMDConfiguration. Although this is a | ||
* CLI test, it resides here to take advantage of {@link net.sourceforge.pmd.lang.DummyLanguageModule} | ||
*/ | ||
@Test | ||
public void testLanguageFromCliToConfiguration() { | ||
PMDParameters params = new PMDParameters(); | ||
String[] args = { "-d", "source_folder", "-f", "ideaj", "-P", "sourcePath=/home/user/source/", "-R", "java-empty", "-force-language", "dummy"}; | ||
PMDCommandLineInterface.extractParameters(params, args, "PMD"); | ||
|
||
Assert.assertEquals(new DummyLanguageModule().getDefaultVersion().getName(), params.toConfiguration().getForceLanguageVersion().getName()); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
pmd-xml/src/test/java/net/sourceforge/pmd/lang/xml/XmlCliTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
*/ | ||
|
||
package net.sourceforge.pmd.lang.xml; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import net.sourceforge.pmd.cli.BaseCLITest; | ||
|
||
public class XmlCliTest extends BaseCLITest { | ||
private static final String BASE_DIR = "src/test/resources/net/sourceforge/pmd/lang/xml/cli-tests/sampleproject"; | ||
private static final String RULE_MESSAGE = "A tags are not allowed"; | ||
|
||
private String[] createArgs(String directory, String ... args) { | ||
List<String> arguments = new ArrayList<>(); | ||
arguments.add("-f"); | ||
arguments.add("text"); | ||
arguments.add("-no-cache"); | ||
arguments.add("-R"); | ||
arguments.add(BASE_DIR + "/ruleset.xml"); | ||
arguments.add("-d"); | ||
arguments.add(BASE_DIR + directory); | ||
arguments.addAll(Arrays.asList(args)); | ||
return arguments.toArray(new String[0]); | ||
} | ||
|
||
@Test | ||
public void analyzeSingleXmlWithoutForceLanguage() { | ||
String resultFilename = runTest(createArgs("/src/file1.ext"), "analyzeSingleXmlWithoutForceLanguage", 0); | ||
assertRuleMessage(0, resultFilename); | ||
} | ||
|
||
@Test | ||
public void analyzeSingleXmlWithForceLanguage() { | ||
String resultFilename = runTest(createArgs("/src/file1.ext", "-force-language", "xml"), | ||
"analyzeSingleXmlWithForceLanguage", 4); | ||
assertRuleMessage(1, resultFilename); | ||
} | ||
|
||
@Test | ||
public void analyzeDirectoryWithForceLanguage() { | ||
String resultFilename = runTest(createArgs("/src/", "-force-language", "xml"), | ||
"analyzeDirectoryWithForceLanguage", 4); | ||
assertRuleMessage(3, resultFilename); | ||
} | ||
|
||
private void assertRuleMessage(int expectedCount, String resultFilename) { | ||
try { | ||
String result = FileUtils.readFileToString(new File(resultFilename), StandardCharsets.UTF_8); | ||
Assert.assertEquals(expectedCount, StringUtils.countMatches(result, RULE_MESSAGE)); | ||
} catch (IOException e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
pmd-xml/src/test/resources/net/sourceforge/pmd/lang/xml/cli-tests/sampleproject/ruleset.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0"?> | ||
|
||
<ruleset name="sample" | ||
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd"> | ||
|
||
<description> | ||
Sample | ||
</description> | ||
|
||
<rule name="A" | ||
language="xml" | ||
message="A tags are not allowed" | ||
class="net.sourceforge.pmd.lang.rule.XPathRule"> | ||
<description> | ||
A tags are not allowed | ||
</description> | ||
<priority>3</priority> | ||
<properties> | ||
<property name="version" value="2.0"/> | ||
<property name="xpath"> | ||
<value> | ||
<![CDATA[ | ||
//a | ||
]]> | ||
</value> | ||
</property> | ||
</properties> | ||
</rule> | ||
</ruleset> |
5 changes: 5 additions & 0 deletions
5
...xml/src/test/resources/net/sourceforge/pmd/lang/xml/cli-tests/sampleproject/src/file1.ext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- BSD-style license; for more info see http://pmd.sourceforge.net/license.html --> | ||
<file> | ||
<a></a> | ||
</file> |
6 changes: 6 additions & 0 deletions
6
...xml/src/test/resources/net/sourceforge/pmd/lang/xml/cli-tests/sampleproject/src/file2.ext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- BSD-style license; for more info see http://pmd.sourceforge.net/license.html --> | ||
<file> | ||
<a></a> | ||
<a></a> | ||
</file> |
3 changes: 3 additions & 0 deletions
3
...xml/src/test/resources/net/sourceforge/pmd/lang/xml/cli-tests/sampleproject/src/file3.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
|
||
Other file that is not a xml file. |
Oops, something went wrong.