Skip to content

Commit

Permalink
Avoid emitting carraige returns when not outputting to a TTY
Browse files Browse the repository at this point in the history
  • Loading branch information
craigfe committed Aug 24, 2020
1 parent 7700959 commit 857e873
Show file tree
Hide file tree
Showing 35 changed files with 212 additions and 135 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Fix compatibility with `fmt.0.8.8+dune` by adding a missing `fmt` dependency
in `alcotest`'s dune file (#266, @NathanReb)

- Only show "in progress" lines when writing to a TTY. (#267, @CraigFe)

### 1.2.1 (2020-07-15)

- Surround pretty-printed diffs with quotes to make trailing whitespace more
Expand Down
13 changes: 8 additions & 5 deletions src/alcotest-engine/core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ struct
let log_dir = log_dir ~via_symlink:true t |> maybe_collapse_home in
Pp.suite_results ~verbose ~show_errors ~json ~compact ~log_dir

let pp_event ~prior_error ~tests_so_far t =
let pp_event ~isatty ~prior_error ~tests_so_far t =
let selector_on_failure =
(not prior_error) && not (t.verbose || t.show_errors)
in
if not t.json then
Pp.event ~compact:t.compact ~max_label:t.max_label
Pp.event ~isatty ~compact:t.compact ~max_label:t.max_label
~doc_of_test_name:(Suite.doc_of_test_name t.suite)
~selector_on_failure ~tests_so_far
else Fmt.nop
Expand Down Expand Up @@ -282,9 +282,12 @@ struct
let perform_test t args { tests_so_far; prior_error } suite =
let open Suite in
let test = suite.fn in
let pp_event = pp_event t ~prior_error ~tests_so_far in
let print_event =
pp_event t ~prior_error ~tests_so_far ~isatty:(P.stdout_isatty ())
Fmt.stdout
in
M.return () >>= fun () ->
pp_event Fmt.stdout (`Start suite.name);
print_event (`Start suite.name);
Fmt.(flush stdout) () (* Show event before any test stderr *);
test args >|= fun result ->
(* Store errors *)
Expand All @@ -306,7 +309,7 @@ struct
(* Show any remaining test output before the event *)
Fmt.(flush stdout ());
Fmt.(flush stderr ());
pp_event Fmt.stdout (`Result (suite.name, result));
print_event (`Result (suite.name, result));
let state =
{ tests_so_far = tests_so_far + 1; prior_error = errored || prior_error }
in
Expand Down
4 changes: 4 additions & 0 deletions src/alcotest-engine/platform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module type S = sig
type 'a promise
(** The type of monadic actions handled by {!with_redirect}. *)

val stdout_isatty : unit -> bool
(** Return [true] if standard output refers to a terminal or console window,
[false] otherwise. *)

val with_redirect : string -> (unit -> 'a promise) -> 'a promise
(** [with_redirect output_file f] is called for each test. On Unix, it it
deals with redirection of standard streams to the [output_file]. The
Expand Down
25 changes: 13 additions & 12 deletions src/alcotest-engine/pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,25 @@ let event_line ~max_label ~doc_of_test_name ppf = function
(info ~max_label ~doc_of_test_name) ppf p
| _ -> assert false

let event ~compact ~max_label ~doc_of_test_name ~selector_on_failure
let event ~isatty ~compact ~max_label ~doc_of_test_name ~selector_on_failure
~tests_so_far ppf event =
match (compact, event) with
| true, `Start _ -> ()
| true, `Result (_, r) ->
pp_result_compact ppf r;
(* Wrap compact output to terminal width manually *)
if (tests_so_far + 1) mod terminal_width () = 0 then
Format.pp_force_newline ppf ();
()
| false, `Start tname ->
match (compact, isatty, event) with
| true, _, `Start _ | _, false, `Start _ -> ()
| false, true, `Start tname ->
Fmt.(
left_padding ~with_selector:false
++ const (left left_c yellow_s) "..."
++ const (info ~max_label ~doc_of_test_name) tname)
ppf ()
| false, `Result (tname, r) ->
Fmt.pf ppf "\r%a@,"
| true, _, `Result (_, r) ->
pp_result_compact ppf r;
(* Wrap compact output to terminal width manually *)
if (tests_so_far + 1) mod terminal_width () = 0 then
Format.pp_force_newline ppf ();
()
| false, _, `Result (tname, r) ->
if isatty then Fmt.pf ppf "\r";
Fmt.pf ppf "%a@,"
(pp_result_full ~max_label ~doc_of_test_name ~selector_on_failure)
(tname, r)

Expand Down
1 change: 1 addition & 0 deletions src/alcotest-engine/pp.mli
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ val event_line :
[ `Result of Test_name.t * [< Run_result.t ] | `Start of Test_name.t ] Fmt.t

val event :
isatty:bool ->
compact:bool ->
max_label:int ->
doc_of_test_name:(Test_name.t -> string) ->
Expand Down
2 changes: 2 additions & 0 deletions src/alcotest-mirage/alcotest_mirage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Make (C : Mirage_clock.MCLOCK) = struct

let prepare ~base:_ ~dir:_ ~name:_ = ()

let stdout_isatty () = true

let with_redirect _ fn = fn ()

let setup_std_outputs ?style_renderer:_ ?utf_8:_ () = ()
Expand Down
2 changes: 2 additions & 0 deletions src/alcotest/alcotest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ module Unix (M : Alcotest_engine.Monad.S) = struct
else if not (Sys.is_directory dir) then
Fmt.failwith "exists but is not a directory: %S" dir

let stdout_isatty () = Unix.(isatty stdout)

let with_redirect file fn =
M.return () >>= fun () ->
Fmt.(flush stdout) ();
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/alcotest-lwt/failing/async_failure.expected
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Testing `foo'.
This run has ID `<uuid>'.

... all 0 one.> [FAIL] all 0 one.
... all 1 two. [OK] all 1 two.
> [FAIL] all 0 one.
[OK] all 1 two.

┌──────────────────────────────────────────────────────────────────────────────┐
│ [FAIL] all 0 one. │
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/alcotest-lwt/failing/fail_with.expected
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Testing `foo'.
This run has ID `<uuid>'.

... all 0 one.> [FAIL] all 0 one.
... all 1 two. [OK] all 1 two.
> [FAIL] all 0 one.
[OK] all 1 two.

┌──────────────────────────────────────────────────────────────────────────────┐
│ [FAIL] all 0 one. │
Expand Down
52 changes: 26 additions & 26 deletions test/e2e/alcotest/failing/check_basic.expected
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
Testing `check_basic'.
This run has ID `<uuid>'.

... different basic 0 bool.ASSERT bool
[FAIL] different basic 0 bool.
... different basic 1 int.ASSERT int
[FAIL] different basic 1 int.
... different basic 2 int32.ASSERT int32
[FAIL] different basic 2 int32.
... different basic 3 int64.ASSERT int64
[FAIL] different basic 3 int64.
... different basic 4 float.ASSERT float
[FAIL] different basic 4 float.
... different basic 5 char.ASSERT char
[FAIL] different basic 5 char.
... different basic 6 string.ASSERT string
[FAIL] different basic 6 string.
... different basic 7 bytes.ASSERT bytes
[FAIL] different basic 7 bytes.
... different composite 0 list.ASSERT list
[FAIL] different composite 0 list.
... different composite 1 array.ASSERT array
[FAIL] different composite 1 array.
... different composite 2 option some.ASSERT option some
[FAIL] different composite 2 option some.
... different composite 3 result.ASSERT result
[FAIL] different composite 3 result.
... different composite 4 pair.ASSERT pair
[FAIL] different composite 4 pair.
ASSERT bool
[FAIL] different basic 0 bool.
ASSERT int
[FAIL] different basic 1 int.
ASSERT int32
[FAIL] different basic 2 int32.
ASSERT int64
[FAIL] different basic 3 int64.
ASSERT float
[FAIL] different basic 4 float.
ASSERT char
[FAIL] different basic 5 char.
ASSERT string
[FAIL] different basic 6 string.
ASSERT bytes
[FAIL] different basic 7 bytes.
ASSERT list
[FAIL] different composite 0 list.
ASSERT array
[FAIL] different composite 1 array.
ASSERT option some
[FAIL] different composite 2 option some.
ASSERT result
[FAIL] different composite 3 result.
ASSERT pair
[FAIL] different composite 4 pair.

┌──────────────────────────────────────────────────────────────────────────────┐
│ [FAIL] different basic 0 bool. │
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/alcotest/failing/check_long.expected
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Testing `check_long'.
This run has ID `<uuid>'.

... wrapping 0 list.ASSERT list
[FAIL] wrapping 0 list.
... wrapping 1 array.ASSERT array
[FAIL] wrapping 1 array.
... wrapping 2 nested options.ASSERT nested options
[FAIL] wrapping 2 nested options.
ASSERT list
[FAIL] wrapping 0 list.
ASSERT array
[FAIL] wrapping 1 array.
ASSERT nested options
[FAIL] wrapping 2 nested options.

┌──────────────────────────────────────────────────────────────────────────────┐
│ [FAIL] wrapping 0 list. │
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/alcotest/failing/exception_in_test.expected
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Testing `suite-with-failures'.
This run has ID `<uuid>'.

... test-a 0 Passing. [OK] test-a 0 Passing.
... test-a 1 Failing.> [FAIL] test-a 1 Failing.
... test-b 0 Another pass. [OK] test-b 0 Another pass.
[OK] test-a 0 Passing.
> [FAIL] test-a 1 Failing.
[OK] test-b 0 Another pass.

┌──────────────────────────────────────────────────────────────────────────────┐
│ [FAIL] test-a 1 Failing. │
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/alcotest/failing/invalid_arg_in_test.expected
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Testing `suite-with-failures'.
This run has ID `<uuid>'.

... test-a 0 Passing. [OK] test-a 0 Passing.
... test-a 1 Failing.> [FAIL] test-a 1 Failing.
... test-b 0 Another pass. [OK] test-b 0 Another pass.
[OK] test-a 0 Passing.
> [FAIL] test-a 1 Failing.
[OK] test-b 0 Another pass.

┌──────────────────────────────────────────────────────────────────────────────┐
│ [FAIL] test-a 1 Failing. │
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/alcotest/failing/long_test_case_name.expected
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Testing `suite-with-failures'.
This run has ID `<uuid>'.

... test-a 0 Passing. [OK] test-a 0 Passing.
... test-a 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse mollis, orci sed venenatis efficitur, eros est imperdiet purus, sit amet tincidunt massa diam ut elit.> [FAIL] test-a 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse mollis, orci sed venenatis efficitur, eros est imperdiet purus, sit amet tincidunt massa diam ut elit.
[OK] test-a 0 Passing.
> [FAIL] test-a 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse mollis, orci sed venenatis efficitur, eros est imperdiet purus, sit amet tincidunt massa diam ut elit.

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ [FAIL] test-a 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse mollis, orci sed venenatis efficitur, eros est imperdiet purus, sit amet tincidunt massa diam ut elit. │
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/alcotest/failing/tail_errors_limit.expected
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Testing `tail_errors_limit'.
This run has ID `<uuid>'.

... failing 0 test.> [FAIL] failing 0 test.
> [FAIL] failing 0 test.

┌──────────────────────────────────────────────────────────────────────────────┐
│ [FAIL] failing 0 test. │
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/alcotest/failing/tail_errors_unlimited.expected
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Testing `tail_errors_unlimited'.
This run has ID `<uuid>'.

... failing 0 test.> [FAIL] failing 0 test.
> [FAIL] failing 0 test.

┌──────────────────────────────────────────────────────────────────────────────┐
│ [FAIL] failing 0 test. │
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/alcotest/inside-dune/color-overridden.expected
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Testing `test_color'.
This run has ID `<uuid>'.

... alpha 0 Output may or may not contain ANSII escape codes. [OK] alpha 0 Output may or may not contain ANSII escape codes.
... alpha 1 according to whether or not [--color] is set. [OK] alpha 1 according to whether or not [--color] is set.
... alpha 2 (See the corresponding [dune] file.). [OK] alpha 2 (See the corresponding [dune] file.).
[OK] alpha 0 Output may or may not contain ANSII escape codes.
[OK] alpha 1 according to whether or not [--color] is set.
[OK] alpha 2 (See the corresponding [dune] file.).

Full test results in `<build-context>/_build/_tests/<test-dir>'.
Test Successful in <test-duration>s. 3 tests run.
2 changes: 1 addition & 1 deletion test/e2e/alcotest/passing/and_exit_false.expected
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Testing `suite-name'.
This run has ID `<uuid>'.

... test-a 0 Test case. [OK] test-a 0 Test case.
[OK] test-a 0 Test case.

Full test results in `<build-context>/_build/_tests/<test-dir>'.
Test Successful in <test-duration>s. 1 test run.
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/alcotest/passing/and_exit_true.expected
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Testing `suite-name'.
This run has ID `<uuid>'.

... test-a 0 Test case. [OK] test-a 0 Test case.
[OK] test-a 0 Test case.

Full test results in `<build-context>/_build/_tests/<test-dir>'.
Test Successful in <test-duration>s. 1 test run.
16 changes: 8 additions & 8 deletions test/e2e/alcotest/passing/assert_and_verbose.expected
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
Testing `assert-and-verbose'.
This run has ID `<uuid>'.

... alpha 0 check → stdout.ASSERT alpha-0 check
ASSERT alpha-0 check
alpha-0 standard out
[OK] alpha 0 check → stdout.
... alpha 1 stdout → check.
[OK] alpha 0 check → stdout.

alpha-1 standard out
ASSERT alpha-1 check
[OK] alpha 1 stdout → check.
... alpha 2 check → stderr.ASSERT alpha-2 check
[OK] alpha 1 stdout → check.
ASSERT alpha-2 check
alpha-2 standard error
[OK] alpha 2 check → stderr.
... beta 0 stdout → check → stderr.
[OK] alpha 2 check → stderr.

beta-0 standard out
ASSERT beta-0 check
beta-0 standard error
[OK] beta 0 stdout → check → stderr.
[OK] beta 0 stdout → check → stderr.

Test Successful in <test-duration>s. 4 tests run.
6 changes: 3 additions & 3 deletions test/e2e/alcotest/passing/assert_not_printed.expected
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Testing `assert-not-printed'.
This run has ID `<uuid>'.

... alpha 0 0. [OK] alpha 0 0.
... alpha 1 1. [OK] alpha 1 1.
... beta 0 2. [OK] beta 0 2.
[OK] alpha 0 0.
[OK] alpha 1 1.
[OK] beta 0 2.

Full test results in `<build-context>/_build/_tests/<test-dir>'.
Test Successful in <test-duration>s. 3 tests run.
8 changes: 4 additions & 4 deletions test/e2e/alcotest/passing/basic.expected
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Testing `suite-name'.
This run has ID `<uuid>'.

... test-a 0 First test case. [OK] test-a 0 First test case.
... test-a 1 Second test case. [OK] test-a 1 Second test case.
... test-b 0 Third test case. [OK] test-b 0 Third test case.
... test-c 0 Fourth test case. [OK] test-c 0 Fourth test case.
[OK] test-a 0 First test case.
[OK] test-a 1 Second test case.
[OK] test-b 0 Third test case.
[OK] test-c 0 Fourth test case.

Full test results in `<build-context>/_build/_tests/<test-dir>'.
Test Successful in <test-duration>s. 4 tests run.
Loading

0 comments on commit 857e873

Please sign in to comment.