Skip to content

Commit

Permalink
Merge pull request #1117 from Julow/cli_tests
Browse files Browse the repository at this point in the history
Add command line tests
  • Loading branch information
emillon authored Nov 4, 2019
2 parents 6f8077c + df847c8 commit a3521a2
Show file tree
Hide file tree
Showing 40 changed files with 216 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/cli/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(include dune.inc)

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

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

(rule
(targets err_default_several_file.output)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocamlformat} %{read-lines:err_default_several_file.opts}"))))

(alias
(name runtest)
(action (diff err_default_several_file.ref err_default_several_file.output)))

(rule
(targets err_inplace_and_check.output)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocamlformat} %{read-lines:err_inplace_and_check.opts}"))))

(alias
(name runtest)
(action (diff err_inplace_and_check.ref err_inplace_and_check.output)))

(rule
(targets err_inplace_and_output.output)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocamlformat} %{read-lines:err_inplace_and_output.opts}"))))

(alias
(name runtest)
(action (diff err_inplace_and_output.ref err_inplace_and_output.output)))

(rule
(targets err_no_arg.output)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocamlformat} %{read-lines:err_no_arg.opts}"))))

(alias
(name runtest)
(action (diff err_no_arg.ref err_no_arg.output)))

(rule
(targets err_output_and_check.output)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocamlformat} %{read-lines:err_output_and_check.opts}"))))

(alias
(name runtest)
(action (diff err_output_and_check.ref err_output_and_check.output)))

(rule
(targets err_output_several_files.output)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocamlformat} %{read-lines:err_output_several_files.opts}"))))

(alias
(name runtest)
(action (diff err_output_several_files.ref err_output_several_files.output)))

(rule
(targets err_stdin_and_file.output)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocamlformat} %{read-lines:err_stdin_and_file.opts}"))))

(alias
(name runtest)
(action (diff err_stdin_and_file.ref err_stdin_and_file.output)))

(rule
(targets err_stdin_and_inplace.output)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocamlformat} %{read-lines:err_stdin_and_inplace.opts}"))))

(alias
(name runtest)
(action (diff err_stdin_and_inplace.ref err_stdin_and_inplace.output)))

(rule
(targets err_stdin_no_kind.output)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocamlformat} %{read-lines:err_stdin_no_kind.opts}"))))

(alias
(name runtest)
(action (diff err_stdin_no_kind.ref err_stdin_no_kind.output)))

(rule
(targets stdin_and_impl.output)
(action
(with-outputs-to %{targets}
(system "%{bin:ocamlformat} %{read-lines:stdin_and_impl.opts} < stdin_and_impl.stdin"))))

(alias
(name runtest)
(action (diff stdin_and_impl.ref stdin_and_impl.output)))

(rule
(targets stdin_and_intf.output)
(action
(with-outputs-to %{targets}
(system "%{bin:ocamlformat} %{read-lines:stdin_and_intf.opts} < stdin_and_intf.stdin"))))

(alias
(name runtest)
(action (diff stdin_and_intf.ref stdin_and_intf.output)))
1 change: 1 addition & 0 deletions test/cli/err_default_several_file.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample/a.ml sample/b.ml
1 change: 1 addition & 0 deletions test/cli/err_default_several_file.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocamlformat: Must specify exactly one input file without --inplace or --check
Empty file.
1 change: 1 addition & 0 deletions test/cli/err_inplace_and_check.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--inplace --check sample/a.ml
1 change: 1 addition & 0 deletions test/cli/err_inplace_and_check.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocamlformat: Cannot specify --inplace with --check
Empty file.
1 change: 1 addition & 0 deletions test/cli/err_inplace_and_output.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--inplace --output o.ml sample/a.ml
1 change: 1 addition & 0 deletions test/cli/err_inplace_and_output.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocamlformat: Cannot specify --output with --inplace
Empty file.
Empty file added test/cli/err_no_arg.opts
Empty file.
1 change: 1 addition & 0 deletions test/cli/err_no_arg.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocamlformat: Must specify at least one input file, or `-` for stdin
Empty file added test/cli/err_no_arg.should-fail
Empty file.
1 change: 1 addition & 0 deletions test/cli/err_output_and_check.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--output x.ml --check sample/a.ml
1 change: 1 addition & 0 deletions test/cli/err_output_and_check.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocamlformat: Cannot specify --output with --check
Empty file.
1 change: 1 addition & 0 deletions test/cli/err_output_several_files.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--output x.ml sample/a.ml sample/b.ml
1 change: 1 addition & 0 deletions test/cli/err_output_several_files.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocamlformat: Must specify exactly one input file without --inplace or --check
Empty file.
1 change: 1 addition & 0 deletions test/cli/err_stdin_and_file.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample/a.ml -
1 change: 1 addition & 0 deletions test/cli/err_stdin_and_file.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocamlformat: Cannot specify stdin together with other inputs
Empty file.
1 change: 1 addition & 0 deletions test/cli/err_stdin_and_inplace.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--inplace -
1 change: 1 addition & 0 deletions test/cli/err_stdin_and_inplace.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocamlformat: Must specify at least one of --impl, --intf or --use-file when reading from stdin
Empty file.
1 change: 1 addition & 0 deletions test/cli/err_stdin_no_kind.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-
1 change: 1 addition & 0 deletions test/cli/err_stdin_no_kind.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocamlformat: Must specify at least one of --impl, --intf or --use-file when reading from stdin
Empty file.
2 changes: 2 additions & 0 deletions test/cli/gen/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(executable
(name gen))
68 changes: 68 additions & 0 deletions test/cli/gen/gen.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module StringMap = Map.Make (String)

