Skip to content

Commit

Permalink
Change AGP and ddmlib to compileOnly instead of implementation (#96)
Browse files Browse the repository at this point in the history
* Convert PluginTestHelper to Kotlin and move to test source set

* Change AGP and ddmlib to compileOnly instead of implementation

This is to avoid pulling a higher version when OkReplay plugin is applied.

* Remove no longer used function
  • Loading branch information
technoir42 authored and felipecsl committed Sep 6, 2019
1 parent da2789c commit 1e6163f
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 127 deletions.
12 changes: 5 additions & 7 deletions okreplay-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'groovy'
apply plugin: 'java-gradle-plugin'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

sourceSets.main.groovy.srcDirs = ["src/main/groovy"]

dependencies {
compileOnly gradleApi()
implementation localGroovy()
implementation dep.androidPlugin
compileOnly dep.androidPlugin
compileOnly dep.ddmlib
implementation dep.kotlinStdLib
implementation dep.ddmlib
testImplementation gradleTestKit()
testImplementation dep.androidPlugin
testImplementation dep.ddmlib
testImplementation dep.junit
testImplementation dep.truth
testImplementation dep.mockito
Expand All @@ -24,4 +22,4 @@ test {
testLogging.showStandardStreams = isCi
}

apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
apply from: rootProject.file('gradle/gradle-mvn-push.gradle')

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package okreplay

import com.android.build.gradle.*
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.internal.scope.GlobalScope
import com.android.build.gradle.internal.variant.BaseVariantData
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
Expand Down Expand Up @@ -77,13 +75,6 @@ class OkReplayPlugin
}
}

private fun BaseVariant.globalScope(): GlobalScope {
val getVariantData = this.javaClass.getDeclaredMethod("getVariantData")
getVariantData.isAccessible = true
val variantData = getVariantData.invoke(this) as BaseVariantData
return variantData.scope.globalScope
}

