Search in sources :

Example 41 with FakeBuildableContext

use of com.facebook.buck.rules.FakeBuildableContext 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)

Example 42 with FakeBuildableContext

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

the class MergeAndroidResourcesSourcesTest method testRuleStepCreation.

@Test
@SuppressWarnings("unchecked")
public void testRuleStepCreation() throws IOException, InterruptedException {
    BuildRuleParams buildRuleParams = new FakeBuildRuleParamsBuilder("//:output_folder").setProjectFilesystem(filesystem).build();
    ImmutableList<SourcePath> directories = ImmutableList.of(new FakeSourcePath(filesystem, "res_in_1"), new FakeSourcePath(filesystem, "res_in_2"));
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    MergeAndroidResourceSources mergeAndroidResourceSourcesStep = new MergeAndroidResourceSources(buildRuleParams, directories);
    ImmutableList<Step> steps = mergeAndroidResourceSourcesStep.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), new FakeBuildableContext());
    assertThat(steps, Matchers.contains(Matchers.hasProperty("shortName", Matchers.equalTo("rm_&&_mkdir")), Matchers.instanceOf(MergeAndroidResourceSourcesStep.class)));
    String resIn1 = filesystem.getRootPath().resolve("res_in_1").toString();
    String resIn2 = filesystem.getRootPath().resolve("res_in_2").toString();
    assertThat(FluentIterable.from(steps).transform(stepDescriptionFunction), Matchers.contains(Matchers.stringContainsInOrder("rm", "mkdir"), Matchers.startsWith(String.format("merge-resources %s,%s -> ", resIn1, resIn2))));
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) Step(com.facebook.buck.step.Step) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 43 with FakeBuildableContext

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

the class HalideLibraryDescriptionTest method extraCompilerFlags.

@Test
public void extraCompilerFlags() throws Exception {
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    ImmutableList<String> extraCompilerFlags = ImmutableList.<String>builder().add("--test-flag").add("test-value").add("$TEST_MACRO").build();
    // Set up a #halide-compile rule, then check its build steps.
    BuildTarget compileTarget = BuildTargetFactory.newInstance("//:rule").withFlavors(HalideLibraryDescription.HALIDE_COMPILE_FLAVOR);
    HalideLibraryBuilder compileBuilder = new HalideLibraryBuilder(compileTarget);
    compileBuilder.setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("main.cpp"))));
    // First, make sure the compile step doesn't include the extra flags.
    TargetGraph targetGraph = TargetGraphFactory.newInstance(compileBuilder.build());
    BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    HalideCompile compile = (HalideCompile) compileBuilder.build(resolver, filesystem, targetGraph);
    ImmutableList<Step> buildSteps = compile.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), new FakeBuildableContext());
    HalideCompilerStep compilerStep = (HalideCompilerStep) buildSteps.get(1);
    ImmutableList<String> shellCommand = compilerStep.getShellCommandInternal(TestExecutionContext.newInstance());
    assertThat(shellCommand, not(hasItems("--test-flag", "test-value")));
    // Next verify that the shell command picks up on the extra compiler flags.
    compileBuilder.setCompilerInvocationFlags(extraCompilerFlags);
    targetGraph = TargetGraphFactory.newInstance(compileBuilder.build());
    resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    compile = (HalideCompile) compileBuilder.build(resolver, filesystem, targetGraph);
    buildSteps = compile.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), new FakeBuildableContext());
    compilerStep = (HalideCompilerStep) buildSteps.get(1);
    shellCommand = compilerStep.getShellCommandInternal(TestExecutionContext.newInstance());
    assertThat(shellCommand, hasItems("--test-flag", "test-value", "test_macro_expansion"));
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) TargetGraph(com.facebook.buck.rules.TargetGraph) Step(com.facebook.buck.step.Step) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) 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 44 with FakeBuildableContext

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

the class HalideLibraryDescriptionTest method functionNameOverride.

@Test
public void functionNameOverride() throws Exception {
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    // Set up a #halide-compile rule, then check its build steps.
    String defaultName = "default-halide-name";
    BuildTarget compileTarget = BuildTargetFactory.newInstance("//:" + defaultName).withFlavors(HalideLibraryDescription.HALIDE_COMPILE_FLAVOR);
    HalideLibraryBuilder compileBuilder = new HalideLibraryBuilder(compileTarget);
    compileBuilder.setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("main.cpp"))));
    // First, make sure the compile step passes the rulename "default-halide-name"
    // for the function output name.
    TargetGraph targetGraph = TargetGraphFactory.newInstance(compileBuilder.build());
    BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    HalideCompile compile = (HalideCompile) compileBuilder.build(resolver, filesystem, targetGraph);
    ImmutableList<Step> buildSteps = compile.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), new FakeBuildableContext());
    HalideCompilerStep compilerStep = (HalideCompilerStep) buildSteps.get(1);
    ImmutableList<String> shellCommand = compilerStep.getShellCommandInternal(TestExecutionContext.newInstance());
    assertThat(shellCommand, hasItem(defaultName));
    // Next verify that the shell command picks up on the override name.
    String overrideName = "override-halide-name";
    compileBuilder.setFunctionNameOverride(overrideName);
    targetGraph = TargetGraphFactory.newInstance(compileBuilder.build());
    resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    compile = (HalideCompile) compileBuilder.build(resolver, filesystem, targetGraph);
    buildSteps = compile.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), new FakeBuildableContext());
    compilerStep = (HalideCompilerStep) buildSteps.get(1);
    shellCommand = compilerStep.getShellCommandInternal(TestExecutionContext.newInstance());
    assertThat(shellCommand, hasItem(overrideName));
    assertThat(shellCommand, not(hasItem(defaultName)));
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) TargetGraph(com.facebook.buck.rules.TargetGraph) Step(com.facebook.buck.step.Step) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) 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 45 with FakeBuildableContext

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

the class RemoteFileTest method runTheMagic.

private Path runTheMagic(@Nullable Downloader downloader, final byte[] contentsOfFile, HashCode hashCode, RemoteFile.Type type) throws Exception {
    if (downloader == null) {
        downloader = (eventBus, uri, output) -> {
            Files.createDirectories(output.getParent());
            Files.write(output, contentsOfFile);
            return true;
        };
    }
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot().toPath().toAbsolutePath());
    BuildRuleParams params = new FakeBuildRuleParamsBuilder("//cake:walk").setProjectFilesystem(filesystem).build();
    RemoteFile remoteFile = new RemoteFile(params, downloader, new URI("http://example.com"), hashCode, "output.txt", type);
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    resolver.addToIndex(remoteFile);
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    ImmutableList<Step> buildSteps = remoteFile.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), new FakeBuildableContext());
    ExecutionContext context = TestExecutionContext.newInstance();
    for (Step buildStep : buildSteps) {
        int result = buildStep.execute(context).getExitCode();
        if (result != 0) {
            break;
        }
    }
    return pathResolver.getAbsolutePath(remoteFile.getSourcePathToOutput());
}
Also used : FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) Step(com.facebook.buck.step.Step) URI(java.net.URI) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)

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