Skip to content

Commit 251c36b

Browse files
authored
feature(boot): strip let%expect_test (#13794)
allows us to write ppx_expect tests in more places without depending on the ppx itself As an example, add some unit tests for Dune_sexp.Escape. These will be handy once we drop support for pre 4.14 Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
1 parent ef49a0f commit 251c36b

25 files changed

Lines changed: 240 additions & 28 deletions

.github/workflows/workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ jobs:
376376
run: opam install --fake binaryen-bin
377377

378378
- name: Install Wasm_of_ocaml
379-
run: opam install "wasm_of_ocaml-compiler>=6.1" csexp pp re spawn uutf
379+
run: opam install "wasm_of_ocaml-compiler>=6.1" csexp pp re spawn uutf ppx_expect
380380

381381
- name: Set Git User
382382
run: |

bench.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
FROM ocaml/opam:debian-13-ocaml-5.4
2-
RUN opam install csexp pp re spawn uutf
2+
RUN opam install csexp pp re spawn uutf ppx_expect
33
COPY --chown=opam:opam . bench-dir
44
WORKDIR bench-dir

boot/bootstrap.ml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ let keep_generated_files =
2525
!keep_generated_files
2626
;;
2727

28-
let modules = [ "boot/types"; "boot/libs"; "boot/duneboot" ]
28+
let pps = "boot/pps"
29+
let modules = pps :: [ "boot/types"; "boot/libs"; "boot/duneboot" ]
2930
let duneboot = ".duneboot"
3031
let prog = duneboot ^ ".exe"
3132

@@ -40,6 +41,8 @@ let () =
4041
if not keep_generated_files
4142
then
4243
at_exit (fun () ->
44+
(try Sys.remove "boot/pps.ml" with
45+
| Sys_error _ -> ());
4346
Array.iter (Sys.readdir ".") ~f:(fun fn ->
4447
if
4548
String.length fn >= String.length duneboot
@@ -71,9 +74,9 @@ let read_file fn =
7174

7275
let () =
7376
let v = Scanf.sscanf Sys.ocaml_version "%d.%d.%d" (fun a b c -> a, b, c) in
74-
let compiler, which =
77+
let compiler, ocamllex, which =
7578
if v >= min_supported_natively
76-
then "ocamlc", None
79+
then "ocamlc", "ocamllex", None
7780
else (
7881
let compiler = "ocamlfind -toolchain secondary ocamlc" in
7982
let output_fn, out = Filename.open_temp_file "duneboot" "ocamlfind-output" in
@@ -95,8 +98,10 @@ let () =
9598
a
9699
b);
97100
exit 2);
98-
compiler, Some "--secondary")
101+
let ocamllex = "ocamlfind -toolchain secondary ocamllex" in
102+
compiler, ocamllex, Some "--secondary")
99103
in
104+
exit_if_non_zero (runf "%s -q -o %s %s" ocamllex (pps ^ ".ml") (pps ^ ".mll"));
100105
exit_if_non_zero
101106
(runf
102107
"%s %s -intf-suffix .dummy -g -o %s -I boot %sunix.cma %s"

boot/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
(action
3232
(diff libs.ml libs.ml.gen)))
3333

34+
(ocamllex pps)
35+
3436
(executable
3537
(name duneboot)
3638
(modules :standard \ configure bootstrap)

boot/duneboot.ml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -539,24 +539,24 @@ module Io = struct
539539
| Ok s -> s
540540
;;
541541

542-
let do_then_copy ~f a b =
542+
let do_then_copy ~f ~pp a b =
543543
let s = read_file a in
544544
with_file_out b ~f:(fun oc ->
545545
f oc;
546-
output_string oc s)
546+
output_string oc (if pp then Pps.pp s else s))
547547
;;
548548

549549
(* copy a file - fails if the file exists *)
550-
let copy a b = do_then_copy ~f:(fun _ -> ()) a b
550+
let copy a b = do_then_copy ~pp:false ~f:(fun _ -> ()) a b
551551

552552
(* copy a file and insert a header - fails if the file exists *)
553-
let copy_with_header ~header a b =
554-
do_then_copy ~f:(fun oc -> output_string oc header) a b
553+
let copy_with_header ~pp ~header a b =
554+
do_then_copy ~pp ~f:(fun oc -> output_string oc header) a b
555555
;;
556556

557557
(* copy a file and insert a directive - fails if the file exists *)
558558
let copy_with_directive ~directive a b =
559-
do_then_copy ~f:(fun oc -> fprintf oc "#%s 1 %S\n" directive a) a b
559+
do_then_copy ~pp:false ~f:(fun oc -> fprintf oc "#%s 1 %S\n" directive a) a b
560560
;;
561561

562562
let rec rm_rf fn =
@@ -1610,8 +1610,11 @@ module Library = struct
16101610
| Header | C _ ->
16111611
Io.copy_with_directive ~directive:"line" fn dst;
16121612
Fiber.return [ mangled ]
1613-
| Ml { kind = `Ml | `Mli; _ } ->
1614-
Io.copy_with_header ~header fn dst;
1613+
| Ml { kind = `Mli; _ } ->
1614+
Io.copy_with_header ~pp:false ~header fn dst;
1615+
Fiber.return [ mangled ]
1616+
| Ml { kind = `Ml; _ } ->
1617+
Io.copy_with_header ~pp:true ~header fn dst;
16151618
Fiber.return [ mangled ]
16161619
| Ml { kind = `Mll; _ } -> copy_lexer fn dst ~header >>> Fiber.return [ mangled ]
16171620
| Ml { kind = `Mly; _ } ->

boot/pps.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val pp : string -> string

boot/pps.mll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
let b = Buffer.create 512
3+
}
4+
5+
rule pp = parse
6+
| "let%expect_test" { skip lexbuf }
7+
| _ as c { Buffer.add_char b c; pp lexbuf }
8+
| eof { () }
9+
10+
and skip = parse
11+
| ";;" { pp lexbuf }
12+
| _ { skip lexbuf }
13+
| eof { failwith "unterminated let%expect_test" }
14+
15+
{
16+
let pp s =
17+
let lb = Lexing.from_string s in
18+
Buffer.clear b;
19+
pp lb;
20+
Buffer.contents b
21+
}

ci/workflow.yml.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ jobs:
375375
run: opam install --fake binaryen-bin
376376

377377
- name: Install Wasm_of_ocaml
378-
run: opam install "wasm_of_ocaml-compiler>=6.1" csexp pp re spawn uutf
378+
run: opam install "wasm_of_ocaml-compiler>=6.1" csexp pp re spawn uutf ppx_expect
379379

380380
- name: Set Git User
381381
run: |

flake.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,9 @@
429429
buildInputs = with pkgs.ocamlPackages; [
430430
csexp
431431
pp
432-
# re shouldn't be needed. this is an issue with the fmt rules
432+
# Some additional dependencies are needed because formatting
433+
# promoted files in boot/ requires building them first
434+
ppx_expect
433435
re
434436
spawn
435437
uutf

src/dune_sexp/dune

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
(library
22
(name dune_sexp)
33
(synopsis "[Internal] S-expression library")
4+
(inline_tests)
5+
(preprocess
6+
(pps ppx_expect))
47
(libraries stdune uutf))
58

69
(ocamllex lexer versioned_file_first_line)

0 commit comments

Comments
 (0)