Skip to content

Commit

Permalink
Fix the task property declaration, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ogolberg authored Feb 25, 2024
1 parent 5a9aeff commit 94093ce
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 1 deletion.
12 changes: 12 additions & 0 deletions coverage-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
plugins {
`kotlin-conventions`
`plugin-publishing-conventions`
jacoco
}

dependencies {
implementation(gradleApi())
implementation(projects.common)

testImplementation(projects.junit5)
testImplementation(libs.junit)
testImplementation(libs.strikt.core)
}

tasks {
test {
systemProperty("javaagent", project.zipTree(project.configurations.getByName("jacocoAgent").asPath).filter {
it.name == "jacocoagent.jar"
}.singleFile.path)
}
}

gradlePlugin {
plugins {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 Toast Inc.
*
* 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 com.toasttab.gradle.testkit

import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import strikt.api.expectThat
import strikt.assertions.isGreaterThan
import java.nio.file.Path
import kotlin.io.path.fileSize
import kotlin.io.path.writeText

class FlushJacocoPluginIntegrationTest {
@TempDir
lateinit var dir: Path

@Test
fun `coverage is flushed`() {
val javaagent = System.getProperty("javaagent")
val file = dir.resolve("build/testkit.exec")

dir.resolve("gradle.properties").writeText(
"org.gradle.jvmargs=-javaagent:$javaagent=destfile=$file"
)

dir.resolve("build.gradle.kts").writeText(
"""
plugins {
java
id("com.toasttab.testkit.coverage")
}
""".trimIndent()
)

GradleRunner.create()
.withProjectDir(dir.toFile())
.withPluginClasspath().withArguments("build").build()

expectThat(file.fileSize()).isGreaterThan(1024)
}
}
12 changes: 12 additions & 0 deletions junit5/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ plugins {
`library-publishing-conventions`
}

tasks {
test {
useJUnitPlatform()

systemProperty("testkit-projects", layout.projectDirectory.file("src/test/projects").asFile.path)

inputs.dir(layout.projectDirectory.file("src/test/projects")).withPropertyName("test-projects").withPathSensitivity(PathSensitivity.RELATIVE)
}
}

dependencies {
implementation(projects.common)
implementation(libs.junit)
implementation(gradleTestKit())
implementation(libs.jacoco.core)

testImplementation(libs.strikt.core)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 Toast Inc.
*
* 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 com.toasttab.gradle.testkit

import org.junit.jupiter.api.Test
import strikt.api.expectThat
import strikt.assertions.contains

@TestKit
class TestKitIntegrationTest {
@Test
fun `basic project`(project: TestProject) {
expectThat(
project.createRunnerWithoutPluginClasspath().withArguments("dependencies").build().output
).contains("compileClasspath")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
java
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "test"
8 changes: 8 additions & 0 deletions testkit-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ plugins {
dependencies {
implementation(projects.common)
implementation(gradleApi())
testImplementation(libs.strikt.core)
}

tasks {
test {
useJUnitPlatform()
systemProperty("test-projects", layout.projectDirectory.dir("src/test/test-projects").asFile.path)
}
}

gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TestkitPlugin @Inject constructor(

doFirst(JacocoOutputCleanupTestTaskAction(fs, destfile))

inputs.file(testProjectDir).withPropertyName("testkit-projects-input").withPathSensitivity(PathSensitivity.RELATIVE)
inputs.dir(testProjectDir).withPropertyName("testkit-projects-input").withPathSensitivity(PathSensitivity.RELATIVE)

// declare an additional jacoco output file so that the JUnit JVM and the TestKit JVM
// do not try to write to the same file
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.toasttab.gradle.testkit

import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import strikt.api.expectThat
import strikt.assertions.isEqualTo
import java.nio.file.Path
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.copyToRecursively
import kotlin.io.path.readText

class TestkitPluginIntegrationTest {
@TempDir
lateinit var dir: Path

@OptIn(ExperimentalPathApi::class)
@Test
fun filtering() {
Path.of(System.getProperty("test-projects")).copyToRecursively(target = dir, followLinks = false, overwrite = false)

val projectDir = dir.resolve("TestkitPluginIntegrationTest/filtering")

GradleRunner.create()
.withProjectDir(projectDir.toFile())
.withPluginClasspath()
.withArguments("test")
.build()

val data = projectDir.resolve("build/test-projects/test-project/foo").readText().trim()

expectThat(data).isEqualTo("hello world!")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
plugins {
java
jacoco
id("com.toasttab.testkit")
}

repositories {
mavenCentral()
}

tasks {
test {
useJUnitPlatform()
}
}

testkitTests {
replaceToken("VALUE", "world!")
}

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "test"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Test {
@org.junit.jupiter.api.Test
void test() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello @VALUE@

0 comments on commit 94093ce

Please sign in to comment.