Search in sources :

Example 26 with FakeBuildableContext

use of com.facebook.buck.rules.FakeBuildableContext in project buck by facebook.

the class CxxLibraryDescriptionTest method libraryWithoutSourcesDoesntBuildAnything.

@Test
public void libraryWithoutSourcesDoesntBuildAnything() throws Exception {
    BuildTarget target = BuildTargetFactory.newInstance("//foo:bar").withFlavors(CxxDescriptionEnhancer.STATIC_FLAVOR, CxxLibraryBuilder.createDefaultPlatform().getFlavor());
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    CxxLibraryBuilder libBuilder = new CxxLibraryBuilder(target, cxxBuckConfig);
    TargetGraph targetGraph = TargetGraphFactory.newInstance(libBuilder.build());
    BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    BuildRule lib = libBuilder.build(resolver, filesystem, targetGraph);
    assertThat(lib.getDeps(), is(empty()));
    assertThat(lib.getBuildSteps(FakeBuildContext.NOOP_CONTEXT, new FakeBuildableContext()), is(empty()));
}
Also used : FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) TargetGraph(com.facebook.buck.rules.TargetGraph) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 27 with FakeBuildableContext

use of com.facebook.buck.rules.FakeBuildableContext in project buck by facebook.

the class JsBundleWorkerJobArgsTest method setUp.

@Before
public void setUp() throws NoSuchBuildTargetException {
    scenario = JsTestScenario.builder().build();
    context = FakeBuildContext.withSourcePathResolver(new SourcePathResolver(new SourcePathRuleFinder(scenario.resolver)));
    fakeBuildableContext = new FakeBuildableContext();
}
Also used : FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) Before(org.junit.Before)

Example 28 with FakeBuildableContext

use of com.facebook.buck.rules.FakeBuildableContext 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");
}
Also used : Path(java.nio.file.Path) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) EstimateDexWeightStep(com.facebook.buck.dalvik.EstimateDexWeightStep) Step(com.facebook.buck.step.Step) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) HashCode(com.google.common.hash.HashCode) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) EstimateDexWeightStep(com.facebook.buck.dalvik.EstimateDexWeightStep) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 29 with FakeBuildableContext

use of com.facebook.buck.rules.FakeBuildableContext in project buck by facebook.

the class DexProducedFromJavaLibraryThatContainsClassFilesTest method testGetBuildStepsWhenThereAreNoClassesToDex.

@Test
public void testGetBuildStepsWhenThereAreNoClassesToDex() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    DefaultJavaLibrary javaLibrary = JavaLibraryBuilder.createBuilder("//foo:bar").build(resolver);
    javaLibrary.getBuildOutputInitializer().setBuildOutput(new JavaLibrary.Data(ImmutableSortedMap.of()));
    BuildContext context = FakeBuildContext.NOOP_CONTEXT;
    FakeBuildableContext buildableContext = new FakeBuildableContext();
    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//foo:bar#dex");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(projectFilesystem).build();
    DexProducedFromJavaLibrary preDex = new DexProducedFromJavaLibrary(params, javaLibrary);
    List<Step> steps = preDex.getBuildSteps(context, buildableContext);
    Path dexOutput = BuildTargets.getGenPath(projectFilesystem, buildTarget, "%s.dex.jar");
    ExecutionContext executionContext = TestExecutionContext.newBuilder().build();
    MoreAsserts.assertSteps("Do not generate a .dex.jar file.", ImmutableList.of(String.format("rm -f %s", projectFilesystem.resolve(dexOutput)), String.format("mkdir -p %s", projectFilesystem.resolve(dexOutput.getParent())), "record_empty_dx"), steps, executionContext);
    Step recordArtifactAndMetadataStep = steps.get(2);
    assertThat(recordArtifactAndMetadataStep.getShortName(), startsWith("record_"));
    int exitCode = recordArtifactAndMetadataStep.execute(executionContext).getExitCode();
    assertEquals(0, exitCode);
}
Also used : Path(java.nio.file.Path) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) EstimateDexWeightStep(com.facebook.buck.dalvik.EstimateDexWeightStep) Step(com.facebook.buck.step.Step) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 30 with FakeBuildableContext

use of com.facebook.buck.rules.FakeBuildableContext in project buck by facebook.

the class DummyRDotJavaTest method testBuildSteps.

