diff --git a/test/cli/dune b/test/cli/dune new file mode 100644 index 0000000000..9e8b171063 --- /dev/null +++ b/test/cli/dune @@ -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))) diff --git a/test/cli/dune.inc b/test/cli/dune.inc new file mode 100644 index 0000000000..d0347b335e --- /dev/null +++ b/test/cli/dune.inc @@ -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))) diff --git a/test/cli/err_default_several_file.opts b/test/cli/err_default_several_file.opts new file mode 100644 index 0000000000..de26e376b3 --- /dev/null +++ b/test/cli/err_default_several_file.opts @@ -0,0 +1 @@ +sample/a.ml sample/b.ml diff --git a/test/cli/err_default_several_file.ref b/test/cli/err_default_several_file.ref new file mode 100644 index 0000000000..8231c3424e --- /dev/null +++ b/test/cli/err_default_several_file.ref @@ -0,0 +1 @@ +ocamlformat: Must specify exactly one input file without --inplace or --check diff --git a/test/cli/err_default_several_file.should-fail b/test/cli/err_default_several_file.should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cli/err_inplace_and_check.opts b/test/cli/err_inplace_and_check.opts new file mode 100644 index 0000000000..3506fed9bd --- /dev/null +++ b/test/cli/err_inplace_and_check.opts @@ -0,0 +1 @@ +--inplace --check sample/a.ml diff --git a/test/cli/err_inplace_and_check.ref b/test/cli/err_inplace_and_check.ref new file mode 100644 index 0000000000..d02df733fc --- /dev/null +++ b/test/cli/err_inplace_and_check.ref @@ -0,0 +1 @@ +ocamlformat: Cannot specify --inplace with --check diff --git a/test/cli/err_inplace_and_check.should-fail b/test/cli/err_inplace_and_check.should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cli/err_inplace_and_output.opts b/test/cli/err_inplace_and_output.opts new file mode 100644 index 0000000000..a4edab7c55 --- /dev/null +++ b/test/cli/err_inplace_and_output.opts @@ -0,0 +1 @@ +--inplace --output o.ml sample/a.ml diff --git a/test/cli/err_inplace_and_output.ref b/test/cli/err_inplace_and_output.ref new file mode 100644 index 0000000000..eab3df1da6 --- /dev/null +++ b/test/cli/err_inplace_and_output.ref @@ -0,0 +1 @@ +ocamlformat: Cannot specify --output with --inplace diff --git a/test/cli/err_inplace_and_output.should-fail b/test/cli/err_inplace_and_output.should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cli/err_no_arg.opts b/test/cli/err_no_arg.opts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cli/err_no_arg.ref b/test/cli/err_no_arg.ref new file mode 100644 index 0000000000..4bc1d3b5e1 --- /dev/null +++ b/test/cli/err_no_arg.ref @@ -0,0 +1 @@ +ocamlformat: Must specify at least one input file, or `-` for stdin diff --git a/test/cli/err_no_arg.should-fail b/test/cli/err_no_arg.should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cli/err_output_and_check.opts b/test/cli/err_output_and_check.opts new file mode 100644 index 0000000000..0f5ea082d4 --- /dev/null +++ b/test/cli/err_output_and_check.opts @@ -0,0 +1 @@ +--output x.ml --check sample/a.ml diff --git a/test/cli/err_output_and_check.ref b/test/cli/err_output_and_check.ref new file mode 100644 index 0000000000..d5ddb63fd2 --- /dev/null +++ b/test/cli/err_output_and_check.ref @@ -0,0 +1 @@ +ocamlformat: Cannot specify --output with --check diff --git a/test/cli/err_output_and_check.should-fail b/test/cli/err_output_and_check.should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cli/err_output_several_files.opts b/test/cli/err_output_several_files.opts new file mode 100644 index 0000000000..5bf5068a24 --- /dev/null +++ b/test/cli/err_output_several_files.opts @@ -0,0 +1 @@ +--output x.ml sample/a.ml sample/b.ml diff --git a/test/cli/err_output_several_files.ref b/test/cli/err_output_several_files.ref new file mode 100644 index 0000000000..8231c3424e --- /dev/null +++ b/test/cli/err_output_several_files.ref @@ -0,0 +1 @@ +ocamlformat: Must specify exactly one input file without --inplace or --check diff --git a/test/cli/err_output_several_files.should-fail b/test/cli/err_output_several_files.should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cli/err_stdin_and_file.opts b/test/cli/err_stdin_and_file.opts new file mode 100644 index 0000000000..21a99e0df7 --- /dev/null +++ b/test/cli/err_stdin_and_file.opts @@ -0,0 +1 @@ +sample/a.ml - diff --git a/test/cli/err_stdin_and_file.ref b/test/cli/err_stdin_and_file.ref new file mode 100644 index 0000000000..8e45bd830d --- /dev/null +++ b/test/cli/err_stdin_and_file.ref @@ -0,0 +1 @@ +ocamlformat: Cannot specify stdin together with other inputs diff --git a/test/cli/err_stdin_and_file.should-fail b/test/cli/err_stdin_and_file.should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cli/err_stdin_and_inplace.opts b/test/cli/err_stdin_and_inplace.opts new file mode 100644 index 0000000000..969edf8227 --- /dev/null +++ b/test/cli/err_stdin_and_inplace.opts @@ -0,0 +1 @@ +--inplace - diff --git a/test/cli/err_stdin_and_inplace.ref b/test/cli/err_stdin_and_inplace.ref new file mode 100644 index 0000000000..7ad1eb3bb9 --- /dev/null +++ b/test/cli/err_stdin_and_inplace.ref @@ -0,0 +1 @@ +ocamlformat: Must specify at least one of --impl, --intf or --use-file when reading from stdin diff --git a/test/cli/err_stdin_and_inplace.should-fail b/test/cli/err_stdin_and_inplace.should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cli/err_stdin_no_kind.opts b/test/cli/err_stdin_no_kind.opts new file mode 100644 index 0000000000..39cdd0ded6 --- /dev/null +++ b/test/cli/err_stdin_no_kind.opts @@ -0,0 +1 @@ +- diff --git a/test/cli/err_stdin_no_kind.ref b/test/cli/err_stdin_no_kind.ref new file mode 100644 index 0000000000..7ad1eb3bb9 --- /dev/null +++ b/test/cli/err_stdin_no_kind.ref @@ -0,0 +1 @@ +ocamlformat: Must specify at least one of --impl, --intf or --use-file when reading from stdin diff --git a/test/cli/err_stdin_no_kind.should-fail b/test/cli/err_stdin_no_kind.should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cli/gen/dune b/test/cli/gen/dune new file mode 100644 index 0000000000..9df6b5100e --- /dev/null +++ b/test/cli/gen/dune @@ -0,0 +1,2 @@ +(executable + (name gen)) diff --git a/test/cli/gen/gen.ml b/test/cli/gen/gen.ml new file mode 100644 index 0000000000..0f7c0bfb27 --- /dev/null +++ b/test/cli/gen/gen.ml @@ -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@}: Missing file %s.ref\n" test_name in + let ok = e ok (not entry.has_opts) "@{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 diff --git a/test/cli/sample/a.ml b/test/cli/sample/a.ml new file mode 100644 index 0000000000..e4a4afaa45 --- /dev/null +++ b/test/cli/sample/a.ml @@ -0,0 +1 @@ +let x = "Hello World" diff --git a/test/cli/sample/a.mli b/test/cli/sample/a.mli new file mode 100644 index 0000000000..66e8138932 --- /dev/null +++ b/test/cli/sample/a.mli @@ -0,0 +1 @@ +val x : string diff --git a/test/cli/sample/b.ml b/test/cli/sample/b.ml new file mode 100644 index 0000000000..7572c13af9 --- /dev/null +++ b/test/cli/sample/b.ml @@ -0,0 +1 @@ +let () = print_endline A.x diff --git a/test/cli/stdin_and_impl.opts b/test/cli/stdin_and_impl.opts new file mode 100644 index 0000000000..f5e9fd6ddc --- /dev/null +++ b/test/cli/stdin_and_impl.opts @@ -0,0 +1 @@ +--impl - diff --git a/test/cli/stdin_and_impl.ref b/test/cli/stdin_and_impl.ref new file mode 100644 index 0000000000..7572c13af9 --- /dev/null +++ b/test/cli/stdin_and_impl.ref @@ -0,0 +1 @@ +let () = print_endline A.x diff --git a/test/cli/stdin_and_impl.stdin b/test/cli/stdin_and_impl.stdin new file mode 120000 index 0000000000..04b0bf39e8 --- /dev/null +++ b/test/cli/stdin_and_impl.stdin @@ -0,0 +1 @@ +sample/b.ml \ No newline at end of file diff --git a/test/cli/stdin_and_intf.opts b/test/cli/stdin_and_intf.opts new file mode 100644 index 0000000000..b42b0826cf --- /dev/null +++ b/test/cli/stdin_and_intf.opts @@ -0,0 +1 @@ +--intf - diff --git a/test/cli/stdin_and_intf.ref b/test/cli/stdin_and_intf.ref new file mode 100644 index 0000000000..66e8138932 --- /dev/null +++ b/test/cli/stdin_and_intf.ref @@ -0,0 +1 @@ +val x : string diff --git a/test/cli/stdin_and_intf.stdin b/test/cli/stdin_and_intf.stdin new file mode 120000 index 0000000000..9222cb771f --- /dev/null +++ b/test/cli/stdin_and_intf.stdin @@ -0,0 +1 @@ +sample/a.mli \ No newline at end of file