Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moyodiallo committed Sep 18, 2023
1 parent 86ba28c commit 02ae26b
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -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))
9 changes: 5 additions & 4 deletions dune_constraints.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

)
5 changes: 4 additions & 1 deletion main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion opam-dune-lint.opam
Original file line number Diff line number Diff line change
Expand Up @@ -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}
]
Expand Down
122 changes: 122 additions & 0 deletions tests/test_dune_constraints.t
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion tests/test_opam.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Create a simple dune project:
> depends: [
> "ocamlfind" {>= "1.0"}
> "libfoo"
> "dune" {>= "2.7"}
> ]
> EOF

Expand All @@ -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 /]
Expand Down
92 changes: 92 additions & 0 deletions tests/test_opam_update.t
Original file line number Diff line number Diff line change
@@ -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}
]
]

0 comments on commit 02ae26b

Please sign in to comment.