Skip to content

Commit

Permalink
Add option to be able to use the default root
Browse files Browse the repository at this point in the history
By default, opam-ci-check revdeps functionality uses a new opam switch
to not mess up a user's switches, etc. But, in CI we could use the
default switch and avoid creating a new separate switch, since we are
running in a container created for this purpose.
  • Loading branch information
punchagan committed Dec 17, 2024
1 parent f4fdae4 commit 3ed5527
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
22 changes: 15 additions & 7 deletions opam-ci-check/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,26 @@ let lint package_specs local_repo_dir =
|> Printf.sprintf "%s\n" |> Result.error
| Error _ as e -> e)

let show_revdeps pkg local_repo_dir no_transitive_revdeps =
let show_revdeps pkg local_repo_dir no_transitive_revdeps use_default_root =
(* Get revdeps for the package *)
let revdeps =
Revdeps.list_revdeps ?opam_repo:local_repo_dir
~transitive:(not no_transitive_revdeps)
pkg
~use_default_root pkg
in
Revdeps.Display.packages revdeps;
Ok ()

let testing_revdeps_confirmed revdeps =
OpamConsole.confirm "Do you want test %d revdeps?" (List.length revdeps)

let test_revdeps pkg local_repo_dir use_dune no_transitive_revdeps =
let test_revdeps pkg local_repo_dir use_dune no_transitive_revdeps
use_default_root =
(* Get revdeps for the package *)
let revdeps =
Revdeps.list_revdeps ?opam_repo:local_repo_dir
~transitive:(not no_transitive_revdeps)
pkg
~use_default_root pkg
in

(* Install and test the first reverse dependency *)
Expand All @@ -119,7 +120,7 @@ let test_revdeps pkg local_repo_dir use_dune no_transitive_revdeps =
| true, None -> Error "Opam local repository path must be specified!\n"
| false, _ ->
let num_failed_installs =
Test.test_packages_with_opam pkg latest_versions
Test.test_packages_with_opam ~use_default_root pkg latest_versions
|> Seq.map (fun e -> Printf.eprintf "%s\n" (Test.error_to_string e))
|> Seq.length
in
Expand Down Expand Up @@ -187,6 +188,13 @@ let no_transitive_revdeps =
in
Arg.value (Arg.flag info)

let use_default_root =
let info =
Arg.info [ "use-default-root" ]
~doc:"Use the default opam root in ~/.opam instead of creating a new one."
in
Arg.value (Arg.flag info)

let use_dune_term =
let info =
Arg.info [ "d"; "use-dune" ]
Expand Down Expand Up @@ -305,7 +313,7 @@ let list_cmd =
let term =
Term.(
const show_revdeps $ pkg_term $ local_opam_repo_term
$ no_transitive_revdeps)
$ no_transitive_revdeps $ use_default_root)
|> to_exit_code
in
let info =
Expand All @@ -318,7 +326,7 @@ let test_cmd =
let term =
Term.(
const test_revdeps $ pkg_term $ local_opam_repo_term $ use_dune_term
$ no_transitive_revdeps)
$ no_transitive_revdeps $ use_default_root)
|> to_exit_code
in
let info =
Expand Down
16 changes: 8 additions & 8 deletions opam-ci-check/lib/env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ let make_repo path =
let repo_url = OpamUrl.parse path in
{ OpamTypes.repo_name; repo_url; repo_trust = None }

let local_opam_root () =
let switch_dir = ".opam-revdeps" in
let get_opam_root ?(use_default_root = false) () =
let switch_dir = if use_default_root then "~/.opam" else ".opam-revdeps" in
OpamFilename.Dir.of_string switch_dir

let create_local_switch_maybe repo_path =
let root_dir = local_opam_root () in
let create_local_switch_maybe ?(use_default_root = false) repo_path =
let root_dir = get_opam_root ~use_default_root () in
let create_switch_dir repo_path =
OpamClientConfig.opam_init ~root_dir ();
(* opam init*)
Expand Down Expand Up @@ -50,14 +50,14 @@ let create_local_switch_maybe repo_path =
create_switch_dir repo_path)
else ()

