Skip to content

Commit

Permalink
Add error when switch does not have a source tree
Browse files Browse the repository at this point in the history
  • Loading branch information
emillon committed Jul 9, 2021
1 parent 9f4153d commit 2812006
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ 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)
2 changes: 1 addition & 1 deletion lib/import.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/op.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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)
Expand Down
35 changes: 34 additions & 1 deletion test/unit/test_op.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -69,4 +77,29 @@ let create_tests =
git+https://github.com/USER/REPO#BRANCH");
]

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) ]

0 comments on commit 2812006

Please sign in to comment.