Skip to content

Commit e16e8cd

Browse files
committed
mdx_test: Lazily initialize OCaml toplevel
This changes the OCaml toplevel to be a lazy value, which is only forced when needed to run tests in an OCaml block. The motivation behind this change is to avoid stderr output from the init process (e.g., errors while registering pretty-printers) spilling into the test executable's output, which can trip up some build tools.
1 parent 1f83f73 commit e16e8cd

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/test/mdx_test.ml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,9 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent
332332
match syntax with Some syntax -> Some syntax | None -> Syntax.infer ~file
333333
in
334334
let c =
335-
Mdx_top.init ~verbose:(not silent_eval) ~silent ~verbose_findlib ~directives
336-
~packages ~predicates ()
335+
lazy
336+
(Mdx_top.init ~verbose:(not silent_eval) ~silent ~verbose_findlib
337+
~directives ~packages ~predicates ())
337338
in
338339
let preludes = preludes ~prelude ~prelude_str in
339340

@@ -350,6 +351,7 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent
350351
let new_content = read_part file_included None in
351352
update_block_content ?syntax ppf t new_content
352353
| OCaml { non_det; env; errors; header = _ } ->
354+
let c = Lazy.force c in
353355
let det () =
354356
assert (syntax <> Some Cram);
355357
Mdx_top.in_env env (fun () ->
@@ -369,6 +371,7 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent
369371
~on_evaluation:(fun () ->
370372
run_cram_tests ?syntax t ?root ppf temp_file tests)
371373
| Toplevel { non_det; env } ->
374+
let c = Lazy.force c in
372375
let phrases = Toplevel.of_lines ~loc:t.loc t.contents in
373376
with_non_det non_deterministic non_det ~on_skip_execution:print_block
374377
~on_keep_old_output:(fun () ->
@@ -398,8 +401,11 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent
398401
let buf = Buffer.create (String.length file_contents + 1024) in
399402
let ppf = Format.formatter_of_buffer buf in
400403
let envs = Document.envs items in
401-
let eval lines () = eval_raw ?root c lines in
402-
let eval_in_env lines env = Mdx_top.in_env env (eval lines) in
404+
let eval c lines () = eval_raw ?root c lines in
405+
let eval_in_env lines env =
406+
let c = Lazy.force c in
407+
Mdx_top.in_env env (eval c lines)
408+
in
403409
List.iter
404410
(function
405411
| `All, lines -> Ocaml_env.Set.iter (eval_in_env lines) envs

0 commit comments

Comments
 (0)