From 3c492e575cefb325a029562d5b0ff815eb77b98a Mon Sep 17 00:00:00 2001 From: "Aaron L. Zeng" Date: Tue, 22 Nov 2022 11:49:06 -0500 Subject: [PATCH] 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. --- lib/test/mdx_test.ml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/test/mdx_test.ml b/lib/test/mdx_test.ml index 9bf6cdd23..ff8de3f77 100644 --- a/lib/test/mdx_test.ml +++ b/lib/test/mdx_test.ml @@ -283,8 +283,9 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent match syntax with Some syntax -> Some syntax | None -> Syntax.infer ~file in let c = - Mdx_top.init ~verbose:(not silent_eval) ~silent ~verbose_findlib ~directives - ~packages ~predicates () + lazy + (Mdx_top.init ~verbose:(not silent_eval) ~silent ~verbose_findlib ~directives + ~packages ~predicates ()) in let preludes = preludes ~prelude ~prelude_str in @@ -304,7 +305,7 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent let det () = assert (syntax <> Some Cram); Mdx_top.in_env env (fun () -> - eval_ocaml ~block:t ?syntax ?root c ppf t.contents errors) + eval_ocaml ~block:t ?syntax ?root (Lazy.force c) ppf t.contents errors) in with_non_det non_deterministic non_det ~command:print_block ~output:det ~det @@ -337,7 +338,7 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent (fun (phrase : Toplevel.t) -> match Mdx_top.in_env env (fun () -> - eval_test ~block:t ?root c phrase.command) + eval_test ~block:t ?root (Lazy.force c) phrase.command) with | Ok _ -> () | Error e -> @@ -348,7 +349,7 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent ~det:(fun () -> assert (syntax <> Some Cram); Mdx_top.in_env env (fun () -> - run_toplevel_tests ?syntax ?root c ppf phrases t)) + run_toplevel_tests ?syntax ?root (Lazy.force c) ppf phrases t)) else print_block () in let gen_corrected file_contents items = @@ -357,7 +358,7 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent let buf = Buffer.create (String.length file_contents + 1024) in let ppf = Format.formatter_of_buffer buf in let envs = Document.envs items in - let eval lines () = eval_raw ?root c lines in + let eval lines () = eval_raw ?root (Lazy.force c) lines in let eval_in_env lines env = Mdx_top.in_env env (eval lines) in List.iter (function