Skip to content

Commit

Permalink
Fix output in tests (ci)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbin committed Aug 22, 2024
1 parent 4b81a35 commit 8219ca5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/commandlang_err/src/err.ml
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,14 @@ let handler ?(state = the_state) ?(exn_handler = Fun.const None) f =
;;

module For_test = struct
let handler ?state ?exn_handler f =
let wrap f =
let init = !am_running_test in
am_running_test := true;
match
Fun.protect
~finally:(fun () -> am_running_test := init)
(fun () -> handler f ?state ?exn_handler)
with
Fun.protect ~finally:(fun () -> am_running_test := init) f
;;

let handler ?state ?exn_handler f =
match wrap (fun () -> handler f ?state ?exn_handler) with
| Ok () -> ()
| Error code -> Stdlib.prerr_endline (Printf.sprintf "[%d]" code)
;;
Expand Down
6 changes: 6 additions & 0 deletions lib/commandlang_err/src/err.mli
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ module For_test : sig
(** Same as [handler], but won't return the exit code, rather print the code
at the end in case of a non zero code, like in cram tests. *)
val handler : ?state:State.t -> ?exn_handler:(exn -> t option) -> (unit -> unit) -> unit

(** Wrap the execution of a function under an environment proper for test
execution. For example, it will turn down the colors in user messages.
{!val:handler} already does a [wrap] - this is exposed if you'd like to
run some test outside of a handler. *)
val wrap : (unit -> 'a) -> 'a
end

(** {1 Other styles}
Expand Down
4 changes: 4 additions & 0 deletions lib/commandlang_err/test/test__err.ml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ let%expect_test "state getters" =
;;

let%expect_test "multiple errors" =
Err.For_test.wrap
@@ fun () ->
Err.error ~loc:(Loc.in_file ~path:(Fpath.v "my/file1")) [ Pp.text "Hello Error1" ];
Err.error ~loc:(Loc.in_file ~path:(Fpath.v "my/file2")) [ Pp.text "Hello Error1" ];
[%expect
Expand All @@ -165,6 +167,8 @@ let%expect_test "multiple errors" =
;;

let%expect_test "error" =
Err.For_test.wrap
@@ fun () ->
let state =
create_state ~config:(Err.Config.create ~mode:Default ~warn_error:false ())
in
Expand Down

0 comments on commit 8219ca5

Please sign in to comment.