Skip to content

Commit

Permalink
Allow overriding the number of columns with ALCOTEST_COLUMNS env var
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterDA committed Mar 27, 2023
1 parent 8c886f8 commit dace603
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
### unreleased

- Fix division by zero when size of the terminal is incorrectly
reported as zero. (fix #356, #381, @MisterDA)

- Enable terminal size reporting on macOS and Windows. Also report the
terminal size even when the test is run buffered by Dune.
(#381, @MisterDA)

- Allow overriding the number of columns with `ALCOTEST_COLUMNS` env
var. (#322, #381, @MisterDA)

### 1.7.0 (2023-02-24)

- Allow skipping a test case from inside the test case (#368, @apeschar)
Expand Down
5 changes: 5 additions & 0 deletions alcotest-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ ENVIRONMENT
ALCOTEST_COLOR
See option --color.

ALCOTEST_COLUMNS
Number of columns after which Alcotest truncates or splits written
lines. Default is to auto-detect using the terminal's dimensions,
or fallback to 80 columns.

ALCOTEST_COMPACT
See option --compact.

Expand Down
16 changes: 15 additions & 1 deletion src/alcotest-engine/cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,22 @@ module Make (P : Platform.MAKER) (M : Monad.S) :
in
Cmdliner.Cmd.Env.info "ALCOTEST_SOURCE_CODE_POSITION" ~doc

let alcotest_columns =
let doc =
"Number of columns after which Alcotest truncates or splits written \
lines. Default is to auto-detect using the terminal's dimensions, or \
fallback to 80 columns."
in
Cmdliner.Cmd.Env.info "ALCOTEST_COLUMNS" ~doc

let envs =
[ ci_env; github_action_env; ocamlci_env; alcotest_source_code_position ]
[
ci_env;
github_action_env;
ocamlci_env;
alcotest_source_code_position;
alcotest_columns;
]

let set_color =
let env = Cmd.Env.info "ALCOTEST_COLOR" in
Expand Down
10 changes: 7 additions & 3 deletions src/alcotest/alcotest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ module Unix_platform (M : Alcotest_engine.Monad.S) = struct
let stdout_isatty () = Unix.(isatty stdout)

let stdout_columns () =
match Terminal.get_dimensions () with
| Some { columns; _ } when columns > 0 -> Some columns
| _ -> None
let bind o f = match o with Some o -> f o | None -> None in
match bind (Sys.getenv_opt "ALCOTEST_COLUMNS") int_of_string_opt with
| Some columns when columns > 0 -> Some columns
| _ -> (
match Terminal.get_dimensions () with
| Some { columns; _ } when columns > 0 -> Some columns
| _ -> None)

external before_test :
output:out_channel -> stdout:out_channel -> stderr:out_channel -> unit
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
; Don't run tests as if Alcotest was run in CI
(CI false)
; Don't guess source code position for compat with < 4.12.
(ALCOTEST_SOURCE_CODE_POSITION false))))
(ALCOTEST_SOURCE_CODE_POSITION false)
; Set to 80 columns for output reproducibility
(ALCOTEST_COLUMNS 80))))

(executable
(name gen_dune_rules)
Expand Down

0 comments on commit dace603

Please sign in to comment.