Skip to content

Commit

Permalink
Record configure command
Browse files Browse the repository at this point in the history
  • Loading branch information
emillon committed Jul 12, 2021
1 parent 7ae2def commit fe78b9b
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 90 deletions.
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
bos
cmdliner
curly
(either :with-test)
github
ocaml-version
re
Expand Down
24 changes: 17 additions & 7 deletions lib/op.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ let create runner github_client source switch_name ~configure_command =
Opam.remove_switch runner ~name:switch_name)
in
let* () = Opam.set_base runner ~name:switch_name in
Source.compiler_sources source
|> Opam.set_compiler_sources runner ~name:switch_name)
match Source.compiler_sources source with
| None -> Ok ()
| Some sources ->
let* () =
Opam.set_compiler_sources runner ~name:switch_name (Some sources)
in
Opam.set_configure_command runner ~name:switch_name configure_command)
|> translate_error "Cannot create switch"

type reinstall_mode = Quick | Full
Expand All @@ -35,12 +40,17 @@ let reinstall_packages_if_needed runner = function
| Quick -> Ok ()
| Full -> Opam.reinstall_packages runner

let unwrap_compiler_sources = function
| None -> Error `No_compiler_sources
| Some v -> Ok v

let reinstall runner mode ~name =
let open Let_syntax.Result in
(let* compiler_sources_opt = Opam.get_compiler_sources runner ~name in
match compiler_sources_opt with
| None -> Error `No_compiler_sources
| Some compiler_sources ->
let* () = Opam.reinstall_compiler runner ~compiler_sources in
reinstall_packages_if_needed runner mode)
let* compiler_sources = unwrap_compiler_sources compiler_sources_opt in
let* configure_command = Opam.get_configure_command runner ~name in
let* () =
Opam.reinstall_compiler runner ~compiler_sources ~configure_command
in
reinstall_packages_if_needed runner mode)
|> translate_error "Could not reinstall"
37 changes: 24 additions & 13 deletions lib/opam.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,11 @@ let reinstall_configure runner ~configure_command ~compiler_sources =
let command = Bos.Cmd.(base_command % "--prefix" % prefix) in
Runner.run ~chdir:compiler_sources runner command

let reinstall_compiler runner ~compiler_sources =
let reinstall_compiler runner ~compiler_sources ~configure_command =
let open Let_syntax.Result in
let make = Bos.Cmd.(v "make") in
let make_install = Bos.Cmd.(v "make" % "install") in
let* () =
reinstall_configure runner ~compiler_sources (* XXX *)
~configure_command:None
in
let* () = reinstall_configure runner ~compiler_sources ~configure_command in
let* () = Runner.run ~chdir:compiler_sources runner make in
Runner.run ~chdir:compiler_sources runner make_install

Expand All @@ -93,11 +90,17 @@ let remove_switch runner ~name =
[ A "switch"; A "remove"; A "--yes"; A (Switch_name.to_string name) ]

module Var = struct
type t = Compiler_sources
type t = Compiler_sources | Configure_command

let prefix = "compiler-"

let suffix = function
| Compiler_sources -> "sources"
| Configure_command -> "configure-command"

let to_string = function Compiler_sources -> "compiler-sources"
let to_string var = prefix ^ suffix var

let get runner ~name var =
let get runner ~name var decode =
let open Let_syntax.Result in
let pattern = Format.sprintf "%%{%s}%%" (to_string var) in
let switch =
Expand All @@ -108,11 +111,11 @@ module Var = struct
in
if String.equal output "" then Ok None
else
match Fpath.of_string output with
match decode output with
| Ok x -> Ok (Some x)
| Error (`Msg _) -> Error `Unknown

let set runner ~name var = function
let set runner ~name var encode = function
| None -> Ok ()
| Some value ->
run_opam runner
Expand All @@ -121,11 +124,19 @@ module Var = struct
switch name;
A "set";
A (to_string var);
A (Fpath.to_string value);
A (encode value);
]
end

let get_compiler_sources runner ~name = Var.get runner ~name Compiler_sources
let get_compiler_sources runner ~name =
Var.get runner ~name Compiler_sources Fpath.of_string

let set_compiler_sources runner ~name value =
Var.set runner ~name Compiler_sources value
Var.set runner ~name Compiler_sources Fpath.to_string value

let get_configure_command runner ~name =
Var.get runner ~name Configure_command Bos.Cmd.of_string

let set_configure_command runner ~name value =
let cmd_to_string cmd = Format.asprintf "%a" pp_cmd cmd in
Var.set runner ~name Configure_command cmd_to_string value
11 changes: 10 additions & 1 deletion lib/opam.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ val set_base : Runner.t -> name:Switch_name.t -> (unit, error) result
val update : Runner.t -> name:Switch_name.t -> (unit, error) result

