Checkstyle different rules for different files
Asked Answered
G

2

17

I have one file which contains rules for the project. I want my unit tests methods to be allowed to have underscore in their names. Like myMethod_should_call_someClass_someMehod. Currently I have one configuration, which is applied to all files in the project.

My question is it possible to somehow configure checkstyle, so, for example I specify specific rules for all files that are ending with *Test.java.

Currently the only solution I found is to provide SuppressionFilter and exclude all files ending with *Test.java. But is there a way I could provide a different MethodNameCheck module with different format for test files?

Gratitude answered 17/9, 2014 at 15:19 Comment(0)
S
32

You must define the MethodName check twice, with one instance checking the regular methods, and the other checking the test methods. Note the id property, which we will use to restrict the checks to their respective domains:

<module name="TreeWalker">
    ...
    <module name="MethodName">
        <property name="id" value="MethodNameRegular"/>
        <property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
    </module>
    <module name="MethodName">
        <property name="id" value="MethodNameTest"/>
        <property name="format" value="^[a-z][a-zA-Z0-9_]*$"/>
    </module>
    ...
</module>

Next, the regular check must be suppressed for test methods and vice versa. This works only if you have a criterion by which to distinguish between the two kinds of classes. I use the Maven directory convention, which puts regular classes under src/main and test classes under src/test. Here is the suppression filter file:

<!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN"
    "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
    <suppress files="[\\/]src[\\/]test[\\/].*" id="MethodNameRegular" />
    <suppress files="[\\/]src[\\/]main[\\/].*" id="MethodNameTest" />
</suppressions>
Scaly answered 17/9, 2014 at 18:39 Comment(2)
I only want to add that in order for suppression.xml file to work in maven and intellij idea, I had to add suppressionsLocation to maven plugin configuration, set file property in module SuppressionFilter to value ${checkstyle.suppressions.file} in checkstyle config and then in intellij idea set checkstyle.suppressions.file property to suppressions.xml in checkstyle config by clicking "pen" button on checkstyle config file on plugin configuration screen. Another option is to provide relative path to suppressions.xml in checkstyle config. Then no additional configuration in intellij idea neededGratitude
I would suggest using [/\\] for the wild-carding instead of [//\] to ensure cross-system compatibility. This is the pattern the docs follow.Looksee
P
5

Building on barfuin's answer, I preferred not to have (yet) another XML file floating around. However, it is possible to configure suppressions directly in the CheckStyle XML config file:

    <module name="TreeWalker">
        ...
        method name checks here
        ...
    </module>

    <module name="SuppressionSingleFilter">
        <metadata name="net.sf.eclipsecs.core.comment" value="Suppress MethodNameRegular check on unit tests"/>
        <property name="files" value=".*[\\/]src[\\/]test[\\/]"/>
        <property name="id" value="MethodNameRegular"/>
    </module>
    <module name="SuppressionSingleFilter">
        <metadata name="net.sf.eclipsecs.core.comment" value="Suppress MethodNameTest check except on unit tests"/>
        <property name="files" value=".*[\\/]src[\\/](?!test[\\/])"/>
        <property name="id" value="MethodNameTest"/>
    </module>

(The two MethodName checks go in the TreeWalker module at the top.)

Perugia answered 6/1, 2021 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.