From e608c6941d263c24fa290dc6279ffb9832c2d863 Mon Sep 17 00:00:00 2001 From: Sebastien Mondet Date: Tue, 11 Oct 2016 12:31:44 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20how=20`output=5Fas=5Fstring`=20deals=20wi?= =?UTF-8?q?th=20=E2=80=9Cfailures=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `output_as_string` now does not kill the script if the command fails; this gives more power to the user who can still implement the previous behavior easily. Cf. the tests: ```ocaml exits 77 Construct.( let tmp = "/tmp/test_error_in_output_as_string" in let cat_tmp = exec ["cat"; tmp] in let succeed_or_die ut = if_then_else (succeeds ut) nop (seq [ printf "Failure !"; fail; ]) in seq [ exec ["rm"; "-f"; tmp]; if_then_else (seq [printf "aaa"; cat_tmp] |> succeed_or_die |> output_as_string =$= string "aaa") (return 11) (return 12); ]; ); ``` --- src/lib/language.ml | 3 +-- src/test/main.ml | 58 +++++++++++++++++++++++++++++++++------------ 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/lib/language.ml b/src/lib/language.ml index dfb4443..3d26a03 100644 --- a/src/lib/language.ml +++ b/src/lib/language.ml @@ -208,8 +208,7 @@ let rec to_shell: type a. _ -> a t -> string = | Literal lit -> Literal.to_shell lit | Output_as_string e -> - sprintf "\"$( { %s || %s ; } | od -t o1 -An -v | tr -d ' \\n' )\"" - (continue e) params.die_command + sprintf "\"$( { %s ; } | od -t o1 -An -v | tr -d ' \\n' )\"" (continue e) | Feed (string, e) -> sprintf {sh| %s | %s |sh} (continue string |> expand_octal) (continue e) diff --git a/src/test/main.ml b/src/test/main.ml index 24d43f2..8a9fbd0 100644 --- a/src/test/main.ml +++ b/src/test/main.ml @@ -148,20 +148,6 @@ let tests = return 10; ]; ); - exits 77 Construct.( (* 77 Is the value of ~exit_with in the call - to with_trap *) - let tmp = "/tmp/test_trapping" in - let cat_tmp = exec ["cat"; tmp] in - seq [ - exec ["rm"; "-f"; tmp]; - if_then_else - (* cat |> to_string should abort the script: *) - (cat_tmp |> output_as_string <$> string "nnnn") - (return 11) - (return 12); - return 13; - ]; - ); begin let minus_f = "one \nwith \\ spaces and \ttabs -dashes -- " in let make ret minus_g single = @@ -264,7 +250,49 @@ let tests = (return 22) ); ] - end + end; + exits 11 Construct.( + let tmp = "/tmp/test_error_in_output_as_string" in + let cat_tmp = exec ["cat"; tmp] in + seq [ + exec ["rm"; "-f"; tmp]; + if_then_else + (* cat |> to_string does not abort the script: *) + (cat_tmp |> output_as_string =$= string "") + (return 11) + (return 12); + ]; + ); + exits 11 Construct.( + let tmp = "/tmp/test_error_in_output_as_string" in + let cat_tmp = exec ["cat"; tmp] in + seq [ + exec ["rm"; "-f"; tmp]; + if_then_else + (seq [printf "aaa"; cat_tmp] |> output_as_string =$= string "aaa") + (return 11) + (return 12); + ]; + ); + exits 77 Construct.( + let tmp = "/tmp/test_error_in_output_as_string" in + let cat_tmp = exec ["cat"; tmp] in + let succeed_or_die ut = + if_then_else (succeeds ut) + nop + (seq [ + printf "Failure !"; + fail; + ]) in + seq [ + exec ["rm"; "-f"; tmp]; + if_then_else + (seq [printf "aaa"; cat_tmp] |> succeed_or_die + |> output_as_string =$= string "aaa") + (return 11) + (return 12); + ]; + ); ]