use of com.facebook.buck.jvm.java.FakeJavaLibrary in project buck by facebook.
the class AndroidInstrumentationApkTest method testAndroidInstrumentationApkExcludesClassesFromInstrumentedApk.
@Test
public void testAndroidInstrumentationApkExcludesClassesFromInstrumentedApk() throws Exception {
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
BuildTarget javaLibrary1Target = BuildTargetFactory.newInstance("//java/com/example:lib1");
final FakeJavaLibrary javaLibrary1 = new FakeJavaLibrary(javaLibrary1Target, pathResolver);
FakeJavaLibrary javaLibrary2 = new FakeJavaLibrary(BuildTargetFactory.newInstance("//java/com/example:lib2"), pathResolver, /* deps */
ImmutableSortedSet.of((BuildRule) javaLibrary1)) {
@Override
public ImmutableSet<SourcePath> getTransitiveClasspaths() {
return ImmutableSet.of(new DefaultBuildTargetSourcePath(javaLibrary1Target), getSourcePathToOutput());
}
};
BuildTarget javaLibrary3Target = BuildTargetFactory.newInstance("//java/com/example:lib3");
final FakeJavaLibrary javaLibrary3 = new FakeJavaLibrary(javaLibrary3Target, pathResolver);
FakeJavaLibrary javaLibrary4 = new FakeJavaLibrary(BuildTargetFactory.newInstance("//java/com/example:lib4"), pathResolver, /* deps */
ImmutableSortedSet.of((BuildRule) javaLibrary3)) {
@Override
public ImmutableSet<SourcePath> getTransitiveClasspaths() {
return ImmutableSet.of(new DefaultBuildTargetSourcePath(javaLibrary3Target), getSourcePathToOutput());
}
};
ruleResolver.addToIndex(javaLibrary1);
ruleResolver.addToIndex(javaLibrary2);
ruleResolver.addToIndex(javaLibrary3);
ruleResolver.addToIndex(javaLibrary4);
BuildRule keystore = KeystoreBuilder.createBuilder(BuildTargetFactory.newInstance("//keystores:debug")).setProperties(new FakeSourcePath("keystores/debug.properties")).setStore(new FakeSourcePath("keystores/debug.keystore")).build(ruleResolver);
// AndroidBinaryRule transitively depends on :lib1, :lib2, and :lib3.
AndroidBinaryBuilder androidBinaryBuilder = AndroidBinaryBuilder.createBuilder(BuildTargetFactory.newInstance("//apps:app"));
ImmutableSortedSet<BuildTarget> originalDepsTargets = ImmutableSortedSet.of(javaLibrary2.getBuildTarget(), javaLibrary3.getBuildTarget());
androidBinaryBuilder.setManifest(new FakeSourcePath("apps/AndroidManifest.xml")).setKeystore(keystore.getBuildTarget()).setOriginalDeps(originalDepsTargets);
AndroidBinary androidBinary = androidBinaryBuilder.build(ruleResolver);
// AndroidInstrumentationApk transitively depends on :lib1, :lib2, :lib3, and :lib4.
ImmutableSortedSet<BuildTarget> apkOriginalDepsTargets = ImmutableSortedSet.of(javaLibrary2.getBuildTarget(), javaLibrary4.getBuildTarget());
AndroidInstrumentationApkDescription.Arg arg = new AndroidInstrumentationApkDescription.Arg();
arg.apk = androidBinary.getBuildTarget();
arg.deps = apkOriginalDepsTargets;
arg.manifest = new FakeSourcePath("apps/InstrumentationAndroidManifest.xml");
BuildRuleParams params = new FakeBuildRuleParamsBuilder(BuildTargetFactory.newInstance("//apps:instrumentation")).setDeclaredDeps(ruleResolver.getAllRules(apkOriginalDepsTargets)).setExtraDeps(ImmutableSortedSet.of(androidBinary)).build();
AndroidInstrumentationApk androidInstrumentationApk = (AndroidInstrumentationApk) new AndroidInstrumentationApkDescription(new ProGuardConfig(FakeBuckConfig.builder().build()), DEFAULT_JAVAC_OPTIONS, ImmutableMap.of(), MoreExecutors.newDirectExecutorService(), CxxPlatformUtils.DEFAULT_CONFIG, new DxConfig(FakeBuckConfig.builder().build())).createBuildRule(TargetGraph.EMPTY, params, ruleResolver, arg);
assertEquals("//apps:app should have three JAR files to dex.", ImmutableSet.of(BuildTargets.getGenPath(javaLibrary1.getProjectFilesystem(), javaLibrary1.getBuildTarget(), "%s.jar"), BuildTargets.getGenPath(javaLibrary2.getProjectFilesystem(), javaLibrary2.getBuildTarget(), "%s.jar"), BuildTargets.getGenPath(javaLibrary3.getProjectFilesystem(), javaLibrary3.getBuildTarget(), "%s.jar")), androidBinary.getAndroidPackageableCollection().getClasspathEntriesToDex().stream().map(pathResolver::getRelativePath).collect(MoreCollectors.toImmutableSet()));
assertEquals("//apps:instrumentation should have one JAR file to dex.", ImmutableSet.of(BuildTargets.getGenPath(javaLibrary4.getProjectFilesystem(), javaLibrary4.getBuildTarget(), "%s.jar")), androidInstrumentationApk.getAndroidPackageableCollection().getClasspathEntriesToDex().stream().map(pathResolver::getRelativePath).collect(MoreCollectors.toImmutableSet()));
}
use of com.facebook.buck.jvm.java.FakeJavaLibrary in project buck by facebook.
the class AndroidBinaryTest method transitivePrebuiltJarsAreFirstOrderDeps.
@Test
public void transitivePrebuiltJarsAreFirstOrderDeps() throws Exception {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
BuildRule keystoreRule = addKeystoreRule(resolver);
BuildRule prebuiltJarGen = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:jar")).setOut("foo.jar").build(resolver);
BuildRule transitivePrebuiltJarDep = PrebuiltJarBuilder.createBuilder(BuildTargetFactory.newInstance("//:transitive_dep")).setBinaryJar(prebuiltJarGen.getSourcePathToOutput()).build(resolver);
FakeJavaLibrary immediateDep = resolver.addToIndex(new FakeJavaLibrary(BuildTargetFactory.newInstance("//:immediate_dep"), pathResolver, ImmutableSortedSet.of(transitivePrebuiltJarDep)));
BuildRule rule = AndroidBinaryBuilder.createBuilder(BuildTargetFactory.newInstance("//:target")).setKeystore(keystoreRule.getBuildTarget()).setManifest(new FakeSourcePath("AndroidManifest.xml")).setOriginalDeps(ImmutableSortedSet.of(immediateDep.getBuildTarget())).build(resolver);
assertThat(rule.getDeps(), Matchers.hasItem(transitivePrebuiltJarDep));
}
use of com.facebook.buck.jvm.java.FakeJavaLibrary in project buck by facebook.
the class GraphEnhancementQueryEnvironmentTest method buildQueryEnvironmentWithGraph.
private GraphEnhancementQueryEnvironment buildQueryEnvironmentWithGraph() {
// Set up target graph: lib -> sublib -> bottom
TargetNode<JavaLibraryDescription.Arg, ?> bottomNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:bottom")).build();
TargetNode<JavaLibraryDescription.Arg, ?> sublibNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:sublib")).addDep(bottomNode.getBuildTarget()).build();
TargetNode<JavaLibraryDescription.Arg, ?> libNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:lib")).addDep(sublibNode.getBuildTarget()).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(bottomNode, libNode, sublibNode);
BuildRuleResolver realResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(realResolver));
FakeJavaLibrary bottomRule = realResolver.addToIndex(new FakeJavaLibrary(bottomNode.getBuildTarget(), pathResolver));
bottomRule.setOutputFile("bottom.jar");
FakeJavaLibrary sublibRule = realResolver.addToIndex(new FakeJavaLibrary(sublibNode.getBuildTarget(), pathResolver, ImmutableSortedSet.of(bottomRule)));
sublibRule.setOutputFile("sublib.jar");
FakeJavaLibrary libRule = realResolver.addToIndex(new FakeJavaLibrary(libNode.getBuildTarget(), pathResolver, ImmutableSortedSet.of(sublibRule)));
libRule.setOutputFile("lib.jar");
return new GraphEnhancementQueryEnvironment(Optional.of(realResolver), Optional.of(targetGraph), cellRoots, BuildTargetPatternParser.forBaseName(libNode.getBuildTarget().getBaseName()), ImmutableSet.of(sublibNode.getBuildTarget()));
}
use of com.facebook.buck.jvm.java.FakeJavaLibrary in project buck by facebook.
the class DexProducedFromJavaLibraryThatContainsClassFilesTest method testGetBuildStepsWhenThereAreClassesToDex.
@Test
public void testGetBuildStepsWhenThereAreClassesToDex() throws IOException, InterruptedException {
ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
FakeJavaLibrary javaLibraryRule = new FakeJavaLibrary(BuildTargetFactory.newInstance(filesystem.getRootPath(), "//foo:bar"), pathResolver, filesystem, ImmutableSortedSet.of()) {
@Override
public ImmutableSortedMap<String, HashCode> getClassNamesToHashes() {
return ImmutableSortedMap.of("com/example/Foo", HashCode.fromString("cafebabe"));
}
};
resolver.addToIndex(javaLibraryRule);
Path jarOutput = BuildTargets.getGenPath(filesystem, javaLibraryRule.getBuildTarget(), "%s.jar");
javaLibraryRule.setOutputFile(jarOutput.toString());
BuildContext context = FakeBuildContext.withSourcePathResolver(pathResolver);
FakeBuildableContext buildableContext = new FakeBuildableContext();
Path dexOutput = BuildTargets.getGenPath(filesystem, javaLibraryRule.getBuildTarget().withFlavors(AndroidBinaryGraphEnhancer.DEX_FLAVOR), "%s.dex.jar");
createFiles(filesystem, dexOutput.toString(), jarOutput.toString());
BuildTarget buildTarget = BuildTargetFactory.newInstance(filesystem.getRootPath(), "//foo:bar#dex");
BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(filesystem).build();
DexProducedFromJavaLibrary preDex = new DexProducedFromJavaLibrary(params, javaLibraryRule);
List<Step> steps = preDex.getBuildSteps(context, buildableContext);
AndroidPlatformTarget androidPlatformTarget = createMock(AndroidPlatformTarget.class);
expect(androidPlatformTarget.getDxExecutable()).andStubReturn(Paths.get("/usr/bin/dx"));
EasyMock.replay(androidPlatformTarget);
ExecutionContext executionContext = TestExecutionContext.newBuilder().setAndroidPlatformTargetSupplier(Suppliers.ofInstance(androidPlatformTarget)).build();
String expectedDxCommand = String.format("%s --dex --no-optimize --force-jumbo --output %s %s", Paths.get("/usr/bin/dx"), filesystem.resolve(dexOutput), filesystem.resolve(jarOutput));
MoreAsserts.assertSteps("Generate bar.dex.jar.", ImmutableList.of(String.format("rm -f %s", filesystem.resolve(dexOutput)), String.format("mkdir -p %s", filesystem.resolve(dexOutput).getParent()), "estimate_dex_weight", "(cd " + filesystem.getRootPath() + " && " + expectedDxCommand + ")", String.format("zip-scrub %s", dexOutput), "record_dx_success"), steps, executionContext);
((EstimateDexWeightStep) steps.get(2)).setWeightEstimateForTesting(250);
Step recordArtifactAndMetadataStep = steps.get(5);
int exitCode = recordArtifactAndMetadataStep.execute(executionContext).getExitCode();
assertEquals(0, exitCode);
assertEquals("The generated .dex.jar file should be in the set of recorded artifacts.", ImmutableSet.of(BuildTargets.getGenPath(filesystem, buildTarget, "%s.dex.jar")), buildableContext.getRecordedArtifacts());
buildableContext.assertContainsMetadataMapping(DexProducedFromJavaLibrary.WEIGHT_ESTIMATE, "250");
}
use of com.facebook.buck.jvm.java.FakeJavaLibrary in project buck by facebook.
the class DexWithClassesTest method testIntermediateDexRuleToDexWithClassesWhenIntermediateDexHasNoClasses.
@Test
public void testIntermediateDexRuleToDexWithClassesWhenIntermediateDexHasNoClasses() {
SourcePathResolver resolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//java/com/example:lib");
JavaLibrary javaLibrary = new FakeJavaLibrary(javaLibraryTarget, resolver);
BuildTarget buildTarget = BuildTargetFactory.newInstance("//java/com/example:lib#dex");
BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).build();
DexProducedFromJavaLibrary dexFromJavaLibrary = new DexProducedFromJavaLibrary(params, javaLibrary);
dexFromJavaLibrary.getBuildOutputInitializer().setBuildOutput(new DexProducedFromJavaLibrary.BuildOutput(/* weightEstimate */
1600, /* classNamesToHashes */
ImmutableSortedMap.of(), Optional.empty()));
DexWithClasses dexWithClasses = DexWithClasses.TO_DEX_WITH_CLASSES.apply(dexFromJavaLibrary);
assertNull("If the JavaLibraryRule does not produce any .class files, " + "then DexWithClasses.TO_DEX_WITH_CLASSES should return null.", dexWithClasses);
}
Aggregations