I'd like to write a custom CodeNarc rule that validates some aspects of Grails integration test classes. e.g. Spock integration tests should extend IntegrationSpec; not Specification.
But to do this, I need to filter on integration tests while visiting the classes in the rule, and I don't know how to do that.
One idea was to look at the source file path to see if it's in test/integration
, but I don't know if it's possible to get the source path of a file.? Any other ideas?
You can definitely limit rule application based on path (as well as class name). Take a look at the configuring rules CodeNarc page.
In particular, you're probably looking for the applyToClassNames
and doNotApplyToClassNames
properties or the applyToFileNames
and doNotApplyToFileNames
properties, which should be available on any of the built-in Rules and any custom rules that extend the provided AbstractRule
class.