Skip to content

Commit

Permalink
Add Ninja escape '$' parsing tests
Browse files Browse the repository at this point in the history
Closes #10942.

PiperOrigin-RevId: 300243386
  • Loading branch information
jin authored and copybara-github committed Mar 11, 2020
1 parent 6ba6ec8 commit f17062d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public boolean tryReadSimpleVariable() {
}

public boolean tryReadEscapedLiteral() {
Preconditions.checkState('$' == fragment.byteAt(position));
if (checkForward(1, '$', ':', ' ')) {
// Escaped literal.
end = position + 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,38 @@ public void testNinjaRuleWithHash() throws Exception {
.isEqualTo("executable --flag ${TARGET} ${out} && sed -e 's/#.*$//' -e '/^$/d'");
}

@Test
public void testNinjaRuleWithDollarSign() throws Exception {
NinjaParserStep parser =
createParser(
"rule testRule \n"
+ " command = something && $\n"
+ " something_else\n"
+ " description = Test $\n"
+ " rule");
NinjaRule ninjaRule = parser.parseNinjaRule();
assertThat(ninjaRule.getVariables().get(NinjaRuleVariable.COMMAND).getRawText())
.isEqualTo("something && $\n something_else");
assertThat(ninjaRule.getVariables().get(NinjaRuleVariable.DESCRIPTION).getRawText())
.isEqualTo("Test $\n rule");
}

@Test
public void testNinjaRuleWithStickyDollarSign() throws Exception {
NinjaParserStep parser =
createParser(
"rule testRule \n"
+ " command = something &&$\n"
+ " something_else\n"
+ " description = Test$\n"
+ " rule");
NinjaRule ninjaRule = parser.parseNinjaRule();
assertThat(ninjaRule.getVariables().get(NinjaRuleVariable.COMMAND).getRawText())
.isEqualTo("something &&$\n something_else");
assertThat(ninjaRule.getVariables().get(NinjaRuleVariable.DESCRIPTION).getRawText())
.isEqualTo("Test$\n rule");
}

@Test
public void testVariableWithoutValue() throws Exception {
NinjaParserStep parser =
Expand Down Expand Up @@ -347,6 +379,20 @@ public void testNinjaTargetsPathWithEscapedSpace() throws Exception {
.containsExactly(PathFragment.create("input with space"), PathFragment.create("other"));
}

@Test
public void testNinjaTargetsPathWithEscapedNewline() throws Exception {
NinjaTarget target =
parseNinjaTarget(
"build $\n" + " output : $\n" + " command input$\n" + " with$\n" + " newline");
assertThat(target.getRuleName()).isEqualTo("command");
assertThat(target.getOutputs()).containsExactly(PathFragment.create("output"));
assertThat(target.getUsualInputs())
.containsExactly(
PathFragment.create("input"),
PathFragment.create("with"),
PathFragment.create("newline"));
}

@Test
public void testNinjaTargetWithScope() throws Exception {
NinjaTarget target = parseNinjaTarget("build output : command input\n pool = abc\n");
Expand Down

0 comments on commit f17062d

Please sign in to comment.