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

Created tests for diktat-gradle-plugin #561

Merged
merged 6 commits into from
Nov 23, 2020
Merged
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
34 changes: 29 additions & 5 deletions diktat-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
`java-gradle-plugin`
kotlin("jvm") version "1.4.10"
jacoco
}

repositories {
Expand All @@ -14,20 +15,24 @@ repositories {
jcenter()
}

val ktlintVersion: String by project
val diktatVersion = project.version
val ktlintVersion = project.properties.getOrDefault("ktlintVersion", "0.39.0") as String
val diktatVersion = project.version.takeIf { it.toString() != Project.DEFAULT_VERSION } ?: "0.1.6-SNAPSHOT"
val junitVersion = project.properties.getOrDefault("junitVersion", "5.7.0") as String
dependencies {
implementation(kotlin("gradle-plugin-api"))

implementation("com.pinterest.ktlint:ktlint-core:$ktlintVersion") {
exclude("com.pinterest.ktlint", "ktlint-ruleset-standard")
}
implementation("com.pinterest.ktlint:ktlint-reporter-plain:$ktlintVersion")
implementation("org.cqfn.diktat:diktat-rules:$version")
implementation("org.cqfn.diktat:diktat-rules:$diktatVersion")

testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
}

val generateVersionsFile by tasks.registering {
val versionsFile = File("$buildDir/generated/src/main/generated/Versions.kt")
val versionsFile = File("$buildDir/generated/src/generated/Versions.kt")

outputs.file(versionsFile)

Expand All @@ -43,14 +48,16 @@ val generateVersionsFile by tasks.registering {
)
}
}
sourceSets.main.get().java.srcDir("$buildDir/generated/src/main")
sourceSets.main.get().java.srcDir("$buildDir/generated/src")

tasks.withType<KotlinCompile> {
kotlinOptions {
// fixme: kotlin 1.3 is required for gradle <6.8
languageVersion = "1.3"
apiVersion = "1.3"
jvmTarget = "1.8"
}

dependsOn.add(generateVersionsFile)
}

Expand All @@ -66,3 +73,20 @@ gradlePlugin {
java {
withSourcesJar()
}

// === testing & code coverage, consistent with maven
tasks.withType<Test> {
useJUnitPlatform()
extensions.configure(JacocoTaskExtension::class) {
setDestinationFile(file("target/jacoco.exec"))
}
}

tasks.jacocoTestReport {
dependsOn(tasks.test)
reports {
// xml report is used by codecov
xml.isEnabled = true
xml.destination = file("target/site/jacoco/jacoco.xml")
}
}
23 changes: 22 additions & 1 deletion diktat-gradle-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,39 @@
<artifactId>exec-maven-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<id>gradle-test</id>
<phase>test</phase>
<configuration>
<executable>${gradle.executable}</executable>
<arguments>
<argument>clean</argument>
<argument>jacocoTestReport</argument>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need test report here? For test coverage?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this tasks will execute test and then generate jacoco report.

<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>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
<execution>
<id>gradle</id>
<phase>prepare-package</phase>
<configuration>
<executable>${gradle.executable}</executable>
<arguments>
<argument>clean</argument>
<argument>${gradle.task}</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>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.cqfn.diktat.plugin.gradle

import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

class DiktatGradlePluginTest {
private val projectBuilder = ProjectBuilder.builder()
private lateinit var project: Project

@BeforeEach
fun setUp() {
project = projectBuilder.build()
project.pluginManager.apply(DiktatGradlePlugin::class.java)
}

@Test
fun `check that tasks are registered`() {
Assertions.assertTrue(project.tasks.findByName("diktatCheck") != null)
Assertions.assertTrue(project.tasks.findByName("diktatFix") != null)
}

@Test
fun `check default extension properties`() {
val diktatExtension = project.extensions.getByName("diktat") as DiktatExtension
Assertions.assertFalse(diktatExtension.debug)
Assertions.assertIterableEquals(project.fileTree("src").files, diktatExtension.inputs.files)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.cqfn.diktat.plugin.gradle

import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.File

class DiktatJavaExecTaskTest {
private val projectBuilder = ProjectBuilder.builder()
private lateinit var project: Project

@BeforeEach
fun setUp() {
project = projectBuilder.build()
}

@Test
fun `check command line for various inputs`() {
val pwd = project.file(".")
assertCommandLineEquals(
listOf(null, "$pwd" + listOf("src", "**", "*.kt").joinToString(File.separator, prefix = File.separator)),
DiktatExtension().apply {
inputs = project.files("src/**/*.kt")
}
)
}

@Test
fun `check command line in debug mode`() {
val pwd = project.file(".")
assertCommandLineEquals(
listOf(null, "--debug", "$pwd${listOf("src", "**", "*.kt").joinToString(File.separator, prefix = File.separator)}"),
DiktatExtension().apply {
inputs = project.files("src/**/*.kt")
debug = true
}
)
}

private fun registerDiktatTask(extension: DiktatExtension) = project.tasks.register(
"test", DiktatJavaExecTaskBase::class.java,
"6.7", extension, project.configurations.create("diktat")
)

private fun assertCommandLineEquals(expected: List<String?>, extension: DiktatExtension) {
val task = registerDiktatTask(extension).get()
Assertions.assertIterableEquals(expected, task.commandLine)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.cqfn.diktat.plugin.gradle

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class UtilsTest {
@Test
fun `test gradle version`() {
Assertions.assertEquals(
GradleVersion.fromString("6.6.1"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to test gradle version. It can be changed in any moment. No?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm testing my GradleVersion class, which is used to determine which type of task is supported by current gradle.

GradleVersion(6, 6, 1, null)
)

Assertions.assertEquals(
GradleVersion.fromString("6.7"),
GradleVersion(6, 7, 0, null)
)

Assertions.assertEquals(
GradleVersion.fromString("6.7-rc-5"),
GradleVersion(6, 7, 0, "rc-5")
)
}
}