@Test
public void testBuildSteps() throws IOException {
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    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());
    setAndroidResourceBuildOutput(resourceRule1);
    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());
    setAndroidResourceBuildOutput(resourceRule2);
    DummyRDotJava dummyRDotJava = new DummyRDotJava(new FakeBuildRuleParamsBuilder(BuildTargetFactory.newInstance("//java/base:rule")).setProjectFilesystem(filesystem).build(), ruleFinder, ImmutableSet.of((HasAndroidResourceDeps) resourceRule1, (HasAndroidResourceDeps) resourceRule2), ANDROID_JAVAC_OPTIONS, /* forceFinalResourceIds */
    false, Optional.empty(), Optional.of("R2"), false);
    FakeBuildableContext buildableContext = new FakeBuildableContext();
    List<Step> steps = dummyRDotJava.getBuildSteps(FakeBuildContext.NOOP_CONTEXT, buildableContext);
    assertEquals("DummyRDotJava returns an incorrect number of Steps.", 10, steps.size());
    String rDotJavaSrcFolder = BuildTargets.getScratchPath(filesystem, dummyRDotJava.getBuildTarget(), "__%s_rdotjava_src__").toString();
    String rDotJavaBinFolder = BuildTargets.getScratchPath(filesystem, dummyRDotJava.getBuildTarget(), "__%s_rdotjava_bin__").toString();
    String rDotJavaAbiFolder = BuildTargets.getGenPath(filesystem, dummyRDotJava.getBuildTarget(), "__%s_dummyrdotjava_abi__").toString();
    String rDotJavaOutputFolder = BuildTargets.getGenPath(filesystem, dummyRDotJava.getBuildTarget(), "__%s_dummyrdotjava_output__").toString();
    String rDotJavaOutputJar = MorePaths.pathWithPlatformSeparators(String.format("%s/%s.jar", rDotJavaOutputFolder, dummyRDotJava.getBuildTarget().getShortNameAndFlavorPostfix()));
    String genFolder = Paths.get("buck-out/gen/java/base/").toString();
    List<String> sortedSymbolsFiles = Stream.of((AndroidResource) resourceRule1, (AndroidResource) resourceRule2).map(Object::toString).collect(MoreCollectors.toImmutableList());
    ImmutableSortedSet<Path> javaSourceFiles = ImmutableSortedSet.of(Paths.get(rDotJavaSrcFolder).resolve("com/facebook/R.java"));
    List<String> expectedStepDescriptions = Lists.newArrayList(makeCleanDirDescription(filesystem.resolve(rDotJavaSrcFolder)), "android-res-merge " + Joiner.on(' ').join(sortedSymbolsFiles), "android-res-merge " + Joiner.on(' ').join(sortedSymbolsFiles), makeCleanDirDescription(filesystem.resolve(rDotJavaBinFolder)), makeCleanDirDescription(filesystem.resolve(rDotJavaAbiFolder)), makeCleanDirDescription(filesystem.resolve(rDotJavaOutputFolder)), String.format("mkdir -p %s", filesystem.resolve(genFolder)), RDotJava.createJavacStepForDummyRDotJavaFiles(javaSourceFiles, BuildTargets.getGenPath(filesystem, dummyRDotJava.getBuildTarget(), "__%s__srcs"), Paths.get(rDotJavaBinFolder), ANDROID_JAVAC_OPTIONS, /* buildTarget */
    null, pathResolver, ruleFinder, new FakeProjectFilesystem()).getDescription(TestExecutionContext.newInstance()), String.format("jar cf %s  %s", rDotJavaOutputJar, rDotJavaBinFolder), String.format("calculate_abi %s", rDotJavaBinFolder));
    MoreAsserts.assertSteps("DummyRDotJava.getBuildSteps() must return these exact steps.", expectedStepDescriptions, steps, TestExecutionContext.newInstance());
    assertEquals(ImmutableSet.of(Paths.get(rDotJavaBinFolder), Paths.get(rDotJavaOutputJar)), buildableContext.getRecordedArtifacts());
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) Step(com.facebook.buck.step.Step) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Aggregations

FakeBuildableContext (com.facebook.buck.rules.FakeBuildableContext)48 Test (org.junit.Test)43 Step (com.facebook.buck.step.Step)42 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)33 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)32 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)29 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)28 BuildTarget (com.facebook.buck.model.BuildTarget)26 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)20 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)20 BuildContext (com.facebook.buck.rules.BuildContext)18 FakeBuildContext (com.facebook.buck.rules.FakeBuildContext)18 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)17 BuildRule (com.facebook.buck.rules.BuildRule)15 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)14 Path (java.nio.file.Path)14 ExecutionContext (com.facebook.buck.step.ExecutionContext)11 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)11 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)10 PathSourcePath (com.facebook.buck.rules.PathSourcePath)6