File tree Expand file tree Collapse file tree 4 files changed +73
-5
lines changed Expand file tree Collapse file tree 4 files changed +73
-5
lines changed Original file line number Diff line number Diff line change @@ -284,13 +284,13 @@ if_clause = !{
284284}
285285
286286else_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
291291conditional_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
Original file line number Diff line number Diff 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) ]
891921fn no_such_file_error_text ( ) -> & ' static str {
892922 if cfg ! ( windows) {
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11FOO=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"
617else
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" ;
841fi
You can’t perform that action at this time.
0 commit comments