Debian Codegraph @

otherlibs/ocamlc-loc/test/ocamlc_loc_tests.ml

raw
1 open Stdune
2
3 let cmd fmt =
4 Printf.ksprintf
5 (fun s ->
6 let (_ : int) = Sys.command s in
7 ())
8 fmt
9 ;;
10
11 module Test = struct
12 type t = { dir : Path.t }
13
14 let restore_cwd =
15 let cwd = Sys.getcwd () in
16 fun () -> Sys.chdir cwd
17 ;;
18
19 let file t ~fname ~contents =
20 let path = Path.relative t.dir fname in
21 Io.write_file path contents;
22 path
23 ;;
24
25 let print_errors =
26 List.iteri ~f:(fun i report ->
27 printfn ">> error %d" i;
28 print_endline (Dyn.to_string (Ocamlc_loc.dyn_of_report report)))
29 ;;
30
31 let create f =
32 let dir = Temp.create Dir ~prefix:"dune." ~suffix:".test" in
33 let t = { dir } in
34 Sys.chdir (Path.to_string dir);
35 let output =
36 let out_file = Exn.protect ~f:(fun () -> f t) ~finally:restore_cwd in
37 let output = Io.read_file out_file in
38 Format.asprintf "%a@." Pp.to_fmt (Ansi_color.parse output)
39 in
40 (* Format.eprintf "print raw output:@.%s@.%!" output; *)
41 Ocamlc_loc.parse output |> print_errors
42 ;;
43 end
44
45 (* FIXME: unused value warning isn't parsed correctly - the file excerpt isn't
46 extracted *)
47 let%expect_test "unused value" =
48 let raw_error =
49 String.trim
50 {|
51 File "test.ml", line 1, characters 4-7:
52 1 | let foo = ()
53 ^^^
54 Error (warning 32 [unused-value-declaration]): unused value foo.
55 |}
56 in
57 String.split_lines raw_error
58 |> String.concat ~sep:"\r\n"
59 |> Ocamlc_loc.parse
60 |> Test.print_errors;
61 [%expect
62 {|
63 >> error 0
64 { loc = { path = "test.ml"; line = Single 1; chars = Some (4, 7) }
65 ; message = "unused value foo."
66 ; related = []
67 ; severity = Error (Some { code = 32; name = "unused-value-declaration" })
68 } |}]
69 ;;
70
71 let test_error raw_error = String.trim raw_error |> Ocamlc_loc.parse |> Test.print_errors
72
73 let test_error_raw raw_error =
74 String.trim raw_error
75 |> Ocamlc_loc.parse_raw
76 |> Ocamlc_loc.dyn_of_raw
77 |> Dyn.to_string
78 |> print_endline
79 ;;
80
81 let%expect_test "mli mismatch" =
82 test_error
83 {|
84 File "test.ml", line 1:
85 Error: The implementation test.ml does not match the interface test.cmi:
86 Values do not match: val x : bool is not included in val x : int
87 The type bool is not compatible with the type int
88 File "test.mli", line 1, characters 0-11: Expected declaration
89 File "test.ml", line 1, characters 4-5: Actual declaration
90 |};
91 [%expect
92 {|
93 >> error 0
94 { loc = { path = "test.ml"; line = Single 1; chars = None }
95 ; message =
96 "The implementation test.ml does not match the interface test.cmi:\n\
97 Values do not match: val x : bool is not included in val x : int\n\
98 The type bool is not compatible with the type int"
99 ; related =
100 [ ({ path = "test.mli"; line = Single 1; chars = Some (0, 11) },
101 "Expected declaration")
102 ; ({ path = "test.ml"; line = Single 1; chars = Some (4, 5) },
103 "Actual declaration")
104 ]
105 ; severity = Error None
106 } |}]
107 ;;
108
109 let%expect_test "" =
110 test_error
111 {|
112 File "test.ml", line 1, characters 9-12:
113 1 | let () = 123
114 ^^^
115 Error: This expression has type int but an expression was expected of type
116 unit
117 |};
118 [%expect
119 {|
120 >> error 0
121 { loc = { path = "test.ml"; line = Single 1; chars = Some (9, 12) }
122 ; message =
123 "This expression has type int but an expression was expected of type\n\
124 \ unit"
125 ; related = []
126 ; severity = Error None
127 } |}]
128 ;;
129
130 let%expect_test "warning" =
131 test_error
132 {|
133 File "test.ml", line 1, characters 13-14:
134 1 | let () = let x = 2 in ()
135 ^
136 Warning 26 [unused-var]: unused variable x.
137 |};
138 [%expect
139 {|
140 >> error 0
141 { loc = { path = "test.ml"; line = Single 1; chars = Some (13, 14) }
142 ; message = "unused variable x."
143 ; related = []
144 ; severity = Warning { code = 26; name = "unused-var" }
145 } |}]
146 ;;
147
148 let%expect_test "" =
149 test_error
150 {|
151 File "test.ml", lines 3-5, characters 6-3:
152 3 | ......struct
153 4 | let x y = y +. 2.0
154 5 | end
155 Error: Signature mismatch:
156 Modules do not match:
157 sig val x : float -> float end
158 is not included in
159 sig val x : int -> int end
160 Values do not match:
161 val x : float -> float
162 is not included in
163 val x : int -> int
164 The type float -> float is not compatible with the type int -> int
165 Type float is not compatible with type int
166 File "test.ml", line 2, characters 2-20: Expected declaration
167 File "test.ml", line 4, characters 6-7: Actual declaration
168 |};
169 [%expect
170 {|
171 >> error 0
172 { loc = { path = "test.ml"; line = Range (3, 5); chars = Some (6, 3) }
173 ; message =
174 "Signature mismatch:\n\
175 Modules do not match:\n\
176 \ sig val x : float -> float end\n\
177 is not included in\n\
178 \ sig val x : int -> int end\n\
179 Values do not match:\n\
180 \ val x : float -> float\n\
181 is not included in\n\
182 \ val x : int -> int\n\
183 The type float -> float is not compatible with the type int -> int\n\
184 Type float is not compatible with type int"
185 ; related =
186 [ ({ path = "test.ml"; line = Single 2; chars = Some (2, 20) },
187 "Expected declaration")
188 ; ({ path = "test.ml"; line = Single 4; chars = Some (6, 7) },
189 "Actual declaration")
190 ]
191 ; severity = Error None
192 } |}]
193 ;;
194
195 let%expect_test "ml mli mismatch 2" =
196 test_error
197 {|
198 File "src/dune_rules/artifacts.ml", line 1:
199 Error: The implementation src/dune_rules/artifacts.ml
200 does not match the interface src/dune_rules/.dune_rules.objs/byte/dune_rules__Artifacts.cmi:
201 ... ... In module Bin.Local:
202 Values do not match:
203 val equal :
204 Import.Path.Build.t Import.String.Set.map ->
205 Import.Path.Build.t Import.String.Set.map -> bool
206 is not included in
207 val equal : t -> bool -> bool
208 The type
209 Import.Path.Build.t Import.String.Set.map ->
210 Import.Path.Build.t Import.String.Set.map -> bool
211 is not compatible with the type t -> bool -> bool
212 Type Import.Path.Build.t Import.String.Set.map
213 is not compatible with type bool
214 File "src/dune_rules/artifacts.mli", line 20, characters 4-33:
215 Expected declaration
216 File "src/dune_rules/artifacts.ml", line 50, characters 8-13:
217 Actual declaration
218 |};
219 [%expect
220 {|
221 >> error 0
222 { loc =
223 { path = "src/dune_rules/artifacts.ml"; line = Single 1; chars = None }
224 ; message =
225 "The implementation src/dune_rules/artifacts.ml\n\
226 does not match the interface src/dune_rules/.dune_rules.objs/byte/dune_rules__Artifacts.cmi:\n\
227 \ ... ... In module Bin.Local:\n\
228 Values do not match:\n\
229 \ val equal :\n\
230 \ Import.Path.Build.t Import.String.Set.map ->\n\
231 \ Import.Path.Build.t Import.String.Set.map -> bool\n\
232 is not included in\n\
233 \ val equal : t -> bool -> bool\n\
234 The type\n\
235 \ Import.Path.Build.t Import.String.Set.map ->\n\
236 \ Import.Path.Build.t Import.String.Set.map -> bool\n\
237 is not compatible with the type t -> bool -> bool\n\
238 Type Import.Path.Build.t Import.String.Set.map\n\
239 is not compatible with type bool"
240 ; related =
241 [ ({ path = "src/dune_rules/artifacts.mli"
242 ; line = Single 20
243 ; chars = Some (4, 33)
244 },
245 "Expected declaration")
246 ; ({ path = "src/dune_rules/artifacts.ml"
247 ; line = Single 50
248 ; chars = Some (8, 13)
249 },
250 "Actual declaration")
251 ]
252 ; severity = Error None
253 } |}]
254 ;;
255
256 let%expect_test "" =
257 test_error
258 {|
259 File "fooexe.ml", line 3, characters 0-7:
260 3 | Bar.run ();;
261 ^^^^^^^
262 Error (alert deprecated): module Bar
263 Will be removed past 2020-20-20. Use Mylib.Bar instead.
264 File "fooexe.ml", line 4, characters 0-7:
265 4 | Foo.run ();;
266 ^^^^^^^
267 Error (alert deprecated): module Foo
268 Will be removed past 2020-20-20. Use Mylib.Foo instead.
269 File "fooexe.ml", line 7, characters 11-22:
270 7 | module X : Intf_only.S = struct end
271 ^^^^^^^^^^^
272 Error (alert deprecated): module Intf_only
273 Will be removed past 2020-20-20. Use Mylib.Intf_only instead.
274 |};
275 [%expect
276 {|
277 >> error 0
278 { loc = { path = "fooexe.ml"; line = Single 3; chars = Some (0, 7) }
279 ; message =
280 "module Bar\n\
281 Will be removed past 2020-20-20. Use Mylib.Bar instead."
282 ; related = []
283 ; severity = Error (Some "deprecated")
284 }
285 >> error 1
286 { loc = { path = "fooexe.ml"; line = Single 4; chars = Some (0, 7) }
287 ; message =
288 "module Foo\n\
289 Will be removed past 2020-20-20. Use Mylib.Foo instead."
290 ; related = []
291 ; severity = Error (Some "deprecated")
292 }
293 >> error 2
294 { loc = { path = "fooexe.ml"; line = Single 7; chars = Some (11, 22) }
295 ; message =
296 "module Intf_only\n\
297 Will be removed past 2020-20-20. Use Mylib.Intf_only instead."
298 ; related = []
299 ; severity = Error (Some "deprecated")
300 } |}]
301 ;;
302
303 let%expect_test "undefined fields" =
304 test_error
305 {|
306 File "test/expect-tests/timer_tests.ml", lines 6-10, characters 2-3:
307 6 | ..{ Scheduler.Config.concurrency = 1
308 7 | ; display = { verbosity = Short; status_line = false }
309 8 | ; stats = None
310 10 | }
311 Error: Some record fields are undefined: signal_watcher
312 |};
313 [%expect
314 {|
315 >> error 0
316 { loc =
317 { path = "test/expect-tests/timer_tests.ml"
318 ; line = Range (6, 10)
319 ; chars = Some (2, 3)
320 }
321 ; message = "Some record fields are undefined: signal_watcher"
322 ; related = []
323 ; severity = Error None
324 } |}]
325 ;;
326
327 let%expect_test "undefined fields" =
328 test_error_raw
329 {|
330 Error: Some record fields are undefined: signal_watcher
331 |};
332 [%expect
333 {|
334 [ "Error: Some record fields are undefined: signal_watcher" ] |}]
335 ;;
336
337 let%expect_test "test error from merlin" =
338 test_error_raw
339 {|Signature mismatch:
340 Modules do not match:
341 sig val x : int end
342 is not included in
343 sig val x : unit end
344 Values do not match: val x : int is not included in val x : unit
345 The type int is not compatible with the type unit
346 File "test.ml", line 2, characters 2-14: Expected declaration
347 File "test.ml", line 4, characters 6-7: Actual declaration
348 |};
349 [%expect
350 {|
351 [ "Signature mismatch:\n\
352 Modules do not match:\n\
353 \ sig val x : int end\n\
354 is not included in\n\
355 \ sig val x : unit end\n\
356 Values do not match: val x : int is not included in val x : unit\n\
357 The type int is not compatible with the type unit"
358 ; { path = "test.ml"; line = Single 2; chars = Some (2, 14) }
359 ; "Expected declaration"
360 ; { path = "test.ml"; line = Single 4; chars = Some (6, 7) }
361 ; "Actual declaration"
362 ] |}]
363 ;;
364
365 let%expect_test "ml/mli error" =
366 test_error
367 {|
368 File "src/dune_engine/build_system.ml", line 1:
369 Error: The implementation src/dune_engine/build_system.ml
370 does not match the interface src/dune_engine/.dune_engine.objs/byte/dune_engine__Build_system.cmi:
371 The value `dune_stats' is required but not provided
372 File "src/dune_engine/build_system.mli", line 8, characters 0-40:
373 Expected declaration
374 |};
375 [%expect
376 {|
377 >> error 0
378 { loc =
379 { path = "src/dune_engine/build_system.ml"
380 ; line = Single 1
381 ; chars = None
382 }
383 ; message =
384 "The implementation src/dune_engine/build_system.ml\n\
385 does not match the interface src/dune_engine/.dune_engine.objs/byte/dune_engine__Build_system.cmi:\n\
386 \ The value `dune_stats' is required but not provided"
387 ; related =
388 [ ({ path = "src/dune_engine/build_system.mli"
389 ; line = Single 8
390 ; chars = Some (0, 40)
391 },
392 "Expected declaration")
393 ]
394 ; severity = Error None
395 } |}]
396 ;;
397
398 let%expect_test "ml/mli error" =
399 test_error
400 {|
401 File "bin/common.ml", line 1004, characters 8-43:
402 1004 | Dune_engine.Build_system.dune_stats := Some stats;
403 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
404 Error: Unbound value Dune_engine.Build_system.dune_stats
405 |};
406 [%expect
407 {|
408 >> error 0
409 { loc = { path = "bin/common.ml"; line = Single 1004; chars = Some (8, 43) }
410 ; message = "Unbound value Dune_engine.Build_system.dune_stats"
411 ; related = []
412 ; severity = Error None
413 } |}]
414 ;;
415
416 let%expect_test "alert" =
417 test_error
418 {|
419 File "foo.ml", line 8, characters 9-12:
420 8 | let () = A.f
421 ^^^
422 Alert deprecated: A.f
423 foo
424 |};
425 [%expect
426 {|
427 >> error 0
428 { loc = { path = "foo.ml"; line = Single 8; chars = Some (9, 12) }
429 ; message = "foo"
430 ; related = []
431 ; severity = Alert { name = "deprecated"; source = "A.f" }
432 } |}];
433 test_error
434 {|
435 File "foo.ml", line 8, characters 9-12:
436 8 | let () = A.f
437 ^^^
438 Alert foobar: A.f
439 blah
440 |};
441 [%expect
442 {|
443 >> error 0
444 { loc = { path = "foo.ml"; line = Single 8; chars = Some (9, 12) }
445 ; message = "blah"
446 ; related = []
447 ; severity = Alert { name = "foobar"; source = "A.f" }
448 } |}]
449 ;;
450
451 let%expect_test "multiple errors in one file" =
452 test_error
453 {|
454 File "foo.ml", line 8, characters 8-11:
455 8 | let f = A.f
456 ^^^
457 Alert deprecated: A.f
458 foo
459 File "foo.ml", line 9, characters 8-11:
460 9 | let g = A.f
461 ^^^
462 Alert deprecated: A.f
463 foo
464 File "foo.ml", line 10, characters 8-11:
465 10 | let h = A.f
466 ^^^
467 Alert deprecated: A.f
468 foo
469 |};
470 [%expect
471 {|
472 >> error 0
473 { loc = { path = "foo.ml"; line = Single 8; chars = Some (8, 11) }
474 ; message = "foo"
475 ; related = []
476 ; severity = Alert { name = "deprecated"; source = "A.f" }
477 }
478 >> error 1
479 { loc = { path = "foo.ml"; line = Single 9; chars = Some (8, 11) }
480 ; message = "foo"
481 ; related = []
482 ; severity = Alert { name = "deprecated"; source = "A.f" }
483 }
484 >> error 2
485 { loc = { path = "foo.ml"; line = Single 10; chars = Some (8, 11) }
486 ; message = "foo"
487 ; related = []
488 ; severity = Alert { name = "deprecated"; source = "A.f" }
489 } |}]
490 ;;
491
492 let%expect_test "fatal alert" =
493 test_error
494 {|
495 File "foo.ml", line 8, characters 8-11:
496 8 | let f = A.f
497 ^^^
498 Error (alert foobar): A.f
499 testing
500 |};
501 [%expect
502 {|
503 >> error 0
504 { loc = { path = "foo.ml"; line = Single 8; chars = Some (8, 11) }
505 ; message = "A.f\n\
506 testing"
507 ; related = []
508 ; severity = Error (Some "foobar")
509 } |}]
510 ;;
511
512 let%expect_test "nultiple errors from multiple files at once" =
513 test_error
514 {|
515 File "src/dune_engine/action.ml", lines 34-96, characters 4-64:
516 34 | ....function
517 35 | | Run (a, xs) -> List (atom "run" :: program a :: List.map xs ~f:string)
518 36 | | With_accepted_exit_codes (pred, t) ->
519 37 | List
520 38 | [ atom "with-accepted-exit-codes"
521 ...
522 93 | List
523 94 | (atom (sprintf "pipe-%s" (Outputs.to_string outputs))
524 95 | :: List.map l ~f:encode)
525 96 | | Extension ext -> List [ atom "ext"; Extension.encode ext ]
526 Error (warning 8 [partial-match]): this pattern-matching is not exhaustive.
527 Here is an example of a case that is not matched:
528 Case
529 File "src/dune_engine/action.ml", lines 291-315, characters 2-22:
530 291 | ..match t with
531 292 | | Chdir (_, t)
532 293 | | Setenv (_, _, t)
533 294 | | Redirect_out (_, _, _, t)
534 295 | | Redirect_in (_, _, t)
535 ...
536 312 | | Mkdir _
537 313 | | Diff _
538 314 | | Merge_files_into _
539 315 | | Extension _ -> acc
540 Error (warning 8 [partial-match]): this pattern-matching is not exhaustive.
541 Here is an example of a case that is not matched:
542 Case
543 File "src/dune_engine/action.ml", lines 339-363, characters 21-24:
544 339 | .....................function
545 340 | | Dynamic_run _ -> true
546 341 | | Chdir (_, t)
547 342 | | Setenv (_, _, t)
548 343 | | Redirect_out (_, _, _, t)
549 ...
550 360 | | Diff _
551 361 | | Mkdir _
552 362 | | Merge_files_into _
553 363 | | Extension _ -> false
554 Error (warning 8 [partial-match]): this pattern-matching is not exhaustive.
555 Here is an example of a case that is not matched:
556 Case
557 File "src/dune_engine/action.ml", lines 391-414, characters 4-70:
558 391 | ....match t with
559 392 | | Chdir (_, t) -> loop t
560 393 | | Setenv (_, _, t) -> loop t
561 394 | | Redirect_out (_, _, _, t) -> memoize || loop t
562 395 | | Redirect_in (_, _, t) -> loop t
563 ...
564 411 | | Dynamic_run _ -> true
565 412 | | System _ -> true
566 413 | | Bash _ -> true
567 414 | | Extension (module A) -> A.Spec.is_useful_to ~distribute ~memoize
568 Error (warning 8 [partial-match]): this pattern-matching is not exhaustive.
569 Here is an example of a case that is not matched:
570 Case
571 |};
572 [%expect
573 {|
574 >> error 0
575 { loc =
576 { path = "src/dune_engine/action.ml"
577 ; line = Range (34, 96)
578 ; chars = Some (4, 64)
579 }
580 ; message =
581 "this pattern-matching is not exhaustive.\n\
582 Here is an example of a case that is not matched:\n\
583 Case"
584 ; related = []
585 ; severity = Error (Some { code = 8; name = "partial-match" })
586 }
587 >> error 1
588 { loc =
589 { path = "src/dune_engine/action.ml"
590 ; line = Range (291, 315)
591 ; chars = Some (2, 22)
592 }
593 ; message =
594 "this pattern-matching is not exhaustive.\n\
595 Here is an example of a case that is not matched:\n\
596 Case"
597 ; related = []
598 ; severity = Error (Some { code = 8; name = "partial-match" })
599 }
600 >> error 2
601 { loc =
602 { path = "src/dune_engine/action.ml"
603 ; line = Range (339, 363)
604 ; chars = Some (21, 24)
605 }
606 ; message =
607 "this pattern-matching is not exhaustive.\n\
608 Here is an example of a case that is not matched:\n\
609 Case"
610 ; related = []
611 ; severity = Error (Some { code = 8; name = "partial-match" })
612 }
613 >> error 3
614 { loc =
615 { path = "src/dune_engine/action.ml"
616 ; line = Range (391, 414)
617 ; chars = Some (4, 70)
618 }
619 ; message =
620 "this pattern-matching is not exhaustive.\n\
621 Here is an example of a case that is not matched:\n\
622 Case"
623 ; related = []
624 ; severity = Error (Some { code = 8; name = "partial-match" })
625 } |}]
626 ;;
627
628 let%expect_test "two errors, second without 'error:'" =
629 test_error
630 {|
631 File "src/dune_threaded_console/dune_threaded_console.ml", line 59, characters 18-23:
632 59 | Queue.clear state.messages;
633 ^^^^^
634 Error: Syntax error: 'end' expected
635 File "src/dune_threaded_console/dune_threaded_console.ml", line 9, characters 17-23:
636 9 | let module T = struct
637 ^^^^^^
638 This 'struct' might be unmatched
639
640 |};
641 [%expect
642 {|
643 >> error 0
644 { loc =
645 { path = "src/dune_threaded_console/dune_threaded_console.ml"
646 ; line = Single 59
647 ; chars = Some (18, 23)
648 }
649 ; message = "Syntax error: 'end' expected"
650 ; related = []
651 ; severity = Error None
652 } |}]
653 ;;