From f1e4c3201fdbacf3dcff9ae894933af503b55596 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 bbed0cd3..c81961cb 100644 --- a/lib/test/mdx_test.ml +++ b/lib/test/mdx_test.ml @@ -332,8 +332,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 @@ -353,7 +354,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 errors) + eval_ocaml ~block:t ?syntax ?root (Lazy.force c) ppf errors) in with_non_det non_deterministic non_det ~on_skip_execution:print_block ~on_keep_old_output:det ~on_evaluation:det @@ -378,7 +379,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 -> @@ -389,7 +390,7 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent ~on_evaluation:(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 = @@ -398,7 +399,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