Skip to content

Commit

Permalink
Make AndroidInstrumentationInfo createable from Skylark.
Browse files Browse the repository at this point in the history
This enables writing tests for android_instrumentation_test that mock
android_binary using a skylark rule that returns an AndroidInstrumentationInfo.

RELNOTES: None
PiperOrigin-RevId: 185417182
  • Loading branch information
aj-michael authored and Copybara-Service committed Feb 12, 2018
1 parent 642c436 commit 3ea6a55
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
// limitations under the License.
package com.google.devtools.build.lib.rules.android;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.packages.NativeProvider;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
import com.google.devtools.build.lib.syntax.FunctionSignature;
import com.google.devtools.build.lib.syntax.SkylarkType;

/**
* A provider for targets that create Android instrumentations. Consumed by Android testing rules.
Expand All @@ -33,9 +37,31 @@
public class AndroidInstrumentationInfo extends NativeInfo {

private static final String SKYLARK_NAME = "AndroidInstrumentationInfo";
private static final FunctionSignature.WithValues<Object, SkylarkType> SIGNATURE =
FunctionSignature.WithValues.create(
FunctionSignature.of(
/*numMandatoryPositionals=*/ 0,
/*numOptionalPositionals=*/ 0,
/*numMandatoryNamedOnly*/ 2,
/*starArg=*/ false,
/*kwArg=*/ false,
"target_apk",
"instrumentation_apk"),
/*defaultValues=*/ null,
/*types=*/ ImmutableList.of(
SkylarkType.of(Artifact.class), // target_apk
SkylarkType.of(Artifact.class))); // instrumentation_apk
public static final NativeProvider<AndroidInstrumentationInfo> PROVIDER =
new NativeProvider<AndroidInstrumentationInfo>(
AndroidInstrumentationInfo.class, SKYLARK_NAME) {};
AndroidInstrumentationInfo.class, SKYLARK_NAME, SIGNATURE) {
@Override
protected AndroidInstrumentationInfo createInstanceFromSkylark(
Object[] args, Location loc) {
return new AndroidInstrumentationInfo(
/*targetApk=*/ (Artifact) args[0],
/*instrumentationApk=*/ (Artifact) args[1]);
}
};

private final Artifact targetApk;
private final Artifact instrumentationApk;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4077,6 +4077,35 @@ public void testInstrumentationInfoAccessibleFromSkylark() throws Exception {
"java/com/google/android/instr/b1.apk", "java/com/google/android/instr/b2.apk");
}

@Test
public void testInstrumentationInfoCreatableFromSkylark() throws Exception {
scratch.file(
"java/com/google/android/instr/BUILD",
"load(':instr.bzl', 'instr')",
"android_binary(name = 'b1',",
" srcs = ['b1.java'],",
" instruments = ':b2',",
" manifest = 'AndroidManifest.xml')",
"android_binary(name = 'b2',",
" srcs = ['b2.java'],",
" manifest = 'AndroidManifest.xml')",
"instr(name = 'instr', dep = ':b1')");
scratch.file(
"java/com/google/android/instr/instr.bzl",
"def _impl(ctx):",
" target = ctx.attr.dep[AndroidInstrumentationInfo].target_apk",
" instr = ctx.attr.dep[AndroidInstrumentationInfo].instrumentation_apk",
" return [AndroidInstrumentationInfo(target_apk=target,instrumentation_apk=instr)]",
"instr = rule(implementation=_impl,",
" attrs={'dep': attr.label(providers=[AndroidInstrumentationInfo])})");
ConfiguredTarget instr = getConfiguredTarget("//java/com/google/android/instr");
assertThat(instr).isNotNull();
assertThat(instr.get(AndroidInstrumentationInfo.PROVIDER).getTargetApk().prettyPrint())
.isEqualTo("java/com/google/android/instr/b2.apk");
assertThat(instr.get(AndroidInstrumentationInfo.PROVIDER).getInstrumentationApk().prettyPrint())
.isEqualTo("java/com/google/android/instr/b1.apk");
}

@Test
public void testInstrumentationInfoProviderHasApks() throws Exception {
scratch.file(
Expand Down

0 comments on commit 3ea6a55

Please sign in to comment.