// TODO: Make this configurable from the plugin extension script
companion object {
const val LOCAL_TAPES_DIR = "src/androidTest/assets/tapes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Test
import java.io.File
import java.io.IOException
import java.io.OutputStreamWriter

class BasicAndroidTest {
@Test fun buildsAndPullsTapeFiles() {
val testProjectDir = setupBasicAndroidProject("basic")
val result = runGradleForProjectDir(testProjectDir, "connectedAndroidTest")
val clearTask = result!!.task(":clearDebugOkReplayTapes")
val clearTask = result.task(":clearDebugOkReplayTapes")
val pullTask = result.task(":pullDebugOkReplayTapes")
assertThat(clearTask).isNotNull()
assertThat(pullTask).isNotNull()
Expand All @@ -25,26 +24,24 @@ class BasicAndroidTest {
@Test fun createsLocalTapesDirectoryIfNotExists() {
val testProjectDir = setupBasicAndroidProject("notapes")
val result = runGradleForProjectDir(testProjectDir, "pullDebugOkReplayTapes")
val pullTask = result!!.task(":pullDebugOkReplayTapes")
val pullTask = result.task(":pullDebugOkReplayTapes")
assertThat(pullTask).isNotNull()
assertThat(pullTask!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(File(testProjectDir, "src/androidTest/assets/tapes").isDirectory).isTrue()
}

private fun runGradleForProjectDir(projectDir: File, taskName: String): BuildResult? {
private fun runGradleForProjectDir(projectDir: File, taskName: String): BuildResult {
return GradleRunner.create()
.withProjectDir(projectDir)
.withPluginClasspath()
.withArguments(taskName, "--stacktrace")
.forwardStdError(OutputStreamWriter(System.err))
.forwardStdOutput(OutputStreamWriter(System.out))
.build()
}

@Throws(IOException::class)
private fun setupBasicAndroidProject(dirName: String, buildScriptName: String = "basic"): File {
val destDir = PluginTestHelper.createTempTestDirectory(dirName)
PluginTestHelper.prepareProjectTestDir(destDir, dirName, buildScriptName)
val destDir = createTempTestDirectory(dirName)
prepareProjectTestDir(destDir, dirName, buildScriptName)
return destDir
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package okreplay

import com.google.common.truth.Truth.assertThat
import okreplay.PluginTestHelper.*
import org.gradle.api.Project
import org.gradle.api.tasks.TaskExecutionException
import org.gradle.testfixtures.ProjectBuilder
Expand Down Expand Up @@ -45,11 +44,10 @@ class OkReplayPluginTest {
}

private fun prepareProject(): Project {
ProjectBuilder.builder().build().let {
setupDefaultAndroidProject(it)
applyOkReplay(it)
evaluate(it)
return it
return ProjectBuilder.builder().build().also { project ->
project.setupDefaultAndroidProject()
project.applyOkReplay()
project.evaluate()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package okreplay

import com.android.build.gradle.AppPlugin
import com.android.build.gradle.BaseExtension
import org.apache.commons.io.FileUtils
import org.gradle.api.Project
import org.gradle.api.internal.project.ProjectInternal
import org.gradle.testkit.runner.internal.PluginUnderTestMetadataReading
import java.io.File
import java.util.Properties

fun Project.setupDefaultAndroidProject() {
prepareLocalProperties(projectDir)

val manifest = File(projectDir, "src/main/AndroidManifest.xml")
manifest.parentFile.mkdirs()
manifest.writeText("<manifest package=\"com.example.okreplay\"/>")
pluginManager.apply(AppPlugin::class.java)

val androidExtension = extensions.getByType(BaseExtension::class.java)
androidExtension.compileSdkVersion(28)
}

fun Project.applyOkReplay() {
pluginManager.apply(OkReplayPlugin::class.java)
}

fun Project.evaluate() {
(this as ProjectInternal).evaluate()
}

fun createTempTestDirectory(testProjectName: String): File {
val dir = File(workingDir, "build/integrationTests/$testProjectName")
FileUtils.deleteDirectory(dir)
FileUtils.forceMkdir(dir)
return dir
}

fun prepareProjectTestDir(destDir: File, testProjectName: String, testBuildScriptName: String) {
val testProjectsRoot = "src/test/testProject"
val projectTypeRoot = File("$testProjectsRoot/android")
val projectUnderTest = File(workingDir, "$projectTypeRoot/$testProjectName")
check(projectUnderTest.isDirectory) { "Couldn't find test project" }

val requestedBuildScript = File(
"$projectTypeRoot/buildScriptFixtures/$testBuildScriptName.gradle")
val requestedSettingsFile = File(
"$projectTypeRoot/buildScriptFixtures/settings.gradle")
check(requestedBuildScript.isFile) { "Couldn't find the test build script" }

prepareLocalProperties(destDir)
projectUnderTest.copyRecursively(destDir)
requestedSettingsFile.copyTo(File(destDir, "settings.gradle"))

val buildScript = requestedBuildScript.readText()
.replace("\$PLUGIN_CLASSPATH", getPluginClasspath())
File(destDir, "build.gradle").writeText(buildScript)
}

private fun prepareLocalProperties(destDir: File) {
val localProperties = File(destDir, "local.properties")
localProperties.writeText("sdk.dir=${androidHome()}")
}

private fun androidHome(): String {
val envVar = System.getenv("ANDROID_HOME")
if (envVar != null) {
return envVar
}
val localPropFile = File(workingDir.parentFile, "local.properties")
if (localPropFile.isFile) {
val props = Properties()
localPropFile.inputStream().use { props.load(it) }
val sdkDir = props.getProperty("sdk.dir")
if (sdkDir != null) {
return sdkDir
}
}
throw IllegalStateException("SDK location not found. Define location with sdk.dir in the " +
"local.properties file or with an ANDROID_HOME environment variable.")
}

private fun getPluginClasspath(): String {
return PluginUnderTestMetadataReading.readImplementationClasspath()
.asSequence()
.map { it.absolutePath.replace("\\", "\\\\") } // escape backslashes on Windows
.joinToString(", ") { "'$it'" }
}

private val workingDir: File
get() = File(System.getProperty("user.dir"))
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ buildscript {

dependencies {
classpath dep.androidPlugin
classpath files($PLUGIN_CLASSPATH)
}
}

plugins {
id 'com.android.application'
id 'okreplay'
}
apply plugin: 'com.android.application'
apply plugin: 'okreplay'

android {
compileSdkVersion androidConfig.compileSdkVersion
Expand Down
Empty file.

0 comments on commit 1e6163f

Please sign in to comment.