Skip to content

Commit

Permalink
If a dune file uses OCaml syntax, use ocamlformat
Browse files Browse the repository at this point in the history
Closes #2012

Signed-off-by: Etienne Millon <[email protected]>
  • Loading branch information
emillon committed Apr 9, 2019
1 parent 94fc4ed commit f52c9dd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ unreleased
`binaries` fields would be ignored, but instead they should be combined.
(#2029, @rgrinberg)

- Format rules: if a dune file uses OCaml syntax, do not format it.
(#2014, fix #2012, @emillon)

1.8.2 (10/03/2019)
------------------

Expand Down
10 changes: 9 additions & 1 deletion src/format_dune_lang.ml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ let format_file ~input =
(Dune_lang.Parse_error.message e);
exit 1
| OCaml_syntax loc ->
Errors.warn loc "OCaml syntax is not supported, skipping."
begin
match input with
| Some path ->
Io.with_file_in path ~f:(fun ic ->
Io.copy_channels ic stdout
)
| None ->
Errors.fail loc "OCaml syntax is not supported."
end
| Sexps sexps ->
Format.printf "%a%!" pp_top_sexps sexps
16 changes: 10 additions & 6 deletions test/blackbox-tests/test-cases/format-dune-file/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,20 @@ When a comment is at the end of a list, the ")" is on a own line.
; multiline
)
Files in OCaml syntax are ignored with a warning.
Files in OCaml syntax are copied verbatim (but error when passed in stdin).
$ dune format-dune-file < ocaml-syntax.dune
File "", line 1, characters 0-20:
Warning: OCaml syntax is not supported, skipping.
Error: OCaml syntax is not supported.
[1]
$ dune format-dune-file ocaml-syntax.dune
File "$TESTCASE_ROOT/ocaml-syntax.dune", line 1, characters 0-20:
1 | (* -*- tuareg -*- *)
^^^^^^^^^^^^^^^^^^^^
Warning: OCaml syntax is not supported, skipping.
(* -*- tuareg -*- *)
let () = Jbuild_plugin.V1.send {|
(alias
(name runtest)
(action (echo "ocaml syntax")))
|}
Non 0 error code:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(* -*- tuareg -*- *)

let
() =
Jbuild_plugin.V1.send {|
(alias
(name runtest)
(action (echo "ocaml syntax")))
|}
3 changes: 3 additions & 0 deletions test/blackbox-tests/test-cases/formatting/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Formatting can be checked using the @fmt target:
$ cp enabled/reason_file.re.orig enabled/reason_file.re
$ cp enabled/dune.orig enabled/dune
$ dune build --display short @fmt
dune enabled/dune-ocaml-syntax/.formatted/dune
ocamldep fake-tools/.ocamlformat.eobjs/ocamlformat.ml.d
ocamldep fake-tools/.ocamlformat.eobjs/refmt.ml.d
ocamlc fake-tools/.ocamlformat.eobjs/byte/ocamlformat.{cmi,cmo,cmt}
Expand Down Expand Up @@ -42,6 +43,7 @@ Configuration files are taken into account for this action:

$ touch enabled/.ocamlformat
$ dune build --display short @fmt
dune enabled/dune-ocaml-syntax/.formatted/dune
refmt enabled/.formatted/reason_file.re
File "enabled/reason_file.re", line 1, characters 0-0:
Files _build/default/enabled/reason_file.re and _build/default/enabled/.formatted/reason_file.re differ.
Expand Down Expand Up @@ -88,6 +90,7 @@ All .ocamlformat files are considered dependencies:

$ echo 'margin = 70' > .ocamlformat
$ dune build --display short @fmt
dune enabled/dune-ocaml-syntax/.formatted/dune
refmt enabled/.formatted/reason_file.re
refmt enabled/.formatted/reason_file.rei
File "enabled/reason_file.rei", line 1, characters 0-0:
Expand Down

0 comments on commit f52c9dd

Please sign in to comment.