@Test public void testValidateAliasName() { BuckConfig.validateAliasName("f"); BuckConfig.validateAliasName("_"); BuckConfig.validateAliasName("F"); BuckConfig.validateAliasName("fb4a"); BuckConfig.validateAliasName("FB4A"); BuckConfig.validateAliasName("FB4_"); try { BuckConfig.validateAliasName(null); fail("Should have thrown HumanReadableException"); } catch (HumanReadableException e) { assertEquals("Alias cannot be null.", e.getHumanReadableErrorMessage()); } try { BuckConfig.validateAliasName(""); fail("Should have thrown HumanReadableException"); } catch (HumanReadableException e) { assertEquals("Alias cannot be the empty string.", e.getHumanReadableErrorMessage()); } try { BuckConfig.validateAliasName("42meaningOfLife"); fail("Should have thrown HumanReadableException"); } catch (HumanReadableException e) { assertEquals("Not a valid alias: 42meaningOfLife.", e.getHumanReadableErrorMessage()); } }
@Test public void testGetShellCommand() { String bash = "rm -rf /usr"; String cmdExe = "rmdir /s /q C:\\Windows"; String cmd = "echo \"Hello\""; String genruleName = "//example:genrule"; ExecutionContext linuxExecutionContext = newEmptyExecutionContext(Platform.LINUX); ExecutionContext windowsExecutionContext = newEmptyExecutionContext(Platform.WINDOWS); // Test platform-specific Genrule rule = Genrule.newGenruleBuilder(new FakeAbstractBuildRuleBuilderParams()) .setBuildTarget(BuildTargetFactory.newInstance(genruleName)) .setBash(Optional.of(bash)) .setCmdExe(Optional.of(cmdExe)) .setOut("out.txt") .build(new BuildRuleResolver()); ImmutableList<String> command = rule.createGenruleStep().getShellCommand(linuxExecutionContext); assertEquals(ImmutableList.of("/bin/bash", "-e", "-c", bash), command); command = rule.createGenruleStep().getShellCommand(windowsExecutionContext); assertEquals(ImmutableList.of("cmd.exe", "/c", cmdExe), command); // Test fallback rule = Genrule.newGenruleBuilder(new FakeAbstractBuildRuleBuilderParams()) .setBuildTarget(BuildTargetFactory.newInstance("//example:genrule")) .setCmd(Optional.of(cmd)) .setOut("out.txt") .build(new BuildRuleResolver()); command = rule.createGenruleStep().getShellCommand(linuxExecutionContext); assertEquals(ImmutableList.of("/bin/bash", "-e", "-c", cmd), command); command = rule.createGenruleStep().getShellCommand(windowsExecutionContext); assertEquals(ImmutableList.of("cmd.exe", "/c", cmd), command); // Test command absent rule = Genrule.newGenruleBuilder(new FakeAbstractBuildRuleBuilderParams()) .setBuildTarget(BuildTargetFactory.newInstance("//example:genrule")) .setOut("out.txt") .build(new BuildRuleResolver()); try { rule.createGenruleStep().getShellCommand(linuxExecutionContext); } catch (HumanReadableException e) { assertEquals( String.format("You must specify either bash or cmd for genrule %s.", genruleName), e.getHumanReadableErrorMessage()); } try { rule.createGenruleStep().getShellCommand(windowsExecutionContext); } catch (HumanReadableException e) { assertEquals( String.format("You must specify either cmd_exe or cmd for genrule %s.", genruleName), e.getHumanReadableErrorMessage()); } }
@Test public void testConstructorThrowsForMalformedBuildTarget() throws IOException { Reader reader = new StringReader(Joiner.on('\n').join("[alias]", "fb4a = :fb4a")); ProjectFilesystem projectFilesystem = EasyMock.createMock(ProjectFilesystem.class); EasyMock.replay(projectFilesystem); try { BuildTargetParser parser = new BuildTargetParser(projectFilesystem); createWithDefaultFilesystem(reader, parser); fail("Should have thrown HumanReadableException."); } catch (HumanReadableException e) { assertEquals(":fb4a must start with //", e.getHumanReadableErrorMessage()); } EasyMock.verify(projectFilesystem); }
@Test public void testUnresolvedAliasThrows() throws IOException, NoSuchBuildTargetException { BuildTargetParser parser = EasyMock.createMock(BuildTargetParser.class); EasyMock.expect(parser.parse("//java/com/example:foo", ParseContext.fullyQualified())) .andReturn(BuildTargetFactory.newInstance("//java/com/example:foo")); EasyMock.replay(parser); Reader reader = new StringReader( Joiner.on('\n').join("[alias]", "foo = //java/com/example:foo", "bar = food")); try { createWithDefaultFilesystem(reader, parser); fail("Should have thrown HumanReadableException."); } catch (HumanReadableException e) { assertEquals("No alias for: food.", e.getHumanReadableErrorMessage()); } EasyMock.verify(parser); }
@Test public void testConstructorThrowsNonExistentBasePath() throws IOException { Reader reader = new StringReader(Joiner.on('\n').join("[alias]", "katana = //java/com/example:fb4a")); ProjectFilesystem projectFilesystem = EasyMock.createMock(ProjectFilesystem.class); EasyMock.expect(projectFilesystem.exists("java/com/example")).andReturn(false); EasyMock.replay(projectFilesystem); try { BuildTargetParser parser = new BuildTargetParser(projectFilesystem); createWithDefaultFilesystem(reader, parser); fail("Should have thrown HumanReadableException."); } catch (HumanReadableException e) { assertEquals( "No directory java/com/example when resolving target //java/com/example:fb4a " + "in context FULLY_QUALIFIED", e.getHumanReadableErrorMessage()); } EasyMock.verify(projectFilesystem); }
@Test public void testCreateWorkerToolWithBadExeValue() throws NoSuchBuildTargetException { BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()); BuildRule nonBinaryBuildRule = new FakeBuildRule( BuildTargetFactory.newInstance("//:fake"), new SourcePathResolver(resolver)); resolver.addToIndex(nonBinaryBuildRule); BuildTarget workerTarget = BuildTargetFactory.newInstance("//:worker_rule"); try { WorkerToolBuilder.newWorkerToolBuilder(workerTarget) .setExe(nonBinaryBuildRule.getBuildTarget()) .build(resolver); } catch (HumanReadableException e) { assertThat( e.getHumanReadableErrorMessage(), Matchers.containsString("needs to correspond to a binary rule")); } }
@Test public void testDuplicateAliasDefinitionThrows() throws IOException, NoSuchBuildTargetException { BuildTargetParser parser = EasyMock.createMock(BuildTargetParser.class); EasyMock.replay(parser); Reader reader = new StringReader( Joiner.on('\n') .join("[alias]", "foo = //java/com/example:foo", "foo = //java/com/example:foo")); try { createWithDefaultFilesystem(reader, parser); fail("Should have thrown HumanReadableException."); } catch (HumanReadableException e) { assertEquals( "Throw an exception if there are duplicate definitions for an alias, " + "even if the values are the same.", "Duplicate definition for foo in [alias].", e.getHumanReadableErrorMessage()); } EasyMock.verify(parser); }