Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(uri): add an e2e test for uri #771

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ocaml-lsp-server/test/e2e-new/dune
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
stdune
fiber
yojson
ppx_yojson_conv_lib
lev_fiber
lev
spawn
Expand Down
48 changes: 48 additions & 0 deletions ocaml-lsp-server/test/e2e-new/metrics.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
open Test.Import

let%expect_test "start/stop" =
let handler =
let on_request (type r) _ (r : r Lsp.Server_request.t) :
(r Lsp_fiber.Rpc.Reply.t * unit) Fiber.t =
match r with
| ShowDocumentRequest p ->
print_endline "client: received show document parms";
let json =
ShowDocumentParams.yojson_of_t { p with uri = "<redacted>" }
in
Yojson.Safe.to_channel stdout json;
print_endline "";
print_endline "metrics contents:";
print_endline (Stdune.Io.String_path.read_file p.uri);
let res = ShowDocumentResult.create ~success:true in
Fiber.return (Lsp_fiber.Rpc.Reply.now res, ())
| _ -> assert false
in
let on_request = { Client.Handler.on_request } in
Client.Handler.make ~on_request ()
in
( Test.run ~handler @@ fun client ->
let run_client () =
let capabilities = ClientCapabilities.create () in
Client.start client (InitializeParams.create ~capabilities ())
in
let run =
let* (_ : InitializeResult.t) = Client.initialized client in
let view_metrics =
ExecuteCommandParams.create ~command:"ocamllsp/view-metrics" ()
in
let+ res = Client.request client (ExecuteCommand view_metrics) in
print_endline "server: receiving response";
Yojson.Safe.to_channel stdout res;
print_endline ""
in
Fiber.fork_and_join_unit run_client (fun () -> run >>> Client.stop client)
);
[%expect
{|
client: received show document parms
{"uri":"<redacted>","takeFocus":true}
metrics contents:
{"traceEvents":[]}
server: receiving response
null |}]
11 changes: 8 additions & 3 deletions ocaml-lsp-server/test/e2e-new/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ end
open Import

module T : sig
val run : (unit Client.t -> unit Fiber.t) -> unit
val run :
?handler:unit Client.Handler.t -> (unit Client.t -> unit Fiber.t) -> unit
end = struct
let _PATH =
Bin.parse_path (Option.value ~default:"" @@ Env.get Env.initial "PATH")
Expand All @@ -18,7 +19,7 @@ end = struct

let env = Spawn.Env.of_list [ "OCAMLLSP_TEST=true" ]

let run f =
let run ?handler f =
let stdin_i, stdin_o = Unix.pipe ~cloexec:true () in
let stdout_i, stdout_o = Unix.pipe ~cloexec:true () in
let pid =
Expand All @@ -27,7 +28,11 @@ end = struct
in
Unix.close stdin_i;
Unix.close stdout_o;
let handler = Client.Handler.make () in
let handler =
match handler with
| Some h -> h
| None -> Client.Handler.make ()
in
let init =
let blockity =
if Sys.win32 then `Blocking
Expand Down
44 changes: 44 additions & 0 deletions ocaml-lsp-server/test/e2e-new/uri.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
open Test.Import

let%expect_test "uri" =
let notif_received = Fiber.Ivar.create () in
let handler =
let on_notification _ (notification : Lsp.Server_notification.t) =
(match notification with
| PublishDiagnostics d ->
print_endline "client: received publish diagnostics notification";
Printf.printf "uri received from the server: %s\n" (d.uri |> DocumentUri.to_string)
| ShowMessage _
| LogMessage _
| TelemetryNotification _
| CancelRequest _
| WorkDoneProgress _
| UnknownNotification _ -> ());
let* () = Fiber.Ivar.fill notif_received () in
Fiber.return ()
in
Client.Handler.make ~on_notification ()
in
( Test.run ~handler @@ fun client ->
let run_client () =
let capabilities = ClientCapabilities.create () in
Client.start client (InitializeParams.create ~capabilities ())
in
let run =
let* (_ : InitializeResult.t) = Client.initialized client in
let uri = DocumentUri.of_path "src/néw/Mödel + Other Thîngß/test.ml" in
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it represents what happens in reality because our DocumentUri.of_path implementation isn't correct so the URI string that will be sent to the server is already "broken".

What happens in real is that VSCode sends a valid URI string, and the server returns an incorrect one (due to an incorrect parsing).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said, I'm not sure that an e2e test is useful for that. We already have unit tests for the URI parsing.

Printf.printf "uri sent to the server: %s\n" (DocumentUri.to_string uri);
let textDocument =
TextDocumentItem.create ~uri ~languageId:"ocaml" ~text:"" ~version:0
in
let params = DidOpenTextDocumentParams.create ~textDocument in
let* () = Client.notification client (TextDocumentDidOpen params) in
Fiber.Ivar.read notif_received
in
Fiber.fork_and_join_unit run_client (fun () -> run >>> Client.stop client)
);
[%expect
{|
uri sent to the server: file:///src/néw/Mödel + Other Thîngß/test.ml
client: received publish diagnostics notification
uri received from the server: file:///src/néw/Mödel + Other Thîngß/test.ml |}]
48 changes: 48 additions & 0 deletions ocaml-lsp-server/test/e2e/__tests__/uri.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import outdent from "outdent";
import * as rpc from "vscode-jsonrpc/node";
import * as LanguageServer from "../src/LanguageServer";
import * as Protocol from "vscode-languageserver-protocol";
import * as Types from "vscode-languageserver-types";
import { URI } from "vscode-uri";

describe("uri", () => {
let languageServer: rpc.MessageConnection;
const uri = URI.file("src/néw/Mödel + Other Thîngß/test.ml").toString();

function openDocument(source: string) {
languageServer.sendNotification(
Protocol.DidOpenTextDocumentNotification.type,
{
textDocument: Types.TextDocumentItem.create(uri, "ocaml", 0, source),
},
);
}

beforeEach(async () => {
languageServer = await LanguageServer.startAndInitialize();
});

afterEach(async () => {
await LanguageServer.exit(languageServer);
});

it("handles uri with special characters", async () => {
let receivedDiganostics: Promise<Protocol.PublishDiagnosticsParams> =
new Promise((resolve, _reject) =>
languageServer.onNotification(
Protocol.PublishDiagnosticsNotification.type,
(params) => {
resolve(params);
},
),
);
openDocument(outdent`
let () =
let x = 123 in
()
`);
const params = await receivedDiganostics;

expect(params.uri).toBe(uri);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails with:

Expected: "file:///src/n%C3%A9w/M%C3%B6del%20%2B%20Other%20Th%C3%AEng%C3%9F/test.ml"
Received: "file:///src/n%C3%A9w/M%C3%B6del %2B Other Th%C3%AEng%C3%9F/test.ml"

#739 should fix it but I'm not sure if we really need an e2e test for that since we already have OCaml tests for the URI parsing?

});
});