Skip to content

Commit c35f348

Browse files
prsabahramicertik
andauthored
fix Correct if syntax (#167)
* Fix the syntax in if_else.sh The test script `if_else.sh` has incorrect syntax for an if statement: $ bash if_else.sh if_else.sh: line 2: syntax error near unexpected token `then' if_else.sh: line 2: `if [[ $FOO -eq 1 ]] then' The correct syntax uses `;` before `then`. * fix if syntax * Added from_file to TestBuilder and minor fix in if parser * Update function name to script_file due to from_* conflict --------- Co-authored-by: Ondřej Čertík <ondrej@certik.us>
1 parent 1113c78 commit c35f348

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

crates/deno_task_shell/src/grammar.pest

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,13 @@ if_clause = !{
284284
}
285285

286286
else_part = !{
287-
Elif ~ conditional_expression ~ Then ~ complete_command ~ linebreak ~ else_part? |
287+
Elif ~ conditional_expression ~ linebreak ~ Then ~ complete_command ~ linebreak ~ else_part? |
288288
Else ~ linebreak ~ complete_command
289289
}
290290

291291
conditional_expression = !{
292-
("[[" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD) ~ "]]") |
293-
("[" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD) ~ "]") |
292+
("[[" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD) ~ "]]" ~ ";"?) |
293+
("[" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD) ~ "]" ~ ";"?) |
294294
("test" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD))
295295
}
296296

crates/tests/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,36 @@ async fn date() {
887887
.await;
888888
}
889889

890+
#[tokio::test]
891+
async fn if_clause() {
892+
TestBuilder::new()
893+
.command(r#"FOO=2; if [[ $FOO == 1 ]]; then echo "FOO is 1"; elif [[ $FOO -eq 2 ]]; then echo "FOO is 2"; else echo "FOO is not 1 or 2"; fi"#)
894+
.assert_stdout("FOO is 2\n")
895+
.run()
896+
.await;
897+
TestBuilder::new()
898+
.command(r#"FOO=3; if [[ $FOO == 1 ]]; then echo "FOO is 1"; elif [[ $FOO -eq 2 ]]; then echo "FOO is 2"; else echo "FOO is not 1 or 2"; fi"#)
899+
.assert_stdout("FOO is not 1 or 2\n")
900+
.run()
901+
.await;
902+
903+
TestBuilder::new()
904+
.command(r#"FOO=1; if [[ $FOO == 1 ]]; then echo "FOO is 1"; elif [[ $FOO -eq 2 ]]; then echo "FOO is 2"; else echo "FOO is not 1 or 2"; fi"#)
905+
.assert_stdout("FOO is 1\n")
906+
.run()
907+
.await;
908+
909+
TestBuilder::new()
910+
.script_file("../../scripts/if_else.sh")
911+
.assert_exit_code(0)
912+
.assert_stdout("FOO is 2\n")
913+
.assert_stdout("FOO is 2\n")
914+
.assert_stdout("FOO is 2\n")
915+
.assert_stdout("FOO is 2\n")
916+
.run()
917+
.await;
918+
}
919+
890920
#[cfg(test)]
891921
fn no_such_file_error_text() -> &'static str {
892922
if cfg!(windows) {

crates/tests/src/test_builder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ impl TestBuilder {
128128
self
129129
}
130130

131+
pub fn script_file(&mut self, path: &str) -> &mut Self {
132+
self.command(fs::read_to_string(path).unwrap().as_str());
133+
self
134+
}
135+
131136
pub fn stdin(&mut self, stdin: &str) -> &mut Self {
132137
self.stdin = stdin.as_bytes().to_vec();
133138
self

scripts/if_else.sh

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
FOO=2
2-
if [[ $FOO -eq 1 ]] then
2+
if [[ $FOO -eq 1 ]];
3+
then
4+
echo "FOO is 1";
5+
elif [[ $FOO -eq 2 ]];
6+
then
7+
echo "FOO is 2";
8+
else
9+
echo "FOO is not 1 or 2";
10+
fi
11+
12+
FOO=2
13+
if [[ $FOO -eq 1 ]]; then
314
echo "FOO is 1"
4-
elif [[ $FOO -eq 2 ]] then
15+
elif [[ $FOO -eq 2 ]]; then
516
echo "FOO is 2"
617
else
718
echo "FOO is not 1 or 2"
19+
fi
20+
21+
FOO=2
22+
if [[ $FOO -eq 1 ]];
23+
then
24+
echo "FOO is 1";
25+
elif [[ $FOO -eq 2 ]];
26+
then
27+
echo "FOO is 2";
28+
else
29+
echo "FOO is not 1 or 2";
30+
fi
31+
32+
FOO=2
33+
if [[ $FOO -eq 1 ]]
34+
then
35+
echo "FOO is 1";
36+
elif [[ $FOO -eq 2 ]]
37+
then
38+
echo "FOO is 2";
39+
else
40+
echo "FOO is not 1 or 2";
841
fi

0 commit comments

Comments
 (0)