type entry = {
has_ref : bool;
has_opts : bool;
has_stdin : bool;
should_fail : bool
}

let empty_entry = {
has_ref = false;
has_opts = false;
has_stdin = false;
should_fail = false;
}

let register_file entries fname =
let update_or_add key ~f =
StringMap.update key (function
| Some entry -> Some (f entry)
| None -> Some (f empty_entry)) entries
in
match String.split_on_char '.' fname with
| [ test_name; "ref" ] ->
update_or_add test_name ~f:(fun e -> { e with has_ref = true })
| [ test_name; "opts" ] ->
update_or_add test_name ~f:(fun e -> { e with has_opts = true })
| [ test_name; "should-fail" ] ->
update_or_add test_name ~f:(fun e -> { e with should_fail = true })
| [ test_name; "stdin" ] ->
update_or_add test_name ~f:(fun e -> { e with has_stdin = true })
| _ ->
entries

let check_test test_name entry ok =
let e ok b =
if b then Format.kfprintf (fun _ -> false) Format.err_formatter
else Format.ikfprintf (fun _ -> ok) Format.err_formatter
in
let ok = e ok (not entry.has_ref) "@{<error>Error@}: Missing file %s.ref\n" test_name in
let ok = e ok (not entry.has_opts) "@{<error>Error@}: Missing file %s.opts\n" test_name in
ok

let emit_test test_name entry =
let bang pf b = if b then Format.fprintf pf "! " else () in
let redir_in pf b = if b then Format.fprintf pf " < %s.stdin" test_name else () in
let run_action pf () =
Format.fprintf pf "(with-outputs-to %%{targets}@\n (system \"%a%%{bin:ocamlformat} %%{read-lines:%s.opts}%a\"))"
bang entry.should_fail test_name redir_in entry.has_stdin
in
Format.printf {|
(rule@
(targets %s.output)@
(action@
@[%a@]))@
|} test_name run_action ();
Format.printf {|
(alias@
(name runtest)@
(action (diff %s.ref %s.output)))@
|} test_name test_name

let () =
let files = Sys.readdir "." in
let tests = Array.fold_left register_file StringMap.empty files in
if not (StringMap.fold check_test tests true) then
exit 1;
StringMap.iter emit_test tests
1 change: 1 addition & 0 deletions test/cli/sample/a.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x = "Hello World"
1 change: 1 addition & 0 deletions test/cli/sample/a.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val x : string
1 change: 1 addition & 0 deletions test/cli/sample/b.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let () = print_endline A.x
1 change: 1 addition & 0 deletions test/cli/stdin_and_impl.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--impl -
1 change: 1 addition & 0 deletions test/cli/stdin_and_impl.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let () = print_endline A.x
1 change: 1 addition & 0 deletions test/cli/stdin_and_impl.stdin
1 change: 1 addition & 0 deletions test/cli/stdin_and_intf.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--intf -
1 change: 1 addition & 0 deletions test/cli/stdin_and_intf.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val x : string
1 change: 1 addition & 0 deletions test/cli/stdin_and_intf.stdin

0 comments on commit a3521a2

Please sign in to comment.