diff --git a/dune-project b/dune-project index eb7bd5a..f37362a 100644 --- a/dune-project +++ b/dune-project @@ -20,5 +20,5 @@ (ocaml (>= 4.08.0)) (bos (>= 0.2.0)) (fmt (>= 0.8.9)) - (opam-state (>= 2.0.9)) + (opam-state (>= 2.1.0)) opam-format)) diff --git a/dune_constraints.ml b/dune_constraints.ml index 5133454..b3f0819 100644 --- a/dune_constraints.ml +++ b/dune_constraints.ml @@ -62,20 +62,21 @@ let check_dune_constraints ~errors ~dune_version pkg_name opam = in if is_build then (pkg_name, DuneIsBuild) :: errors else errors -let msg_of_errors = +let print_msg_of_errors = List.iter (fun (package, err) -> let pkg = OpamPackage.Name.to_string package in match err with | DuneConstraintMissing -> - Fmt.epr "Warning in %s: The package has a dune-project file but no explicit dependency on dune was found." pkg + Fmt.epr "Warning in %s: The package has a dune-project file but no explicit dependency on dune was found.@." pkg | DuneIsBuild -> Fmt.epr "Warning in %s: The package tagged dune as a build dependency. \ Due to a bug in dune (https://github.com/ocaml/dune/issues/2147) this should never be the case. \ - Please remove the {build} tag from its filter." + Please remove the {build} tag from its filter.@." pkg | BadDuneConstraint (dep, ver) -> Fmt.failwith "Error in %s: Your dune-project file indicates that this package requires at least dune %s \ - but your opam file only requires dune >= %s. Please check which requirement is the right one, and fix the other." + but your opam file only requires dune >= %s. Please check which requirement is the right one, and fix the other." pkg ver dep + ) diff --git a/main.ml b/main.ml index 0f100f0..d498009 100644 --- a/main.ml +++ b/main.ml @@ -214,7 +214,10 @@ let main force dir = let pkg_name = (OpamPackage.Name.of_string (Filename.chop_suffix path ".opam")) in Dune_constraints.check_dune_constraints ~errors:[] ~dune_version pkg_name opam) |> List.flatten - |> Dune_constraints.msg_of_errors; + |> (fun errors -> + try Dune_constraints.print_msg_of_errors errors with + | Failure msg -> Fmt.epr "%s@." msg; exit 1 + | _ -> Fmt.epr "Error from dune_constraints errors printing"; exit 1); if not (Paths.is_empty stale_files) then exit 1 open Cmdliner diff --git a/opam-dune-lint.opam b/opam-dune-lint.opam index da30215..8573885 100644 --- a/opam-dune-lint.opam +++ b/opam-dune-lint.opam @@ -17,7 +17,7 @@ depends: [ "ocaml" {>= "4.08.0"} "bos" {>= "0.2.0"} "fmt" {>= "0.8.9"} - "opam-state" {>= "2.0.9"} + "opam-state" {>= "2.1.0"} "opam-format" "odoc" {with-doc} ] diff --git a/tests/test_dune_constraints.t b/tests/test_dune_constraints.t new file mode 100644 index 0000000..09d288b --- /dev/null +++ b/tests/test_dune_constraints.t @@ -0,0 +1,122 @@ +Create a simple dune project: for testing opam-dune-lint, if dune constraint matches +the dune-project file + + + $ cat > dune-project << EOF + > (lang dune 2.7) + > (package + > (name test) + > (synopsis "Test package")) + > EOF + + $ cat > test.opam << EOF + > # Preserve comments + > opam-version: "2.0" + > synopsis: "Test package" + > build: [ + > ["dune" "build"] + > ] + > depends: [ + > "ocamlfind" {>= "1.0"} + > "libfoo" + > ] + > EOF + + $ cat > dune << EOF + > (executable + > (name main) + > (public_name main) + > (modules main) + > (libraries findlib fmt)) + > (test + > (name test) + > (modules test) + > (libraries bos opam-state)) + > EOF + + $ touch main.ml test.ml + $ dune build + +Replace all version numbers with "1.0" to get predictable output. + + $ export OPAM_DUNE_LINT_TESTS=y + +Check that the missing libraries get added: + + $ opam-dune-lint -f + test.opam: changes needed: + "fmt" {>= "1.0"} [from /] + "bos" {with-test & >= "1.0"} [from /] + "opam-state" {with-test & >= "1.0"} [from /] + Note: version numbers are just suggestions based on the currently installed version. + Wrote "./test.opam" + Warning in test: The package has a dune-project file but no explicit dependency on dune was found. + + $ cat test.opam | sed 's/= [^&)}]*/= */g' + # Preserve comments + opam-version: "2.0" + synopsis: "Test package" + build: [ + ["dune" "build"] + ] + depends: [ + "ocamlfind" {>= *} + "libfoo" + "fmt" {>= *} + "bos" {>= *& with-test} + "opam-state" {>= *& with-test} + ] + + $ cat > test.opam << EOF + > # Preserve comments + > opam-version: "2.0" + > synopsis: "Test package" + > build: [ + > ["dune" "build"] + > ] + > depends: [ + > "ocamlfind" {>= "1.0"} + > "libfoo" + > "dune" {> "2.7" & build} + > ] + > EOF + $ dune build + +Check that the missing libraries get added: + + $ opam-dune-lint -f + test.opam: changes needed: + "fmt" {>= "1.0"} [from /] + "bos" {with-test & >= "1.0"} [from /] + "opam-state" {with-test & >= "1.0"} [from /] + Note: version numbers are just suggestions based on the currently installed version. + Wrote "./test.opam" + Warning in test: The package tagged dune as a build dependency. Due to a bug in dune (https://github.com/ocaml/dune/issues/2147) this should never be the case. Please remove the {build} tag from its filter. + + $ cat > test.opam << EOF + > # Preserve comments + > opam-version: "2.0" + > synopsis: "Test package" + > build: [ + > ["dune" "build"] + > ] + > depends: [ + > "ocamlfind" {>= "1.0"} + > "libfoo" + > "dune" {> "1.0" & build} + > ] + > EOF + $ dune build + +Check that the missing libraries get added: + + $ opam-dune-lint -f + test.opam: changes needed: + "fmt" {>= "1.0"} [from /] + "bos" {with-test & >= "1.0"} [from /] + "opam-state" {with-test & >= "1.0"} [from /] + Note: version numbers are just suggestions based on the currently installed version. + Wrote "./test.opam" + Warning in test: The package tagged dune as a build dependency. Due to a bug in dune (https://github.com/ocaml/dune/issues/2147) this should never be the case. Please remove the {build} tag from its filter. + Error in test: Your dune-project file indicates that this package requires at least dune 2.7 but your opam file only requires dune >= 1.0. Please check which requirement is the right one, and fix the other. + [1] diff --git a/tests/test_opam.t b/tests/test_opam.t index 39e2c31..34d1e3a 100644 --- a/tests/test_opam.t +++ b/tests/test_opam.t @@ -17,6 +17,7 @@ Create a simple dune project: > depends: [ > "ocamlfind" {>= "1.0"} > "libfoo" + > "dune" {>= "2.7"} > ] > EOF @@ -43,7 +44,6 @@ Check that the missing libraries get added: $ opam-dune-lint -f test.opam: changes needed: - "dune" {>= "1.0"} "fmt" {>= "1.0"} [from /] "bos" {with-test & >= "1.0"} [from /] "opam-state" {with-test & >= "1.0"} [from /] diff --git a/tests/test_opam_update.t b/tests/test_opam_update.t new file mode 100644 index 0000000..15a9827 --- /dev/null +++ b/tests/test_opam_update.t @@ -0,0 +1,92 @@ +Create a simple dune project: testing if opam-dune-lint update opam files +using the dune-project file, before the linting process. + + $ cat > dune-project << EOF + > (lang dune 2.7) + > (generate_opam_files true) + > (package + > (name test) + > (synopsis "Test package") + > (depends cmdliner)) + > EOF + + $ cat > dune << EOF + > (executable + > (name main) + > (public_name main) + > (modules main) + > (libraries findlib fmt)) + > (test + > (name test) + > (modules test) + > (libraries bos opam-state)) + > EOF + + $ touch main.ml test.ml + $ dune build + $ cat test.opam + # This file is generated by dune, edit dune-project instead + opam-version: "2.0" + synopsis: "Test package" + depends: [ + "dune" {>= "2.7"} + "cmdliner" + "odoc" {with-doc} + ] + build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] + ] + +Replace all version numbers with "1.0" to get predictable output. + + $ export OPAM_DUNE_LINT_TESTS=y + +Check that the missing libraries get added: + + $ opam-dune-lint -f + test.opam: changes needed: + "fmt" {>= "1.0"} [from /] + "ocamlfind" {>= "1.0"} [from /] + "bos" {with-test & >= "1.0"} [from /] + "opam-state" {with-test & >= "1.0"} [from /] + Note: version numbers are just suggestions based on the currently installed version. + Wrote "dune-project" + + $ cat test.opam | sed 's/= [^&)}]*/= */g' + # This file is generated by dune, edit dune-project instead + opam-version: "2.0" + synopsis: "Test package" + depends: [ + "dune" {>= *} + "opam-state" {>= *& with-test} + "bos" {>= *& with-test} + "ocamlfind" {>= *} + "fmt" {>= *} + "cmdliner" + "odoc" {with-doc} + ] + build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] + ]