Skip to content

Commit

Permalink
Add tests for failures
Browse files Browse the repository at this point in the history
Co-authored-by: Jules Aguillon <juloo.dsi@gmail.com>
  • Loading branch information
craigfe and Julow committed Oct 3, 2019
1 parent 9a4224c commit 46d23a7
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 23 deletions.
3 changes: 1 addition & 2 deletions test/bin/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
(executable
(name gen_dune_rules)
(libraries cmdliner fmt unix)
(modules gen_dune_rules)
)
(modules gen_dune_rules))
37 changes: 31 additions & 6 deletions test/bin/gen_dune_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ let read_file filename =
close_in chan;
List.rev !lines

let expected_file file =
let e = file ^ ".expected" in
if not (Sys.file_exists e) then
Fmt.failwith "No %s file found when generating an expect rule" e;
e

(* Copied from Filename (stdlib) for pre-4.04 compatibility *)
let chop_extension name =
let is_dir_sep s i = match Sys.os_type with
Expand Down Expand Up @@ -71,11 +77,7 @@ let test_fixpoint_rule file =
(* Tests that the output of 'ocaml-mdx test [options] <file>' is equal to the
contents of '<file>.expected'. *)
let test_expect_rule file =
let expected_file = file ^ ".expected" in
if not (Sys.file_exists expected_file) then
failwith (
Fmt.strf "No %s file found when generating an expect rule" expected_file
);
let expected_file = expected_file file in
let options = options_of_file file in
Fmt.pr
{|
Expand All @@ -93,6 +95,27 @@ let test_expect_rule file =
expected_file
pp_options options

let test_failure_rule file =
let expected_file = expected_file file in
let options = options_of_file file in
let target_file = file ^ ".actual" in
Fmt.pr
{|
(rule
(targets %s)
(action
(with-outputs-to %%{targets}
(system "! %%{bin:ocaml-mdx} test %a %%{dep:%s}"))))

(alias
(name runtest)
(action
(diff %s %s)))
|}
target_file
pp_options options file
expected_file target_file

let rule_gen rule_type () =
Sys.readdir "."
|> Array.to_list
Expand All @@ -101,14 +124,16 @@ let rule_gen rule_type () =
|> List.iter (match rule_type with
| `Test_fixpoint -> test_fixpoint_rule
| `Test_expect -> test_expect_rule
| `Test_failure -> test_failure_rule
)

open Cmdliner

let cmds =
Term.[
const (rule_gen `Test_fixpoint) $ const (), info "test_fixpoint";
const (rule_gen `Test_expect) $ const (), info "test_expect"
const (rule_gen `Test_expect) $ const (), info "test_expect";
const (rule_gen `Test_failure) $ const (), info "test_failure";
]

let default =
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions test/bin/test_failure/both_prelude.md.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
only one of --prelude or --prelude-str shoud be used
1 change: 1 addition & 0 deletions test/bin/test_failure/both_prelude.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--prelude=a --prelude-str=b
12 changes: 12 additions & 0 deletions test/bin/test_failure/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(include dune.inc)

(rule
(targets dune.gen)
(deps (source_tree .))
(action
(with-stdout-to %{targets}
(run ../gen_dune_rules.exe test_failure))))

(alias
(name runtest)
(action (diff dune.inc dune.gen)))
44 changes: 44 additions & 0 deletions test/bin/test_failure/dune.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

(rule
(targets both_prelude.md.actual)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocaml-mdx} test --prelude=a --prelude-str=b %{dep:both_prelude.md}"))))

(alias
(name runtest)
(action
(diff both_prelude.md.expected both_prelude.md.actual)))

(rule
(targets in_toplevel.md.actual)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocaml-mdx} test %{dep:in_toplevel.md}"))))

(alias
(name runtest)
(action
(diff in_toplevel.md.expected in_toplevel.md.actual)))

(rule
(targets ml_file_not_found.md.actual)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocaml-mdx} test %{dep:ml_file_not_found.md}"))))

(alias
(name runtest)
(action
(diff ml_file_not_found.md.expected ml_file_not_found.md.actual)))

(rule
(targets part_not_found.md.actual)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocaml-mdx} test --direction=to-md %{dep:part_not_found.md}"))))

(alias
(name runtest)
(action
(diff part_not_found.md.expected part_not_found.md.actual)))
File renamed without changes.
2 changes: 2 additions & 0 deletions test/bin/test_failure/in_toplevel.md.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Error in the toplevel code block in in_toplevel.md at line 4:
./not_found.ml: No such file or directory
File renamed without changes.
2 changes: 2 additions & 0 deletions test/bin/test_failure/ml_file_not_found.md.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Error in the OCaml code block in ml_file_not_found.md at line 4:
./not_found.ml: No such file or directory
4 changes: 4 additions & 0 deletions test/bin/test_failure/part_not_found.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Run this with `--direction=to-md`

```ocaml file=part_not_found.ml,part=part1
```
2 changes: 2 additions & 0 deletions test/bin/test_failure/part_not_found.md.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Error in the OCaml code block in part_not_found.md at line 4:
Cannot find part "part1" in ./part_not_found.ml
File renamed without changes.
1 change: 1 addition & 0 deletions test/bin/test_failure/part_not_found.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--direction=to-md
8 changes: 8 additions & 0 deletions test/bin/test_fixpoint/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@
(progn
(run ocaml-mdx test --direction=to-md %{x})
(diff? %{x} %{x}.corrected))))
(alias
(name runtest)
(deps
(:x module_alias.md))
(action
(progn
(run ocaml-mdx test --direction=to-md %{x})
(diff? %{x} %{x}.corrected))))
(alias
(name runtest)
(deps
Expand Down
File renamed without changes.
11 changes: 0 additions & 11 deletions test/failures.expected

This file was deleted.

4 changes: 0 additions & 4 deletions test/failures/part_not_found.md

This file was deleted.

0 comments on commit 46d23a7

Please sign in to comment.