use of com.facebook.buck.rules.FakeSourcePath 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.rules.FakeSourcePath in project buck by facebook.
the class AndroidLibraryGraphEnhancerTest method testBuildableIsCreated.
@Test
public void testBuildableIsCreated() {
BuildTarget buildTarget = BuildTargetFactory.newInstance("//java/com/example:library");
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
BuildRule resourceRule1 = ruleResolver.addToIndex(AndroidResourceRuleBuilder.newBuilder().setRuleFinder(ruleFinder).setBuildTarget(BuildTargetFactory.newInstance("//android_res/com/example:res1")).setRDotJavaPackage("com.facebook").setRes(new FakeSourcePath("android_res/com/example/res1")).build());
BuildRule resourceRule2 = ruleResolver.addToIndex(AndroidResourceRuleBuilder.newBuilder().setRuleFinder(ruleFinder).setBuildTarget(BuildTargetFactory.newInstance("//android_res/com/example:res2")).setRDotJavaPackage("com.facebook").setRes(new FakeSourcePath("android_res/com/example/res2")).build());
BuildRuleParams buildRuleParams = new FakeBuildRuleParamsBuilder(buildTarget).setDeclaredDeps(ImmutableSortedSet.of(resourceRule1, resourceRule2)).build();
AndroidLibraryGraphEnhancer graphEnhancer = new AndroidLibraryGraphEnhancer(buildTarget, buildRuleParams, DEFAULT_JAVAC_OPTIONS, DependencyMode.FIRST_ORDER, /* forceFinalResourceIds */
false, /* unionPackage */
Optional.empty(), /* rName */
Optional.empty(), false);
Optional<DummyRDotJava> dummyRDotJava = graphEnhancer.getBuildableForAndroidResources(ruleResolver, /* createBuildableIfEmptyDeps */
false);
assertTrue(dummyRDotJava.isPresent());
assertEquals("DummyRDotJava must contain these exact AndroidResourceRules.", // Note: these are the reverse order to which they are in the buildRuleParams.
ImmutableList.of(resourceRule1, resourceRule2), dummyRDotJava.get().getAndroidResourceDeps());
assertEquals("//java/com/example:library#dummy_r_dot_java", dummyRDotJava.get().getFullyQualifiedName());
assertEquals("DummyRDotJava must depend on the two AndroidResourceRules.", ImmutableSet.of("//android_res/com/example:res1", "//android_res/com/example:res2"), dummyRDotJava.get().getDeps().stream().map(Object::toString).collect(MoreCollectors.toImmutableSet()));
}
use of com.facebook.buck.rules.FakeSourcePath in project buck by facebook.
the class AndroidPackageableCollectorTest method testGetAndroidResourceDeps.
/**
* Create the following dependency graph of {@link AndroidResource}s:
* <pre>
* A
* / | \
* B | D
* \ | /
* C
* </pre>
* Note that an ordinary breadth-first traversal would yield either {@code A B C D} or
* {@code A D C B}. However, either of these would be <em>wrong</em> in this case because we need
* to be sure that we perform a topological sort, the resulting traversal of which is either
* {@code A B D C} or {@code A D B C}.
* <p>
* The reason for the correct result being reversed is because we want the resources with the most
* dependencies listed first on the path, so that they're used in preference to the ones that they
* depend on (presumably, the reason for extending the initial set of resources was to override
* values).
*/
@Test
public void testGetAndroidResourceDeps() throws Exception {
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
BuildRule c = ruleResolver.addToIndex(AndroidResourceRuleBuilder.newBuilder().setRuleFinder(ruleFinder).setBuildTarget(BuildTargetFactory.newInstance("//:c")).setRes(new FakeSourcePath("res_c")).setRDotJavaPackage("com.facebook").build());
BuildRule b = ruleResolver.addToIndex(AndroidResourceRuleBuilder.newBuilder().setRuleFinder(ruleFinder).setBuildTarget(BuildTargetFactory.newInstance("//:b")).setRes(new FakeSourcePath("res_b")).setRDotJavaPackage("com.facebook").setDeps(ImmutableSortedSet.of(c)).build());
BuildRule d = ruleResolver.addToIndex(AndroidResourceRuleBuilder.newBuilder().setRuleFinder(ruleFinder).setBuildTarget(BuildTargetFactory.newInstance("//:d")).setRes(new FakeSourcePath("res_d")).setRDotJavaPackage("com.facebook").setDeps(ImmutableSortedSet.of(c)).build());
AndroidResource a = ruleResolver.addToIndex(AndroidResourceRuleBuilder.newBuilder().setRuleFinder(ruleFinder).setBuildTarget(BuildTargetFactory.newInstance("//:a")).setRes(new FakeSourcePath("res_a")).setRDotJavaPackage("com.facebook").setDeps(ImmutableSortedSet.of(b, c, d)).build());
AndroidPackageableCollector collector = new AndroidPackageableCollector(a.getBuildTarget());
collector.addPackageables(ImmutableList.of(a));
// Note that a topological sort for a DAG is not guaranteed to be unique, but we order nodes
// within the same depth of the search.
ImmutableList<BuildTarget> result = ImmutableList.of(a, d, b, c).stream().map(BuildRule::getBuildTarget).collect(MoreCollectors.toImmutableList());
assertEquals("Android resources should be topologically sorted.", result, collector.build().getResourceDetails().getResourcesWithNonEmptyResDir());
// Introduce an AndroidBinaryRule that depends on A and C and verify that the same topological
// sort results. This verifies that both AndroidResourceRule.getAndroidResourceDeps does the
// right thing when it gets a non-AndroidResourceRule as well as an AndroidResourceRule.
BuildTarget keystoreTarget = BuildTargetFactory.newInstance("//keystore:debug");
KeystoreBuilder.createBuilder(keystoreTarget).setStore(new FakeSourcePath("keystore/debug.keystore")).setProperties(new FakeSourcePath("keystore/debug.keystore.properties")).build(ruleResolver);
ImmutableSortedSet<BuildTarget> declaredDepsTargets = ImmutableSortedSet.of(a.getBuildTarget(), c.getBuildTarget());
AndroidBinary androidBinary = AndroidBinaryBuilder.createBuilder(BuildTargetFactory.newInstance("//:e")).setManifest(new FakeSourcePath("AndroidManfiest.xml")).setKeystore(keystoreTarget).setOriginalDeps(declaredDepsTargets).build(ruleResolver);
assertEquals("Android resources should be topologically sorted.", result, androidBinary.getAndroidPackageableCollection().getResourceDetails().getResourcesWithNonEmptyResDir());
}
use of com.facebook.buck.rules.FakeSourcePath in project buck by facebook.
the class AndroidPackageableCollectorTest method testFindTransitiveDependencies.
/**
* This is a regression test to ensure that an additional 1 second startup cost is not
* re-introduced to fb4a.
*/
@Test
public void testFindTransitiveDependencies() throws Exception {
ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
Path prebuiltNativeLibraryPath = Paths.get("java/com/facebook/prebuilt_native_library/libs");
projectFilesystem.mkdirs(prebuiltNativeLibraryPath);
// Create an AndroidBinaryRule that transitively depends on two prebuilt JARs. One of the two
// prebuilt JARs will be listed in the AndroidBinaryRule's no_dx list.
BuildTarget guavaTarget = BuildTargetFactory.newInstance("//third_party/guava:guava");
TargetNode<?, ?> guava = PrebuiltJarBuilder.createBuilder(guavaTarget).setBinaryJar(Paths.get("third_party/guava/guava-10.0.1.jar")).build();
BuildTarget jsr305Target = BuildTargetFactory.newInstance("//third_party/jsr-305:jsr-305");
TargetNode<?, ?> jsr = PrebuiltJarBuilder.createBuilder(jsr305Target).setBinaryJar(Paths.get("third_party/jsr-305/jsr305.jar")).build();
TargetNode<?, ?> ndkLibrary = new NdkLibraryBuilder(BuildTargetFactory.newInstance("//java/com/facebook/native_library:library"), projectFilesystem).build();
BuildTarget prebuiltNativeLibraryTarget = BuildTargetFactory.newInstance("//java/com/facebook/prebuilt_native_library:library");
TargetNode<?, ?> prebuiltNativeLibraryBuild = PrebuiltNativeLibraryBuilder.newBuilder(prebuiltNativeLibraryTarget, projectFilesystem).setNativeLibs(prebuiltNativeLibraryPath).setIsAsset(true).build();
BuildTarget libraryRuleTarget = BuildTargetFactory.newInstance("//java/src/com/facebook:example");
TargetNode<?, ?> library = JavaLibraryBuilder.createBuilder(libraryRuleTarget).setProguardConfig(new FakeSourcePath("debug.pro")).addSrc(Paths.get("Example.java")).addDep(guavaTarget).addDep(jsr305Target).addDep(prebuiltNativeLibraryBuild.getBuildTarget()).addDep(ndkLibrary.getBuildTarget()).build();
BuildTarget manifestTarget = BuildTargetFactory.newInstance("//java/src/com/facebook:res");
TargetNode<?, ?> manifest = AndroidResourceBuilder.createBuilder(manifestTarget).setManifest(new PathSourcePath(projectFilesystem, Paths.get("java/src/com/facebook/module/AndroidManifest.xml"))).setAssets(new FakeSourcePath("assets")).build();
BuildTarget keystoreTarget = BuildTargetFactory.newInstance("//keystore:debug");
TargetNode<?, ?> keystore = KeystoreBuilder.createBuilder(keystoreTarget).setStore(new FakeSourcePath(projectFilesystem, "keystore/debug.keystore")).setProperties(new FakeSourcePath(projectFilesystem, "keystore/debug.keystore.properties")).build();
ImmutableSortedSet<BuildTarget> originalDepsTargets = ImmutableSortedSet.of(libraryRuleTarget, manifestTarget);
BuildTarget binaryTarget = BuildTargetFactory.newInstance("//java/src/com/facebook:app");
TargetNode<?, ?> binary = AndroidBinaryBuilder.createBuilder(binaryTarget).setOriginalDeps(originalDepsTargets).setBuildTargetsToExcludeFromDex(ImmutableSet.of(BuildTargetFactory.newInstance("//third_party/guava:guava"))).setManifest(new FakeSourcePath("java/src/com/facebook/AndroidManifest.xml")).setKeystore(keystoreTarget).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(binary, library, manifest, keystore, ndkLibrary, prebuiltNativeLibraryBuild, guava, jsr);
BuildRuleResolver ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
AndroidBinary binaryRule = (AndroidBinary) ruleResolver.requireRule(binaryTarget);
NdkLibrary ndkLibraryRule = (NdkLibrary) ruleResolver.requireRule(ndkLibrary.getBuildTarget());
NativeLibraryBuildRule prebuildNativeLibraryRule = (NativeLibraryBuildRule) ruleResolver.requireRule(prebuiltNativeLibraryTarget);
// Verify that the correct transitive dependencies are found.
AndroidPackageableCollection packageableCollection = binaryRule.getAndroidPackageableCollection();
assertResolvedEquals("Because guava was passed to no_dx, it should not be in the classpathEntriesToDex list", pathResolver, ImmutableSet.of(ruleResolver.getRule(jsr305Target).getSourcePathToOutput(), ruleResolver.getRule(libraryRuleTarget).getSourcePathToOutput()), packageableCollection.getClasspathEntriesToDex());
assertResolvedEquals("Because guava was passed to no_dx, it should not be treated as a third-party JAR whose " + "resources need to be extracted and repacked in the APK. If this is done, then code " + "in the guava-10.0.1.dex.1.jar in the APK's assets/ tmp may try to load the resource " + "from the APK as a ZipFileEntry rather than as a resource within " + "guava-10.0.1.dex.1.jar. Loading a resource in this way could take substantially " + "longer. Specifically, this was observed to take over one second longer to load " + "the resource in fb4a. Because the resource was loaded on startup, this introduced a " + "substantial regression in the startup time for the fb4a app.", pathResolver, ImmutableSet.of(ruleResolver.getRule(jsr305Target).getSourcePathToOutput()), packageableCollection.getPathsToThirdPartyJars());
assertResolvedEquals("Because assets directory was passed an AndroidResourceRule it should be added to the " + "transitive dependencies", pathResolver, ImmutableSet.of(new DefaultBuildTargetSourcePath(manifestTarget.withAppendedFlavors(AndroidResourceDescription.ASSETS_SYMLINK_TREE_FLAVOR))), packageableCollection.getAssetsDirectories());
assertResolvedEquals("Because a native library was declared as a dependency, it should be added to the " + "transitive dependencies.", pathResolver, ImmutableSet.<SourcePath>of(new PathSourcePath(new FakeProjectFilesystem(), ndkLibraryRule.getLibraryPath())), ImmutableSet.copyOf(packageableCollection.getNativeLibsDirectories().values()));
assertResolvedEquals("Because a prebuilt native library was declared as a dependency (and asset), it should " + "be added to the transitive dependecies.", pathResolver, ImmutableSet.<SourcePath>of(new PathSourcePath(new FakeProjectFilesystem(), prebuildNativeLibraryRule.getLibraryPath())), ImmutableSet.copyOf(packageableCollection.getNativeLibAssetsDirectories().values()));
assertEquals(ImmutableSet.of(new FakeSourcePath("debug.pro")), packageableCollection.getProguardConfigs());
}
use of com.facebook.buck.rules.FakeSourcePath in project buck by facebook.
the class AndroidResourceDescriptionTest method testNonAssetFilesAndDirsAreIgnored.
@Test
public void testNonAssetFilesAndDirsAreIgnored() throws IOException {
tmpFolder.newFolder("res");
tmpFolder.newFile("res/image.png");
tmpFolder.newFile("res/layout.xml");
tmpFolder.newFile("res/_file");
tmpFolder.newFile("res/.gitkeep");
tmpFolder.newFile("res/.svn");
tmpFolder.newFile("res/.git");
tmpFolder.newFile("res/.ds_store");
tmpFolder.newFile("res/.scc");
tmpFolder.newFile("res/CVS");
tmpFolder.newFile("res/thumbs.db");
tmpFolder.newFile("res/picasa.ini");
tmpFolder.newFile("res/file.bak~");
tmpFolder.newFolder("res", "dirs", "values");
tmpFolder.newFile("res/dirs/values/strings.xml");
tmpFolder.newFile("res/dirs/values/strings.xml.orig");
tmpFolder.newFolder("res", "dirs", ".gitkeep");
tmpFolder.newFile("res/dirs/.gitkeep/ignore");
tmpFolder.newFolder("res", "dirs", ".svn");
tmpFolder.newFile("res/dirs/.svn/ignore");
tmpFolder.newFolder("res", "dirs", ".git");
tmpFolder.newFile("res/dirs/.git/ignore");
tmpFolder.newFolder("res", "dirs", ".ds_store");
tmpFolder.newFile("res/dirs/.ds_store/ignore");
tmpFolder.newFolder("res", "dirs", ".scc");
tmpFolder.newFile("res/dirs/.scc/ignore");
tmpFolder.newFolder("res", "dirs", "CVS");
tmpFolder.newFile("res/dirs/CVS/ignore");
tmpFolder.newFolder("res", "dirs", "thumbs.db");
tmpFolder.newFile("res/dirs/thumbs.db/ignore");
tmpFolder.newFolder("res", "dirs", "picasa.ini");
tmpFolder.newFile("res/dirs/picasa.ini/ignore");
tmpFolder.newFolder("res", "dirs", "file.bak~");
tmpFolder.newFile("res/dirs/file.bak~/ignore");
tmpFolder.newFolder("res", "dirs", "_dir");
tmpFolder.newFile("res/dirs/_dir/ignore");
AndroidResourceDescription description = new AndroidResourceDescription(false);
ProjectFilesystem filesystem = new ProjectFilesystem(tmpFolder.getRoot().toPath());
Map<Path, SourcePath> inputs = description.collectInputFiles(filesystem, Paths.get("res"));
assertThat(inputs, equalTo(ImmutableMap.<Path, SourcePath>of(Paths.get("image.png"), new FakeSourcePath(filesystem, "res/image.png"), Paths.get("layout.xml"), new FakeSourcePath(filesystem, "res/layout.xml"), Paths.get("_file"), new FakeSourcePath(filesystem, "res/_file"), Paths.get("dirs/values/strings.xml"), new FakeSourcePath(filesystem, "res/dirs/values/strings.xml"))));
}
Aggregations