@@ -190,10 +190,19 @@ let rec remove_padding ?(front = true) = function
190190 let xs = remove_padding ~front xs in
191191 x :: xs
192192
193- let update_ocaml ~errors = function
194- | { Block. value = OCaml v ; _ } as b ->
195- { b with value = OCaml { v with errors } }
196- (* [eval_ocaml] only called on OCaml blocks *)
193+ let update_errors ~errors t =
194+ let update_ocaml_value (ov : Block.ocaml_value ) = { ov with errors } in
195+ match t.Block. value with
196+ | OCaml v -> { t with value = OCaml (update_ocaml_value v) }
197+ | Include
198+ ({ file_kind = Fk_ocaml ({ ocaml_value = Some v; _ } as fk); _ } as i) ->
199+ let ocaml_value = Some (update_ocaml_value v) in
200+ let file_kind = Block. Fk_ocaml { fk with ocaml_value } in
201+ { t with value = Include { i with file_kind } }
202+ | _ -> assert false
203+
204+ let update_include ~contents = function
205+ | { Block. value = Include _ ; _ } as b -> { b with contents }
197206 | _ -> assert false
198207
199208let rec error_padding = function
@@ -206,7 +215,7 @@ let rec error_padding = function
206215let contains_warnings l =
207216 String. is_prefix ~affix: " Warning" l || String. is_infix ~affix: " \n Warning" l
208217
209- let eval_ocaml ~(block : Block.t ) ?syntax ? root c ppf errors =
218+ let eval_ocaml ~(block : Block.t ) ?root c errors =
210219 let cmd = block.contents |> remove_padding in
211220 let error_lines =
212221 match eval_test ?root ~block c cmd with
@@ -229,8 +238,7 @@ let eval_ocaml ~(block : Block.t) ?syntax ?root c ppf errors =
229238 | `Output x -> `Output (ansi_color_strip x))
230239 (Output. merge output errors)
231240 in
232- let updated_block = update_ocaml ~errors block in
233- Block. pp ?syntax ppf updated_block
241+ update_errors ~errors block
234242
235243let lines = function Ok x | Error x -> x
236244
@@ -278,9 +286,12 @@ let read_part file part =
278286 (match part with None -> " " | Some p -> p)
279287 file
280288 | Some lines ->
289+ (* in any [string] element of lines, there might be newlines. *)
281290 let contents = String. concat ~sep: " \n " lines in
282291 String. drop contents ~rev: true ~sat: Char.Ascii. is_white
283292 |> String. drop ~sat: (function '\n' -> true | _ -> false )
293+ |> (fun contents -> " \n " ^ contents ^ " \n " )
294+ |> String. cuts ~sep: " \n "
284295
285296let write_parts ~force_output file parts =
286297 let output_file = file ^ " .corrected" in
@@ -292,18 +303,13 @@ let write_parts ~force_output file parts =
292303 flush oc;
293304 close_out oc
294305
295- let update_block_content ?syntax ppf t content =
296- Block. pp_header ?syntax ppf t;
297- Fmt. string ppf " \n " ;
298- Output. pp ppf (`Output content);
299- Fmt. string ppf " \n " ;
300- Block. pp_footer ?syntax ppf t
301-
302- let update_file_or_block ?syntax ?root ppf md_file ml_file block part =
306+ let update_file_or_block ?root md_file ml_file block part =
303307 let root = root_dir ?root ~block () in
304308 let dir = Filename. dirname md_file in
305309 let ml_file = resolve_root ml_file dir root in
306- update_block_content ?syntax ppf block (read_part ml_file part)
310+ let contents = read_part ml_file part in
311+ let new_block = update_include ~contents block in
312+ new_block
307313
308314exception Test_block_failure of Block. t * string
309315
@@ -337,26 +343,44 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent
337343 in
338344 let preludes = preludes ~prelude ~prelude_str in
339345
346+ let run_ocaml_value t Block. { env; non_det; errors; header = _ ; _ } =
347+ let det () =
348+ Mdx_top. in_env env (fun () -> eval_ocaml ~block: t ?root c errors)
349+ in
350+ with_non_det non_deterministic non_det
351+ ~on_skip_execution: (fun () -> t)
352+ ~on_keep_old_output: det ~on_evaluation: det
353+ in
354+
340355 let test_block ~ppf ~temp_file t =
341356 let print_block () = Block. pp ?syntax ppf t in
342357 if Block. is_active ?section t then
343358 match Block. value t with
344359 | Raw _ -> print_block ()
345- | Include { file_included; file_kind = Fk_ocaml { part_included } } ->
360+ | Include
361+ {
362+ file_included;
363+ file_kind = Fk_ocaml { part_included; ocaml_value; _ };
364+ } ->
346365 assert (syntax <> Some Cram );
347- update_file_or_block ?syntax ?root ppf file file_included t
348- part_included
349- | Include { file_included; file_kind = Fk_other _ } ->
350- let new_content = read_part file_included None in
351- update_block_content ?syntax ppf t new_content
352- | OCaml { non_det; env; errors; header = _ } ->
353- let det () =
354- assert (syntax <> Some Cram );
355- Mdx_top. in_env env (fun () ->
356- eval_ocaml ~block: t ?syntax ?root c ppf errors)
366+ let new_block =
367+ update_file_or_block ?root file file_included t part_included
357368 in
358- with_non_det non_deterministic non_det ~on_skip_execution: print_block
359- ~on_keep_old_output: det ~on_evaluation: det
369+ let updated_block =
370+ match ocaml_value with
371+ (* including without executing *)
372+ | Some _ when t.skip -> new_block
373+ | Some ocaml_value -> run_ocaml_value new_block ocaml_value
374+ | _ -> new_block
375+ in
376+ Block. pp ?syntax ppf updated_block
377+ | Include { file_included; file_kind = Fk_other _ } ->
378+ let contents = read_part file_included None in
379+ let new_block = update_include ~contents t in
380+ Block. pp ?syntax ppf new_block
381+ | OCaml ov ->
382+ let updated_block = run_ocaml_value t ov in
383+ Block. pp ?syntax ppf updated_block
360384 | Cram { language = _ ; non_det } ->
361385 let tests = Cram. of_lines t.contents in
362386 with_non_det non_deterministic non_det ~on_skip_execution: print_block
0 commit comments