All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.api.plugins.quality.codenarc.CodeNarcPluginIntegrationTest.groovy Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.api.plugins.quality.codenarc

import org.gradle.api.plugins.quality.CodeNarcPlugin
import org.gradle.integtests.fixtures.ToBeFixedForInstantExecution
import org.gradle.integtests.fixtures.WellBehavedPluginTest
import spock.lang.Unroll

class CodeNarcPluginIntegrationTest extends WellBehavedPluginTest {
    @Override
    String getPluginName() {
        return "codenarc"
    }

    @Override
    String getMainTask() {
        return "check"
    }

    def setup() {
        writeBuildFile()
        writeConfigFile()
    }

    @ToBeFixedForInstantExecution(because = "Task.getProject() during execution")
    def "adds codenarc task for each source set"() {
        given:
        buildFile << '''
            sourceSets {
                other {
                    groovy
                }
            }
            def assertTaskConfiguration(taskName, sourceSet) {
                def task = project.tasks.findByName(taskName)
                assert task instanceof CodeNarc
                task.with {
                    assert description == "Run CodeNarc analysis for ${sourceSet.name} classes"
                    assert source as List == sourceSet.allGroovy  as List
                    assert codenarcClasspath == project.configurations.codenarc
                    assert config.inputFiles.singleFile == project.file("config/codenarc/codenarc.xml")
                    assert configFile == project.file("config/codenarc/codenarc.xml")
                    assert maxPriority1Violations == 0
                    assert maxPriority2Violations == 0
                    assert maxPriority3Violations == 0
                    assert reports.enabled*.name == ["html"]
                    assert reports.html.destination == project.file("build/reports/codenarc/${sourceSet.name}.html")
                    assert ignoreFailures == false
                }
            }
            task assertTaskForEachSourceSet {
                doLast {
                    assertTaskConfiguration('codenarcMain', project.sourceSets.main)
                    assertTaskConfiguration('codenarcTest', project.sourceSets.test)
                    assertTaskConfiguration('codenarcOther', project.sourceSets.other)
                }
            }
        '''.stripIndent()

        expect:
        succeeds 'assertTaskForEachSourceSet'
    }

    @ToBeFixedForInstantExecution
    def "adds codenarc tasks from each source sets to check lifecycle task"() {
        given:
        buildFile << '''
            sourceSets {
                other {
                    groovy
                }
            }
            task codenarcCustom(type: CodeNarc)
        '''.stripIndent()

        when:
        succeeds 'check'

        then:
        executed(":codenarcMain")
        executed(":codenarcTest")
        executed(":codenarcOther")
        notExecuted(":codenarcCustom")
    }

    @ToBeFixedForInstantExecution(because = "Task.getProject() during execution")
    def "can customize per-source-set tasks via extension"() {
        given:
        buildFile << '''
            sourceSets {
                other {
                    groovy
                }
            }
            codenarc {
                configFile = project.file("codenarc-config")
                maxPriority1Violations = 10
                maxPriority2Violations = 50
                maxPriority3Violations = 200
                reportFormat = "xml"
                reportsDir = project.file("codenarc-reports")
                ignoreFailures = true
            }
            def hasCustomizedSettings(taskName, sourceSet) {
                def task = project.tasks.findByName(taskName)
                assert task instanceof CodeNarc
                task.with {
                    assert description == "Run CodeNarc analysis for ${sourceSet.name} classes"
                    assert source as List == sourceSet.allGroovy as List
                    assert codenarcClasspath == project.configurations.codenarc
                    assert config.inputFiles.singleFile == project.file("codenarc-config")
                    assert configFile == project.file("codenarc-config")
                    assert maxPriority1Violations == 10
                    assert maxPriority2Violations == 50
                    assert maxPriority3Violations == 200
                    assert reports.enabled*.name == ["xml"]
                    assert reports.xml.destination == project.file("codenarc-reports/${sourceSet.name}.xml")
                    assert ignoreFailures == true
                }
            }
            task assertHasCustomizedSettings {
                doLast {
                    hasCustomizedSettings('codenarcMain', project.sourceSets.main)
                    hasCustomizedSettings('codenarcTest', project.sourceSets.test)
                    hasCustomizedSettings('codenarcOther', project.sourceSets.other)
                }
            }
        '''.stripIndent()

        expect:
        succeeds 'assertHasCustomizedSettings'
    }

    @ToBeFixedForInstantExecution
    def "can customize which tasks are added to check lifecycle task"() {
        given:
        buildFile << '''
            sourceSets {
                other {
                    groovy
                }
            }
            task codenarcCustom(type: CodeNarc)
            codenarc {
                sourceSets = [project.sourceSets.main]
            }
        '''.stripIndent()

        when:
        succeeds 'check'

        then:
        executed(':codenarcMain')
        notExecuted(':codenarcTest')
        notExecuted(':codenarcOther')
        notExecuted(':codenarcCustom')
    }

    @ToBeFixedForInstantExecution(because = "Task.getProject() during execution")
    def "can use legacy configFile extension property"() {
        given:
        buildFile << '''
            codenarc {
                configFile = project.file("codenarc-config")
            }
            task assertCodeNarcConfiguration {
                doLast {
                    assert project.codenarc.configFile == project.file("codenarc-config") // computed property
                    assert project.tasks.codenarcMain.configFile == project.file("codenarc-config")
                    assert project.tasks.codenarcTest.configFile == project.file("codenarc-config")
                }
            }
        '''.stripIndent()

        expect:
        succeeds 'assertCodeNarcConfiguration'
    }

    @Unroll
    @ToBeFixedForInstantExecution
    def "allows configuring tool dependencies explicitly via #method"(String method, String buildScriptSnippet) {
        expect: //defaults exist and can be inspected
        succeeds("dependencies", "--configuration", "codenarc")
        output.contains "org.codenarc:CodeNarc:${CodeNarcPlugin.DEFAULT_CODENARC_VERSION}"

        when:
        buildFile << buildScriptSnippet

        then:
        succeeds("dependencies", "--configuration", "codenarc")
        output.contains "org.codenarc:CodeNarc:0.17"
        !output.contains("FAILED")

        where:
        method         | buildScriptSnippet
        'dependencies' | "dependencies { codenarc 'org.codenarc:CodeNarc:0.17' }"
        'toolVersion'  | "codenarc { toolVersion '0.17' } "
    }

    private void writeBuildFile() {
        file("build.gradle") << """
            apply plugin: "groovy"
            apply plugin: "codenarc"

            ${mavenCentralRepository()}

            dependencies {
                implementation localGroovy()
            }
        """.stripIndent()
    }

    private void writeConfigFile() {
        file("config/codenarc/codenarc.xml") << """
            
                
            
        """.stripIndent()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy