Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MPMD-371] Using two ruleset files with same name in different directories #127

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/it/MPMD-296-rulesetsTargetDirectory/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()

// default configuration
File defaultRuleset = new File( basedir, 'target/pmd/rulesets/maven-pmd-plugin-default.xml' )
File defaultRuleset = new File( basedir, 'target/pmd/rulesets/001-maven-pmd-plugin-default.xml' )
assert defaultRuleset.exists()

// backwards compatible configuration (profile customTargetOld)
File customTargetOld = new File( basedir, 'target/maven-pmd-plugin-default.xml' )
File customTargetOld = new File( basedir, 'target/001-maven-pmd-plugin-default.xml' )
assert customTargetOld.exists()

// custom configuration (profile customTarget)
File customTarget = new File( basedir, 'target/pmd-custom/maven-pmd-plugin-default.xml' )
File customTarget = new File( basedir, 'target/pmd-custom/001-maven-pmd-plugin-default.xml' )
assert customTarget.exists()
4 changes: 2 additions & 2 deletions src/it/MPMD-323-multi-module-basedir/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

String[] files = [ "module-a/target/pmd.xml", "module-a/target/pmd/rulesets/ruleset.xml",
"module-b/target/pmd.xml", "module-b/target/pmd/rulesets/ruleset.xml" ]
String[] files = [ "module-a/target/pmd.xml", "module-a/target/pmd/rulesets/001-ruleset.xml",
"module-b/target/pmd.xml", "module-b/target/pmd/rulesets/001-ruleset.xml" ]

