Skip to content

More restrictive check for violation location in tests#6559

Draft
zbynek wants to merge 1 commit into
pmd:mainfrom
zbynek:restrict-location
Draft

More restrictive check for violation location in tests#6559
zbynek wants to merge 1 commit into
pmd:mainfrom
zbynek:restrict-location

Conversation

@zbynek

@zbynek zbynek commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Describe the PR

Related to #6084, this changes the interpretation of expected-linenumbers in tests to be more restrictive, making it easier to check that the location is specific enough

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@zbynek zbynek force-pushed the restrict-location branch from 505ef4f to 79e8fd2 Compare March 31, 2026 06:38
@pmd-actions-helper

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.

Regression Tester Report

(comment created at 2026-03-31 06:54:35+00:00 for 79e8fd2)

@adangel

adangel commented Apr 30, 2026

Copy link
Copy Markdown
Member

While finishing up #6084, I have now an idea what we need.
The workaround I used currently for a missing feature is this: I place every token on a separate line to be sure, which part exactly is reported.
It would be better, if we would verify in our rule tests the exact location (including columns) and not only the line numbers.

According to https://docs.pmd-code.org/latest/pmd_userdocs_extending_testing.html#test-xml-reference a test case looks currently like this:

    <test-code>
        <description>Verify location of violation</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>2-2</expected-linenumbers>
        <code><![CDATA[
public class
Foo // <--violation: line 2
{}
        ]]></code>
    </test-code>

What about this way?

    <test-code>
        <description>Verify location of violation</description>
        <expected-violations>
          <violation loc="1:14-1:17">optional message</violation>
        </expected-violations>
        <code><![CDATA[
public class Foo {}
        ]]></code>
    </test-code>

@zbynek WDYT?

@zbynek

zbynek commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

@adangel I'm not sure the columns are needed -- for rules where multiple violations may realistically happen on the same line, it would make sense to include the violating token in the message. But having an option to use columns may be useful.

Also an idea that PMD could borrow from Checkstyle is using comment markers to indicate expected violations. For backward compatibility this could be something like

  <test-code>
        <description>Verify location of violation</description>
        <expected-problem-marker>pmd-violation</expected-problem-marker>
        <code><![CDATA[
public class
Foo // pmd-violation: there is a reason for this error 
{}
        ]]></code>
    </test-code>

(since PMD supports very different languages, the marker cannot be always part of // comment, but in principle should be possible everywhere)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants