Skip to content

Commit fc7132d

Browse files
Merge pull request #366 from Leonidas-from-XIV/dune-gen-non-deterministic
Allow running non-deterministic MDX stanzas using `dune-gen`
2 parents ba41f92 + 122e46b commit fc7132d

File tree

7 files changed

+134
-2
lines changed

7 files changed

+134
-2
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
- Add support for adding language tags and metadata labels in `mli` files.
66
(#339, #357, @julow, @Leonidas-from-XIV)
7+
- Add support for running non-deterministic tests in `dune` MDX 0.2 stanza by
8+
setting the `MDX_RUN_NON_DETERMINISTIC` environment variable. (#365,
9+
#366, @Leonidas-from-XIV)
710

811
#### Changed
912

bin/dune_gen.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ let run (`Setup ()) (`Prelude prelude) (`Directories dirs) =
3232
line " ]";
3333
line " in";
3434
line " let predicates = Predicate.[ byte; toploop ] in";
35+
line " let non_deterministic =";
36+
line " match Sys.getenv_opt \"MDX_RUN_NON_DETERMINISTIC\" with";
37+
line " | Some _ -> true";
38+
line " | None -> false";
39+
line " in";
3540
line " run_exn ~packages ~predicates ~prelude_str:[]";
36-
line " ~non_deterministic:false";
41+
line " ~non_deterministic";
3742
line " ~silent_eval:false ~record_backtrace:false";
3843
line " ~syntax:None ~silent:false";
3944
line " ~verbose_findlib:false ~section:None";

test/bin/mdx-dune-gen/misc/basic/dune.gen.expected

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ let run_exn_defaults =
99
]
1010
in
1111
let predicates = Predicate.[ byte; toploop ] in
12+
let non_deterministic =
13+
match Sys.getenv_opt "MDX_RUN_NON_DETERMINISTIC" with
14+
| Some _ -> true
15+
| None -> false
16+
in
1217
run_exn ~packages ~predicates ~prelude_str:[]
13-
~non_deterministic:false
18+
~non_deterministic
1419
~silent_eval:false ~record_backtrace:false
1520
~syntax:None ~silent:false
1621
~verbose_findlib:false ~section:None
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
(* small helper to determine whether two files differ *)
2+
3+
type comparison = Same | Different
4+
5+
let rec compare first second =
6+
match input_line first with
7+
| first_line -> (
8+
match input_line second with
9+
| second_line ->
10+
match String.equal first_line second_line with
11+
| true -> compare first second
12+
| false ->
13+
(* we found a difference between the lines *)
14+
Different
15+
| exception End_of_file ->
16+
(* the second file ended before the first *)
17+
Different)
18+
| exception End_of_file ->
19+
(* the first file ended first *)
20+
match input_line second with
21+
| _ ->
22+
(* the second file continues: a difference *)
23+
Different
24+
| exception End_of_file ->
25+
(* the second file ended too *)
26+
Same
27+
28+
let main () =
29+
let first = Sys.argv.(1) |> open_in in
30+
let second = Sys.argv.(2) |> open_in in
31+
let comparison = compare first second in
32+
close_in first;
33+
close_in second;
34+
match comparison with
35+
| Same ->
36+
prerr_endline "The files appear to be identical";
37+
(* we didn't find a difference, exit with a failure code *)
38+
exit 1
39+
| Different -> ()
40+
41+
let () =
42+
main ()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
(rule
2+
(with-stdout-to
3+
dune_gen.ml
4+
(run ocaml-mdx dune-gen)))
5+
6+
(executable
7+
(name dune_gen)
8+
(modules dune_gen)
9+
(modes byte)
10+
(libraries mdx.test))
11+
12+
(rule
13+
(with-stdout-to
14+
dune-mdx-nondeterministic.deterministic
15+
(run ./dune_gen.exe %{dep:dune-mdx-nondeterministic})))
16+
17+
(rule
18+
(setenv
19+
MDX_RUN_NON_DETERMINISTIC
20+
1
21+
(with-stdout-to
22+
dune-mdx-nondeterministic.nondeterministic
23+
(run ./dune_gen.exe %{dep:dune-mdx-nondeterministic}))))
24+
25+
(rule
26+
(alias runtest)
27+
(action
28+
(diff dune-mdx-nondeterministic.expected
29+
dune-mdx-nondeterministic.deterministic)))
30+
31+
;; make sure the non-deterministic is different from the deterministic
32+
33+
(executable
34+
(name different)
35+
(modules different))
36+
37+
(rule
38+
(alias runtest)
39+
(action
40+
(run ./different.exe %{dep:dune-mdx-nondeterministic.expected}
41+
%{dep:dune-mdx-nondeterministic.nondeterministic})))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
This test checks whether the non-deterministic mode works with the `dune` `mdx`
2+
stanza.
3+
4+
Deterministic stanzas should get run and corrected, as for 1 plus one is not 3:
5+
6+
```ocaml
7+
# 1 + 1;;
8+
- : int = 42
9+
```
10+
11+
Non-deterministic ones should not be updated, since whatever `Random` outputs
12+
should be random:
13+
14+
<!-- $MDX non-deterministic=command -->
15+
```ocaml
16+
# Random.int 1000;;
17+
- : int = 42
18+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
This test checks whether the non-deterministic mode works with the `dune` `mdx`
2+
stanza.
3+
4+
Deterministic stanzas should get run and corrected, as for 1 plus one is not 3:
5+
6+
```ocaml
7+
# 1 + 1;;
8+
- : int = 2
9+
```
10+
11+
Non-deterministic ones should not be updated, since whatever `Random` outputs
12+
should be random:
13+
14+
<!-- $MDX non-deterministic=command -->
15+
```ocaml
16+
# Random.int 1000;;
17+
- : int = 42
18+
```

0 commit comments

Comments
 (0)