@Test public void replaceLocationOfFullyQualifiedBuildTarget() { ProjectFilesystem filesystem = EasyMock.createNiceMock(ProjectFilesystem.class); EasyMock.expect(filesystem.getPathRelativizer()).andStubReturn(relativeToAbsolutePathFunction); EasyMock.replay(filesystem); BuildRuleResolver ruleResolver = new BuildRuleResolver(); JavaBinaryRule javaBinary = createSampleJavaBinaryRule(ruleResolver); String originalCmd = String.format( "$(location :%s) $(location %s) $OUT", javaBinary.getBuildTarget().getShortName(), javaBinary.getBuildTarget().getFullyQualifiedName()); String contextBasePath = javaBinary.getBuildTarget().getBasePath(); Set<? extends BuildRule> deps = ImmutableSet.of(javaBinary); Genrule rule = createGenrule(ruleResolver, originalCmd, contextBasePath, deps); AbstractGenruleStep genruleStep = rule.createGenruleStep(); // Interpolate the build target in the genrule cmd string. String transformedString = genruleStep.replaceMatches(filesystem, originalCmd); // Verify that the correct cmd was created. Path pathToOutput = getAbsolutePathInBase(GEN_DIR + "/java/com/facebook/util/ManifestGenerator.jar"); String expectedCmd = String.format("%s %s $OUT", pathToOutput, pathToOutput); assertEquals(expectedCmd, transformedString); EasyMock.verify(filesystem); }
@Test public void testDepsGenrule() { BuildRuleResolver ruleResolver = new BuildRuleResolver(); JavaBinaryRule javaBinary = createSampleJavaBinaryRule(ruleResolver); // Interpolate the build target in the genrule cmd string. String originalCmd = "$(exe :ManifestGenerator) $OUT"; Set<? extends BuildRule> deps = ImmutableSet.of(javaBinary); String contextBasePath = "java/com/facebook/util"; Genrule rule = createGenrule(ruleResolver, originalCmd, contextBasePath, deps); AbstractGenruleStep genruleStep = rule.createGenruleStep(); String transformedString = genruleStep.replaceMatches(fakeFilesystem, originalCmd); // Verify that the correct cmd was created. Path expectedClasspath = getAbsolutePathInBase(GEN_DIR + "/java/com/facebook/util/lib__util__output/util.jar"); String expectedCmd = String.format( "java -classpath %s com.facebook.util.ManifestGenerator $OUT", expectedClasspath); assertEquals(expectedCmd, transformedString); }
@Test public void testDepsEnvironmentVariableIsComplete() { BuildTarget depTarget = new BuildTarget("//foo", "bar"); BuildRule dep = new FakeBuildRule(BuildRuleType.JAVA_LIBRARY, depTarget) { @Override public String getPathToOutputFile() { return "buck-out/gen/foo/bar.jar"; } }; BuildRuleResolver ruleResolver = new BuildRuleResolver(ImmutableMap.of(depTarget, dep)); AbstractBuildRuleBuilderParams params = new FakeAbstractBuildRuleBuilderParams(); Builder builder = Genrule.newGenruleBuilder(params); builder.setBuildTarget(new BuildTarget("//foo", "baz")); builder.setBash(Optional.of("cat $DEPS > $OUT")); builder.setOut("deps.txt"); builder.addDep(depTarget); Genrule genrule = builder.build(ruleResolver); AbstractGenruleStep genruleStep = genrule.createGenruleStep(); ExecutionContext context = newEmptyExecutionContext(Platform.LINUX); Map<String, String> environmentVariables = genruleStep.getEnvironmentVariables(context); assertEquals( "Make sure that the use of $DEPS pulls in $GEN_DIR, as well.", ImmutableMap.of( "DEPS", "$GEN_DIR/foo/bar.jar", "GEN_DIR", "buck-out/gen", "OUT", "buck-out/gen/foo/deps.txt"), environmentVariables); // Ensure that $GEN_DIR is declared before $DEPS. List<String> keysInOrder = ImmutableList.copyOf(environmentVariables.keySet()); assertEquals("GEN_DIR", keysInOrder.get(1)); assertEquals("DEPS", keysInOrder.get(2)); }