From 8f7423c84d2718b0f4b7d3349728c2c908276f9d Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Mon, 4 Nov 2019 16:03:06 +0100 Subject: [PATCH] Take file kind from --name when formatting stdin (#1119) * Add tests, stdin_and_name failing * With stdin, take file kind from --name Closes #1112 --- CHANGES.md | 1 + src/Conf.ml | 29 +++++++++++------- test/cli/dune.inc | 30 +++++++++++++++++++ test/cli/err_stdin_and_inplace.ref | 2 +- test/cli/err_stdin_name_unknown_ext.opts | 1 + test/cli/err_stdin_name_unknown_ext.ref | 1 + .../err_stdin_name_unknown_ext.should-fail | 0 test/cli/err_stdin_name_unknown_ext.stdin | 1 + test/cli/err_stdin_no_kind.ref | 2 +- test/cli/name_unknown_ext.opts | 1 + test/cli/name_unknown_ext.ref | 1 + test/cli/stdin_and_name.opts | 1 + test/cli/stdin_and_name.ref | 1 + test/cli/stdin_and_name.stdin | 1 + 14 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 test/cli/err_stdin_name_unknown_ext.opts create mode 100644 test/cli/err_stdin_name_unknown_ext.ref create mode 100644 test/cli/err_stdin_name_unknown_ext.should-fail create mode 120000 test/cli/err_stdin_name_unknown_ext.stdin create mode 100644 test/cli/name_unknown_ext.opts create mode 100644 test/cli/name_unknown_ext.ref create mode 100644 test/cli/stdin_and_name.opts create mode 100644 test/cli/stdin_and_name.ref create mode 120000 test/cli/stdin_and_name.stdin diff --git a/CHANGES.md b/CHANGES.md index fc70d04071..9b8d0359ca 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,6 @@ ### (master) + + Internal: Take file kind from --name when formatting stdin (#1119) (Jules Aguillon) + Fix unstabilizing comments on assignments (#1093) (Guillaume Petiot) + Internal: Make Fmt.t abstract (#1109) (Jules Aguillon) + Improve: give a hint when warning 50 is raised (#1111) (Guillaume Petiot) diff --git a/src/Conf.ml b/src/Conf.ml index 2a6709c411..6e55034bf7 100644 --- a/src/Conf.ml +++ b/src/Conf.ml @@ -1709,6 +1709,12 @@ let (_profile : t option C.t) = let is_stdin = function Stdin -> true | File _ -> false +let kind_of_ext fname = + match Filename.extension fname with + | ".ml" | ".mlt" -> Some `Impl + | ".mli" -> Some `Intf + | _ -> None + let validate () = let inputs_len = List.length !inputs in let has_stdin = List.exists ~f:is_stdin !inputs in @@ -1721,11 +1727,14 @@ let validate () = `Error (false, "Must specify at least one input file, or `-` for stdin") else if has_stdin && inputs_len > 1 then `Error (false, "Cannot specify stdin together with other inputs") - else if has_stdin && Option.is_none !kind then + else if + has_stdin && Option.is_none !kind + && Option.is_none (Option.bind ~f:kind_of_ext !name) + then `Error ( false - , "Must specify at least one of --impl, --intf or --use-file when \ - reading from stdin" ) + , "Must specify at least one of --name, --impl or --intf when reading \ + from stdin" ) else if has_stdin && !inplace then `Error (false, "Cannot specify stdin together with --inplace") else if !inplace && Option.is_some !output then @@ -1973,13 +1982,13 @@ let kind_of file = match !kind with | Some kind -> kind | None -> ( - match file with - | Stdin -> impossible "checked by validate" - | File fname -> ( - match Filename.extension fname with - | ".ml" -> `Impl - | ".mli" -> `Intf - | _ -> `Impl ) ) + match Option.bind ~f:kind_of_ext !name with + | Some kind -> kind + | None -> ( + match file with + | Stdin -> impossible "checked by validate" + | File fname -> ( + match kind_of_ext fname with Some kind -> kind | None -> `Impl ) ) ) let name_of file = match !name with diff --git a/test/cli/dune.inc b/test/cli/dune.inc index d0347b335e..7ae621c294 100644 --- a/test/cli/dune.inc +++ b/test/cli/dune.inc @@ -79,6 +79,16 @@ (name runtest) (action (diff err_stdin_and_inplace.ref err_stdin_and_inplace.output))) +(rule + (targets err_stdin_name_unknown_ext.output) + (action + (with-outputs-to %{targets} + (system "! %{bin:ocamlformat} %{read-lines:err_stdin_name_unknown_ext.opts} < err_stdin_name_unknown_ext.stdin")))) + +(alias + (name runtest) + (action (diff err_stdin_name_unknown_ext.ref err_stdin_name_unknown_ext.output))) + (rule (targets err_stdin_no_kind.output) (action @@ -89,6 +99,16 @@ (name runtest) (action (diff err_stdin_no_kind.ref err_stdin_no_kind.output))) +(rule + (targets name_unknown_ext.output) + (action + (with-outputs-to %{targets} + (system "%{bin:ocamlformat} %{read-lines:name_unknown_ext.opts}")))) + +(alias + (name runtest) + (action (diff name_unknown_ext.ref name_unknown_ext.output))) + (rule (targets stdin_and_impl.output) (action @@ -108,3 +128,13 @@ (alias (name runtest) (action (diff stdin_and_intf.ref stdin_and_intf.output))) + +(rule + (targets stdin_and_name.output) + (action + (with-outputs-to %{targets} + (system "%{bin:ocamlformat} %{read-lines:stdin_and_name.opts} < stdin_and_name.stdin")))) + +(alias + (name runtest) + (action (diff stdin_and_name.ref stdin_and_name.output))) diff --git a/test/cli/err_stdin_and_inplace.ref b/test/cli/err_stdin_and_inplace.ref index 7ad1eb3bb9..2f730f5204 100644 --- a/test/cli/err_stdin_and_inplace.ref +++ b/test/cli/err_stdin_and_inplace.ref @@ -1 +1 @@ -ocamlformat: Must specify at least one of --impl, --intf or --use-file when reading from stdin +ocamlformat: Must specify at least one of --name, --impl or --intf when reading from stdin diff --git a/test/cli/err_stdin_name_unknown_ext.opts b/test/cli/err_stdin_name_unknown_ext.opts new file mode 100644 index 0000000000..a131047f72 --- /dev/null +++ b/test/cli/err_stdin_name_unknown_ext.opts @@ -0,0 +1 @@ +--name b.cpp - diff --git a/test/cli/err_stdin_name_unknown_ext.ref b/test/cli/err_stdin_name_unknown_ext.ref new file mode 100644 index 0000000000..2f730f5204 --- /dev/null +++ b/test/cli/err_stdin_name_unknown_ext.ref @@ -0,0 +1 @@ +ocamlformat: Must specify at least one of --name, --impl or --intf when reading from stdin diff --git a/test/cli/err_stdin_name_unknown_ext.should-fail b/test/cli/err_stdin_name_unknown_ext.should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cli/err_stdin_name_unknown_ext.stdin b/test/cli/err_stdin_name_unknown_ext.stdin new file mode 120000 index 0000000000..e514812e00 --- /dev/null +++ b/test/cli/err_stdin_name_unknown_ext.stdin @@ -0,0 +1 @@ +sample/a.ml \ No newline at end of file diff --git a/test/cli/err_stdin_no_kind.ref b/test/cli/err_stdin_no_kind.ref index 7ad1eb3bb9..2f730f5204 100644 --- a/test/cli/err_stdin_no_kind.ref +++ b/test/cli/err_stdin_no_kind.ref @@ -1 +1 @@ -ocamlformat: Must specify at least one of --impl, --intf or --use-file when reading from stdin +ocamlformat: Must specify at least one of --name, --impl or --intf when reading from stdin diff --git a/test/cli/name_unknown_ext.opts b/test/cli/name_unknown_ext.opts new file mode 100644 index 0000000000..983c4e9b55 --- /dev/null +++ b/test/cli/name_unknown_ext.opts @@ -0,0 +1 @@ +--name b.cpp sample/b.ml diff --git a/test/cli/name_unknown_ext.ref b/test/cli/name_unknown_ext.ref new file mode 100644 index 0000000000..7572c13af9 --- /dev/null +++ b/test/cli/name_unknown_ext.ref @@ -0,0 +1 @@ +let () = print_endline A.x diff --git a/test/cli/stdin_and_name.opts b/test/cli/stdin_and_name.opts new file mode 100644 index 0000000000..8c69558934 --- /dev/null +++ b/test/cli/stdin_and_name.opts @@ -0,0 +1 @@ +--name a.ml - diff --git a/test/cli/stdin_and_name.ref b/test/cli/stdin_and_name.ref new file mode 100644 index 0000000000..e4a4afaa45 --- /dev/null +++ b/test/cli/stdin_and_name.ref @@ -0,0 +1 @@ +let x = "Hello World" diff --git a/test/cli/stdin_and_name.stdin b/test/cli/stdin_and_name.stdin new file mode 120000 index 0000000000..e514812e00 --- /dev/null +++ b/test/cli/stdin_and_name.stdin @@ -0,0 +1 @@ +sample/a.ml \ No newline at end of file