Skip to content

Commit

Permalink
Fix how output_as_string deals with “failures”
Browse files Browse the repository at this point in the history
`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);
    ];
  );
```
  • Loading branch information
smondet committed Oct 11, 2016
1 parent f22331f commit e608c69
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/lib/language.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 43 additions & 15 deletions src/test/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <absent-file> |> 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 =
Expand Down Expand Up @@ -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 <absent-file> |> 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);
];
);
]


Expand Down

0 comments on commit e608c69

Please sign in to comment.