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);
}
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))));
}
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"));
}
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)));
}
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());
}
Aggregations