nebula-plugins / gradle-lint-plugin

A pluggable and configurable linter tool for identifying and reporting on patterns of misuse or deprecations in Gradle scripts.
Apache License 2.0
769 stars 88 forks source link

Test for 'custom' rule #195

Open Lisanderl opened 6 years ago

Lisanderl commented 6 years ago

Hi! I have written simple rule for my project, and it works. But in test it didn't. Probably test runner can't find rule id. Maybe someone have had the same problem ?

It's my test:
........................................................................................................

def "should fall"() {
        given:
        createFiles()
        buildFile << """

        plugins {

        id 'java'
        id 'groovy'
        id 'meta-apps-lint'
        }

        task somwTask {
            while(true){
                println ' '
                break;
            }
        }

        repositories {
                  mavenCentral()
        }   

        dependencies{
            compile 'junit:junit:4.12'
        }

        gradleLint {

        criticalRules = ['wrong-script-rule'] 
            }

        """.trim()

        when:
        def result = gradle('build', '--stacktrace')

        then:
        assert result.task(":build").outcome == SUCCESS

    }

It's my rule: ............................................................................................

String description = 'Finds java code in script';
    @Override 
    void visitBuildscript (MethodCallExpression call) {
        Pattern pattern = Pattern.compile(badScriptRegex);
        Matcher matcher = pattern.matcher(buildFiles.getText());
        if ( matcher.find() ) {
            println 'Bad scrypt detected'
            addBuildLintViolation(message)
        }}}

Also i create src/main/resources/META-INF/lint-rules/wrong-script-rule.properties file . If i use 'default rule' like unused-dependency it works.

chali commented 6 years ago

Hi @Lisanderl, I don't see anything obvious at the first look. I assume that you apply our plugin through meta-apps-lint. I would look around parent class of your test. Here is one of our test which is kind of similar https://github.com/nebula-plugins/gradle-lint-plugin/blob/master/src/test/groovy/com/netflix/nebula/lint/rule/dependency/MinimumDependencyVersionRuleSpec.groovy Here is the parent class which we use https://github.com/nebula-plugins/gradle-lint-plugin/blob/master/src/test/groovy/com/netflix/nebula/lint/TestKitSpecification.groovy My guess is that Gradle which you run in the test doesn't see the project under test on its classpath. Our TestKitSpecification is providing that feature. I believe a similar class is also provided by nebula-test project (https://github.com/nebula-plugins/nebula-test/blob/master/src/main/groovy/nebula/test/IntegrationTestKitSpec.groovy) so you don't have to copy-paste this code. We haven't migrated this project to nebula-test yet.

rpalcolea commented 6 years ago

Complementing @chali comment, have you tried to look at your build/resources/main/META-INF/lint-rules to check that the properties file is available when you execute your test?

Lisanderl commented 5 years ago

Complementing @chali comment, have you tried to look at your build/resources/main/META-INF/lint-rules to check that the properties file is available when you execute your test?

Yeah, it's available, and default rules works if i add them to my src/main/resources/META-INF/lint-rules/wrong-script-rule.properties, like 'includes=unused-dependency,all-dependency'