Skip to content

Commit

Permalink
Use automatic plugin classpath injection for tests. (google#374)
Browse files Browse the repository at this point in the history
This change migrates to use GradleRunner's automatic classpath injection for code under test.

- Removed unnecessary gradle/groovy dependencies from plugin's build.gradle, as the Java Gradle Plugin development plugin already does so.

- Removed createClasspathManifest and the corresponding bridging code in test project setup.

- As mentioned in its documentation, automatic classpath inject only works if the plugin under test is applied using the plugins DSL.

- Previously we inject Kotlin dependency by putting it on testProjectRuntime and adding to the test project's classpath via classpath manifest. Now we set this in TestProjectBuilder.

- Refactored methods for building Android test project.
  • Loading branch information
voidzcy authored Jan 2, 2020
1 parent 2d295c6 commit 8aca60d
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 143 deletions.
34 changes: 0 additions & 34 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,52 +51,18 @@ configurations {
testProjectRuntime
}

/**
* Write file containing all the things that should be in the test projects' class path. This
* includes the current SNAPSHOT version of protobuf-gradle-plugin and whatever else may be
* needed by the test projects such as other plugins or libraries.
*/
task createClasspathManifest {
def outputDir = file("$buildDir/$name")

inputs.files sourceSets.main.runtimeClasspath
inputs.files configurations.testProjectRuntime
outputs.dir outputDir

doLast {
outputDir.mkdirs()
file("$outputDir/plugin-classpath.txt").text = inputs.files.join("\n")
}
}

dependencies {
compileOnly gradleApi()
compileOnly localGroovy()
compileOnly "org.gradle:gradle-kotlin-dsl:1.0.4"

compile 'com.google.guava:guava:27.0.1-jre'
compile 'com.google.gradle:osdetector-gradle-plugin:1.6.2'
compile 'commons-lang:commons-lang:2.6'

testCompile gradleTestKit()
testCompile gradleApi()
testCompile localGroovy()
testCompile 'junit:junit:4.12'
testCompile ('org.spockframework:spock-core:1.0-groovy-2.4') {
exclude module : 'groovy-all'
}
testCompile 'commons-io:commons-io:2.5'

// This kotlin version needs to be compatible with the
// android plugin being used in the test runtime.
testProjectRuntime "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.20"

// Add android plugin to the test classpath, so that we can jump into class definitions,
// read their sources, set break points, etc while debugging android unit tests.
// Change the version if necessary to match the specific unit test.
testRuntime 'com.android.tools.build:gradle:2.3.0'
// Add the classpath file to the test runtime classpath
testRuntime files(createClasspathManifest)
}

test.inputs.files fileTree("$projectDir/testProject")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import spock.lang.Unroll
* Verify android projects are identified correctly
*/
class AndroidProjectDetectionTest extends Specification {
// Current supported version is Android plugin 3.3.0+.
private static final List<String> GRADLE_VERSION = ["5.6"]
private static final List<String> ANDROID_PLUGIN_VERSION = ["3.5.0"]

Expand All @@ -32,13 +31,13 @@ class AndroidProjectDetectionTest extends Specification {
given: "a project with android plugin"
File mainProjectDir = ProtobufPluginTestHelper.projectBuilder("singleModuleAndroidProject")
.copyDirs('testProjectAndroid', 'testProjectAndroidBare')
.withAndroidPlugin(agpVersion)
.build()
appendUtilIsAndroidProjectCheckTask(new File(mainProjectDir, "build.gradle"), true)

when: "checkForAndroidPlugin task evaluates Utils.isAndroidProject"
BuildResult result = buildAndroidProject(
mainProjectDir,
agpVersion,
gradleVersion,
"checkForAndroidPlugin"
)
Expand All @@ -64,13 +63,13 @@ class AndroidProjectDetectionTest extends Specification {
File mainProjectDir = ProtobufPluginTestHelper.projectBuilder("rootModuleAndroidProject")
.copyDirs('testProjectAndroid', 'testProjectAndroidBare')
.copySubProjects(subProjectStaging)
.withAndroidPlugin(agpVersion)
.build()
appendUtilIsAndroidProjectCheckTask(new File(mainProjectDir, "build.gradle"), true)

when: "checkForAndroidPlugin task evaluates Utils.isAndroidProject"
BuildResult result = buildAndroidProject(
mainProjectDir,
agpVersion,
gradleVersion,
"checkForAndroidPlugin"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import spock.lang.Unroll
class ProtobufAndroidPluginTest extends Specification {
private static final List<String> GRADLE_VERSION = ["5.6"]
private static final List<String> ANDROID_PLUGIN_VERSION = ["3.5.0"]
private static final List<String> KOTLIN_VERSION = ["1.3.20"]

@Unroll
void "testProjectAndroid should be successfully executed [android #agpVersion, gradle #gradleVersion]"() {
Expand All @@ -28,11 +29,11 @@ class ProtobufAndroidPluginTest extends Specification {
.build()
File mainProjectDir = ProtobufPluginTestHelper.projectBuilder('testProjectAndroidMain')
.copySubProjects(testProjectStaging, testProjectLiteStaging, testProjectAndroidStaging)
.withAndroidPlugin(agpVersion)
.build()
when: "build is invoked"
BuildResult result = buildAndroidProject(
mainProjectDir,
agpVersion,
gradleVersion,
"testProjectAndroid:build"
)
Expand All @@ -46,7 +47,7 @@ class ProtobufAndroidPluginTest extends Specification {
}

@Unroll
void "testProjectAndroidKotlin should be successfully executed [android #agpVersion, gradle #gradleVersion]"() {
void "testProjectAndroidKotlin [android #agpVersion, gradle #gradleVersion, kotlin #kotlinVersion]"() {
given: "project from testProject, testProjectLite & testProjectAndroid"
File testProjectStaging = ProtobufPluginTestHelper.projectBuilder('testProject')
.copyDirs('testProjectBase', 'testProject')
Expand All @@ -59,11 +60,12 @@ class ProtobufAndroidPluginTest extends Specification {
.build()
File mainProjectDir = ProtobufPluginTestHelper.projectBuilder('testProjectAndroidMain')
.copySubProjects(testProjectStaging, testProjectLiteStaging, testProjectAndroidStaging)
.withAndroidPlugin(agpVersion)
.withKotlin(kotlinVersion)
.build()
when: "build is invoked"
BuildResult result = buildAndroidProject(
mainProjectDir,
agpVersion,
gradleVersion,
"testProjectAndroid:build"
)
Expand All @@ -74,5 +76,6 @@ class ProtobufAndroidPluginTest extends Specification {
where:
agpVersion << ANDROID_PLUGIN_VERSION
gradleVersion << GRADLE_VERSION
kotlinVersion << KOTLIN_VERSION
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import spock.lang.Unroll
class ProtobufJavaPluginTest extends Specification {
// Current supported version is Gradle 5+.
private static final List<String> GRADLE_VERSIONS = ["5.6", "6.0"]
private static final List<String> KOTLIN_VERSIONS = ["1.3.20", "1.3.30"]

void "testApplying java and com.google.protobuf adds corresponding task to project"() {
given: "a basic project with java and com.google.protobuf"
Expand Down Expand Up @@ -61,6 +62,7 @@ class ProtobufJavaPluginTest extends Specification {
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('build', '--stacktrace')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.err))
Expand All @@ -86,6 +88,7 @@ class ProtobufJavaPluginTest extends Specification {
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('build', '--stacktrace')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.err))
Expand All @@ -100,16 +103,18 @@ class ProtobufJavaPluginTest extends Specification {
}
@Unroll
void "testProjectKotlin should be successfully executed (kotlin-only project) [gradle #gradleVersion]"() {
void "testProjectKotlin (kotlin-only project) [gradle #gradleVersion, kotlin #kotlinVersion]"() {
given: "project from testProjectKotlin overlaid on testProject"
File projectDir = ProtobufPluginTestHelper.projectBuilder('testProjectKotlin')
.copyDirs('testProjectBase', 'testProjectKotlin')
.withKotlin(kotlinVersion)
.build()
when: "build is invoked"
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('build', '--stacktrace')
.withPluginClasspath()
.withDebug(true)
.withGradleVersion(gradleVersion)
.build()
Expand All @@ -120,19 +125,22 @@ class ProtobufJavaPluginTest extends Specification {
where:
gradleVersion << GRADLE_VERSIONS
kotlinVersion << KOTLIN_VERSIONS
}
@Unroll
void "testProjectJavaAndKotlin should be successfully executed (java+kotlin project) [gradle #gradleVersion]"() {
void "testProjectJavaAndKotlin (java+kotlin project) [gradle #gradleVersion, kotlin #kotlinVersion]"() {
given: "project from testProjecJavaAndKotlin overlaid on testProjectKotlin, testProject"
File projectDir = ProtobufPluginTestHelper.projectBuilder('testProjectJavaAndKotlin')
.copyDirs('testProjectBase', 'testProject', 'testProjectKotlin', 'testProjectJavaAndKotlin')
.withKotlin(kotlinVersion)
.build()
when: "build is invoked"
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('build')
.withPluginClasspath()
.withDebug(true)
.withGradleVersion(gradleVersion)
.build()
Expand All @@ -143,6 +151,7 @@ class ProtobufJavaPluginTest extends Specification {
where:
gradleVersion << GRADLE_VERSIONS
kotlinVersion << KOTLIN_VERSIONS
}
@Unroll
Expand All @@ -156,6 +165,7 @@ class ProtobufJavaPluginTest extends Specification {
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('build', '--stacktrace')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.err))
Expand Down Expand Up @@ -187,6 +197,7 @@ class ProtobufJavaPluginTest extends Specification {
BuildResult result = GradleRunner.create()
.withProjectDir(mainProjectDir)
.withArguments('testProjectDependent:build', '--stacktrace')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.err))
Expand All @@ -211,6 +222,7 @@ class ProtobufJavaPluginTest extends Specification {
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('build', '--stacktrace')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.err))
Expand Down Expand Up @@ -243,6 +255,7 @@ class ProtobufJavaPluginTest extends Specification {
BuildResult result = GradleRunner.create()
.withProjectDir(mainProjectDir)
.withArguments('testProjectDependentApp:build', '--stacktrace')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.err))
Expand All @@ -267,6 +280,7 @@ class ProtobufJavaPluginTest extends Specification {
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('build', '--stacktrace')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.err))
Expand All @@ -291,6 +305,7 @@ class ProtobufJavaPluginTest extends Specification {
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('idea')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ProtobufKotlinDslPluginTest extends Specification {
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('build', '--stacktrace')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.err))
Expand Down
Loading

0 comments on commit 8aca60d

Please sign in to comment.