use of com.facebook.buck.rules.FakeBuildRuleParamsBuilder in project buck by facebook.
the class DexWithClassesTest method testIntermediateDexRuleToDexWithClassesWhenIntermediateDexHasNoClasses.
@Test
public void testIntermediateDexRuleToDexWithClassesWhenIntermediateDexHasNoClasses() {
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(), Optional.empty()));
DexWithClasses dexWithClasses = DexWithClasses.TO_DEX_WITH_CLASSES.apply(dexFromJavaLibrary);
assertNull("If the JavaLibraryRule does not produce any .class files, " + "then DexWithClasses.TO_DEX_WITH_CLASSES should return null.", dexWithClasses);
}
use of com.facebook.buck.rules.FakeBuildRuleParamsBuilder 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());
}
use of com.facebook.buck.rules.FakeBuildRuleParamsBuilder 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());
}
use of com.facebook.buck.rules.FakeBuildRuleParamsBuilder in project buck by facebook.
the class GenAidlTest method testSimpleGenAidlRule.
@Test
public void testSimpleGenAidlRule() throws IOException {
ProjectFilesystem stubFilesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
Files.createDirectories(stubFilesystem.getRootPath().resolve("java/com/example/base"));
FakeSourcePath pathToAidl = new FakeSourcePath(stubFilesystem, "java/com/example/base/IWhateverService.aidl");
String importPath = Paths.get("java/com/example/base").toString();
BuildTarget target = BuildTargetFactory.newInstance(stubFilesystem.getRootPath(), "//java/com/example/base:IWhateverService");
BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).setProjectFilesystem(stubFilesystem).build();
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
GenAidl genAidlRule = new GenAidl(params, pathToAidl, importPath);
GenAidlDescription description = new GenAidlDescription();
assertEquals(Description.getBuildRuleType(GenAidlDescription.class), Description.getBuildRuleType(description));
assertTrue(genAidlRule.getProperties().is(ANDROID));
List<Step> steps = genAidlRule.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), new FakeBuildableContext());
final String pathToAidlExecutable = Paths.get("/usr/local/bin/aidl").toString();
final String pathToFrameworkAidl = Paths.get("/home/root/android/platforms/android-16/framework.aidl").toString();
final AndroidPlatformTarget androidPlatformTarget = createMock(AndroidPlatformTarget.class);
expect(androidPlatformTarget.getAidlExecutable()).andReturn(Paths.get(pathToAidlExecutable));
expect(androidPlatformTarget.getAndroidFrameworkIdlFile()).andReturn(Paths.get(pathToFrameworkAidl));
replay(androidPlatformTarget);
ExecutionContext executionContext = TestExecutionContext.newBuilder().setAndroidPlatformTargetSupplier(Suppliers.ofInstance(androidPlatformTarget)).build();
assertEquals(executionContext.getAndroidPlatformTarget(), androidPlatformTarget);
Path outputDirectory = BuildTargets.getScratchPath(stubFilesystem, target, "__%s.aidl");
MakeCleanDirectoryStep mkdirStep = (MakeCleanDirectoryStep) steps.get(1);
assertEquals("gen_aidl() should make a directory at " + outputDirectory, outputDirectory, mkdirStep.getPath());
ShellStep aidlStep = (ShellStep) steps.get(2);
assertEquals("gen_aidl() should use the aidl binary to write .java files.", String.format("(cd %s && %s -p%s -I%s -o%s %s)", stubFilesystem.getRootPath(), pathToAidlExecutable, pathToFrameworkAidl, stubFilesystem.resolve(importPath), stubFilesystem.resolve(outputDirectory), pathToAidl.getRelativePath()), aidlStep.getDescription(executionContext));
assertEquals(5, steps.size());
verify(androidPlatformTarget);
}
use of com.facebook.buck.rules.FakeBuildRuleParamsBuilder in project buck by facebook.
the class ApkGenruleTest method testCreateAndRunApkGenrule.
@Test
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
public void testCreateAndRunApkGenrule() throws IOException, NoSuchBuildTargetException {
ProjectFilesystem projectFilesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
FileSystem fileSystem = projectFilesystem.getRootPath().getFileSystem();
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
createSampleAndroidBinaryRule(ruleResolver, projectFilesystem);
// From the Python object, create a ApkGenruleBuildRuleFactory to create a ApkGenrule.Builder
// that builds a ApkGenrule from the Python object.
BuildTargetParser parser = EasyMock.createNiceMock(BuildTargetParser.class);
final BuildTarget apkTarget = BuildTargetFactory.newInstance(projectFilesystem.getRootPath(), "//:fb4a");
EasyMock.expect(parser.parse(EasyMock.eq(":fb4a"), EasyMock.anyObject(BuildTargetPatternParser.class), EasyMock.anyObject())).andStubReturn(apkTarget);
EasyMock.replay(parser);
BuildTarget buildTarget = BuildTargetFactory.newInstance(projectFilesystem.getRootPath(), "//src/com/facebook:sign_fb4a");
ApkGenruleDescription description = new ApkGenruleDescription();
ApkGenruleDescription.Arg arg = description.createUnpopulatedConstructorArg();
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
arg.apk = new FakeInstallable(apkTarget, pathResolver).getBuildTarget();
arg.bash = Optional.of("");
arg.cmd = Optional.of("python signer.py $APK key.properties > $OUT");
arg.cmdExe = Optional.of("");
arg.type = Optional.empty();
arg.out = "signed_fb4a.apk";
arg.srcs = ImmutableList.of(new PathSourcePath(projectFilesystem, fileSystem.getPath("src/com/facebook/signer.py")), new PathSourcePath(projectFilesystem, fileSystem.getPath("src/com/facebook/key.properties")));
arg.tests = ImmutableSortedSet.of();
BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(projectFilesystem).build();
ApkGenrule apkGenrule = (ApkGenrule) description.createBuildRule(TargetGraph.EMPTY, params, ruleResolver, arg);
ruleResolver.addToIndex(apkGenrule);
// Verify all of the observers of the Genrule.
String expectedApkOutput = projectFilesystem.resolve(projectFilesystem.getBuckPaths().getGenDir().toString() + "/src/com/facebook/sign_fb4a/sign_fb4a.apk").toString();
assertEquals(expectedApkOutput, apkGenrule.getAbsoluteOutputFilePath(pathResolver));
assertEquals("The apk that this rule is modifying must have the apk in its deps.", ImmutableSet.of(apkTarget.toString()), apkGenrule.getDeps().stream().map(Object::toString).collect(MoreCollectors.toImmutableSet()));
BuildContext buildContext = FakeBuildContext.withSourcePathResolver(pathResolver);
Iterable<Path> expectedInputsToCompareToOutputs = ImmutableList.of(fileSystem.getPath("src/com/facebook/signer.py"), fileSystem.getPath("src/com/facebook/key.properties"));
MoreAsserts.assertIterablesEquals(expectedInputsToCompareToOutputs, pathResolver.filterInputsToCompareToOutput(apkGenrule.getSrcs()));
// Verify that the shell commands that the genrule produces are correct.
List<Step> steps = apkGenrule.getBuildSteps(buildContext, new FakeBuildableContext());
assertEquals(6, steps.size());
ExecutionContext executionContext = newEmptyExecutionContext();
Step firstStep = steps.get(0);
assertTrue(firstStep instanceof MakeCleanDirectoryStep);
MakeCleanDirectoryStep mkdirCommand = (MakeCleanDirectoryStep) firstStep;
Path mkdirDir = projectFilesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/sign_fb4a");
assertEquals("First command should make sure the output directory exists and is empty.", mkdirDir, mkdirCommand.getPath());
Step secondStep = steps.get(1);
assertTrue(secondStep instanceof MakeCleanDirectoryStep);
MakeCleanDirectoryStep secondMkdirCommand = (MakeCleanDirectoryStep) secondStep;
Path relativePathToTmpDir = projectFilesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/sign_fb4a__tmp");
assertEquals("Second command should make sure the temp directory exists.", relativePathToTmpDir, secondMkdirCommand.getPath());
Step thirdStep = steps.get(2);
assertTrue(thirdStep instanceof MakeCleanDirectoryStep);
MakeCleanDirectoryStep thirdMkdirCommand = (MakeCleanDirectoryStep) thirdStep;
Path relativePathToSrcDir = projectFilesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/sign_fb4a__srcs");
assertEquals("Third command should make sure the temp directory exists.", relativePathToSrcDir, thirdMkdirCommand.getPath());
MkdirAndSymlinkFileStep linkSource1 = (MkdirAndSymlinkFileStep) steps.get(3);
assertEquals(fileSystem.getPath("src/com/facebook/signer.py"), linkSource1.getSource());
assertEquals(fileSystem.getPath(relativePathToSrcDir + "/signer.py"), linkSource1.getTarget());
MkdirAndSymlinkFileStep linkSource2 = (MkdirAndSymlinkFileStep) steps.get(4);
assertEquals(fileSystem.getPath("src/com/facebook/key.properties"), linkSource2.getSource());
assertEquals(fileSystem.getPath(relativePathToSrcDir + "/key.properties"), linkSource2.getTarget());
Step sixthStep = steps.get(5);
assertTrue(sixthStep instanceof AbstractGenruleStep);
AbstractGenruleStep genruleCommand = (AbstractGenruleStep) sixthStep;
assertEquals("genrule", genruleCommand.getShortName());
ImmutableMap<String, String> environmentVariables = genruleCommand.getEnvironmentVariables(executionContext);
assertEquals(new ImmutableMap.Builder<String, String>().put("APK", projectFilesystem.resolve(BuildTargets.getGenPath(projectFilesystem, apkTarget, "%s.apk")).toString()).put("OUT", expectedApkOutput).build(), environmentVariables);
Path scriptFilePath = genruleCommand.getScriptFilePath(executionContext);
String scriptFileContents = genruleCommand.getScriptFileContents(executionContext);
assertEquals(ImmutableList.of("/bin/bash", "-e", scriptFilePath.toString()), genruleCommand.getShellCommand(executionContext));
assertEquals("python signer.py $APK key.properties > $OUT", scriptFileContents);
EasyMock.verify(parser);
}
Aggregations