@@ -259,17 +259,31 @@ let line_number =
259
259
seq [ set " 123456789" ; rep digit ]
260
260
;;
261
261
262
- let rewrite_paths build_path_prefix_map ~parent_script ~command_script s =
262
+ let absolute_paths =
263
+ let not_dir = Printf. sprintf " \n\r\t %c" Bin. path_sep in
264
+ Re. (compile (seq [ char '/' ; rep1 (diff any (set not_dir)) ]))
265
+ ;;
266
+
267
+ let known_paths map =
268
+ List. filter_map
269
+ ~f: (function
270
+ | None | Some { Build_path_prefix_map. source = "" ; _ } -> None
271
+ | Some pair -> Some (Re. str pair.source))
272
+ map
273
+ |> List. rev
274
+ (* prefer right-most paths in the list, as required by the build-path-prefix-map spec *)
275
+ |> Re. alt
276
+ |> Re. compile
277
+ ;;
278
+
279
+ let rewrite_paths ~version ~build_path_prefix_map ~parent_script ~command_script s =
263
280
match Build_path_prefix_map. decode_map build_path_prefix_map with
264
281
| Error msg ->
265
282
Code_error. raise
266
283
" Cannot decode build prefix map"
267
284
[ " build_path_prefix_map" , String build_path_prefix_map; " msg" , String msg ]
268
285
| Ok map ->
269
- let abs_path_re =
270
- let not_dir = Printf. sprintf " \n\r\t %c" Bin. path_sep in
271
- Re. (compile (seq [ char '/' ; rep1 (diff any (set not_dir)) ]))
272
- in
286
+ let known_paths = if version < (3 , 18 ) then absolute_paths else known_paths map in
273
287
let error_msg =
274
288
let open Re in
275
289
let command_script = str (Path. to_absolute_filename command_script) in
@@ -281,12 +295,12 @@ let rewrite_paths build_path_prefix_map ~parent_script ~command_script s =
281
295
let b = seq [ command_script; str " : line " ; line_number; str " : " ] in
282
296
[ a; b ] |> List. map ~f: (fun re -> seq [ bol; re ]) |> alt |> compile
283
297
in
284
- Re. replace abs_path_re s ~f: (fun g ->
298
+ Re. replace ~all: true known_paths s ~f: (fun g ->
285
299
Build_path_prefix_map. rewrite map (Re.Group. get g 0 ))
286
300
|> Re. replace_string error_msg ~by: " "
287
301
;;
288
302
289
- let sanitize ~parent_script cram_to_output
303
+ let sanitize ~version ~ parent_script cram_to_output
290
304
: (block_result * metadata_result * string ) Cram_lexer. block list
291
305
=
292
306
List. map cram_to_output ~f: (fun (t : (block_result * _) Cram_lexer.block ) ->
@@ -300,9 +314,10 @@ let sanitize ~parent_script cram_to_output
300
314
Io. read_file ~binary: false block_result.output_file
301
315
|> Ansi_color. strip
302
316
|> rewrite_paths
317
+ ~version
303
318
~parent_script
304
319
~command_script: block_result.script
305
- build_path_prefix_map
320
+ ~ build_path_prefix_map
306
321
in
307
322
Command (block_result, metadata, output))
308
323
;;
@@ -389,7 +404,7 @@ let create_sh_script cram_stanzas ~temp_dir : sh_script Fiber.t =
389
404
390
405
let _display_with_bars s = List. iter (String. split_lines s) ~f: (Printf. eprintf " | %s\n " )
391
406
392
- let run ~env ~script lexbuf : string Fiber.t =
407
+ let run ~version ~ env ~script lexbuf : string Fiber.t =
393
408
let temp_dir =
394
409
let suffix =
395
410
let basename = Path. basename script in
@@ -406,19 +421,16 @@ let run ~env ~script lexbuf : string Fiber.t =
406
421
let open Fiber.O in
407
422
let * sh_script = create_sh_script cram_stanzas ~temp_dir in
408
423
let cwd = Path. parent_exn script in
424
+ let temp_dir = Path. relative temp_dir " tmp" in
425
+ Path. mkdir_p temp_dir;
409
426
let env =
410
427
let env = Env. add env ~var: " LC_ALL" ~value: " C" in
411
- let temp_dir = Path. relative temp_dir " tmp" in
412
- let env =
413
- Dune_util.Build_path_prefix_map. extend_build_path_prefix_map
414
- env
415
- `New_rules_have_precedence
416
- [ Some { source = Path. to_absolute_filename cwd; target = " $TESTCASE_ROOT" }
417
- ; Some { source = Path. to_absolute_filename temp_dir; target = " $TMPDIR" }
418
- ]
419
- in
420
- Path. mkdir_p temp_dir;
421
- Env. add env ~var: Env.Var. temp_dir ~value: (Path. to_absolute_filename temp_dir)
428
+ Dune_util.Build_path_prefix_map. extend_build_path_prefix_map
429
+ env
430
+ `New_rules_have_precedence
431
+ [ Some { source = Path. to_absolute_filename cwd; target = " $TESTCASE_ROOT" }
432
+ ; Some { source = Path. to_absolute_filename temp_dir; target = " $TMPDIR" }
433
+ ]
422
434
in
423
435
let open Fiber.O in
424
436
let + () =
@@ -442,29 +454,38 @@ let run ~env ~script lexbuf : string Fiber.t =
442
454
~display: Quiet
443
455
~metadata
444
456
~dir: cwd
457
+ ~temp_dir
445
458
~env
446
459
Strict
447
460
sh
448
461
[ Path. to_string sh_script.script ]
449
462
in
450
463
let raw = read_and_attach_exit_codes sh_script in
451
- let sanitized = sanitize ~parent_script: sh_script.script raw in
464
+ let sanitized = sanitize ~version ~ parent_script: sh_script.script raw in
452
465
compose_cram_output sanitized
453
466
;;
454
467
455
- let run ~env ~script = run_expect_test script ~f: (fun lexbuf -> run ~env ~script lexbuf)
468
+ let run ~version ~env ~script =
469
+ run_expect_test script ~f: (fun lexbuf -> run ~version ~env ~script lexbuf)
470
+ ;;
456
471
457
472
module Spec = struct
458
- type ('path, _) t = 'path
473
+ type ('path, _) t = Dune_lang.Syntax.Version .t * 'path
459
474
460
475
let name = " cram"
461
476
let version = 2
462
- let bimap path f _ = f path
477
+ let bimap ( version , path ) f _ = version, f path
463
478
let is_useful_to ~memoize :_ = true
464
- let encode script path _ : Sexp.t = List [ path script ]
465
- let action script ~ectx :_ ~(eenv : Action.env ) = run ~env: eenv.env ~script
479
+
480
+ let encode (version , script ) path _ : Sexp.t =
481
+ List [ Dune_lang.Syntax.Version. encode version |> Dune_sexp. to_sexp; path script ]
482
+ ;;
483
+
484
+ let action (version , script ) ~ectx :_ ~(eenv : Action.env ) =
485
+ run ~version ~env: eenv.env ~script
486
+ ;;
466
487
end
467
488
468
489
module Action = Action_ext. Make (Spec )
469
490
470
- let action = Action. action
491
+ let action ~ version path = Action. action (version, path)
0 commit comments