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

org.gradle.api.plugins.quality.checkstyle.CheckstylePluginMultiProjectTest.groovy Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2011 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.checkstyle

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.ToBeFixedForInstantExecution
import org.gradle.test.fixtures.file.TestFile

import static org.gradle.util.TextUtil.getPlatformLineSeparator

class CheckstylePluginMultiProjectTest extends AbstractIntegrationSpec {

    @ToBeFixedForInstantExecution
    def "configures checkstyle extension to read config from root project in a single project build"() {
        given:
        buildFile << javaProjectUsingCheckstyle()
        file('src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
        file('config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()

        expect:
        succeeds('checkstyleMain')
        checkStyleReportFile(testDirectory).text.contains('Dummy.java')
    }

    @ToBeFixedForInstantExecution
    def "fails when root project does contain config in default location"() {
        given:
        settingsFile << "include 'child'"
        file('child/build.gradle') << javaProjectUsingCheckstyle()
        file('child/src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
        file('child/config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()

        expect:
        fails(':child:checkstyleMain')
        checkStyleReportFile(file('child')).assertDoesNotExist()
    }

    @ToBeFixedForInstantExecution
    def "configures checkstyle extension to read config from root project in a flat multi-project build"() {
        given:
        settingsFile << "include 'child:grand'"
        file('child/grand/build.gradle') << javaProjectUsingCheckstyle()
        file('child/grand/src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
        file('config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()

        expect:
        succeeds(':child:grand:checkstyleMain')
        checkStyleReportFile(file('child/grand')).text.contains('Dummy.java')
    }

    @ToBeFixedForInstantExecution
    def "configures checkstyle extension to read config from root project in a deeply nested multi-project build"() {
        given:
        settingsFile << "include 'a:b:c'"
        file('a/b/c/build.gradle') << javaProjectUsingCheckstyle()
        file('a/b/c/src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
        file('config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()

        expect:
        succeeds(':a:b:c:checkstyleMain')
        checkStyleReportFile(file('a/b/c')).text.contains('Dummy.java')
    }

    @ToBeFixedForInstantExecution
    def "configures checkstyle extension to read config from root project in a multi-project build even if sub project config is available"() {
        given:
        settingsFile << "include 'child:grand'"
        file('child/grand/build.gradle') << javaProjectUsingCheckstyle()
        file('child/grand/src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
        file('child/grand/config/checkstyle/checkstyle.xml') << invalidCheckStyleConfig()
        file('config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()

        expect:
        succeeds(':child:grand:checkstyleMain')
        checkStyleReportFile(file('child/grand')).text.contains('Dummy.java')
    }

    @ToBeFixedForInstantExecution
    def "explicitly configures checkstyle extension to point to config directory"() {
        given:
        settingsFile << "include 'child'"
        file('child/build.gradle') << javaProjectUsingCheckstyle()
        file('child/build.gradle') << """
            checkstyle {
                configDirectory = file('config/checkstyle')
            }
        """
        file('child/src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
        file('child/config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()

        expect:
        succeeds(':child:checkstyleMain')
        checkStyleReportFile(file('child')).text.contains('Dummy.java')
    }

    static String simpleCheckStyleConfig() {
        """


    

        """
    }

    static String invalidCheckStyleConfig() {
        'INVALID AND SHOULD NEVER BE READ'
    }

    static TestFile checkStyleReportFile(File projectDir) {
        new TestFile(projectDir, 'build/reports/checkstyle/main.html')
    }

    static String javaProjectUsingCheckstyle() {
        """
            apply plugin: 'java'
            apply plugin: 'checkstyle'

            ${mavenCentralRepository()}
        """
    }

    static String javaClassWithNewLineAtEnd() {
        "public class Dummy {}${getPlatformLineSeparator()}"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy