This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-pmd-plugin.git
commit acc3022d92b5b545eb89f9d04c5f309f116d8d31 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Thu Nov 5 02:12:05 2026 +0000 Write the MPMD-205 report at test time and narrow the cpd cs source root The fixture pmd.xml named the file without a directory, so PmdViolationCheckMojo.getFilename never stripped a path. The test now writes the report with an absolute path. The cpd cs configuration walked the whole basedir; it now scans only the cs directory. --- .../pmd/PmdViolationCheckMojoOnLinuxTest.java | 31 +++++++++++++++++++--- .../default-configuration/cpd-cs-plugin-config.xml | 2 +- .../default-configuration/pmd-report-js/pmd.xml | 27 ------------------- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoOnLinuxTest.java b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoOnLinuxTest.java index 466ada9..fb646dd 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoOnLinuxTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoOnLinuxTest.java @@ -18,6 +18,9 @@ */ package org.apache.maven.plugins.pmd; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.stream.Collectors; @@ -30,8 +33,10 @@ import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.io.TempDir; import org.mockito.ArgumentCaptor; +import static org.apache.maven.api.plugin.testing.MojoExtension.setVariableValueToObject; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -226,15 +231,33 @@ public class PmdViolationCheckMojoOnLinuxTest { } /** - * MPMD-205: a violation in a file without a java class, like javascript, is reported by its file name and fails - * the build. + * MPMD-205: a violation in a file without a java class, like javascript, is reported by its file name, without + * the directory of the absolute path in pmd.xml, and fails the build. */ @Basedir("/unit/default-configuration") @InjectMojo(goal = "check", pom = "pmd-check-default-configuration-plugin-config.xml") @MojoParameter(name = "siteDirectory", value = "src/site") - @MojoParameter(name = "targetDirectory", value = "pmd-report-js") @Test - public void testJavascriptViolationFailsTheBuild(PmdViolationCheckMojo mojo) throws Exception { + public void testJavascriptViolationFailsTheBuild(PmdViolationCheckMojo mojo, @TempDir Path targetDirectory) + throws Exception { + String file = targetDirectory + .resolve("src") + .resolve("js") + .resolve("PmdJsCheck.js") + .toString(); + Files.write( + targetDirectory.resolve("pmd.xml"), + ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<pmd xmlns=\"http://pmd.sourceforge.net/report/2.0.0\" version=\"7.18.0\">\n" + + "<file name=\"" + file + "\">\n" + + "<violation beginline=\"23\" endline=\"23\" begincolumn=\"5\" endcolumn=\"23\"" + + " rule=\"GlobalVariable\" ruleset=\"Best Practices\" priority=\"1\">\n" + + "Avoid using global variables\n" + + "</violation>\n" + + "</file>\n" + + "</pmd>\n") + .getBytes(StandardCharsets.UTF_8)); + setVariableValueToObject(mojo, "targetDirectory", targetDirectory.toFile()); Log log = mock(Log.class); mojo.setLog(log); diff --git a/src/test/resources/unit/default-configuration/cpd-cs-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-cs-plugin-config.xml index 7350628..4248c3a 100644 --- a/src/test/resources/unit/default-configuration/cpd-cs-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/cpd-cs-plugin-config.xml @@ -44,7 +44,7 @@ under the License. <include>**/*.cs</include> </includes> <compileSourceRoots> - <compileSourceRoot>${basedir}</compileSourceRoot> + <compileSourceRoot>${basedir}/cs</compileSourceRoot> </compileSourceRoots> <inputEncoding>UTF-8</inputEncoding> </configuration> diff --git a/src/test/resources/unit/default-configuration/pmd-report-js/pmd.xml b/src/test/resources/unit/default-configuration/pmd-report-js/pmd.xml deleted file mode 100644 index 68156bf..0000000 --- a/src/test/resources/unit/default-configuration/pmd-report-js/pmd.xml +++ /dev/null @@ -1,27 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. ---> -<!-- The file name has no directory, so that the check goal reports the same name on every operating system. --> -<pmd xmlns="http://pmd.sourceforge.net/report/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sourceforge.net/report/2.0.0 https://pmd.github.io/schema/report_2_0_0.xsd" version="7.18.0" timestamp="2025-12-15T19:12:14.544"> -<file name="PmdJsCheck.js"> -<violation beginline="23" endline="23" begincolumn="5" endcolumn="23" rule="GlobalVariable" ruleset="Best Practices" externalInfoUrl="https://docs.pmd-code.org/pmd-doc-7.18.0/pmd_rules_ecmascript_bestpractices.html#globalvariable" priority="1"> -Avoid using global variables -</violation> -</file> -</pmd>
