Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Correct if syntax #167

Merged
merged 4 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading