diff options
author | 2017-10-26 21:12:21 +0200 | |
---|---|---|
committer | 2017-10-27 16:29:31 +0200 | |
commit | 19c5039ad76476951095f603aad380051086a832 (patch) | |
tree | ad5c2ae916e5d02a4bf1f9ec2941a9766dc690cc /src/main/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTest.java | |
parent | 67deac4e0dc1d65012cfba4f41ccbfe246306421 (diff) |
Modify android_instrumentation_test.instrumentation to accept android_binary instead of android_instrumentation.
GITHUB: #903
RELNOTES: None.
PiperOrigin-RevId: 173572553
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTest.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTest.java | 61 |
1 files changed, 40 insertions, 21 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTest.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTest.java index 78a21cc5a0..20e9f4bbb8 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTest.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTest.java @@ -35,12 +35,13 @@ import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTa import com.google.devtools.build.lib.analysis.test.ExecutionInfo; import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; +import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.syntax.Type; import com.google.devtools.build.lib.util.ResourceFileLoader; import java.io.IOException; import javax.annotation.Nullable; -/** An implementation of the {@code android_instrumentation} rule. */ +/** An implementation of the {@code android_instrumentation_test} rule. */ public class AndroidInstrumentationTest implements RuleConfiguredTargetFactory { private static final Template ANDROID_INSTRUMENTATION_TEST_STUB_SCRIPT = @@ -48,15 +49,29 @@ public class AndroidInstrumentationTest implements RuleConfiguredTargetFactory { AndroidInstrumentationTest.class, "android_instrumentation_test_template.txt"); private static final String TEST_SUITE_PROPERTY_NAME_FILE = "test_suite_property_name.txt"; + /** Checks expected rule invariants, throws rule errors if anything is set wrong. */ + private static void validateRuleContext(RuleContext ruleContext) + throws InterruptedException, RuleErrorException { + if (getInstrumentationProvider(ruleContext) == null) { + ruleContext.throwWithAttributeError( + "instrumentation", + String.format( + "The android_binary target at %s is missing an 'instruments' attribute. Please set " + + "it as the label of the android_binary under test.", + ruleContext.attributes().get("instrumentation", BuildType.LABEL))); + } + } + @Override public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException { + validateRuleContext(ruleContext); + // The wrapper script that invokes the test entry point. Artifact testExecutable = createTestExecutable(ruleContext); ImmutableList<TransitiveInfoCollection> runfilesDeps = ImmutableList.<TransitiveInfoCollection>builder() - .addAll(ruleContext.getPrerequisites("instrumentations", Mode.TARGET)) .addAll(ruleContext.getPrerequisites("fixtures", Mode.TARGET)) .add(ruleContext.getPrerequisite("target_device", Mode.HOST)) .add(ruleContext.getPrerequisite("$test_entry_point", Mode.HOST)) @@ -65,6 +80,8 @@ public class AndroidInstrumentationTest implements RuleConfiguredTargetFactory { Runfiles runfiles = new Runfiles.Builder(ruleContext.getWorkspaceName()) .addArtifact(testExecutable) + .addArtifact(getInstrumentationApk(ruleContext)) + .addArtifact(getTargetApk(ruleContext)) .addTargets(runfilesDeps, RunfilesProvider.DEFAULT_RUNFILES) .addTransitiveArtifacts(AndroidCommon.getSupportApks(ruleContext)) .addTransitiveArtifacts(getAdb(ruleContext).getFilesToRun()) @@ -105,9 +122,8 @@ public class AndroidInstrumentationTest implements RuleConfiguredTargetFactory { .add(executableSubstitution("%adb%", getAdb(ruleContext))) .add(executableSubstitution("%device_script%", getTargetDevice(ruleContext))) .add(executableSubstitution("%test_entry_point%", getTestEntryPoint(ruleContext))) - .add(artifactListSubstitution("%target_apks%", getTargetApks(ruleContext))) - .add( - artifactListSubstitution("%instrumentation_apks%", getInstrumentationApks(ruleContext))) + .add(artifactSubstitution("%target_apk%", getTargetApk(ruleContext))) + .add(artifactSubstitution("%instrumentation_apk%", getInstrumentationApk(ruleContext))) .add(artifactListSubstitution("%support_apks%", getAllSupportApks(ruleContext))) .add(Substitution.ofSpaceSeparatedMap("%test_args%", getTestArgs(ruleContext))) .add(Substitution.ofSpaceSeparatedMap("%fixture_args%", getFixtureArgs(ruleContext))) @@ -167,6 +183,10 @@ public class AndroidInstrumentationTest implements RuleConfiguredTargetFactory { return Substitution.of(key, filesToRunProvider.getExecutable().getRunfilesPathString()); } + private static Substitution artifactSubstitution(String key, Artifact artifact) { + return Substitution.of(key, artifact.getRunfilesPathString()); + } + private static Substitution artifactListSubstitution(String key, Iterable<Artifact> artifacts) { return Substitution.ofSpaceSeparatedList( key, @@ -175,26 +195,25 @@ public class AndroidInstrumentationTest implements RuleConfiguredTargetFactory { .collect(ImmutableList.toImmutableList())); } - /** - * The target APKs from each {@code android_instrumentation} in the {@code instrumentations} - * attribute. - */ - private static Iterable<Artifact> getTargetApks(RuleContext ruleContext) { - return Iterables.transform( - ruleContext.getPrerequisites( - "instrumentations", Mode.TARGET, AndroidInstrumentationInfo.PROVIDER), - AndroidInstrumentationInfo::getTargetApk); + @Nullable + private static AndroidInstrumentationInfo getInstrumentationProvider(RuleContext ruleContext) { + return ruleContext.getPrerequisite( + "instrumentation", Mode.TARGET, AndroidInstrumentationInfo.PROVIDER); + } + + /** The target APK from the {@code android_binary} in the {@code instrumentation} attribute. */ + @Nullable + private static Artifact getTargetApk(RuleContext ruleContext) { + return getInstrumentationProvider(ruleContext).getTargetApk(); } /** - * The instrumentation APKs from each {@code android_instrumentation} in the {@code - * instrumentations} attribute. + * The instrumentation APK from the {@code android_binary} in the {@code instrumentation} + * attribute. */ - private static Iterable<Artifact> getInstrumentationApks(RuleContext ruleContext) { - return Iterables.transform( - ruleContext.getPrerequisites( - "instrumentations", Mode.TARGET, AndroidInstrumentationInfo.PROVIDER), - AndroidInstrumentationInfo::getInstrumentationApk); + @Nullable + private static Artifact getInstrumentationApk(RuleContext ruleContext) { + return getInstrumentationProvider(ruleContext).getInstrumentationApk(); } /** The support APKs from the {@code support_apks} and {@code fixtures} attributes. */ |