files.each
{
Expand Down
4 changes: 2 additions & 2 deletions src/it/multi-module/verify.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ for ( String path : paths )


// check PMD rulesets target copy
File rule = new File( basedir, "mod-1/target/pmd/rulesets/latin-1.xml" );
File rule = new File( basedir, "mod-1/target/pmd/rulesets/002-latin-1.xml" );
String nonascii = "-CHARS: \u00C4\u00D6\u00DC\u00E4\u00F6\u00FC\u00DF\u00BC\u00BD\u00BE\u00A4";
String content = FileUtils.fileRead( rule, "ISO-8859-1" );
if ( content.indexOf( nonascii ) < 0 )
{
throw new IOException( "non-ascii content corrupted in Latin1." );
}
rule = new File( basedir, "mod-1/target/pmd/rulesets/utf-8.xml" );
rule = new File( basedir, "mod-1/target/pmd/rulesets/001-utf-8.xml" );
content = FileUtils.fileRead( rule, "UTF-8" );
if ( content.indexOf( nonascii ) < 0 )
{
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ private List<String> resolveRulesets() throws MavenReportException {
String set = rulesets[idx];
getLog().debug("Preparing ruleset: " + set);
String rulesetFilename = determineRulesetFilename(set);
File ruleset = locator.getResourceAsFile(rulesetFilename, getLocationTemp(set));
File ruleset = locator.getResourceAsFile(rulesetFilename, getLocationTemp(set, idx + 1));
if (null == ruleset) {
throw new MavenReportException("Could not resolve " + set);
}
Expand Down Expand Up @@ -449,9 +449,10 @@ private void generateMavenSiteReport(Locale locale) throws MavenReportException
* Convenience method to get the location of the specified file name.
*
* @param name the name of the file whose location is to be resolved
* @param position position in the list of rulesets (1-based)
* @return a String that contains the absolute file name of the file
*/
protected String getLocationTemp(String name) {
protected String getLocationTemp(String name, int position) {
String loc = name;
if (loc.indexOf('/') != -1) {
loc = loc.substring(loc.lastIndexOf('/') + 1);
Expand All @@ -466,9 +467,10 @@ protected String getLocationTemp(String name) {
// replace all occurrences of the following characters: ? : & = %
loc = loc.replaceAll("[\\?\\:\\&\\=\\%]", "_");

if (!loc.endsWith(".xml")) {
loc = loc + ".xml";
if (loc.endsWith(".xml")) {
loc = loc.substring(0, loc.length() - 4);
}
loc = String.format("%03d-%s.xml", position, loc);

getLog().debug("Before: " + name + " After: " + loc);
return loc;
Expand Down
59 changes: 32 additions & 27 deletions src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testDefaultConfiguration() throws Exception {
// check if the rulesets, that have been applied, have been copied
generatedFile = new File(
getBasedir(),
"target/test/unit/default-configuration/target/pmd/rulesets/maven-pmd-plugin-default.xml");
"target/test/unit/default-configuration/target/pmd/rulesets/001-maven-pmd-plugin-default.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

// check if there's a link to the JXR files
Expand Down Expand Up @@ -159,16 +159,16 @@ public void testJavascriptConfiguration() throws Exception {
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

// these are the rulesets, that have been applied...
generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/bestpractices.xml");
generatedFile = new File(
getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/001-bestpractices.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/codestyle.xml");
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/002-codestyle.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/errorprone.xml");
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/003-errorprone.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

String str = readFile(generatedReport);
Expand Down Expand Up @@ -231,20 +231,21 @@ public void testFileURL() throws Exception {
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

// the resolved and extracted rulesets
generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/basic.xml");
generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/001-basic.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/imports.xml");
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/002-unusedcode.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/unusedcode.xml");
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/003-imports.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile = new File(
getBasedir(),
"target/test/unit/default-configuration/target/pmd/rulesets/export_format_pmd_language_java_name_Sonar_2520way.xml");
"target/test/unit/default-configuration/target/pmd/rulesets/004-export_format_pmd_language_java_name_Sonar_2520way.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

// check if there's a link to the JXR files
Expand Down Expand Up @@ -278,7 +279,9 @@ public void testCustomConfiguration() throws Exception {
File generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/pmd.csv");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/pmd/rulesets/custom.xml");
// 001-maven-pmd-plugin-default.xml is also generated, so we get 002-custom.xml
generatedFile =
new File(getBasedir(), "target/test/unit/custom-configuration/target/pmd/rulesets/002-custom.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

// check if custom ruleset was applied
Expand Down Expand Up @@ -419,9 +422,10 @@ public void testLocationTemp() throws Exception {

assertEquals(
"locationTemp is not correctly encoding filename",
"export_format_pmd_language_java_name_some_2520name.xml",
"001-export_format_pmd_language_java_name_some_2520name.xml",
mojo.getLocationTemp(
"http://nemo.sonarsource.org/sonar/profiles/export?format=pmd&language=java&name=some%2520name"));
"http://nemo.sonarsource.org/sonar/profiles/export?format=pmd&language=java&name=some%2520name",
1));
}

/**
Expand Down Expand Up @@ -485,23 +489,24 @@ public void testJspConfiguration() throws Exception {
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

// these are the rulesets, that have been applied...
generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/bestpractices.xml");
generatedFile = new File(
getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/001-bestpractices.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/codestyle.xml");
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/002-codestyle.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/design.xml");
generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/003-design.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/errorprone.xml");
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/004-errorprone.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/security.xml");
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/005-security.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

String str = readFile(generatedReport);
Expand Down Expand Up @@ -659,30 +664,30 @@ public void testPmdReportResolveRulesets() throws Exception {
generateReport(mojo, testPom);

// these are the rulesets, that have been copied to target/pmd/rulesets
File generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/custom-rules.xml");
File generatedFile = new File(
getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/001-custom-rules.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/bestpractices.xml");
generatedFile = new File(
getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/002-bestpractices.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/java-design.xml");
generatedFile = new File(
getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/003-java-design.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile = new File(
getBasedir(),
"target/test/unit/default-configuration/target/pmd/rulesets/export_format_pmd_language_java_name_Sonar_2520way.xml");
"target/test/unit/default-configuration/target/pmd/rulesets/004-export_format_pmd_language_java_name_Sonar_2520way.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/my-ruleset.xml");
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/005-my-ruleset.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile = new File(
getBasedir(),
"target/test/unit/default-configuration/target/pmd/rulesets/InProgressRuleset.xml_at_refs_2Fheads_2Fmaster.xml");
"target/test/unit/default-configuration/target/pmd/rulesets/006-InProgressRuleset.xml_at_refs_2Fheads_2Fmaster.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

mockServer.stop();
Expand Down