Skip to content

Commit

Permalink
Merge pull request #11 from joomcode/feature/plugin-tests
Browse files Browse the repository at this point in the history
ParanoidPlugin functional test
  • Loading branch information
dkostyrev committed Jun 29, 2023
2 parents e96fc4b + 2667071 commit f8a2954
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
- name: Publish bootstrap artifacts
run: ./gradlew -p paranoid publishToMavenLocal -Ppablo.shadow.enabled=true ${{ env.PABLO_OPTS }}

- name: Run functional test
run: ./gradlew -p paranoid -Ppablo.shadow.enabled=true functionalTest

- name: Run unit tests
run: ./gradlew check -x lint -Pdevelopment=false -Dorg.gradle.unsafe.configuration-cache=true

Expand Down
51 changes: 37 additions & 14 deletions paranoid/build.gradle
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
buildscript {
apply from: '../versions.gradle'
}

allprojects {
group = PARANOID_GROUP
version = PARANOID_VERSION
allprojects {
group = PARANOID_GROUP
version = PARANOID_VERSION

buildscript {
repositories {
google()
mavenCentral()
}

dependencies {
classpath "com.android.tools.build:gradle:$androidToolsVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "io.michaelrocks.pablo:pablo:$pabloVersion"
}
}

buildscript {
repositories {
google()
mavenCentral()
}

dependencies {
classpath "com.android.tools.build:gradle:$androidToolsVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "io.michaelrocks.pablo:pablo:$pabloVersion"
}
}
}

repositories {
google()
mavenCentral()
apply plugin: 'kotlin'

dependencies {
testImplementation gradleTestKit()
testImplementation "junit:junit:$junitVersion"
}

tasks.register("functionalTest", Test.class) {
description = "Runs the functional tests."
group = "verification"

testClassesDirs = sourceSets.test.output.classesDirs
classpath = sourceSets.test.runtimeClasspath
}

afterEvaluate {
tasks.named("functionalTest") {
subprojects.each {
dependsOn(it.tasks.named("publishToMavenLocal"))
}
}
}
4 changes: 4 additions & 0 deletions paranoid/pablo.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ pablo {
url = 'https://github.com/joomcode/paranoid'
}
}

signing {
enabled.set(providers.gradleProperty("development").map { !it.toBoolean() })
}
}
142 changes: 142 additions & 0 deletions paranoid/src/test/java/com/joom/paranoid/plugin/ParanoidPluginTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.joom.paranoid.plugin

import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.BuildTask
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.intellij.lang.annotations.Language
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File

class ParanoidPluginTest {

@get:Rule
val temporaryFolder = TemporaryFolder()

@Test
fun `agp version with legacy transform`() {
val projectRoot = createProjectDirectory(agpVersion = "7.1.0")

val result = createGradleRunner(projectRoot).build()

val tasks = result.parseDryRunExecution()
Assert.assertTrue(tasks.any { it.path == ":transformClassesWithParanoidForDebug" })
}

@Test
fun `agp version with all classes transform`() {
val projectRoot = createProjectDirectory(agpVersion = "7.2.0")

val result = createGradleRunner(projectRoot).build()

val tasks = result.parseDryRunExecution()
Assert.assertTrue(tasks.any { it.path == ":paranoidTransformClassesDebug" })
}

@Test
fun `actual agp version`() {
val projectRoot = createProjectDirectory(agpVersion = "7.4.2")

val result = createGradleRunner(projectRoot).build()

val tasks = result.parseDryRunExecution()
Assert.assertTrue(tasks.any { it.path == ":paranoidTransformClassesDebug" })
}

private fun createProjectDirectory(agpVersion: String): File {
val projectRoot = temporaryFolder.newFolder()
writeText(createBuildGradle(agpVersion), File(projectRoot, "build.gradle"))
writeText(ANDROID_MANIFEST, File(projectRoot, "src/main/AndroidManifest.xml"))
return projectRoot
}

private fun createGradleRunner(projectDir: File): GradleRunner {
return GradleRunner.create()
.forwardOutput()
.withProjectDir(projectDir)
.withArguments("assembleDebug", "--dry-run", "--stacktrace")
}

private fun BuildResult.parseDryRunExecution(): List<BuildTask> {
val split = output.split('\n')
return split.mapNotNull {
if (it.startsWith(":")) {
val (path, _) = it.split(" ")
TestBuildTask(path = path, outcome = TaskOutcome.SKIPPED)
} else {
null
}
}
}

private fun writeText(content: String, destination: File) {
if (!destination.parentFile.exists() && !destination.parentFile.mkdirs()) {
error("Failed to create parent directory ${destination.parentFile}")
}

destination.writeText(content)
}

@Language("gradle")
private fun createBuildGradle(agpVersion: String, compileSdk: Int = 31, buildToolsVersion: String = "30.0.3"): String {
return """
buildscript {
repositories {
google()
mavenLocal()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:$agpVersion"
classpath "com.joom.paranoid:paranoid-gradle-plugin:+"
}
}
apply plugin: "com.android.application"
apply plugin: "com.joom.paranoid"
repositories {
google()
mavenCentral()
}
android {
compileSdk $compileSdk
buildToolsVersion "$buildToolsVersion"
defaultConfig {
applicationId "com.joom.paranoid.test"
namespace "com.joom.paranoid.test"
versionCode 1
versionName "1"
}
}
""".trimIndent()
}

private companion object {
@Language("xml")
private const val ANDROID_MANIFEST = """
<?xml version="1.0" encoding="utf-8"?>
<manifest />
"""
}

private data class TestBuildTask(
private val path: String,
private val outcome: TaskOutcome,
) : BuildTask {
override fun getPath(): String {
return path
}

override fun getOutcome(): TaskOutcome {
return outcome
}

}
}

0 comments on commit f8a2954

Please sign in to comment.