-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: serialize and deserialize RPC error messages (#8411)
We demonstrate a bug with how error messages are serialized. Currently there are two "Error:" prefixes after serializing and deserializing. This is because the prefix hasn't been stripped in the initial serialization. We should strip the prefix since the severity information is recorded elsewhere in the diagnostic data. Signed-off-by: Ali Caglayan <alizter@gmail.com>
- Loading branch information
Showing
8 changed files
with
351 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(library | ||
(name dune_rpc_impl_tests) | ||
(modules dune_rpc_impl_tests) | ||
(inline_tests) | ||
(libraries | ||
; ocaml_config | ||
; dune_util | ||
dune_console | ||
dune_rpc_private | ||
dune_rpc_impl | ||
dune_engine | ||
dune_re | ||
stdune | ||
;; This is because of the (implicit_transitive_deps false) | ||
;; in dune-project | ||
ppx_expect.config | ||
ppx_expect.config_types | ||
ppx_expect.common | ||
base | ||
ppx_inline_test.config) | ||
(preprocess | ||
(pps ppx_expect))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
open Stdune | ||
module Dune_rpc = Dune_rpc_private | ||
module Re = Dune_re | ||
|
||
let () = | ||
Stdune.Path.set_root (Stdune.Path.External.of_filename_relative_to_initial_cwd "."); | ||
Stdune.Path.Build.set_build_dir (Stdune.Path.Outside_build_dir.of_string "_build") | ||
;; | ||
|
||
let test ~dir ~f main = | ||
let description = | ||
`Diagnostic (Dune_engine.Compound_user_error.make ~main ~related:[]) | ||
in | ||
Dune_console.printf "---- Original ----"; | ||
f main; | ||
Dune_console.printf "------- RPC ------"; | ||
Dune_engine.Build_system.Error.For_tests.make ~description ~dir ~promotion:None () | ||
|> Dune_rpc_impl.Diagnostics.For_tests.diagnostic_of_error | ||
|> Dune_rpc_private.Diagnostic.to_user_message | ||
|> f | ||
;; | ||
|
||
let test_plain ~dir main = test main ~dir ~f:Dune_console.print_user_message | ||
|
||
let test_dyn ~dir main = | ||
test main ~dir ~f:(fun x -> | ||
Stdune.User_message.pp x | ||
|> Pp.to_dyn Stdune.User_message.Style.to_dyn | ||
|> Dyn.to_string | ||
|> print_endline) | ||
;; | ||
|
||
let scrub output = | ||
Re.replace_string | ||
(Re.compile (Re.str Stdune.Path.(to_absolute_filename root))) | ||
~by:"TEST" | ||
output | ||
|> print_endline | ||
;; | ||
|
||
let%expect_test "serialize and deserialize error message" = | ||
let dir = None in | ||
let message = User_error.make [ Pp.verbatim "Oh no!" ] in | ||
test_plain ~dir message; | ||
test_dyn ~dir message; | ||
[%expect | ||
{| | ||
---- Original ---- | ||
Error: Oh no! | ||
------- RPC ------ | ||
Error: Error: Oh no! | ||
---- Original ---- | ||
Vbox | ||
0,Seq | ||
Box | ||
0,Concat | ||
Break ("", 1, ""),("", 0, ""),[ Seq | ||
Tag Error,Verbatim "Error", | ||
Char | ||
: | ||
; Verbatim "Oh no!" | ||
],Break ("", 0, ""),("", 0, "") | ||
------- RPC ------ | ||
Vbox | ||
0,Seq | ||
Box | ||
0,Concat | ||
Break ("", 1, ""),("", 0, ""),[ Tag Error,Text "Error:" | ||
; Concat | ||
Break ("", 1, ""),("", 0, ""), | ||
[ Seq | ||
Tag | ||
Details,Verbatim "Error", | ||
Char | ||
: | ||
; Verbatim "Oh no!" | ||
] | ||
],Break ("", 0, ""),("", 0, "") |}] | ||
;; | ||
|
||
let%expect_test "serialize and deserialize error message with location" = | ||
let loc = Stdune.Loc.of_pos ("Bar", 1, 2, 3) in | ||
let dir = Some (Stdune.Path.of_string "/Foo") in | ||
let message = User_error.make ~loc [ Pp.verbatim "An error with location!" ] in | ||
test_plain ~dir message; | ||
test_dyn ~dir message; | ||
[%expect | ||
{| | ||
---- Original ---- | ||
File "Bar", line 1, characters 2-3: | ||
Error: An error with location! | ||
------- RPC ------ | ||
Error: (In directory /Foo) | ||
File "/Foo/Bar", line 1, characters 2-3: | ||
|
||
Error: An error with location! | ||
---- Original ---- | ||
Vbox | ||
0,Concat | ||
Nop,[ Seq | ||
Box 0,Tag Loc,Text "File \"Bar\", line 1, characters 2-3:", | ||
Break | ||
("", 0, ""),("", 0, "") | ||
; Seq | ||
Box | ||
0,Concat | ||
Break ("", 1, ""),("", 0, ""),[ Seq | ||
Tag | ||
Error,Verbatim | ||
"Error", | ||
Char | ||
: | ||
; Verbatim | ||
"An error with location!" | ||
],Break | ||
("", 0, ""),("", 0, "") | ||
] | ||
------- RPC ------ | ||
Vbox | ||
0,Concat | ||
Nop,[ Seq | ||
Box | ||
0,Concat | ||
Break ("", 1, ""),("", 0, ""),[ Tag Error,Text "Error:" | ||
; Tag | ||
Loc,Text | ||
"(In directory /Foo)" | ||
],Break | ||
("", 0, ""),("", 0, "") | ||
; Seq | ||
Box | ||
0,Seq | ||
Seq | ||
Tag | ||
Loc,Verbatim | ||
"File \"/Foo/Bar\", line 1, characters 2-3:",Newline,Nop, | ||
Break | ||
("", 0, ""),("", 0, "") | ||
; Seq | ||
Box | ||
0,Concat | ||
Break ("", 1, ""),("", 0, ""),[ Seq | ||
Tag | ||
Details,Verbatim | ||
"Error", | ||
Char | ||
: | ||
; Verbatim | ||
"An error with location!" | ||
],Break | ||
("", 0, ""),("", 0, "") | ||
] |}] | ||
;; | ||
|
||
let%expect_test "serialize and deserialize error with location exerpt and hint" = | ||
Io.String_path.write_file "foo.ml" "let x = 1\nlet y = 2\nlet z = 3\n"; | ||
let loc = Stdune.Loc.of_pos ("foo.ml", 1, 2, 3) in | ||
let dir = Some (Stdune.Path.of_string ".") in | ||
let hints = [ Pp.verbatim "Hint 1"; Pp.verbatim "Hint 2" ] in | ||
let message = User_error.make ~loc ~hints [ Pp.verbatim "An error with location!" ] in | ||
test_plain ~dir message; | ||
test_dyn ~dir message; | ||
scrub [%expect.output]; | ||
[%expect | ||
{| | ||
---- Original ---- | ||
File "foo.ml", line 1, characters 2-3: | ||
1 | let x = 1 | ||
^ | ||
Error: An error with location! | ||
Hint: Hint 1 | ||
Hint: Hint 2 | ||
------- RPC ------ | ||
Error: (In directory | ||
TEST) | ||
File "TEST/foo.ml", line 1, characters 2-3: | ||
1 | let x = 1 | ||
^ | ||
|
||
Error: An error with location! | ||
---- Original ---- | ||
Vbox | ||
0,Concat | ||
Nop,[ Seq | ||
Box 0,Tag Loc,Text "File \"foo.ml\", line 1, characters 2-3:", | ||
Break | ||
("", 0, ""),("", 0, "") | ||
; Seq | ||
Box | ||
0,Concat | ||
Break ("", 1, ""),("", 0, ""),[ Seq | ||
Tag | ||
Error,Verbatim | ||
"Error", | ||
Char | ||
: | ||
; Verbatim | ||
"An error with location!" | ||
],Break | ||
("", 0, ""),("", 0, "") | ||
; Seq | ||
Box | ||
0,Seq | ||
Seq | ||
Tag Hint,Verbatim "Hint:",Break ("", 1, ""),("", 0, ""), | ||
Verbatim | ||
"Hint 1",Break ("", 0, ""),("", 0, "") | ||
; Seq | ||
Box | ||
0,Seq | ||
Seq | ||
Tag Hint,Verbatim "Hint:",Break ("", 1, ""),("", 0, ""), | ||
Verbatim | ||
"Hint 2",Break ("", 0, ""),("", 0, "") | ||
] | ||
------- RPC ------ | ||
Vbox | ||
0,Concat | ||
Nop,[ Seq | ||
Box | ||
0,Concat | ||
Break ("", 1, ""),("", 0, ""),[ Tag Error,Text "Error:" | ||
; Tag | ||
Loc,Text | ||
"(In directory TEST)" | ||
],Break | ||
("", 0, ""),("", 0, "") | ||
; Seq | ||
Box | ||
0,Seq | ||
Seq | ||
Tag | ||
Loc,Verbatim | ||
"File \"TEST/foo.ml\", line 1, characters 2-3:",Newline, | ||
Seq | ||
Seq | ||
Seq | ||
Seq | ||
Seq Verbatim "1",Verbatim " | ",Verbatim | ||
"let x = 1",Newline, | ||
Verbatim | ||
" ^",Newline,Break ("", 0, ""),("", 0, "") | ||
; Seq | ||
Box | ||
0,Concat | ||
Break ("", 1, ""),("", 0, ""),[ Seq | ||
Tag | ||
Details,Verbatim | ||
"Error", | ||
Char | ||
: | ||
; Verbatim | ||
"An error with location!" | ||
],Break | ||
("", 0, ""),("", 0, "") | ||
] |}] | ||
;; |