Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2 admin commands to compare 2 package versions #6196

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions doc/man/opam-admin-topics.inc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@
(diff opam-admin-list.err %{dep:opam-admin-list.0})))
(package opam))

(rule
(with-stdout-to opam-admin-compare-versions.0 (echo "")))
(rule
(targets opam-admin-compare-versions.1 opam-admin-compare-versions.err)
(deps using-built-opam)
(action (progn (with-stderr-to opam-admin-compare-versions.err
(with-stdout-to opam-admin-compare-versions.1 (run %{bin:opam} admin compare-versions --help=groff)))
(diff opam-admin-compare-versions.err %{dep:opam-admin-compare-versions.0})))
(package opam))

(rule
(with-stdout-to opam-admin-assert-compare-versions.0 (echo "")))
(rule
(targets opam-admin-assert-compare-versions.1 opam-admin-assert-compare-versions.err)
(deps using-built-opam)
(action (progn (with-stderr-to opam-admin-assert-compare-versions.err
(with-stdout-to opam-admin-assert-compare-versions.1 (run %{bin:opam} admin assert-compare-versions --help=groff)))
(diff opam-admin-assert-compare-versions.err %{dep:opam-admin-assert-compare-versions.0})))
(package opam))

(rule
(with-stdout-to opam-admin-check.0 (echo "")))
(rule
Expand Down Expand Up @@ -130,6 +150,8 @@
opam-admin-add-constraint.1
opam-admin-filter.1
opam-admin-list.1
opam-admin-compare-versions.1
opam-admin-assert-compare-versions.1
opam-admin-check.1
opam-admin-lint.1
opam-admin-upgrade.1
Expand Down
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ users)
* Bump opam-root-version to 2.2 [#5980 @kit-ty-kate]

## Global CLI
* ◈ Add `opam admin {compare-versions,assert-compare-versions}` to compare package versions for sanity checks [#6196 @mbarbin]
* Add cli version 2.3 [#6045 #6151 @rjbou]

## Plugins
Expand Down
90 changes: 90 additions & 0 deletions src/client/opamAdminCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,94 @@ let check_command cli =
Term.(const cmd $ global_options cli $ ignore_test_arg $ print_short_arg
$ installability_arg $ cycles_arg $ obsolete_arg)

let compare_versions_command_doc = "Compare 2 package versions"
let compare_versions_command cli =
let version_arg n =
let doc =
Arg.info
~docv:(Printf.sprintf "V%d" n)
~doc:"Package version to compare" []
in
Arg.(required & pos n (some OpamArg.package_version) None & doc)
in
let command = "compare-versions" in
let doc = compare_versions_command_doc in
let man = [
`S Manpage.s_description;
`P "This command compares 2 package versions. It outputs 'V0 OP V1' to \
the console with OP in {<,>,=} such that the equation holds. \
For example:";
`Pre "\n\
\\$ opam admin compare-versions 0.0.9 0.0.10\n\
0.0.9 < 0.0.10";
`S Manpage.s_arguments;
`S Manpage.s_options;
]
in
let cmd global_options v0 v1 () =
OpamArg.apply_global_options cli global_options;
let result = OpamPackage.Version.compare v0 v1 in
OpamConsole.formatted_msg "%s %s %s\n"
(OpamPackage.Version.to_string v0)
(if result < 0 then "<" else if result = 0 then "=" else ">")
(OpamPackage.Version.to_string v1);
in
OpamArg.mk_command ~cli OpamArg.cli_original command ~doc ~man
Term.(const cmd $ global_options cli $ version_arg 0 $ version_arg 1)

let assert_compare_versions_command_doc =
"Verify an equation comparing 2 package versions"
let assert_compare_versions_command cli =
let operators : (string * OpamFormula.relop) list =
[ "lt", `Lt
; "le", `Leq
; "eq", `Eq
]
in
let operator =
let doc =
Arg.info
~docv:"OP"
~doc:(Printf.sprintf
"$(docv) must be %s.\n" (Arg.doc_alts_enum ~quoted:true operators))
[]
in
Arg.(required & pos 1 (some (enum operators)) None & doc)
in
let version_arg n =
let doc =
Arg.info
~docv:(Printf.sprintf "V%d" (if n = 0 then 0 else 1))
~doc:"Package version to compare" []
in
Arg.(required & pos n (some OpamArg.package_version) None & doc)
in
let command = "assert-compare-versions" in
let doc = assert_compare_versions_command_doc in
let man = [
`S Manpage.s_description;
`P "This command verifies a stated equation comparing 2 package versions. \
It exit 0 if the comparison holds, and 1 if it doesn't. For example:";
`Pre "\n\
\\$ opam admin assert-compare-versions 0.0.9 lt 0.0.10\n\
[0]\n\
\n\
\\$ opam admin assert-compare-versions 0.0.9 eq 0.0.10\n\
[1]";
`S Manpage.s_arguments;
`S Manpage.s_options;
]
in
let cmd global_options v0 operator v1 () =
OpamArg.apply_global_options cli global_options;
OpamStd.Sys.exit_because
(if OpamFormula.eval_relop operator v0 v1
then `Success
else `False)
in
OpamArg.mk_command ~cli OpamArg.cli_original command ~doc ~man
Term.(const cmd $ global_options cli $ version_arg 0 $ operator $ version_arg 2)

let pattern_list_arg =
OpamArg.arg_list "PATTERNS"
"Package patterns with globs. matching against $(b,NAME) or \
Expand Down Expand Up @@ -1227,6 +1315,8 @@ let admin_subcommands cli =
upgrade_command cli;
lint_command cli;
check_command cli;
assert_compare_versions_command cli;
compare_versions_command cli;
list_command cli;
filter_command cli;
add_constraint_command cli;
Expand Down
21 changes: 21 additions & 0 deletions tests/reftests/compare-versions.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
N0REP0
### opam admin compare-versions 0.0.9 0.0.10
0.0.9 < 0.0.10
### opam admin compare-versions 1.2.3 1.2.3~preview
1.2.3 > 1.2.3~preview
### opam admin compare-versions 1.2.3 1.2.3-option
1.2.3 < 1.2.3-option
### opam admin compare-versions 0.1.0 0.01.0
0.1.0 = 0.01.0
### opam admin compare-versions 0.2.2 0.2.0
0.2.2 > 0.2.0
### opam admin assert-compare-versions 0.0.9 lt 0.0.10
### opam admin assert-compare-versions 1.2.3 lt 1.2.3~preview
# Return code 1 #
### opam admin assert-compare-versions 0.1.0 eq 0.01.0
### opam admin assert-compare-versions 0.0.9 eq 0.0.10
# Return code 1 #
### opam admin assert-compare-versions 0.1.0 le 0.1.0
### opam admin assert-compare-versions 0.0.9 le 0.0.10
### opam admin assert-compare-versions 0.2.2 le 0.2.1
# Return code 1 #
18 changes: 18 additions & 0 deletions tests/reftests/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@
%{targets}
(run ./run.exe %{exe:../../src/client/opamMain.exe.exe} %{dep:cli-versioning.test} %{read-lines:testing-env}))))

(rule
(alias reftest-compare-versions)
(action
(diff compare-versions.test compare-versions.out)))

(alias
(name reftest)
(deps (alias reftest-compare-versions)))

(rule
(targets compare-versions.out)
(deps root-N0REP0)
(package opam)
(action
(with-stdout-to
%{targets}
(run ./run.exe %{exe:../../src/client/opamMain.exe.exe} %{dep:compare-versions.test} %{read-lines:testing-env}))))

(rule
(alias reftest-config)
(enabled_if (and (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
Expand Down
Loading