-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from joomcode/feature/plugin-tests
ParanoidPlugin functional test
- Loading branch information
Showing
4 changed files
with
186 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
paranoid/src/test/java/com/joom/paranoid/plugin/ParanoidPluginTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} | ||
} |