Skip to content

Commit 1eb8a29

Browse files
committed
Allow overriding the number of columns with ALCOTEST_COLUMNS env var
1 parent 72bd8e1 commit 1eb8a29

File tree

7 files changed

+41
-5
lines changed

7 files changed

+41
-5
lines changed

CHANGES.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
### unreleased
22

3+
- Fix division by zero when size of the terminal is incorrectly
4+
reported as zero. (fix #356, #381, @MisterDA)
5+
6+
- Enable terminal size reporting on macOS and Windows. Also report the
7+
terminal size even when the test is run buffered by Dune.
8+
(#381, @MisterDA)
9+
10+
- Allow overriding the number of columns with `ALCOTEST_COLUMNS` env
11+
var. (#322, #381, @MisterDA)
12+
313
### 1.7.0 (2023-02-24)
414

515
- Allow skipping a test case from inside the test case (#368, @apeschar)

alcotest-help.txt

+5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ ENVIRONMENT
7777
ALCOTEST_COLOR
7878
See option --color.
7979

80+
ALCOTEST_COLUMNS
81+
Number of columns after which Alcotest truncates or splits written
82+
lines. Default is to auto-detect using the terminal's dimensions,
83+
or fallback to 80 columns.
84+
8085
ALCOTEST_COMPACT
8186
See option --compact.
8287

src/alcotest-engine/cli.ml

+15-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,22 @@ module Make (P : Platform.MAKER) (M : Monad.S) :
6969
in
7070
Cmdliner.Cmd.Env.info "ALCOTEST_SOURCE_CODE_POSITION" ~doc
7171

72+
let alcotest_columns =
73+
let doc =
74+
"Number of columns after which Alcotest truncates or splits written \
75+
lines. Default is to auto-detect using the terminal's dimensions, or \
76+
fallback to 80 columns."
77+
in
78+
Cmdliner.Cmd.Env.info "ALCOTEST_COLUMNS" ~doc
79+
7280
let envs =
73-
[ ci_env; github_action_env; ocamlci_env; alcotest_source_code_position ]
81+
[
82+
ci_env;
83+
github_action_env;
84+
ocamlci_env;
85+
alcotest_source_code_position;
86+
alcotest_columns;
87+
]
7488

7589
let set_color =
7690
let env = Cmd.Env.info "ALCOTEST_COLOR" in

src/alcotest-stdlib-ext/alcotest_stdlib_ext.ml

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ end
8686
module Option = struct
8787
let is_some = function Some _ -> true | None -> false
8888
let map f = function Some x -> Some (f x) | None -> None
89+
let bind o f = match o with Some o -> f o | None -> None
8990

9091
let get_exn = function
9192
| Some x -> x

src/alcotest-stdlib-ext/alcotest_stdlib_ext.mli

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ end
4242

4343
module Option : sig
4444
val map : ('a -> 'b) -> 'a option -> 'b option
45+
val bind : 'a option -> ('a -> 'b option) -> 'b option
4546
val is_some : _ option -> bool
4647
val get_exn : 'a option -> 'a
4748
val value : default:'a -> 'a option -> 'a

src/alcotest/alcotest.ml

+6-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ module Unix_platform (M : Alcotest_engine.Monad.S) = struct
7474
let stdout_isatty () = Unix.(isatty stdout)
7575

7676
let stdout_columns () =
77-
match Terminal.get_dimensions () with
78-
| Some { columns; _ } when columns > 0 -> Some columns
79-
| _ -> None
77+
match Option.bind (Sys.getenv_opt "ALCOTEST_COLUMNS") int_of_string_opt with
78+
| Some columns when columns > 0 -> Some columns
79+
| _ -> (
80+
match Terminal.get_dimensions () with
81+
| Some { columns; _ } when columns > 0 -> Some columns
82+
| _ -> None)
8083

8184
external before_test :
8285
output:out_channel -> stdout:out_channel -> stderr:out_channel -> unit

test/e2e/dune

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
; Don't run tests as if Alcotest was run in CI
55
(CI false)
66
; Don't guess source code position for compat with < 4.12.
7-
(ALCOTEST_SOURCE_CODE_POSITION false))))
7+
(ALCOTEST_SOURCE_CODE_POSITION false)
8+
; Set to 80 columns for output reproducibility
9+
(ALCOTEST_COLUMNS 80))))
810

911
(executable
1012
(name gen_dune_rules)

0 commit comments

Comments
 (0)