val reinstall_compiler :
Runner.t -> compiler_sources:Fpath.t -> (unit, error) result
Runner.t ->
compiler_sources:Fpath.t ->
configure_command:Bos.Cmd.t option ->
(unit, error) result

val reinstall_packages : Runner.t -> (unit, error) result

Expand All @@ -26,3 +29,9 @@ val get_compiler_sources :

val set_compiler_sources :
Runner.t -> name:Switch_name.t -> Fpath.t option -> (unit, error) result

val get_configure_command :
Runner.t -> name:Switch_name.t option -> (Bos.Cmd.t option, error) result

val set_configure_command :
Runner.t -> name:Switch_name.t -> Bos.Cmd.t option -> (unit, error) result
1 change: 1 addition & 0 deletions opam-compiler.opam
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ depends: [
"bos"
"cmdliner"
"curly"
"either" {with-test}
"github"
"ocaml-version"
"re"
Expand Down
12 changes: 8 additions & 4 deletions test/cram/reinstall.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
$ opam-compiler reinstall --dry-run
Run_out: OPAMCLI=2.0 opam config expand %{compiler-sources}%
Run_out: OPAMCLI=2.0 opam config expand %{compiler-configure-command}%
Run_out: OPAMCLI=2.0 opam config var prefix
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && ./configure --prefix "$(OPAMCLI=2.0 opam config var prefix)"
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && $(OPAMCLI=2.0 opam config expand %{compiler-configure-command}%) --prefix "$(OPAMCLI=2.0 opam config var prefix)"
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && make
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && make install
Run: OPAMCLI=2.0 opam reinstall --assume-built --working-dir ocaml-variants
Expand All @@ -10,17 +11,19 @@ Quick can be passed:

$ opam-compiler reinstall --dry-run --quick
Run_out: OPAMCLI=2.0 opam config expand %{compiler-sources}%
Run_out: OPAMCLI=2.0 opam config expand %{compiler-configure-command}%
Run_out: OPAMCLI=2.0 opam config var prefix
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && ./configure --prefix "$(OPAMCLI=2.0 opam config var prefix)"
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && $(OPAMCLI=2.0 opam config expand %{compiler-configure-command}%) --prefix "$(OPAMCLI=2.0 opam config var prefix)"
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && make
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && make install

Full will reinstall opam packages (and is the default):

$ opam-compiler reinstall --dry-run --full
Run_out: OPAMCLI=2.0 opam config expand %{compiler-sources}%
Run_out: OPAMCLI=2.0 opam config expand %{compiler-configure-command}%
Run_out: OPAMCLI=2.0 opam config var prefix
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && ./configure --prefix "$(OPAMCLI=2.0 opam config var prefix)"
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && $(OPAMCLI=2.0 opam config expand %{compiler-configure-command}%) --prefix "$(OPAMCLI=2.0 opam config var prefix)"
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && make
Run: cd $(OPAMCLI=2.0 opam config expand %{compiler-sources}%) && make install
Run: OPAMCLI=2.0 opam reinstall --assume-built --working-dir ocaml-variants
Expand All @@ -29,8 +32,9 @@ Switch name can be passed explicitly:

$ opam-compiler reinstall --dry-run --switch NAME
Run_out: OPAMCLI=2.0 opam config --switch NAME expand %{compiler-sources}%
Run_out: OPAMCLI=2.0 opam config --switch NAME expand %{compiler-configure-command}%
Run_out: OPAMCLI=2.0 opam config var prefix
Run: cd $(OPAMCLI=2.0 opam config --switch NAME expand %{compiler-sources}%) && ./configure --prefix "$(OPAMCLI=2.0 opam config var prefix)"
Run: cd $(OPAMCLI=2.0 opam config --switch NAME expand %{compiler-sources}%) && $(OPAMCLI=2.0 opam config --switch NAME expand %{compiler-configure-command}%) --prefix "$(OPAMCLI=2.0 opam config var prefix)"
Run: cd $(OPAMCLI=2.0 opam config --switch NAME expand %{compiler-sources}%) && make
Run: cd $(OPAMCLI=2.0 opam config --switch NAME expand %{compiler-sources}%) && make install
Run: OPAMCLI=2.0 opam reinstall --assume-built --working-dir ocaml-variants
3 changes: 2 additions & 1 deletion test/opam/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ let () =
| [| _; "reinstall-compiler"; compiler_sources_s |] ->
(let* compiler_sources = Fpath.of_string compiler_sources_s in
translate_error "reinstall-compiler"
(Opam.reinstall_compiler runner ~compiler_sources))
(Opam.reinstall_compiler runner ~compiler_sources
~configure_command:None))
|> Rresult.R.failwith_error_msg
| _ -> assert false
2 changes: 1 addition & 1 deletion test/unit/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
(name test)
(action
(run %{test} --compact))
(libraries alcotest opam_compiler))
(libraries alcotest either opam_compiler))
5 changes: 0 additions & 5 deletions test/unit/helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@ open Opam_compiler
let github_client_fail_all =
let pr_info _ = assert false in
{ Github_client.pr_info }

