Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkstyle different rules for different files

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?

like image 865
dhblah Avatar asked Sep 17 '14 15:09

dhblah


People also ask

What is checkstyle XML file?

Checkstyle obtains a configuration from an XML document whose elements specify the configuration's hierarchy of modules and their properties. You provide a file that contains the configuration document when you invoke Checkstyle at the command line, and when you run a Checkstyle task in ant.

Is checkstyle a Linter?

Checkstyle is one of the most popular linters available.

How do I check my checkstyle violations?

To view to violation report, go to Window -> Show View -> Other, and search for Checkstyle. Options for Violations and Violations Chart should be displayed.

How do I get the configuration document for Checkstyle?

Checkstyle obtains a configuration from an XML document whose elements specify the configuration's hierarchy of modules and their properties. You provide a file that contains the configuration document when you invoke Checkstyle at the command line, and when you run a Checkstyle task in ant.

What are the Checkstyle checks?

The Standard Checkstyle Checks are applicable to general Java coding style and require no external libraries. The standard checks are included in the base distribution. The site navigation menu lets you browse the individual checks by functionality. Checkstyle provides many checks that you can apply to your source code.

How does Checkstyle check if a source file has no stars?

(Modules AvoidStarImport, ConstantName, and EmptyBlock check that a Java source file has no star imports, has valid constant names, and has no empty blocks, respectively.) For each configuration module, Checkstyle loads a class identified by the name attribute of the module.

What to do if Checkstyle Rejects my configuration document's encoding?

If Checkstyle rejects your configuration document's encoding, correct the value of the encoding attribute, or remove the encoding attribute entirely. For a complete example of a configuration XML document, examine files sun_checks.xml and google_checks.xml that checks the coding conventions.


2 Answers

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>
like image 85
barfuin Avatar answered Sep 20 '22 17:09

barfuin


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="SuppressionSingleFilter">
    <metadata name="net.sf.eclipsecs.core.comment" value="Suppress MethodNameMain 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>

(This would be in addition to the two MethodName checks.)

like image 35
PMah Avatar answered Sep 18 '22 17:09

PMah