Skip to content

Commit

Permalink
Merge branch 'main' into ci-env-var
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterDA authored May 31, 2024
2 parents 154f207 + 1cc743c commit bee850c
Show file tree
Hide file tree
Showing 17 changed files with 248 additions and 60 deletions.
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

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

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

- Be able to allocate and use user's formatters for stdout/stderr
(#399, @dinosaure)

- Stop detecting ocamlci specifically, since there's nothing specific
about it. Simply use the `CI` env var to detect CIs. Improve CI
detection.
Expand Down
2 changes: 2 additions & 0 deletions src/alcotest-engine/alcotest_engine.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ module Platform = Platform
module Private = struct
module Pp = Pp
end

module Formatters = Formatters
2 changes: 2 additions & 0 deletions src/alcotest-engine/alcotest_engine.mli
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ module Platform = Platform
module Private : sig
module Pp = Pp
end

module Formatters = Formatters
41 changes: 25 additions & 16 deletions src/alcotest-engine/cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module Make (P : Platform.MAKER) (M : Monad.S) :
alcotest_columns;
]

let set_color =
let set_color stdout stderr =
let env = Cmd.Env.info "ALCOTEST_COLOR" in
let+ color_flag =
let enum = [ ("auto", `Auto); ("always", `Ansi_tty); ("never", `None) ] in
Expand All @@ -102,49 +102,57 @@ module Make (P : Platform.MAKER) (M : Monad.S) :
Some `Ansi_tty
with Not_found -> None)
in
P.setup_std_outputs ?style_renderer ()
P.setup_std_outputs ?style_renderer stdout stderr

let default_cmd config args library_name tests =
let and_exit = Config.User.and_exit config
and record_backtrace = Config.User.record_backtrace config
and ci = Config.User.ci config in
and ci = Config.User.ci config
and stdout = Config.User.stdout config
and stderr = Config.User.stderr config in
let exec_name = Filename.basename Sys.argv.(0) in
let doc = "Run all the tests." in
let term =
let+ () = set_color
and+ cli_config = Config.User.term ~and_exit ~record_backtrace ~ci
let+ () = set_color stdout stderr
and+ cli_config =
Config.User.term ~stdout ~stderr ~and_exit ~record_backtrace ~ci
and+ args = args in
let config = Config.User.(cli_config || config) in
run_with_args' config library_name args tests
in
(term, Cmd.info exec_name ~doc ~envs)

let test_cmd config args library_name tests =
let ci = Config.User.ci config in
let ci = Config.User.ci config
and stdout = Config.User.stdout config
and stderr = Config.User.stderr config in
let doc = "Run a subset of the tests." in
let term =
let+ () = set_color
let+ () = set_color stdout stderr
and+ cli_config =
Config.User.term ~and_exit:true ~record_backtrace:true ~ci
Config.User.term ~stdout ~stderr ~and_exit:true ~record_backtrace:true
~ci
and+ args = args in
let config = Config.User.(cli_config || config) in
run_with_args' config library_name args tests
in
(term, Cmd.info "test" ~doc ~envs)

let list_cmd tests =
let list_cmd ~stdout ~stderr tests =
let doc = "List all available tests." in
( (let+ () = set_color in
( (let+ () = set_color stdout stderr in
list_tests tests),
Cmd.info "list" ~doc )

let run_with_args' (type a) ~argv config name (args : a Term.t)
(tl : a test list) =
let ( >>= ) = M.bind in
let stdout = Config.User.stdout config
and stderr = Config.User.stderr config in
let choices =
List.map
(fun (term, info) -> Cmd.v info term)
[ list_cmd tl; test_cmd config args name tl ]
[ list_cmd ~stdout ~stderr tl; test_cmd config args name tl ]
in
let and_exit = Config.User.and_exit config in
let exit_or_return exit_code =
Expand All @@ -162,11 +170,12 @@ module Make (P : Platform.MAKER) (M : Monad.S) :
| Error (`Parse | `Term) -> exit_or_return Cmd.Exit.cli_error
| Error `Exn -> exit Cmd.Exit.internal_error

let run_with_args ?and_exit ?verbose ?compact ?tail_errors ?quick_only
?show_errors ?json ?filter ?log_dir ?bail ?record_backtrace ?ci ?argv =
Config.User.kcreate (run_with_args' ~argv) ?and_exit ?verbose ?compact
?tail_errors ?quick_only ?show_errors ?json ?filter ?log_dir ?bail
?record_backtrace ?ci
let run_with_args ?stdout ?stderr ?and_exit ?verbose ?compact ?tail_errors
?quick_only ?show_errors ?json ?filter ?log_dir ?bail ?record_backtrace
?ci ?argv =
Config.User.kcreate (run_with_args' ~argv) ?stdout ?stderr ?and_exit
?verbose ?compact ?tail_errors ?quick_only ?show_errors ?json ?filter
?log_dir ?bail ?record_backtrace ?ci

let run =
Config.User.kcreate (fun config ?argv name tl ->
Expand Down
29 changes: 27 additions & 2 deletions src/alcotest-engine/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,22 @@ module User = struct
bail : Bail.t option;
record_backtrace : Record_backtrace.t option;
ci : CI.t option;
stdout : Formatters.stdout;
stderr : Formatters.stderr;
}

let ( || ) a b =
let merge_on f = Option.(f a || f b) in
let stdout =
merge_on @@ fun t ->
if t.stdout == Formatters.ocaml_stdout then None else Some t.stdout
in
let stderr =
merge_on @@ fun t ->
if t.stderr == Formatters.ocaml_stderr then None else Some t.stderr
in
let stdout = Option.value ~default:Formatters.ocaml_stdout stdout in
let stderr = Option.value ~default:Formatters.ocaml_stderr stderr in
{
and_exit = merge_on (fun t -> t.and_exit);
verbose = merge_on (fun t -> t.verbose);
Expand All @@ -262,9 +274,11 @@ module User = struct
bail = merge_on (fun t -> t.bail);
record_backtrace = merge_on (fun t -> t.record_backtrace);
ci = merge_on (fun t -> t.ci);
stdout;
stderr;
}

let term ~and_exit ~record_backtrace ~ci =
let term ~stdout ~stderr ~and_exit ~record_backtrace ~ci =
let+ verbose = Verbose.term
and+ compact = Compact.term
and+ tail_errors = Tail_errors.term
Expand All @@ -287,12 +301,15 @@ module User = struct
bail;
record_backtrace = Some record_backtrace;
ci = Some ci;
stdout;
stderr;
}

(* Lift a config-sensitive function to one that consumes optional arguments that
override config defaults. *)
let kcreate : 'a. (t -> 'a) -> 'a with_options =
fun f ?and_exit ?verbose ?compact ?tail_errors ?quick_only ?show_errors ?json
fun f ?(stdout = Formatters.ocaml_stdout) ?(stderr = Formatters.ocaml_stderr)
?and_exit ?verbose ?compact ?tail_errors ?quick_only ?show_errors ?json
?filter ?log_dir ?bail ?record_backtrace ?ci ->
f
{
Expand All @@ -308,6 +325,8 @@ module User = struct
bail;
record_backtrace;
ci;
stdout;
stderr;
}

let create : (unit -> t) with_options = kcreate (fun t () -> t)
Expand All @@ -317,6 +336,8 @@ module User = struct
Option.value ~default:Record_backtrace.default t.record_backtrace

let ci t = Option.value ~default:CI.default t.ci
let stdout t = t.stdout
let stderr t = t.stderr
end

let apply_defaults ~default_log_dir : User.t -> t =
Expand All @@ -333,6 +354,8 @@ let apply_defaults ~default_log_dir : User.t -> t =
bail;
record_backtrace;
ci;
stdout;
stderr;
} ->
let open Key in
object (self)
Expand All @@ -357,4 +380,6 @@ let apply_defaults ~default_log_dir : User.t -> t =
Option.value ~default:Record_backtrace.default record_backtrace

method ci = Option.value ~default:CI.default ci
method stdout = stdout
method stderr = stderr
end
17 changes: 15 additions & 2 deletions src/alcotest-engine/config_intf.ml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module Types = struct
type stdout = Formatters.stdout
type stderr = Formatters.stderr
type bound = [ `Unlimited | `Limit of int ]
type filter = name:string -> index:int -> [ `Run | `Skip ]

type ci = [ `Github_actions | `Unknown | `Disabled ]
(** All supported Continuous Integration (CI) systems. *)

type t =
< and_exit : bool
< stdout : stdout
; stderr : stderr
; and_exit : bool
; verbose : bool
; compact : bool
; tail_errors : bound
Expand All @@ -20,6 +24,8 @@ module Types = struct
; ci : ci >

type 'a with_options =
?stdout:stdout ->
?stderr:stderr ->
?and_exit:bool ->
?verbose:bool ->
?compact:bool ->
Expand Down Expand Up @@ -51,7 +57,12 @@ module type Config = sig
rather than returning directly. *)

val term :
and_exit:bool -> record_backtrace:bool -> ci:ci -> t Cmdliner.Term.t
stdout:Formatters.stdout ->
stderr:Formatters.stderr ->
and_exit:bool ->
record_backtrace:bool ->
ci:ci ->
t Cmdliner.Term.t
(** [term] provides a command-line interface for building configs. *)

val ( || ) : t -> t -> t
Expand All @@ -63,6 +74,8 @@ module type Config = sig
val and_exit : t -> bool
val record_backtrace : t -> bool
val ci : t -> ci
val stdout : t -> Formatters.stdout
val stderr : t -> Formatters.stderr
end

val apply_defaults : default_log_dir:string -> User.t -> t
Expand Down
Loading

0 comments on commit bee850c

Please sign in to comment.