Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First attempt to add Kotlin support for rule #9

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,4 @@ interface BuildEnforcerExtension extends EnforcerExtension {
* Configure rules for all matching projects.
*/
void projects(List<String> projectPaths, Action<? extends EnforcerRuleConfiguration> configurer)

interface EnforcerRuleConfiguration {
/**
* Define a rule.
*/
public <R extends EnforcerRule> void rule(Class<R> ruleType)

/**
* Define and con figure a rule.
*/
public <R extends EnforcerRule> void rule(Class<R> ruleType, Action<R> configurer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.gradle.api.provider.Property
* @since 0.1.0
*/
@CompileStatic
interface EnforcerExtension {
interface EnforcerExtension extends EnforcerRuleConfiguration {
/**
* Whether enforcer behavior is enabled or not. Defaults to {@code true}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020 The author and/or original 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
*
* https://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.kordamp.gradle.plugin.enforcer.api

import org.gradle.api.Action

import groovy.transform.CompileStatic


@CompileStatic
interface EnforcerRuleConfiguration {
/**
* Define a rule.
*/
public <R extends EnforcerRule> void rule(Class<R> ruleType)

/**
* Define and configure a rule.
*/
public <R extends EnforcerRule> void rule(Class<R> ruleType, Action<R> configurer)
}
3 changes: 2 additions & 1 deletion plugins/enforcer-gradle-plugin/enforcer-gradle-plugin.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
plugins{
id 'org.kordamp.gradle.integration-test'
id 'org.jetbrains.kotlin.jvm' version '1.4.10'
}

dependencies {
Expand Down Expand Up @@ -73,6 +74,6 @@ integrationTest {
project(':enforcer-api').jar,
project(':enforcer-rules').jar,
jar
].collect { "'${it.destinationDirectory.get().asFile.absolutePath}'" }
].collect { "'${it.destinationDirectory.get().asFile.absolutePath.replace('\\', '/')}'" }
.join(',')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020 The author and/or original 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
*
* https://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.kordamp.gradle.plugin.enforcer

import static org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE

import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.junit.Rule
import org.junit.rules.TemporaryFolder

import spock.lang.IgnoreRest
import spock.lang.Specification

class BuildEnforcerPluginKotlinIntegrationTest extends Specification {
@Rule
TemporaryFolder testProjectDir = new TemporaryFolder()

File buildFile
File settingsFile

def setup() {
buildFile = testProjectDir.newFile('build.gradle.kts')
buildFile << """
plugins {
id ("base")
}
"""

settingsFile = testProjectDir.newFile('settings.gradle.kts')
settingsFile << """
import org.kordamp.gradle.plugin.enforcer.rule
import org.kordamp.gradle.plugin.enforcer.api.BuildEnforcerExtension

buildscript {
repositories {
gradlePluginPortal()
flatDir { dirs (${System.getProperty('jars.dir').replace("'", '"')}) }
}
dependencies {
classpath ("org.kordamp.gradle:enforcer-api:${System.getProperty('project.version')}")
classpath ("org.kordamp.gradle:enforcer-rules:${System.getProperty('project.version')}")
classpath ("org.kordamp.gradle:enforcer-gradle-plugin:${System.getProperty('project.version')}")
classpath ("org.kordamp.gradle:enforcer-gradle-plugin-tests:${System.getProperty('project.version')}")
classpath ("org.apache.commons:commons-lang3:${System.getProperty('commonsLang3Version')}")
classpath ("commons-codec:commons-codec:${System.getProperty('commonsCodecVersion')}")
classpath ("org.apache.maven:maven-artifact:${System.getProperty('mavenVersion')}")
classpath ("kr.motd.maven:os-maven-plugin:${System.getProperty('osMavenPluginVersion')}")
}
}
apply(plugin= "org.kordamp.gradle.enforcer")
"""
}

def "Can disable all enforcer rules"() {
given:
settingsFile << """
configure<BuildEnforcerExtension> {
enabled.set(false)
rule<enforcer.rules.AlwaysFail>()
// allprojects {
// rule<enforcer.rules.AlwaysFail>()
// }
}
"""

when:
BuildResult result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments('clean', '--stacktrace')
.withPluginClasspath()
.build()

then:
result.task(':clean').outcome == UP_TO_DATE
}

def "Can disable a single enforcer rule"() {
given:
settingsFile << """
configure<BuildEnforcerExtension> {
rule<enforcer.rules.AlwaysFail> {
enabled.set(false)
}
}
"""

when:
BuildResult result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments('clean', '--stacktrace')
.withPluginClasspath()
.build()

then:
result.task(':clean').outcome == UP_TO_DATE
}

def "Can fail a build"() {
given:
settingsFile << """
configure<BuildEnforcerExtension> {
rule<enforcer.rules.AlwaysFail> ()
}
"""

when:
BuildResult result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments('clean', '--stacktrace')
.withPluginClasspath()
.buildAndFail()

then:
result.output.contains("[BEFORE_BUILD] An Enforcer rule has failed")
result.output.contains("Enforcer rule 'enforcer.rules.AlwaysFail' was triggered.")
}

def "Can fail a build fast"() {
given:
settingsFile << """
configure<BuildEnforcerExtension> {
failFast.set(true)
rule<enforcer.rules.AlwaysFail>()
rule<org.kordamp.gradle.plugin.enforcer.Fail>()
}
"""

when:
BuildResult result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments('clean', '--stacktrace')
.withPluginClasspath()
.buildAndFail()

then:
result.output.contains("[BEFORE_BUILD] An Enforcer rule has failed")
result.output.contains("Enforcer rule 'enforcer.rules.AlwaysFail' was triggered.")
}

def "Can fail multiple rules in the same phase"() {
given:
settingsFile << """
configure<BuildEnforcerExtension> {
failFast.set(false)
rule<enforcer.rules.AlwaysFail>()
rule<org.kordamp.gradle.plugin.enforcer.Fail>()
}
"""

when:
BuildResult result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments('clean', '--stacktrace')
.withPluginClasspath()
.buildAndFail()

then:
result.output.contains('[BEFORE_BUILD] 2 Enforcer rules have failed')
result.output.contains("Enforcer rule 'enforcer.rules.AlwaysFail' was triggered.")
result.output.contains("Enforcer rule 'org.kordamp.gradle.plugin.enforcer.Fail' was triggered.")
}
}
Loading