let runner_fail_all =
let run ?extra_env:_ ?chdir:_ _ = assert false in
let run_out ?extra_env:_ _ = assert false in
{ Runner.run; run_out }
2 changes: 0 additions & 2 deletions test/unit/helpers.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
open Opam_compiler

val github_client_fail_all : Github_client.t

val runner_fail_all : Runner.t
65 changes: 52 additions & 13 deletions test/unit/mock.ml
Original file line number Diff line number Diff line change
@@ -1,27 +1,66 @@
type ('a, 'b) expectation = 'a * 'b

type ('a, 'b) t = {
testable : 'a Alcotest.testable;
let expect expected ~and_return:return_value = (expected, return_value)

type ('a1, 'b1, 'a2, 'b2) t = {
testable1 : 'a1 Alcotest.testable;
testable2 : 'a2 Alcotest.testable;
loc : string;
mutable expectations : ('a, 'b) expectation list;
mutable expectations :
(('a1, 'b1) expectation, ('a2, 'b2) expectation) Either.t list;
}

let expect expected ~and_return:return_value = (expected, return_value)
let call1 t got =
match t.expectations with
| [] ->
Alcotest.failf "Got call at %s but no more expectations: %a" t.loc
(Alcotest.pp t.testable1) got
| Left (expected, rv) :: other_expectations ->
t.expectations <- other_expectations;
Alcotest.check t.testable1 t.loc expected got;
rv
| Right (expected, _) :: _ ->
Alcotest.failf "Got a call of the wrong type at %s: got %a, expected %a"
t.loc (Alcotest.pp t.testable1) got (Alcotest.pp t.testable2) expected

let call t got =
let call2 t got =
(* XXX dedup with call1 *)
match t.expectations with
| [] ->
Alcotest.failf "Got call at %s but no more expectations: %a" t.loc
(Alcotest.pp t.testable) got
| (expected, rv) :: other_expectations ->
(Alcotest.pp t.testable2) got
| Right (expected, rv) :: other_expectations ->
t.expectations <- other_expectations;
Alcotest.check t.testable t.loc expected got;
Alcotest.check t.testable2 t.loc expected got;
rv
| Left (expected, _) :: _ ->
Alcotest.failf "Got a call of the wrong type at %s: got %a, expected %a"
t.loc (Alcotest.pp t.testable2) got (Alcotest.pp t.testable1) expected

let check_empty2 t =
match t.expectations with
| [] -> ()
| Left (remaining, _) :: _ ->
Alcotest.check (Alcotest.option t.testable1) t.loc None (Some remaining)
| Right (remaining, _) :: _ ->
Alcotest.check (Alcotest.option t.testable2) t.loc None (Some remaining)

let create2 testable1 testable2 loc expectations =
let t = { testable1; testable2; loc; expectations } in
(call1 t, call2 t, fun () -> check_empty2 t)

module Void = struct
type t = |

let absurd : t -> _ = function _ -> .

let pp _ppf = absurd

let check_empty t =
let remaining = List.map fst t.expectations in
Alcotest.check (Alcotest.list t.testable) t.loc [] remaining
let equal = absurd
end

let create testable loc expectations =
let t = { testable; loc; expectations } in
(call t, fun () -> check_empty t)
let f, (_ : Void.t -> _), check =
create2 testable (module Void) loc (List.map Either.left expectations)
in
(f, check)
7 changes: 7 additions & 0 deletions test/unit/mock.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ val create :
A check function is returned that should be run at the end of its lifetime.
It is possible to use [(let$)] to run it at the end of a binding, e.g.
[let$ f = create ... in ...]. *)

val create2 :
'a1 Alcotest.testable ->
'a2 Alcotest.testable ->
string ->
(('a1, 'b1) expectation, ('a2, 'b2) expectation) Either.t list ->
('a1 -> 'b1) * ('a2 -> 'b2) * (unit -> unit)
Loading

0 comments on commit fe78b9b

Please sign in to comment.