Checkstyle不同文件的不同规则 [英] Checkstyle different rules for different files

查看:304
本文介绍了Checkstyle不同文件的不同规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中包含项目规则。
我希望我的单元测试方法的名称中带有下划线。
类似于 myMethod_should_call_someClass_someMehod 。当前,我有一个配置,该配置适用于项目中的所有文件。

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.

我的问题是可以以某种方式配置checkstyle,因此,例如,我为所有以 * Test.java 结尾的文件。

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.

目前,我发现的唯一解决方案是提供 SuppressionFilter 并排除所有以 * Test.java 结尾的文件。但是有办法我可以为测试文件提供不同格式的 MethodNameCheck 模块吗?

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?

推荐答案

您必须定义 MethodName 进行两次检查,其中一个实例检查常规方法,并且其他检查测试方法。请注意 id 属性,我们将使用该属性将支票限制在各自的域中:

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>

接下来,对于测试方法,必须取消常规检查,反之亦然。仅当您具有区分两种类的标准时,此方法才有效。我使用了 Maven目录约定,该规则将常规类置于 src / main src / test 下的测试类。这是抑制过滤器文件:

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>

这篇关于Checkstyle不同文件的不同规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