Skip to content

Commit 107fce3

Browse files
committed
Add SetupAndroidProject.kt
A rule in case you have not exported the ANDROID_SDK_ROOT environment property.
1 parent 9ab9943 commit 107fce3

File tree

2 files changed

+63
-18
lines changed

2 files changed

+63
-18
lines changed

affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
package com.dropbox.affectedmoduledetector
22

3+
import com.dropbox.affectedmoduledetector.rules.SetupAndroidProject
34
import com.google.common.truth.Truth.assertThat
45
import org.gradle.testkit.runner.GradleRunner
56
import org.junit.Rule
67
import org.junit.Test
7-
import org.junit.rules.TemporaryFolder
88

99
class AffectedModuleDetectorIntegrationTest {
1010

1111
@Rule
1212
@JvmField
13-
val tmpFolder = TemporaryFolder()
13+
val tmpFolder = SetupAndroidProject()
1414

1515
@Test
1616
fun `GIVEN single project WHEN plugin is applied THEN tasks are added`() {
1717
// GIVEN
1818
// expected tasks
1919
val tasks = listOf(
20-
"runAffectedUnitTests",
21-
"runAffectedAndroidTests",
22-
"assembleAffectedAndroidTests"
20+
"runAffectedUnitTests",
21+
"runAffectedAndroidTests",
22+
"assembleAffectedAndroidTests"
2323
)
2424
tmpFolder.newFile("build.gradle").writeText(
25-
"""plugins {
25+
"""plugins {
2626
| id "com.dropbox.affectedmoduledetector"
2727
|}""".trimMargin()
2828
)
2929

3030
// WHEN
3131
val result = GradleRunner.create()
32-
.withProjectDir(tmpFolder.root)
33-
.withPluginClasspath()
34-
.withArguments("tasks")
35-
.build()
32+
.withProjectDir(tmpFolder.root)
33+
.withPluginClasspath()
34+
.withArguments("tasks")
35+
.build()
3636

3737
// THEN
3838
tasks.forEach { taskName ->
@@ -43,17 +43,18 @@ class AffectedModuleDetectorIntegrationTest {
4343
@Test
4444
fun `GIVEN multiple project WHEN plugin is applied THEN tasks has dependencies`() {
4545
// GIVEN
46+
tmpFolder.setupAndroidSdkLocation()
4647
tmpFolder.newFolder("sample-app")
4748
tmpFolder.newFolder("sample-core")
4849
tmpFolder.newFile("settings.gradle").writeText(
49-
"""
50+
"""
5051
|include ':sample-app'
5152
|include ':sample-core'
5253
""".trimMargin()
5354
)
5455

5556
tmpFolder.newFile("build.gradle").writeText(
56-
"""buildscript {
57+
"""buildscript {
5758
| repositories {
5859
| google()
5960
| jcenter()
@@ -75,7 +76,7 @@ class AffectedModuleDetectorIntegrationTest {
7576
)
7677

7778
tmpFolder.newFile("sample-app/build.gradle").writeText(
78-
"""plugins {
79+
"""plugins {
7980
| id 'com.android.application'
8081
| id 'kotlin-android'
8182
| }
@@ -89,7 +90,7 @@ class AffectedModuleDetectorIntegrationTest {
8990
)
9091

9192
tmpFolder.newFile("sample-core/build.gradle").writeText(
92-
"""plugins {
93+
"""plugins {
9394
| id 'com.android.library'
9495
| id 'kotlin-android'
9596
| }
@@ -104,10 +105,10 @@ class AffectedModuleDetectorIntegrationTest {
104105

105106
// WHEN
106107
val result = GradleRunner.create()
107-
.withProjectDir(tmpFolder.root)
108-
.withPluginClasspath()
109-
.withArguments("assembleAffectedAndroidTests", "--dry-run")
110-
.build()
108+
.withProjectDir(tmpFolder.root)
109+
.withPluginClasspath()
110+
.withArguments("assembleAffectedAndroidTests", "--dry-run")
111+
.build()
111112

112113
// THEN
113114
assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED")
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.dropbox.affectedmoduledetector.rules
2+
3+
import org.junit.rules.TemporaryFolder
4+
import java.io.File
5+
import java.io.FileNotFoundException
6+
7+
/**
8+
* TestRule that allows setup of the Android SDK within the context of a GradleRunner test.
9+
*/
10+
class SetupAndroidProject : TemporaryFolder() {
11+
12+
/**
13+
* Setup the Android SDK location for test involving Gradle and the Android Gradle DSL.
14+
*
15+
* Users may have configured their machine with an environment variable ANDROID_SDK_ROOT in
16+
* which case the Android SDK will automatically be found using this. Otherwise we attempt to
17+
* local the machine specific local.properties file and copy the contents.
18+
*/
19+
fun setupAndroidSdkLocation() {
20+
// If we happen to have already set this environment variable then we are all good to go.
21+
// Nothing to see here. Move along.
22+
if (!System.getenv("ANDROID_SDK_ROOT").isNullOrBlank()) return
23+
24+
// Find the local.properties file. File works relative to the nearest build.gradle which
25+
// is the one for the Gradle module that we are currently running tests in - not the root
26+
// project. But assuming that we will only ever be one level deep then we can simply use
27+
// ".." to go up one level where we _should_ find our file.
28+
val localDotProperties = File("../local.properties")
29+
30+
if (!localDotProperties.exists()) throw FileNotFoundException(
31+
"""
32+
|Unable to locate Android SDK. Ensure that you have either the ANDROID_SDK_ROOT
33+
|environment variable set or the sdk.dir location correctly set in local
34+
|.properties within the Gradle root project directory.
35+
""".trimMargin()
36+
)
37+
38+
// Read our machine specific local.properties file and copy it to our temp Gradle test
39+
// directory for the test.
40+
localDotProperties.bufferedReader().use {
41+
newFile("local.properties").writeText(it.readText())
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)