Search in sources :

Example 6 with FakeJavaLibrary

use of com.facebook.buck.jvm.java.FakeJavaLibrary in project buck by facebook.

the class DexWithClassesTest method testIntermediateDexRuleToDexWithClasses.

@Test
public void testIntermediateDexRuleToDexWithClasses() {
    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("com/example/Main", HashCode.fromString(Strings.repeat("cafebabe", 5))), Optional.empty()));
    DexWithClasses dexWithClasses = DexWithClasses.TO_DEX_WITH_CLASSES.apply(dexFromJavaLibrary);
    assertEquals(BuildTargets.getGenPath(javaLibrary.getProjectFilesystem(), buildTarget, "%s.dex.jar"), dexWithClasses.getPathToDexFile());
    assertEquals(ImmutableSet.of("com/example/Main"), dexWithClasses.getClassNames());
    assertEquals(1600, dexWithClasses.getWeightEstimate());
}
Also used : JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 7 with FakeJavaLibrary

use of com.facebook.buck.jvm.java.FakeJavaLibrary in project buck by facebook.

the class TrimUberRDotJavaTest method doTrimingTest.

private void doTrimingTest(Optional<String> keepResourcePattern, String rDotJavaContentsAfterFiltering) throws IOException, InterruptedException {
    ProjectFilesystem filesystem = new ProjectFilesystem(tmpFolder.getRoot());
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    AaptPackageResources aaptPackageResources = new AaptPackageResources(new FakeBuildRuleParamsBuilder(BuildTargetFactory.newInstance("//:aapt")).setProjectFilesystem(filesystem).build(), ruleFinder, resolver, null, new IdentityResourcesProvider(ImmutableList.of()), ImmutableList.of(), ImmutableSortedSet.of(), ImmutableSet.of(), Optional.empty(), false, false, /* includesVectorDrawables */
    false, EnumSet.noneOf(RType.class), null);
    resolver.addToIndex(aaptPackageResources);
    String rDotJavaContents = "package com.test;\n" + "\n" + "public class R {\n" + "  public static class string {\n" + "    public static final int my_first_resource=0x7f08005c;\n" + "    public static final int my_second_resource=0x7f083bc1;\n" + "    public static final int keep_resource=0x7f083bc2;\n" + "  }\n" + "}\n";
    Path rDotJavaPath = aaptPackageResources.getPathToGeneratedRDotJavaSrcFiles().resolve("com/test/R.java");
    filesystem.createParentDirs(rDotJavaPath);
    filesystem.writeContentsToPath(rDotJavaContents, rDotJavaPath);
    DexProducedFromJavaLibrary dexProducedFromJavaLibrary = new DexProducedFromJavaLibrary(new FakeBuildRuleParamsBuilder(BuildTargetFactory.newInstance("//:dex")).setProjectFilesystem(filesystem).build(), new FakeJavaLibrary(BuildTargetFactory.newInstance("//:lib"), null));
    dexProducedFromJavaLibrary.getBuildOutputInitializer().setBuildOutput(dexProducedFromJavaLibrary.initializeFromDisk(new FakeOnDiskBuildInfo().putMetadata(DexProducedFromJavaLibrary.WEIGHT_ESTIMATE, "1").putMetadata(DexProducedFromJavaLibrary.CLASSNAMES_TO_HASHES, "{}").putMetadata(DexProducedFromJavaLibrary.REFERENCED_RESOURCES, ImmutableList.of("com.test.my_first_resource"))));
    resolver.addToIndex(dexProducedFromJavaLibrary);
    TrimUberRDotJava trimUberRDotJava = new TrimUberRDotJava(new FakeBuildRuleParamsBuilder(BuildTargetFactory.newInstance("//:trim")).setProjectFilesystem(filesystem).build(), aaptPackageResources, ImmutableList.of(dexProducedFromJavaLibrary), keepResourcePattern);
    resolver.addToIndex(trimUberRDotJava);
    BuildContext buildContext = FakeBuildContext.withSourcePathResolver(pathResolver);
    BuildableContext buildableContext = new FakeBuildableContext();
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    ImmutableList<Step> steps = trimUberRDotJava.getBuildSteps(buildContext, buildableContext);
    for (Step step : steps) {
        step.execute(executionContext);
    }
    ZipInspector inspector = new ZipInspector(pathResolver.getAbsolutePath(trimUberRDotJava.getSourcePathToOutput()));
    inspector.assertFileContents("com/test/R.java", rDotJavaContentsAfterFiltering);
}
Also used : Path(java.nio.file.Path) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) RType(com.facebook.buck.android.aapt.RDotTxtEntry.RType) ZipInspector(com.facebook.buck.testutil.integration.ZipInspector) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) Step(com.facebook.buck.step.Step) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) BuildableContext(com.facebook.buck.rules.BuildableContext) FakeOnDiskBuildInfo(com.facebook.buck.rules.FakeOnDiskBuildInfo) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)

Aggregations

FakeJavaLibrary (com.facebook.buck.jvm.java.FakeJavaLibrary)7 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)7 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)7 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)7 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)7 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)5 Test (org.junit.Test)5 BuildTarget (com.facebook.buck.model.BuildTarget)4 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)4 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)2 JavaLibrary (com.facebook.buck.jvm.java.JavaLibrary)2 BuildContext (com.facebook.buck.rules.BuildContext)2 BuildRule (com.facebook.buck.rules.BuildRule)2 FakeBuildContext (com.facebook.buck.rules.FakeBuildContext)2 FakeBuildableContext (com.facebook.buck.rules.FakeBuildableContext)2 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)2 ExecutionContext (com.facebook.buck.step.ExecutionContext)2 Step (com.facebook.buck.step.Step)2 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)2 Path (java.nio.file.Path)2