Skip to content

Commit

Permalink
fix Correct if syntax (#167)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
prsabahrami and certik authored Sep 28, 2024
1 parent 1113c78 commit c35f348
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
6 changes: 3 additions & 3 deletions crates/deno_task_shell/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ if_clause = !{
}

else_part = !{
Elif ~ conditional_expression ~ Then ~ complete_command ~ linebreak ~ else_part? |
Elif ~ conditional_expression ~ linebreak ~ Then ~ complete_command ~ linebreak ~ else_part? |
Else ~ linebreak ~ complete_command
}

conditional_expression = !{
("[[" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD) ~ "]]") |
("[" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD) ~ "]") |
("[[" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD) ~ "]]" ~ ";"?) |
("[" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD) ~ "]" ~ ";"?) |
("test" ~ (unary_conditional_expression | binary_conditional_expression | UNQUOTED_PENDING_WORD))
}

Expand Down
30 changes: 30 additions & 0 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,36 @@ async fn date() {
.await;
}

#[tokio::test]
async fn if_clause() {
TestBuilder::new()
.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"#)
.assert_stdout("FOO is 2\n")
.run()
.await;
TestBuilder::new()
.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"#)
.assert_stdout("FOO is not 1 or 2\n")
.run()
.await;

TestBuilder::new()
.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"#)
.assert_stdout("FOO is 1\n")
.run()
.await;

TestBuilder::new()
.script_file("../../scripts/if_else.sh")
.assert_exit_code(0)
.assert_stdout("FOO is 2\n")
.assert_stdout("FOO is 2\n")
.assert_stdout("FOO is 2\n")
.assert_stdout("FOO is 2\n")
.run()
.await;
}

#[cfg(test)]
fn no_such_file_error_text() -> &'static str {
if cfg!(windows) {
Expand Down
5 changes: 5 additions & 0 deletions crates/tests/src/test_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ impl TestBuilder {
self
}

pub fn script_file(&mut self, path: &str) -> &mut Self {
self.command(fs::read_to_string(path).unwrap().as_str());
self
}

pub fn stdin(&mut self, stdin: &str) -> &mut Self {
self.stdin = stdin.as_bytes().to_vec();
self
Expand Down
37 changes: 35 additions & 2 deletions scripts/if_else.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
FOO=2
if [[ $FOO -eq 1 ]] then
if [[ $FOO -eq 1 ]];
then
echo "FOO is 1";
elif [[ $FOO -eq 2 ]];
then
echo "FOO is 2";
else
echo "FOO is not 1 or 2";
fi

FOO=2
if [[ $FOO -eq 1 ]]; then
echo "FOO is 1"
elif [[ $FOO -eq 2 ]] then
elif [[ $FOO -eq 2 ]]; then
echo "FOO is 2"
else
echo "FOO is not 1 or 2"
fi

FOO=2
if [[ $FOO -eq 1 ]];
then
echo "FOO is 1";
elif [[ $FOO -eq 2 ]];
then
echo "FOO is 2";
else
echo "FOO is not 1 or 2";
fi

FOO=2
if [[ $FOO -eq 1 ]]
then
echo "FOO is 1";
elif [[ $FOO -eq 2 ]]
then
echo "FOO is 2";
else
echo "FOO is not 1 or 2";
fi

0 comments on commit c35f348

Please sign in to comment.