let with_locked_switch () =
let root_dir = local_opam_root () in
let with_locked_switch ?(use_default_root = false) () =
let root_dir = get_opam_root ~use_default_root () in
OpamClientConfig.opam_init ~root_dir ();
OpamGlobalState.with_ `Lock_none @@ fun gt ->
OpamSwitchState.with_ `Lock_write gt

let with_unlocked_switch () =
let root_dir = local_opam_root () in
let with_unlocked_switch ?(use_default_root = false) () =
let root_dir = get_opam_root ~use_default_root () in
OpamClientConfig.opam_init ~root_dir ();
OpamGlobalState.with_ `Lock_none @@ fun gt ->
OpamSwitchState.with_ `Lock_none gt
8 changes: 4 additions & 4 deletions opam-ci-check/lib/revdeps.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ let non_transitive_revdeps st package_set =
all_known_packages

let list_revdeps ?(opam_repo = "https://opam.ocaml.org") ?(transitive = true)
pkg =
(* Create local opam root and switch *)
Env.create_local_switch_maybe opam_repo;
?(use_default_root = false) pkg =
(* Create local opam root and switch if required *)
if not use_default_root then Env.create_local_switch_maybe opam_repo;
let package = OpamPackage.of_string pkg in
let package_set = OpamPackage.Set.singleton package in
Env.with_unlocked_switch () (fun st ->
Env.with_unlocked_switch ~use_default_root () (fun st ->
let transitive_deps =
if transitive then transitive_revdeps st package_set
else OpamPackage.Set.empty
Expand Down
9 changes: 8 additions & 1 deletion opam-ci-check/lib/revdeps.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
(** Analyze and test the reverse dependencies of a package. *)

val list_revdeps :
?opam_repo:string -> ?transitive:bool -> string -> OpamPackage.t list
?opam_repo:string ->
?transitive:bool ->
?use_default_root:bool ->
string ->
OpamPackage.t list
(** [list_revdeps pkg] is a list of the transitive reverse dependencies of [pkg].
@param opam_repo The package repository to use when calculating
Expand All @@ -14,6 +18,9 @@ val list_revdeps :
@param transitive Whether or to list all transitive reverse dependencies.
Defaults to [true].
@param use_default_root Whether or to use the default root ~/.opam or
create a new one. Defaults to [false].
@param pkg The package for which reverse dependencies will be listed, in a
form like ["pkgname.0.0.1"].
Expand Down
5 changes: 3 additions & 2 deletions opam-ci-check/lib/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ let test_package_with_opam_in_state st revdep =
But, we may be able to do better, since we are not a shell script? *)
Some (revdep, e)

let test_packages_with_opam target_pkg revdeps_list =
let test_packages_with_opam ?(use_default_root = false) target_pkg revdeps_list
=
let target = OpamPackage.of_string target_pkg in
match
OpamConsole.confirm "Do you want test %d revdeps?"
Expand All @@ -44,7 +45,7 @@ let test_packages_with_opam target_pkg revdeps_list =
~confirm_level:`unsafe_yes (* Don't prompt for install / remove *) ();
OpamConsole.msg "Installing reverse dependencies with pinned %s\n"
(OpamPackage.to_string target);
Env.with_locked_switch () @@ fun st ->
Env.with_locked_switch ~use_default_root () @@ fun st ->
let st' = OpamClient.install st [ pkg_atom target ] in
revdeps_list |> List.to_seq
|> Seq.filter_map (test_package_with_opam_in_state st')
Expand Down
3 changes: 2 additions & 1 deletion opam-ci-check/lib/test.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ type error = OpamPackage.t * exn

val error_to_string : error -> string

val test_packages_with_opam : string -> OpamPackage.t list -> error Seq.t
val test_packages_with_opam :
?use_default_root:bool -> string -> OpamPackage.t list -> error Seq.t
(** [test_packages_with_opam package revdeps] is the sequence of errors encountered
while trying to install and test all the packages in [revdeps].
Expand Down

0 comments on commit 3ed5527

Please sign in to comment.