From c35f348cd72899587f9ef59066fe28f40b7f3e90 Mon Sep 17 00:00:00 2001
From: Parsa Bahraminejad
Date: Sat, 28 Sep 2024 19:57:34 -0400
Subject: [PATCH] fix Correct if syntax (#167)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* 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
---
crates/deno_task_shell/src/grammar.pest | 6 ++--
crates/tests/src/lib.rs | 30 ++++++++++++++++++++
crates/tests/src/test_builder.rs | 5 ++++
scripts/if_else.sh | 37 +++++++++++++++++++++++--
4 files changed, 73 insertions(+), 5 deletions(-)
diff --git a/crates/deno_task_shell/src/grammar.pest b/crates/deno_task_shell/src/grammar.pest
index c3e081f..abc6947 100644
--- a/crates/deno_task_shell/src/grammar.pest
+++ b/crates/deno_task_shell/src/grammar.pest
@@ -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))
}
diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs
index eb6e23e..5646006 100644
--- a/crates/tests/src/lib.rs
+++ b/crates/tests/src/lib.rs
@@ -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) {
diff --git a/crates/tests/src/test_builder.rs b/crates/tests/src/test_builder.rs
index 4b1ad3c..6899ea1 100644
--- a/crates/tests/src/test_builder.rs
+++ b/crates/tests/src/test_builder.rs
@@ -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
diff --git a/scripts/if_else.sh b/scripts/if_else.sh
index f3fd04f..5da3368 100644
--- a/scripts/if_else.sh
+++ b/scripts/if_else.sh
@@ -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
\ No newline at end of file