Checkstyle different rules for different files

大兔子大兔子 提交于 2019-11-27 06:01:13

问题


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?


回答1:


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="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>

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>


来源:https://stackoverflow.com/questions/25894431/checkstyle-different-rules-for-different-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!