Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/local-variable-loops(#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
aktsay6 authored Dec 14, 2020
2 parents 670d7b6 + 4edbd78 commit 4cabf6d
Show file tree
Hide file tree
Showing 47 changed files with 753 additions and 70 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ name: Build and test
on:
pull_request

env:
GRADLE_OPTS: -Dorg.gradle.daemon=false

jobs:
test:
name: Unit Test
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/functional_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ master ]

env:
DIKTAT_VERSION: 0.1.6
DIKTAT_VERSION: 0.1.7
KTLINT_VERSION: 0.39.0
GRADLE_OPTS: -Dorg.gradle.daemon=false # to speed up gradle run

Expand Down
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ jobs:
mvn -B versions:set -DnextSnapshot=true -DprocessAllModules=true versions:commit
mvn versions:set-property -Dproperty=diktat-check.version -DnewVersion=${{ env.RELEASE_VERSION }}
sed -i "s/$PREVIOUS_VERSION/$RELEASE_VERSION/g" README.md || echo "File README.md hasn't been updated!"
sed -i "s/$PREVIOUS_VERSION/$RELEASE_VERSION/g" .github/workflows/functional_tests.yml || echo "File functional_tests.yml hasn't been updated!"
for file in examples/maven/pom.xml examples/gradle-groovy-dsl/build.gradle examples/gradle-kotlin-dsl/build.gradle.kts
do
sed -i "s/$PREVIOUS_VERSION/$RELEASE_VERSION/g" $file || echo "File $file hasn't been updated!"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ Main features of diktat are the following:
# another option is "brew install ktlint"
```

2. Load diKTat manually: [here](https://github.com/cqfn/diKTat/releases/download/v0.1.6/diktat.jar)
2. Load diKTat manually: [here](https://github.com/cqfn/diKTat/releases/download/v0.1.7/diktat.jar)

**OR** use curl:
```bash
$ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v0.1.6/diktat-0.1.6.jar
$ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v0.1.7/diktat-0.1.7.jar
```

3. Finally, run KTlint (with diKTat injected) to check your `*.kt` files in `dir/your/dir`:
Expand Down Expand Up @@ -110,7 +110,7 @@ This plugin is available since version 0.1.5. You can see how the plugin is conf
Add this plugin to your `build.gradle.kts`:
```kotlin
plugins {
id("org.cqfn.diktat.diktat-gradle-plugin") version "0.1.6"
id("org.cqfn.diktat.diktat-gradle-plugin") version "0.1.7"
}
```

Expand All @@ -121,7 +121,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath("org.cqfn.diktat:diktat-gradle-plugin:0.1.6")
classpath("org.cqfn.diktat:diktat-gradle-plugin:0.1.7")
}
}
Expand Down
3 changes: 3 additions & 0 deletions diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,7 @@
enabled: true
# Checks if there is class/object that can be replace with extension function
- name: AVOID_USING_UTILITY_CLASS
enabled: true
# If there is stateless class it is preferred to use object
- name: OBJECT_IS_PREFERRED
enabled: true
2 changes: 1 addition & 1 deletion diktat-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-parent</artifactId>
<version>0.1.7-SNAPSHOT</version>
<version>0.1.8-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down
47 changes: 38 additions & 9 deletions diktat-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.getCurrentOperatingSystem

plugins {
`java-gradle-plugin`
kotlin("jvm") version "1.4.20"
kotlin("jvm") version "1.4.21"
jacoco
id("pl.droidsonroids.jacoco.testkit") version "1.0.7"
}

repositories {
Expand All @@ -17,7 +19,7 @@ repositories {

// default value is needed for correct gradle loading in IDEA; actual value from maven is used during build
val ktlintVersion = project.properties.getOrDefault("ktlintVersion", "0.39.0") as String
val diktatVersion = project.version.takeIf { it.toString() != Project.DEFAULT_VERSION } ?: "0.1.6"
val diktatVersion = project.version.takeIf { it.toString() != Project.DEFAULT_VERSION } ?: "0.1.7"
val junitVersion = project.properties.getOrDefault("junitVersion", "5.7.0") as String
dependencies {
implementation(kotlin("gradle-plugin-api"))
Expand All @@ -39,7 +41,8 @@ val generateVersionsFile by tasks.registering {

doFirst {
versionsFile.parentFile.mkdirs()
versionsFile.writeText("""
versionsFile.writeText(
"""
package generated
internal const val DIKTAT_VERSION = "$diktatVersion"
Expand Down Expand Up @@ -75,19 +78,45 @@ java {
withSourcesJar()
}

// === testing & code coverage, consistent with maven
// === testing & code coverage, jacoco is run independent from maven
val functionalTestTask by tasks.register<Test>("functionalTest")
val jacocoMergeTask by tasks.register<JacocoMerge>("jacocoMerge")
tasks.withType<Test> {
useJUnitPlatform()
extensions.configure(JacocoTaskExtension::class) {
setDestinationFile(file("target/jacoco.exec"))
}
}

// === integration testing
// fixme: should probably use KotlinSourceSet instead
val functionalTest = sourceSets.create("functionalTest") {
compileClasspath += sourceSets.main.get().output + configurations.testRuntimeClasspath
runtimeClasspath += output + compileClasspath
}
tasks.getByName<Test>("functionalTest") {
dependsOn("test")
testClassesDirs = functionalTest.output.classesDirs
classpath = functionalTest.runtimeClasspath
doLast {
if (getCurrentOperatingSystem().isWindows) {
// workaround for https://github.com/koral--/jacoco-gradle-testkit-plugin/issues/9
logger.lifecycle("Sleeping for 5 sec after functionalTest to avoid error with file locking")
Thread.sleep(5_000)
}
}
finalizedBy(jacocoMergeTask)
}
tasks.check { dependsOn(tasks.jacocoTestReport) }
jacocoTestKit {
applyTo("functionalTestRuntimeOnly", tasks.named("functionalTest"))
}
tasks.getByName("jacocoMerge", JacocoMerge::class) {
dependsOn(functionalTestTask)
executionData(tasks.test, functionalTestTask)
}
tasks.jacocoTestReport {
dependsOn(tasks.test)
dependsOn(jacocoMergeTask)
executionData("$buildDir/jacoco/jacocoMerge.exec")
reports {
// xml report is used by codecov
xml.isEnabled = true
xml.destination = file("target/site/jacoco/jacoco.xml")
}
}
2 changes: 1 addition & 1 deletion diktat-gradle-plugin/gradle-plugin-marker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>diktat-gradle-plugin</artifactId>
<groupId>org.cqfn.diktat</groupId>
<version>0.1.7-SNAPSHOT</version>
<version>0.1.8-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Empty file modified diktat-gradle-plugin/gradlew.bat
100644 → 100755
Empty file.
23 changes: 20 additions & 3 deletions diktat-gradle-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>diktat-parent</artifactId>
<groupId>org.cqfn.diktat</groupId>
<version>0.1.7-SNAPSHOT</version>
<version>0.1.8-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -18,6 +18,9 @@
<gradle.task>build</gradle.task>
<gradle.builddir>build</gradle.builddir>
<skip.gradle.build>false</skip.gradle.build>
<skip.gradle.test>false</skip.gradle.test>
<!-- This property is set to excluded task in the specific build profile -->
<gradle.exclude.check></gradle.exclude.check>
</properties>

<dependencies>
Expand Down Expand Up @@ -69,15 +72,15 @@
<executable>${gradle.executable}</executable>
<arguments>
<argument>clean</argument>
<argument>jacocoTestReport</argument>
<argument>test</argument>
<argument>-Pgroup=${project.groupId}</argument>
<argument>-Pversion=${project.version}</argument>
<argument>-Pdescription=${project.description}</argument>
<argument>-PktlintVersion=${ktlint.version}</argument>
<argument>-PjunitVersion=${junit.version}</argument>
<argument>-S</argument>
</arguments>
<skip>${skip.gradle.build}</skip>
<skip>${skip.gradle.test}</skip>
</configuration>
<goals>
<goal>exec</goal>
Expand All @@ -96,6 +99,7 @@
<argument>-PktlintVersion=${ktlint.version}</argument>
<argument>-PjunitVersion=${junit.version}</argument>
<argument>-S</argument>
<argument>${gradle.exclude.check}</argument>
</arguments>
<skip>${skip.gradle.build}</skip>
</configuration>
Expand Down Expand Up @@ -185,5 +189,18 @@
<gradle.executable>${project.basedir}/gradlew.bat</gradle.executable>
</properties>
</profile>
<profile>
<id>skip-gradle-tests</id>
<activation>
<property>
<name>skipTests</name>
<value>true</value>
</property>
</activation>
<properties>
<gradle.exclude.check>-xcheck</gradle.exclude.check>
<skip.gradle.test>true</skip.gradle.test>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.cqfn.diktat.plugin.gradle

import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_CHECK_TASK
import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.File

class DiktatGradlePluginFunctionalTest {
private val testProjectDir = TemporaryFolder()
private lateinit var buildFile: File

@BeforeEach
fun setUp() {
testProjectDir.create()
File("../examples/gradle-kotlin-dsl").copyRecursively(testProjectDir.root)
File(testProjectDir.root, "build.gradle.kts").delete()
buildFile = testProjectDir.newFile("build.gradle.kts").apply {
writeText(
"""
plugins {
id("org.cqfn.diktat.diktat-gradle-plugin")
}
repositories {
mavenLocal()
mavenCentral()
}
""".trimIndent()
)
}
}

@AfterEach
fun tearDown() {
testProjectDir.delete()
}

@Test
fun `should execute diktatCheck on default values`() {
val result = runDiktat()

val diktatCheckBuildResult = result.task(":$DIKTAT_CHECK_TASK")
requireNotNull(diktatCheckBuildResult)
Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome)
Assertions.assertTrue(
result.output.contains("[HEADER_MISSING_OR_WRONG_COPYRIGHT]")
)
}

@Test
fun `should execute diktatCheck with explicit inputs`() {
buildFile.appendText(
"""${System.lineSeparator()}
diktat {
inputs = files("src/**/*.kt")
}
""".trimIndent()
)
val result = runDiktat()

val diktatCheckBuildResult = result.task(":$DIKTAT_CHECK_TASK")
requireNotNull(diktatCheckBuildResult)
Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome)
Assertions.assertTrue(
result.output.contains("[HEADER_MISSING_OR_WRONG_COPYRIGHT]")
)
}

private fun runDiktat() = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments(DIKTAT_CHECK_TASK)
.withPluginClasspath()
.withJaCoCo()
.forwardOutput()
.runCatching {
buildAndFail()
}
.also {
require(it.isSuccess) { "Running gradle returned exception ${it.exceptionOrNull()}" }
}
.getOrNull()!!

/**
* This is support for jacoco reports in tests run with gradle TestKit
*/
private fun GradleRunner.withJaCoCo() = apply {
javaClass.classLoader
.getResourceAsStream("testkit-gradle.properties")
.also { it ?: println("properties file for testkit is not available, check build configuration") }
?.use { propertiesFileStream ->
File(projectDir, "gradle.properties").outputStream().use {
propertiesFileStream.copyTo(it)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ open class DiktatJavaExecTaskBase @Inject constructor(
}
.files
.forEach {
add("\"${it.path}\"")
add(it.path)
}
diktatExtension.excludes?.files?.forEach {
add("\"!${it.path}\"")
add(it.path)
}
}
logger.debug("Setting JavaExec args to $args")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DiktatJavaExecTaskTest {
@Test
fun `check command line for various inputs`() {
assertCommandLineEquals(
listOf(null, "\"${combinePathParts("src", "**", "*.kt")}\""),
listOf(null, combinePathParts("src", "**", "*.kt")),
DiktatExtension().apply {
inputs = project.files("src/**/*.kt")
}
Expand All @@ -29,7 +29,7 @@ class DiktatJavaExecTaskTest {
@Test
fun `check command line in debug mode`() {
assertCommandLineEquals(
listOf(null, "--debug", "\"${combinePathParts("src", "**", "*.kt")}\""),
listOf(null, "--debug", combinePathParts("src", "**", "*.kt")),
DiktatExtension().apply {
inputs = project.files("src/**/*.kt")
debug = true
Expand All @@ -40,8 +40,8 @@ class DiktatJavaExecTaskTest {
@Test
fun `check command line with excludes`() {
assertCommandLineEquals(
listOf(null, "\"${combinePathParts("src", "**", "*.kt")}\"",
"\"!${combinePathParts("src", "main", "kotlin", "generated")}\""
listOf(null, combinePathParts("src", "**", "*.kt"),
combinePathParts("src", "main", "kotlin", "generated")
),
DiktatExtension().apply {
inputs = project.files("src/**/*.kt")
Expand Down Expand Up @@ -70,6 +70,6 @@ class DiktatJavaExecTaskTest {
Assertions.assertIterableEquals(expected, task.commandLine)
}

private fun combinePathParts(vararg parts: String) = project.projectDir.absolutePath +
private fun combinePathParts(vararg parts: String) = project.rootDir.absolutePath +
parts.joinToString(File.separator, prefix = File.separator)
}
Loading

0 comments on commit 4cabf6d

Please sign in to comment.