1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
include Config_intf
include Config_intf.Types
open! Import
open Cmdliner_syntax
module Key = struct
module type S = sig
type t
val default : t
end
module type Cli = sig
include S
val term : t option Cmdliner.Term.t
end
module Flag (X : sig
val term : bool Cmdliner.Term.t
end) : Cli with type t = bool = struct
type t = bool
let default = false
let term = X.term >>| function true -> Some true | false -> None
end
(** {1 Definitions of supported keys} *)
module Arg = Cmdliner.Arg
module Cmd = Cmdliner.Cmd
module And_exit = struct
type t = bool
let default = true
end
module Record_backtrace = struct
type t = bool
let default = true
end
module CI = struct
type t = ci
let default =
let getenv var =
match Sys.getenv var with
| "true" | "True" -> true
| _ | (exception Not_found) -> false
in
let ci = getenv "CI" and github_actions = getenv "GITHUB_ACTIONS" in
match (ci, github_actions) with
| true, true -> `Github_actions
| true, false -> `Unknown
| _ -> `Disabled
end
module Verbose = Flag (struct
let term =
let env = Cmd.Env.info "ALCOTEST_VERBOSE" in
let doc =
"Display the test outputs. $(b,WARNING:) when using this option the \
output logs will not be available for further inspection."
in
Arg.(value & flag & info ~env [ "v"; "verbose" ] ~docv:"" ~doc)
end)
module Compact = Flag (struct
let term =
let env = Cmd.Env.info "ALCOTEST_COMPACT" in
let doc = "Compact the output of the tests." in
Arg.(value & flag & info ~env [ "c"; "compact" ] ~docv:"" ~doc)
end)
module Bail = Flag (struct
let term =
let env = Cmd.Env.info "ALCOTEST_BAIL" in
let doc = "Stop running tests after the first failure." in
Arg.(value & flag & info ~env [ "bail" ] ~docv:"" ~doc)
end)
module Json = Flag (struct
let term =
let doc = "Display JSON for the results, to be used by a script." in
Arg.(value & flag & info [ "json" ] ~docv:"" ~doc)
end)
module Show_errors = Flag (struct
let term =
let env = Cmd.Env.info "ALCOTEST_SHOW_ERRORS" in
let doc = "Display the test errors." in
Arg.(value & flag & info ~env [ "e"; "show-errors" ] ~docv:"" ~doc)
end)
module Quick_only = Flag (struct
let term =
let env = Cmd.Env.info "ALCOTEST_QUICK_TESTS" in
let doc = "Run only the quick tests." in
Arg.(value & flag & info ~env [ "q"; "quick-tests" ] ~docv:"" ~doc)
end)
module Tail_errors = struct
type t = bound
let default = `Unlimited
let limit_parser s =
match s with
| "unlimited" -> Ok `Unlimited
| s -> (
try
let n = int_of_string s in
if n < 0 then
Error (`Msg "numeric limit must be nonnegative or 'unlimited'")
else Ok (`Limit n)
with Failure _ -> Error (`Msg "invalid numeric limit"))
let limit_printer ppf limit =
match limit with
| `Unlimited -> Fmt.pf ppf "unlimited"
| `Limit n -> Fmt.pf ppf "%i" n
let limit = Arg.conv (limit_parser, limit_printer)
let term =
let env = Cmd.Env.info "ALCOTEST_TAIL_ERRORS" in
let doc =
"Show only the last $(docv) lines of output in case of an error."
in
Arg.(
value
& opt (some limit) None
& info ~env [ "tail-errors" ] ~docv:"N" ~doc)
end
module Log_dir = struct
type t = string option
let term =
let doc = "Where to store the log files of the tests." in
Arg.(value & opt (some dir) None & info [ "o" ] ~docv:"DIR" ~doc)
end
module Filter = struct
type t = filter
let regex : Re.re Arg.conv =
let parse s =
try Ok Re.(compile @@ Pcre.re s) with
| Re.Perl.Parse_error ->
Error (`Msg "Perl-compatible regexp parse error")
| Re.Perl.Not_supported -> Error (`Msg "unsupported regexp feature")
in
let print = Re.pp_re in
Arg.conv (parse, print)
let int_range_list : int list Arg.conv =
let exception Invalid_format in
let parse s =
let rec range lower upper acc =
if lower > upper then acc else range (succ lower) upper (lower :: acc)
in
let process_range acc s =
String.cuts ~sep:".." s
|> List.concat_map (String.cuts ~sep:"-")
|> List.map String.to_int
|> function
| [ Some i ] -> i :: acc
| [ Some lower; Some upper ] when lower <= upper ->
range lower upper acc
| _ -> raise Invalid_format
in
let ranges = String.cuts ~sep:"," s in
match List.fold_left process_range [] ranges with
| list -> Ok list
| exception Invalid_format ->
Error
(`Msg
"must be a comma-separated list of integers / integer ranges")
in
let print ppf set = Fmt.(braces @@ list ~sep:comma int) ppf set in
Arg.conv (parse, print)
let term =
let+ name_regex =
let doc = "A regular expression matching the names of tests to run" in
Arg.(value & pos 0 (some regex) None & info [] ~doc ~docv:"NAME_REGEX")
and+ index_cases =
let doc =
"A comma-separated list of test case numbers (and ranges of numbers) \
to run, e.g: '4,6-10,19'. When specifying ranges, both '-' and '..' \
are accepted as valid separators."
in
Arg.(
value
& pos 1 (some int_range_list) None
& info [] ~doc ~docv:"TESTCASES")
in
match (name_regex, index_cases) with
| None, None -> None
| _, _ ->
let name_filter =
match name_regex with
| None -> fun _ -> true
| Some r -> fun n -> Re.execp r n
in
let index_filter =
match index_cases with
| None -> fun _ -> true
| Some ints ->
let set = Int.Set.of_list ints in
fun i -> Int.Set.mem i set
in
Some
(fun ~name ~index ->
if name_filter name && index_filter index then `Run else `Skip)
end
end
module User = struct
open Key
type t = {
and_exit : And_exit.t option;
verbose : Verbose.t option;
compact : Compact.t option;
tail_errors : Tail_errors.t option;
quick_only : Quick_only.t option;
show_errors : Show_errors.t option;
json : Json.t option;
filter : Filter.t option;
log_dir : Log_dir.t;
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);
compact = merge_on (fun t -> t.compact);
tail_errors = merge_on (fun t -> t.tail_errors);
quick_only = merge_on (fun t -> t.quick_only);
show_errors = merge_on (fun t -> t.show_errors);
json = merge_on (fun t -> t.json);
filter = merge_on (fun t -> t.filter);
log_dir = merge_on (fun t -> t.log_dir);
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 ~stdout ~stderr ~and_exit ~record_backtrace ~ci =
let+ verbose = Verbose.term
and+ compact = Compact.term
and+ tail_errors = Tail_errors.term
and+ show_errors = Show_errors.term
and+ quick_only = Quick_only.term
and+ json = Json.term
and+ filter = Filter.term
and+ log_dir = Log_dir.term
and+ bail = Bail.term in
{
and_exit = Some and_exit;
verbose;
compact;
tail_errors;
show_errors;
quick_only;
json;
filter;
log_dir;
bail;
record_backtrace = Some record_backtrace;
ci = Some ci;
stdout;
stderr;
}
let kcreate : 'a. (t -> 'a) -> 'a with_options =
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
{
and_exit;
verbose;
compact;
tail_errors;
quick_only;
show_errors;
json;
filter;
log_dir;
bail;
record_backtrace;
ci;
stdout;
stderr;
}
let create : (unit -> t) with_options = kcreate (fun t () -> t)
let and_exit t = Option.value ~default:And_exit.default t.and_exit
let record_backtrace t =
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 =
fun {
and_exit;
verbose;
compact;
tail_errors;
quick_only;
show_errors;
json;
filter;
log_dir;
bail;
record_backtrace;
ci;
stdout;
stderr;
} ->
let open Key in
object (self)
method and_exit = Option.value ~default:And_exit.default and_exit
method verbose = Option.value ~default:Verbose.default verbose
method compact = Option.value ~default:Compact.default compact
method tail_errors = Option.value ~default:Tail_errors.default tail_errors
method quick_only = Option.value ~default:Quick_only.default quick_only
method show_errors =
match (show_errors, self#ci) with
| Some show_errors, _ -> show_errors
| None, `Disabled -> Show_errors.default
| None, _ -> true
method json = Option.value ~default:Json.default json
method filter = filter
method log_dir = Option.value ~default:default_log_dir log_dir
method bail = Option.value ~default:Bail.default bail
method record_backtrace =
Option.value ~default:Record_backtrace.default record_backtrace
method ci = Option.value ~default:CI.default ci
method stdout = stdout
method stderr = stderr
end