@Test public void replaceLocationOfFullyQualifiedBuildTarget() throws IOException, MacroException { ProjectFilesystem filesystem = new FakeProjectFilesystem(); BuildRuleResolver ruleResolver = new BuildRuleResolver(); BuildRule javaBinary = createSampleJavaBinaryRule(ruleResolver); Path outputPath = javaBinary.getPathToOutput(); Path absolutePath = outputPath.toAbsolutePath(); String originalCmd = String.format( "$(location :%s) $(location %s) $OUT", javaBinary.getBuildTarget().getShortNameAndFlavorPostfix(), javaBinary.getBuildTarget().getFullyQualifiedName()); // Interpolate the build target in the genrule cmd string. MacroHandler macroHandler = new MacroHandler( ImmutableMap.<String, MacroExpander>of("location", new LocationMacroExpander())); String transformedString = macroHandler.expand( javaBinary.getBuildTarget(), createCellRoots(filesystem), ruleResolver, filesystem, originalCmd); // Verify that the correct cmd was created. String expectedCmd = String.format("%s %s $OUT", absolutePath, absolutePath); assertEquals(expectedCmd, transformedString); }
@Test public void testMissingBuildRule() throws NoSuchBuildTargetException { BuildTarget target = BuildTargetFactory.newInstance("//:java"); ProjectFilesystem filesystem = new FakeProjectFilesystem(); MacroHandler macroHandler = new MacroHandler(ImmutableMap.<String, MacroExpander>of("maven_coords", expander)); try { macroHandler.expand(target, createCellRoots(filesystem), resolver, "$(maven_coords //:foo)"); fail("Expected MacroException; Rule does not exist"); } catch (MacroException e) { assertTrue( "Expected MacroException that indicates target does not exist", e.getMessage().contains("no rule //:foo")); } }
@Test public void testExpansionOfMavenCoordinates() throws NoSuchBuildTargetException { String mavenCoords = "org.foo:bar:1.0"; BuildTarget target = BuildTargetFactory.newInstance("//:java"); JavaLibraryBuilder.createBuilder(target).setMavenCoords(mavenCoords).build(resolver); ProjectFilesystem filesystem = new FakeProjectFilesystem(); MacroHandler macroHandler = new MacroHandler(ImmutableMap.<String, MacroExpander>of("maven_coords", expander)); try { String expansion = macroHandler.expand( target, createCellRoots(filesystem), resolver, "$(maven_coords //:java)"); assertEquals("Return maven coordinates do not match provides ones", mavenCoords, expansion); } catch (MacroException e) { fail(String.format("Unexpected MacroException: %s", e.getMessage())); } }
@Test public void testShouldWarnUsersWhenThereIsNoOutputForARuleButLocationRequested() { BuildRuleResolver resolver = new BuildRuleResolver(); JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//cheese:java")) .build(resolver); BuildTarget target = BuildTargetFactory.newInstance("//cheese:cake"); ProjectFilesystem filesystem = new FakeProjectFilesystem(); MacroHandler macroHandler = new MacroHandler( ImmutableMap.<String, MacroExpander>of("location", new LocationMacroExpander())); try { macroHandler.expand( target, createCellRoots(filesystem), resolver, filesystem, "$(location //cheese:java)"); fail("Location was null. Expected HumanReadableException with helpful message."); } catch (MacroException e) { assertEquals( "expanding $(location //cheese:java): //cheese:java used" + " in location macro does not produce output", e.getMessage()); } }