From d0a29efbac442964cc8a5db9ec9c8446789a3de4 Mon Sep 17 00:00:00 2001 From: Etienne Millon Date: Tue, 29 Jun 2021 15:50:28 +0200 Subject: [PATCH] Add error when switch does not have a source tree --- lib/import.ml | 5 +++-- lib/import.mli | 2 +- lib/op.ml | 4 ++-- test/unit/test_op.ml | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/import.ml b/lib/import.ml index 4e7b987..e9fa3fb 100644 --- a/lib/import.ml +++ b/lib/import.ml @@ -42,10 +42,11 @@ let pp_cmd ppf cmd = Bos.Cmd.to_list cmd |> List.map quote_if_needed |> String.concat " " |> Format.pp_print_string ppf -type error = [ `Command_failed of Bos.Cmd.t | `Unknown ] +type error = [ `Command_failed of Bos.Cmd.t | `Unknown | `No_compiler_sources ] let translate_error s = let open Rresult.R in reword_error (function | `Unknown -> msgf "%s" s - | `Command_failed cmd -> msgf "%s - command failed: %a" s pp_cmd cmd) + | `Command_failed cmd -> msgf "%s - command failed: %a" s pp_cmd cmd + | `No_compiler_sources -> msgf "%s - switch is not linked to compiler sources" s) diff --git a/lib/import.mli b/lib/import.mli index 9187937..c4159bf 100644 --- a/lib/import.mli +++ b/lib/import.mli @@ -27,7 +27,7 @@ val pp_env : Format.formatter -> (string * string) list option -> unit val pp_cmd : Format.formatter -> Bos.Cmd.t -> unit -type error = [ `Command_failed of Bos.Cmd.t | `Unknown ] +type error = [ `Command_failed of Bos.Cmd.t | `Unknown | `No_compiler_sources ] val translate_error : string -> ('a, [< error ]) result -> ('a, [> Rresult.R.msg ]) result diff --git a/lib/op.ml b/lib/op.ml index 3338a09..b250683 100644 --- a/lib/op.ml +++ b/lib/op.ml @@ -16,7 +16,7 @@ let create runner github_client source switch_name ~configure_command = Source.compiler_sources source |> Opam.set_compiler_sources runner ~name:switch_name | Error (`Command_failed _) -> Opam.remove_switch runner ~name:switch_name - | Error `Unknown -> Error `Unknown) + | Error e -> Error e) |> translate_error "Cannot create switch" type reinstall_mode = Quick | Full @@ -29,7 +29,7 @@ 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 -> (* XXX *) assert false + | None -> Error `No_compiler_sources | Some compiler_sources -> let* () = Opam.reinstall_compiler runner ~compiler_sources in reinstall_packages_if_needed runner mode) diff --git a/test/unit/test_op.ml b/test/unit/test_op.ml index ab40405..27225d7 100644 --- a/test/unit/test_op.ml +++ b/test/unit/test_op.ml @@ -14,6 +14,14 @@ let run_mock loc expectations = let run ?extra_env ?chdir cmd = run_mock (cmd, extra_env, chdir) in (run, check) +let run_out_mock loc expectations = + let testable = + Alcotest.(pair (module Bos.Cmd) (option (list (pair string string)))) + in + let run_out_mock, check = Mock.create testable loc expectations in + let run_out ?extra_env cmd = run_out_mock (cmd, extra_env) in + (run_out, check) + let opam_cli_env = Some [ ("OPAMCLI", "2.0") ] let create_tests = @@ -57,4 +65,29 @@ let create_tests = ~expected:(Ok ()); ] -let tests = [ ("Op create", create_tests) ] +let reinstall_tests = + [ + ( "reinstall: switch does not have sources", + `Quick, + fun () -> + let expectations = + [ + Mock.expect + ( Bos.Cmd.(v "opam" % "config" % "expand" % "%{compiler-sources}%"), + opam_cli_env ) + ~and_return:(Ok ""); + ] + in + let run_out, check = run_out_mock __LOC__ expectations in + let runner = { Helpers.runner_fail_all with run_out } in + let got = Op.reinstall runner Quick ~name:None in + let expected = + Error + (`Msg + "Could not reinstall - switch is not linked to compiler sources") + in + Alcotest.check Alcotest.(result unit msg) __LOC__ expected got; + check () ); + ] + +let tests = [ ("Op create", create_tests); ("Op reinstall", reinstall_tests) ]