| 1 |
(* end to end tests of dune_rpc. Verify that whatever is exposed to the client |
| 2 |
is usable *) |
| 3 |
|
| 4 |
open Stdune |
| 5 |
open Lwt.Syntax |
| 6 |
open Dune_rpc.V1 |
| 7 |
open Dune_rpc_lwt.V1 |
| 8 |
|
| 9 |
let _XDG_STATE_HOME = "XDG_STATE_HOME" |
| 10 |
let xdg_state_dir = Temp.create Dir ~prefix:"lwt" ~suffix:"dune" |
| 11 |
let () = Unix.putenv _XDG_STATE_HOME (Stdune.Path.to_absolute_filename xdg_state_dir) |
| 12 |
|
| 13 |
let connect ~root_dir = |
| 14 |
let build_dir = Filename.concat root_dir "_build" in |
| 15 |
let env = |
| 16 |
let env = |
| 17 |
Env.add |
| 18 |
Env.initial |
| 19 |
~var:_XDG_STATE_HOME |
| 20 |
~value:(Stdune.Path.to_absolute_filename xdg_state_dir) |
| 21 |
in |
| 22 |
Env.get env |
| 23 |
in |
| 24 |
let* res = Where.get ~env ~build_dir in |
| 25 |
match res with |
| 26 |
| Error e -> Lwt.fail e |
| 27 |
| Ok None -> Lwt.fail_with (sprintf "unable to establish to connection in %s" build_dir) |
| 28 |
| Ok (Some where) -> |
| 29 |
let where = |
| 30 |
match where with |
| 31 |
| `Unix addr -> |
| 32 |
(* this hackery is needed because the temp dir we wrote the socket to is |
| 33 |
symlinked on a mac *) |
| 34 |
let addr = |
| 35 |
match Unix.realpath addr with |
| 36 |
| s -> s |
| 37 |
| exception Unix.Unix_error _ -> addr |
| 38 |
in |
| 39 |
`Unix |
| 40 |
(match String.drop_prefix addr ~prefix:(Sys.getcwd () ^ "/") with |
| 41 |
| None -> addr |
| 42 |
| Some addr -> Filename.concat "." addr) |
| 43 |
| _ as s -> s |
| 44 |
in |
| 45 |
connect_chan where |
| 46 |
;; |
| 47 |
|
| 48 |
let build_watch ~root_dir = |
| 49 |
Lwt_process.open_process_none |
| 50 |
~stdin:`Close |
| 51 |
~stderr:`Dev_null |
| 52 |
( "dune" |
| 53 |
, [| "dune" |
| 54 |
; "build" |
| 55 |
; "--no-print-directory" |
| 56 |
; "--root" |
| 57 |
; root_dir |
| 58 |
; "-w" |
| 59 |
; "--file-watcher" |
| 60 |
; "manual" |
| 61 |
; "@install" |
| 62 |
|] ) |
| 63 |
;; |
| 64 |
|
| 65 |
let run_with_timeout f = |
| 66 |
Lwt.catch |
| 67 |
(fun () -> |
| 68 |
let+ () = |
| 69 |
Lwt_unix.with_timeout 3.0 (fun () -> |
| 70 |
let+ _ = f () in |
| 71 |
()) |
| 72 |
in |
| 73 |
print_endline "success") |
| 74 |
(fun exn -> |
| 75 |
(match exn with |
| 76 |
| Lwt_unix.Timeout -> print_endline "timeout" |
| 77 |
| _ -> ()); |
| 78 |
Lwt.return_unit) |
| 79 |
;; |
| 80 |
|
| 81 |
let initial_cwd = Sys.getcwd () |
| 82 |
|
| 83 |
let%expect_test "run and connect" = |
| 84 |
let initialize = Request.Initialize.create ~id:(Request.Id.make (Csexp.Atom "test")) in |
| 85 |
Sys.chdir initial_cwd; |
| 86 |
Lwt_main.run |
| 87 |
(let* root_dir = Lwt_io.create_temp_dir () in |
| 88 |
Sys.chdir root_dir; |
| 89 |
let build = build_watch ~root_dir in |
| 90 |
let rpc = |
| 91 |
let* () = Lwt_unix.sleep 0.5 in |
| 92 |
connect ~root_dir |
| 93 |
in |
| 94 |
let run_client = |
| 95 |
let* rpc = rpc in |
| 96 |
Client.connect rpc initialize ~f:(fun t -> |
| 97 |
print_endline "started session"; |
| 98 |
let* ping = Client.Versioned.prepare_request t Request.ping in |
| 99 |
let ping = |
| 100 |
match ping with |
| 101 |
| Ok p -> p |
| 102 |
| Error _ -> assert false |
| 103 |
in |
| 104 |
let* res = Client.request t ping () in |
| 105 |
match res with |
| 106 |
| Error _ -> failwith "unexpected" |
| 107 |
| Ok () -> |
| 108 |
print_endline "received ping. shutting down."; |
| 109 |
let* shutdown = |
| 110 |
Client.Versioned.prepare_notification t Notification.shutdown |
| 111 |
in |
| 112 |
let shutdown = |
| 113 |
match shutdown with |
| 114 |
| Ok s -> s |
| 115 |
| Error _ -> assert false |
| 116 |
in |
| 117 |
Client.notification t shutdown ()) |
| 118 |
in |
| 119 |
let run_build = |
| 120 |
let+ res = build#status in |
| 121 |
match res with |
| 122 |
| WEXITED i -> printfn "dune build finished with %i" i |
| 123 |
| _ -> assert false |
| 124 |
in |
| 125 |
Lwt.finalize |
| 126 |
(fun () -> run_with_timeout (fun () -> Lwt.all [ run_client; run_build ])) |
| 127 |
(fun () -> |
| 128 |
build#terminate; |
| 129 |
Lwt.return_unit)); |
| 130 |
[%expect |
| 131 |
{| |
| 132 |
started session |
| 133 |
received ping. shutting down. |
| 134 |
dune build finished with 0 |
| 135 |
success |}] |
| 136 |
;; |
| 137 |
|
| 138 |
module Logger = struct |
| 139 |
(* A little helper to make the output from the client and server |
| 140 |
deterministic. Log messages are batched and outputted at the end. *) |
| 141 |
type t = |
| 142 |
{ mutable messages : string list |
| 143 |
; name : string |
| 144 |
} |
| 145 |
|
| 146 |
let create ~name = { messages = []; name } |
| 147 |
let log t fmt = Printf.ksprintf (fun m -> t.messages <- m :: t.messages) fmt |
| 148 |
|
| 149 |
let print { messages; name } = |
| 150 |
List.rev messages |> List.iter ~f:(fun msg -> printfn "%s: %s" name msg) |
| 151 |
;; |
| 152 |
end |