From 65ad0d1c789d34bedbe39da63627c05144adf335 Mon Sep 17 00:00:00 2001 From: Maurice Lam Date: Thu, 4 May 2017 12:06:00 -0700 Subject: [PATCH] Support multiple library test APKs - Extracted the instrumentation test APK generation code to dist-library-instrumentation-tests.gradle, which is enhanced to support multiple test APKs for different build variants, so that the output APK name will not conflict. - Extracted Robolectric test code to dist-unit-tests.gradle so that it can be more easily reused and for better organization. Test: ./gradlew, build_for_build_server.sh builds expected artifacts Change-Id: I7a3c4b3b2c3528fda80deb7778a5af5f7703c913 --- library/self.gradle | 44 +++++-------------- .../dist-library-instrumentation-tests.gradle | 33 ++++++++++++++ tools/gradle/dist-unit-tests.gradle | 38 ++++++++++++++++ 3 files changed, 83 insertions(+), 32 deletions(-) create mode 100644 tools/gradle/dist-library-instrumentation-tests.gradle create mode 100644 tools/gradle/dist-unit-tests.gradle diff --git a/library/self.gradle b/library/self.gradle index b7cbd55..6a405e2 100644 --- a/library/self.gradle +++ b/library/self.gradle @@ -1,8 +1,10 @@ /** * This self.gradle build file is only run when built in ub-setupwizard-* branches. */ -apply plugin: 'dist' apply from: 'standalone-rules.gradle' +apply from: '../tools/gradle/dist-library-instrumentation-tests.gradle' +apply from: '../tools/gradle/dist-unit-tests.gradle' + // Add targets for tests android.sourceSets { androidTest { @@ -62,36 +64,14 @@ android.lintOptions { android.libraryVariants.all { variant -> variant.assemble.dependsOn(tasks.findByName('lint')) } -def distTask = tasks.findByName('dist') -if (distTask) { - // Output all test APKs to the distribution folder - android.testVariants.all { variant -> - // Make the dist task depend on the test variant, so the test APK will be built - distTask.dependsOn variant.assemble - // TODO: remap the different test variants to different file names - } - // Output the Robolectric test results to host-test-reports/*.zip - afterEvaluate { - android.unitTestVariants.all { variant -> - def task = tasks.findByName('test' + variant.name.capitalize()) - gradle.taskGraph.whenReady { taskGraph -> - task.ignoreFailures = taskGraph.hasTask(distTask) - } - - // Create a zip file of the XML test reports and dist it to host-test-reports. - // The file path and format should match GradleHostBasedTest class in TradeFed. - def junitReport = task.reports.junitXml - if (junitReport.enabled) { - def zipTask = project.tasks.create("zipResultsOf${task.name.capitalize()}", Zip) { - from junitReport.destination - archiveName = task.name + 'Result.zip' - destinationDir = junitReport.destination.parentFile - } - tasks.dist.dependsOn zipTask - zipTask.mustRunAfter task - dist.file zipTask.archivePath.path, "host-test-reports/${zipTask.archiveName}" - } - } - } +// For compatibility with existing continuous test configurations, copy the file to +// setup-wizard-libTest.apk +// TODO: Remove this once continuous test configurations are updated to handle the new file name +task createLegacyTestApk(type: Copy) { + from "${project.ext.distDir}/setup-wizard-lib-gingerbreadCompat-debug-androidTest.apk" + into "${project.ext.distDir}" + rename ('setup-wizard-lib-gingerbreadCompat-debug-androidTest.apk', 'setup-wizard-libTest.apk') } + +tasks.dist.finalizedBy createLegacyTestApk diff --git a/tools/gradle/dist-library-instrumentation-tests.gradle b/tools/gradle/dist-library-instrumentation-tests.gradle new file mode 100644 index 0000000..2be6349 --- /dev/null +++ b/tools/gradle/dist-library-instrumentation-tests.gradle @@ -0,0 +1,33 @@ +/** + * This script plugin is used to build and dist the test APK outputs of a library with multiple + * build flavors. + * + * Compared to the defaults of the 'dist' plugin, it does two additional things: + * 1. It builds the "debug" test APKs when the 'dist' task is run. + * 2. It dist the test APKs using the original output file name instead of hard coding + * "${project.archivesBaseName}Tests.apk". This allows multiple flavors of test APKs to be built + * without conflicting file names. + */ + +apply plugin: 'dist' + +// Set the dist files to empty map, and to tell DistExtension to not include the default files, +// because the default output only supports one test APK output for libraries. +dist.files = [:] +android.testVariants.all { variant -> + // "Debug" tests are not built by BuildSrc by default. Depend on the task so it will be built. + tasks.dist.dependsOn variant.assemble + + // Output all test APKs to the distribution folder. + // For a project named "setup-wizard-lib" with build flavor "platform" and build type "debug", + // the output file will be named "setup-wizard-lib-platform-debug-androidTest.apk" + variant.outputs.each { output -> + dist.file output.outputFile.canonicalPath, output.outputFile.name + } +} +android.libraryVariants.all { variant -> + // Output all library AARs to the distribution folder + variant.outputs.each { output -> + dist.file output.outputFile.canonicalPath, output.outputFile.name + } +} diff --git a/tools/gradle/dist-unit-tests.gradle b/tools/gradle/dist-unit-tests.gradle new file mode 100644 index 0000000..faae260 --- /dev/null +++ b/tools/gradle/dist-unit-tests.gradle @@ -0,0 +1,38 @@ +/** + * This script plugin is used to bundle the host test (e.g. Robolectric) results and dist it in + * a location where TradeFed knows how to parse. + * + * - If a non-dist build is run with test, it will run the normal unit tests, failing the build if + * there are test failures. + * - If a dist build is run with test (e.g. ./gradlew dist test), the build will ignore any test + * failures, and will create a zip of the XML test reports for each test run, and copy them to + * dist/host-test-reports for consumption by TradeFed. + */ + +apply plugin: 'dist' + +// If unit tests are run as part of the build, dist the test XML reports to host-test-reports/*.zip +android.unitTestVariants.all { variant -> + def task = tasks.findByName('test' + variant.name.capitalize()) + gradle.taskGraph.whenReady { taskGraph -> + // Ignore the failures, so the build continues even on test errors when the build is + // running with 'dist'. (Usually as part of a build server build) + task.ignoreFailures = taskGraph.hasTask(tasks.dist) + } + + def junitReport = task.reports.junitXml + if (junitReport.enabled) { + // Create a zip file of the XML test reports + def zipTask = tasks.create("zipResultsOf${task.name.capitalize()}", Zip) { + from junitReport.destination + archiveName = task.name + 'Result.zip' + destinationDir = junitReport.destination.parentFile + } + zipTask.mustRunAfter task + + // Copy the test reports to dist/host-test-reports + // The file path and format should match GradleHostBasedTest class in TradeFed. + tasks.dist.dependsOn zipTask + dist.file zipTask.archivePath.path, "host-test-reports/${zipTask.archiveName}" + } +}