diff --git a/lsp/src/uri0.ml b/lsp/src/uri0.ml index 6391189c6..605bc10f1 100644 --- a/lsp/src/uri0.ml +++ b/lsp/src/uri0.ml @@ -1,5 +1,9 @@ open Import +module Private = struct + let win32 = ref Sys.win32 +end + type t = Uri_lexer.t = { scheme : string option ; authority : string @@ -46,7 +50,7 @@ let to_path t = |> String.replace_all ~pattern:"%3D" ~with_:"=" |> String.replace_all ~pattern:"%3F" ~with_:"?" in - if Sys.win32 then path else Filename.concat "/" path + if !Private.win32 then path else Filename.concat "/" path let of_path (path : string) = let path = Uri_lexer.escape_path path in diff --git a/lsp/src/uri0.mli b/lsp/src/uri0.mli index b28ea36d9..d186e85b0 100644 --- a/lsp/src/uri0.mli +++ b/lsp/src/uri0.mli @@ -17,3 +17,7 @@ val to_path : t -> string val of_path : string -> t val to_string : t -> string + +module Private : sig + val win32 : bool ref +end diff --git a/lsp/test/uri_tests.ml b/lsp/test/uri_tests.ml index ed1467bf8..c18a0ec2a 100644 --- a/lsp/test/uri_tests.ml +++ b/lsp/test/uri_tests.ml @@ -1,25 +1,43 @@ open Lsp +let run_with_modes f = + print_endline "Unix:"; + Lsp.Uri.Private.win32 := false; + f (); + print_endline "Windows:"; + Lsp.Uri.Private.win32 := true; + f () + +let test_uri_parsing = + let test s = + let uri = Uri.t_of_yojson (`String s) in + Printf.printf "%s -> %s\n" s (Uri.to_path uri) + in + fun uris -> run_with_modes (fun () -> List.iter test uris) + let%expect_test "test uri parsing" = - let uri = Uri.t_of_yojson (`String "file:///Users/foo") in - print_endline (Uri.to_path uri); - [%expect {| - /Users/foo |}]; - print_endline (Uri.to_string uri); - [%expect {| file:///Users/foo |}]; - let uri = Uri.t_of_yojson (`String "file:///c:/Users/foo") in - print_endline (Uri.to_path uri); - [%expect {| /c:/Users/foo |}]; - print_endline (Uri.to_string uri); + test_uri_parsing [ "file:///Users/foo"; "file:///c:/Users/foo" ]; [%expect {| - file:///c:/Users/foo |}] + Unix: + file:///Users/foo -> /Users/foo + file:///c:/Users/foo -> /c:/Users/foo + Windows: + file:///Users/foo -> Users/foo + file:///c:/Users/foo -> c:/Users/foo |}] + +let uri_of_path = + let test path = + let uri = Uri.of_path path in + Printf.printf "%s -> %s\n" path (Uri.to_string uri) + in + fun uris -> run_with_modes (fun () -> List.iter test uris) let%expect_test "uri of path" = - let uri = Uri.of_path "/foo/bar.ml" in - print_endline (Uri.to_string uri); - [%expect {| - file:///foo/bar.ml |}]; - let uri = Uri.of_path "foo/bar.mli" in - print_endline (Uri.to_string uri); + uri_of_path [ "/foo/bar.ml"; "foo/bar.mli" ]; [%expect {| - file:///foo/bar.mli |}] + Unix: + /foo/bar.ml -> file:///foo/bar.ml + foo/bar.mli -> file:///foo/bar.mli + Windows: + /foo/bar.ml -> file:///foo/bar.ml + foo/bar.mli -> file:///foo/bar.mli |}]