Skip to content

Commit

Permalink
Basic Gradle plugin test
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Oct 9, 2024
1 parent d57c154 commit 5dd2ebf
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package app.cash.burst.gradle

import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.containsExactlyInAnyOrder
import assertk.assertions.isFalse
import assertk.assertions.isTrue
import java.io.File
import org.gradle.testkit.runner.GradleRunner
Expand All @@ -29,13 +31,34 @@ class BurstGradlePluginTest {
fun happyPath() {
val projectDir = File("src/test/projects/basic")

val taskName = ":lib:compileProductionExecutableKotlinJsZipline"
val taskName = ":lib:jvmTest"
val result = createRunner(projectDir, "clean", taskName).build()
assertThat(SUCCESS_OUTCOMES)
.contains(result.task(taskName)!!.outcome)

val ziplineOut = projectDir.resolve("lib/build/zipline/Production")
assertThat(ziplineOut.resolve("basic-lib.zipline").exists()).isTrue()
val testResults = projectDir.resolve("lib/build/test-results")
val jvmTestXmlFile = testResults.resolve("jvmTest/TEST-CoffeeTest.xml")

val testSuite = readTestSuite(jvmTestXmlFile)

assertThat(testSuite.testCases.map { it.name }).containsExactlyInAnyOrder(
"test[jvm]",
"test_Decaf_Oat[jvm]",
"test_Regular_Milk[jvm]",
"test_Regular_None[jvm]",
"test_Decaf_Milk[jvm]",
"test_Decaf_None[jvm]",
"test_Double_Milk[jvm]",
"test_Double_None[jvm]",
"test_Regular_Oat[jvm]",
"test_Double_Oat[jvm]",
)

val originalTest = testSuite.testCases.single { it.name == "test[jvm]" }
assertThat(originalTest.skipped).isTrue()

val sampleVariant = testSuite.testCases.single { it.name == "test_Decaf_Oat[jvm]" }
assertThat(sampleVariant.skipped).isFalse()
}

private fun createRunner(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2024 Cash App
*
* 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 app.cash.burst.gradle

import java.io.File
import javax.xml.parsers.DocumentBuilderFactory
import org.w3c.dom.Element

fun readTestSuite(xmlFile: File): TestSuite {
val builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
val document = builder.parse(xmlFile)
return document.documentElement.toTestSuite()
}

internal fun Element.toTestSuite(): TestSuite {
val testCases = mutableListOf<TestCase>()
for (i in 0 until childNodes.length) {
val item = childNodes.item(i)
if (item !is Element || item.tagName != "testcase") continue
testCases += item.toTestCase()
}

return TestSuite(
name = getAttribute("name"),
testCases = testCases,
)
}

internal fun Element.toTestCase(): TestCase {
var skipped = false
for (i in 0 until childNodes.length) {
val item = childNodes.item(i)
if (item is Element && item.tagName == "skipped") {
skipped = true
}
}

return TestCase(
name = getAttribute("name"),
skipped = skipped,
)
}

class TestSuite(
val name: String,
val testCases: List<TestCase>,
)

class TestCase(
val name: String,
val skipped: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ kotlin {
commonTest {
dependencies {
implementation("app.cash.burst:burst:${project.property("burstVersion")}")
implementation(kotlin("test"))
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import app.cash.burst.Burst
import kotlin.test.Test

@Burst
class CoffeeTest {
val log = mutableListOf<String>()

@Test
fun test(espresso: Espresso, dairy: Dairy) {
log += "running $espresso $dairy"
}
}

enum class Espresso { Decaf, Regular, Double }

enum class Dairy { None, Milk, Oat }

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class BurstKotlinPluginTest {
package app.cash.burst.testing
import app.cash.burst.Burst
import kotlin.test.Ignore
import kotlin.test.Test
@Burst
Expand All @@ -50,19 +49,6 @@ class BurstKotlinPluginTest {
fun test(espresso: Espresso, dairy: Dairy) {
log += "running ${'$'}espresso ${'$'}dairy"
}
// Generate this
@Test
@Ignore
fun x_test() {
x_test_Decaf_None()
}
// Generate this
@Test
fun x_test_Decaf_None() {
test(Espresso.Decaf, Dairy.None)
}
}
enum class Espresso { Decaf, Regular, Double }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrGetEnumValueImpl
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classFqName
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.types.starProjectedType
import org.jetbrains.kotlin.ir.util.classId
Expand Down Expand Up @@ -87,9 +88,10 @@ internal class BurstRewriter(
createVariant(originalDispatchReceiver, variantArguments)
}

// Side-effect: add `@Ignore`
// TODO: if its' absent!
original.annotations += burstApis.ignoreClassSymbol.asAnnotation()
// Side-effect: drop `@Test` from the original's annotations.
original.annotations = original.annotations.filter {
it.type.classFqName != burstApis.testClassSymbol.starProjectedType.classFqName
}

val result = mutableListOf<IrDeclaration>()
result += createFunctionThatCallsAllVariants(originalDispatchReceiver, variants)
Expand Down

0 comments on commit 5dd2ebf

Please sign in to comment.