Skip to content

Commit

Permalink
Remove obsolete fields from AndroidInstrumentationInfo.
Browse files Browse the repository at this point in the history
AndroidInstrumentationInfo is used to access the target/instrumentation app while ApkInfo is used to access the test app.

RELNOTES: None
PiperOrigin-RevId: 252087467
  • Loading branch information
Googler authored and copybara-github committed Jun 7, 2019
1 parent 89d664b commit 12fc87c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -574,11 +574,8 @@ public static RuleConfiguredTargetBuilder createAndroidBinary(
ApkInfo targetApkProvider =
ruleContext.getPrerequisite("instruments", Mode.TARGET, ApkInfo.PROVIDER);

Artifact targetApk = targetApkProvider.getApk();
Artifact instrumentationApk = zipAlignedApk;

AndroidInstrumentationInfo instrumentationProvider =
new AndroidInstrumentationInfo(targetApk, instrumentationApk, targetApkProvider);
new AndroidInstrumentationInfo(targetApkProvider);

builder.addNativeDeclaredProvider(instrumentationProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,31 @@
// limitations under the License.
package com.google.devtools.build.lib.rules.android;

import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.skylarkbuildapi.android.AndroidInstrumentationInfoApi;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.Runtime;

/**
* A provider for targets that create Android instrumentations. Consumed by Android testing rules.
*/
@Immutable
public class AndroidInstrumentationInfo extends NativeInfo
implements AndroidInstrumentationInfoApi<Artifact, ApkInfo> {
implements AndroidInstrumentationInfoApi<ApkInfo> {

private static final String SKYLARK_NAME = "AndroidInstrumentationInfo";

public static final AndroidInstrumentationInfoProvider PROVIDER =
new AndroidInstrumentationInfoProvider();

private final Artifact targetApk;
private final Artifact instrumentationApk;
private final ApkInfo target;

AndroidInstrumentationInfo(Artifact targetApk, Artifact instrumentationApk) {
AndroidInstrumentationInfo(ApkInfo target) {
super(PROVIDER);
this.targetApk = targetApk;
this.instrumentationApk = instrumentationApk;
this.target = null;
}

AndroidInstrumentationInfo(Artifact targetApk, Artifact instrumentationApk, ApkInfo target) {
super(PROVIDER);
this.targetApk = targetApk;
this.instrumentationApk = instrumentationApk;
this.target = target;
}

@Override
public Artifact getTargetApk() {
return targetApk;
}

@Override
public Artifact getInstrumentationApk() {
return instrumentationApk;
}

@Override
public ApkInfo getTarget() {
return target;
Expand All @@ -69,17 +46,15 @@ public ApkInfo getTarget() {
/** Provider for {@link AndroidInstrumentationInfo}. */
public static class AndroidInstrumentationInfoProvider
extends BuiltinProvider<AndroidInstrumentationInfo>
implements AndroidInstrumentationInfoApiProvider<Artifact, ApkInfo> {
implements AndroidInstrumentationInfoApiProvider<ApkInfo> {

private AndroidInstrumentationInfoProvider() {
super(SKYLARK_NAME, AndroidInstrumentationInfo.class);
}

@Override
public AndroidInstrumentationInfoApi<Artifact, ApkInfo> createInfo(
Artifact targetApk, Artifact instrumentationApk, Object target) throws EvalException {
return new AndroidInstrumentationInfo(
targetApk, instrumentationApk, target == Runtime.NONE ? null : (ApkInfo) target);
public AndroidInstrumentationInfoApi<ApkInfo> createInfo(ApkInfo target) throws EvalException {
return new AndroidInstrumentationInfo(target);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,15 @@ private static AndroidInstrumentationInfo getInstrumentationProvider(RuleContext
"test_app", Mode.TARGET, AndroidInstrumentationInfo.PROVIDER);
}

@Nullable
private static ApkInfo getApkProvider(RuleContext ruleContext) {
return ruleContext.getPrerequisite("test_app", Mode.TARGET, ApkInfo.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();
return getInstrumentationProvider(ruleContext).getTarget().getApk();
}

/**
Expand All @@ -219,7 +224,7 @@ private static Artifact getTargetApk(RuleContext ruleContext) {
*/
@Nullable
private static Artifact getInstrumentationApk(RuleContext ruleContext) {
return getInstrumentationProvider(ruleContext).getInstrumentationApk();
return getApkProvider(ruleContext).getApk();
}

/** The support APKs from the {@code support_apks} and {@code fixtures} attributes. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class AndroidBootstrap implements Bootstrap {

private final AndroidSkylarkCommonApi<?, ?> androidCommon;
private final ApkInfoApiProvider apkInfoProvider;
private final AndroidInstrumentationInfoApiProvider<?, ?> androidInstrumentationInfoProvider;
private final AndroidInstrumentationInfoApiProvider<?> androidInstrumentationInfoProvider;
private final AndroidDeviceBrokerInfoApiProvider androidDeviceBrokerInfoProvider;
private final AndroidResourcesInfoApi.AndroidResourcesInfoApiProvider<?, ?, ?>
androidResourcesInfoProvider;
Expand All @@ -40,7 +40,7 @@ public class AndroidBootstrap implements Bootstrap {
public AndroidBootstrap(
AndroidSkylarkCommonApi<?, ?> androidCommon,
ApkInfoApiProvider apkInfoProvider,
AndroidInstrumentationInfoApiProvider<?, ?> androidInstrumentationInfoProvider,
AndroidInstrumentationInfoApiProvider<?> androidInstrumentationInfoProvider,
AndroidDeviceBrokerInfoApiProvider androidDeviceBrokerInfoProvider,
AndroidResourcesInfoApiProvider<?, ?, ?> androidResourcesInfoProvider,
AndroidNativeLibsInfoApiProvider androidNativeLibsInfoProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.
package com.google.devtools.build.lib.skylarkbuildapi.android;

import com.google.devtools.build.lib.skylarkbuildapi.FileApi;
import com.google.devtools.build.lib.skylarkbuildapi.ProviderApi;
import com.google.devtools.build.lib.skylarkbuildapi.StructApi;
import com.google.devtools.build.lib.skylarkinterface.Param;
Expand All @@ -34,28 +33,13 @@
+ "Android instrumentation and target APKs to run in a test",
documented = false,
category = SkylarkModuleCategory.PROVIDER)
public interface AndroidInstrumentationInfoApi<FileT extends FileApi, ApkT extends ApkInfoApi<?>>
extends StructApi {
public interface AndroidInstrumentationInfoApi<ApkT extends ApkInfoApi<?>> extends StructApi {

/**
* Name of this info object.
*/
public static String NAME = "AndroidInstrumentationInfo";

@SkylarkCallable(
name = "target_apk",
doc = "Returns the target APK of the instrumentation test.",
documented = false,
structField = true)
FileT getTargetApk();

@SkylarkCallable(
name = "instrumentation_apk",
doc = "Returns the instrumentation APK that should be executed.",
documented = false,
structField = true)
FileT getInstrumentationApk();

@SkylarkCallable(
name = "target",
doc = "Returns the target ApkInfo of the instrumentation test.",
Expand All @@ -71,36 +55,22 @@ public interface AndroidInstrumentationInfoApi<FileT extends FileApi, ApkT exten
"Do not use this module. It is intended for migration purposes only. If you depend on "
+ "it, you will be broken when it is removed.",
documented = false)
public interface AndroidInstrumentationInfoApiProvider<
FileT extends FileApi, ApkT extends ApkInfoApi<?>>
public interface AndroidInstrumentationInfoApiProvider<ApkT extends ApkInfoApi<?>>
extends ProviderApi {

@SkylarkCallable(
name = "AndroidInstrumentationInfo",
doc = "The <code>AndroidInstrumentationInfo</code> constructor.",
documented = false,
parameters = {
@Param(
name = "target_apk",
type = FileApi.class,
named = true,
doc = "The target APK of the instrumentation test."),
@Param(
name = "instrumentation_apk",
type = FileApi.class,
named = true,
doc = "The instrumentation APK that should be executed."),
@Param(
name = "target",
type = ApkInfoApi.class,
named = true,
defaultValue = "None",
noneable = true,
doc = "The target ApkInfo of the instrumentation test.")
},
selfCall = true)
@SkylarkConstructor(objectType = AndroidInstrumentationInfoApi.class, receiverNameForDoc = NAME)
public AndroidInstrumentationInfoApi<FileT, ApkT> createInfo(
FileT targetApk, FileT instrumentationApk, Object target) throws EvalException;
public AndroidInstrumentationInfoApi<ApkT> createInfo(ApkT target) throws EvalException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,14 @@
package com.google.devtools.build.skydoc.fakebuildapi.android;

import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.skylarkbuildapi.FileApi;
import com.google.devtools.build.lib.skylarkbuildapi.android.AndroidInstrumentationInfoApi;
import com.google.devtools.build.lib.skylarkbuildapi.android.ApkInfoApi;
import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
import com.google.devtools.build.lib.syntax.EvalException;

/** Fake implementation of {@link AndroidInstrumentationInfoApi}. */
public class FakeAndroidInstrumentationInfo
implements AndroidInstrumentationInfoApi<FileApi, ApkInfoApi<?>> {

@Override
public FileApi getTargetApk() {
return null;
}

@Override
public FileApi getInstrumentationApk() {
return null;
}
implements AndroidInstrumentationInfoApi<ApkInfoApi<?>> {

@Override
public ApkInfoApi<?> getTarget() {
Expand All @@ -55,11 +44,11 @@ public void repr(SkylarkPrinter printer) {}

/** Fake implementation of {@link AndroidInstrumentationInfoApiProvider}. */
public static class FakeAndroidInstrumentationInfoProvider
implements AndroidInstrumentationInfoApiProvider<FileApi, ApkInfoApi<?>> {
implements AndroidInstrumentationInfoApiProvider<ApkInfoApi<?>> {

@Override
public AndroidInstrumentationInfoApi<FileApi, ApkInfoApi<?>> createInfo(
FileApi targetApk, FileApi instrumentationApk, Object target) throws EvalException {
public AndroidInstrumentationInfoApi<ApkInfoApi<?>> createInfo(ApkInfoApi<?> target)
throws EvalException {
return new FakeAndroidInstrumentationInfo();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4267,16 +4267,14 @@ public void testInstrumentationInfoAccessibleFromSkylark() throws Exception {
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 [DefaultInfo(files=depset([target,instr]))]",
" target = ctx.attr.dep[AndroidInstrumentationInfo].target.signed_apk",
" return [DefaultInfo(files=depset([target]))]",
"instr = rule(implementation=_impl,",
" attrs={'dep': attr.label(providers=[AndroidInstrumentationInfo])})");
ConfiguredTarget instr = getConfiguredTarget("//java/com/google/android/instr");
assertThat(instr).isNotNull();
assertThat(prettyArtifactNames(instr.getProvider(FilesToRunProvider.class).getFilesToRun()))
.containsExactly(
"java/com/google/android/instr/b1.apk", "java/com/google/android/instr/b2.apk");
.containsExactly("java/com/google/android/instr/b2.apk");
}

@Test
Expand All @@ -4295,17 +4293,14 @@ public void testInstrumentationInfoCreatableFromSkylark() throws Exception {
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)]",
" target = ctx.attr.dep[AndroidInstrumentationInfo].target",
" return [AndroidInstrumentationInfo(target=target)]",
"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())
assertThat(instr.get(AndroidInstrumentationInfo.PROVIDER).getTarget().getApk().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
Expand All @@ -4321,12 +4316,9 @@ public void testInstrumentationInfoProviderHasApks() throws Exception {
" manifest = 'AndroidManifest.xml')");
ConfiguredTarget b1 = getConfiguredTarget("//java/com/google/android/instr:b1");
AndroidInstrumentationInfo provider = b1.get(AndroidInstrumentationInfo.PROVIDER);
assertThat(provider.getTargetApk()).isNotNull();
assertThat(provider.getTargetApk().prettyPrint())
assertThat(provider.getTarget()).isNotNull();
assertThat(provider.getTarget().getApk().prettyPrint())
.isEqualTo("java/com/google/android/instr/b2.apk");
assertThat(provider.getInstrumentationApk()).isNotNull();
assertThat(provider.getInstrumentationApk().prettyPrint())
.isEqualTo("java/com/google/android/instr/b1.apk");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ private static Artifact getDeviceFixtureScript(ConfiguredTarget deviceScriptFixt
}

private static Artifact getInstrumentationApk(ConfiguredTarget instrumentation) {
return instrumentation.get(AndroidInstrumentationInfo.PROVIDER).getInstrumentationApk();
return instrumentation.get(AndroidInstrumentationInfo.PROVIDER).getTarget().getApk();
}

private static Artifact getTargetApk(ConfiguredTarget instrumentation) {
return instrumentation.get(AndroidInstrumentationInfo.PROVIDER).getTargetApk();
return instrumentation.get(ApkInfo.PROVIDER).getApk();
}

private String getTestStubContents(ConfiguredTarget androidInstrumentationTest) throws Exception {
Expand Down

0 comments on commit 12fc87c

Please sign in to comment.