1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
open Printf
open Testo_util
open Fpath_.Operators
open Promise.Operators
module T = Types
module P = Promise
type status_output_style =
| Long_all
| Compact_all
| Long_important
| Compact_important
type status_stats = {
total_tests : int;
selected_tests : int;
flaky_tests : int;
skipped_tests : int ref;
pass : int ref;
fail : int ref;
xfail : int ref;
xpass : int ref;
miss : int ref;
needs_approval : int ref;
}
type success = OK | OK_but_new | Not_OK of T.fail_reason option
type alcotest_test_case = string * [ `Quick | `Slow ] * (unit -> unit Promise.t)
type alcotest_test = string * alcotest_test_case list
type caught_exn = {
exn_txt : string;
exn : exn;
trace : Printexc.raw_backtrace;
}
type test_result = (unit, caught_exn) result Promise.t
let bullet = Style.color Faint "• "
let if_plural num s = if num >= 2 then s else ""
let if_singular num s = if num <= 1 then s else ""
let get_checksum (tests : T.test list) =
tests
|> Helpers.list_map (fun (x : T.test) -> x.id)
|> String.concat " " |> Digest.string |> Digest.to_hex
let check_checksum_or_abort ~expected_checksum tests =
match expected_checksum with
| None -> ()
| Some expected ->
let checksum = get_checksum tests in
if checksum <> expected then
Multiprocess.Server.fatal_error
"Checksum mismatch: the test suite in a worker process is different \
than the list of tests in the master process. You need to make sure \
that the name and the order of the tests in the test suite is \
deterministic and independent of any internal command-line option \
(e.g. '--worker')."
let check_id_uniqueness (tests : T.test list) =
let id_tbl = Hashtbl.create 1000 in
tests
|> List.iter (fun (test : T.test) ->
let id = test.id in
let name = test.internal_full_name in
match Hashtbl.find_opt id_tbl id with
| None -> Hashtbl.add id_tbl id test.internal_full_name
| Some name0 ->
if name = name0 then
Error.user_error
(sprintf "Two tests have the same name: %s" name)
else
Error.user_error
(sprintf
"Hash collision for two tests with different names:\n\
\ %S\n\
\ %S\n\
These names result in the same hash ID: %s\n\
If this is accidental, please report the problem to the \
authors of\n\
testo."
name0 name id))
let check_snapshot_uniqueness (tests : T.test list) =
let path_tbl = Hashtbl.create 1000 in
tests
|> List.iter (fun (test : T.test) ->
test |> Store.all_capture_paths_of_test
|> List.iter (fun (paths : Store.capture_paths) ->
match paths.path_to_expected_output with
| None -> ()
| Some path -> (
let test_name = test.internal_full_name in
match Hashtbl.find_opt path_tbl path with
| None -> Hashtbl.add path_tbl path test_name
| Some test_name0 ->
if test_name = test_name0 then
Error.user_error
(sprintf
"A test uses the same snapshot path twice:\n\
- test name: %S\n\
- conflicting snapshot path: %s\n\
Fix it in the test definition.\n"
test_name !!path)
else
Error.user_error
(sprintf
"Two different tests use the same snapshot path:\n\
- first test: %S\n\
- second test: %S\n\
- conflicting snapshot path: %s\n"
test_name0 test_name !!path))))
let check_test_definitions tests =
check_id_uniqueness tests;
check_snapshot_uniqueness tests
let string_of_status_summary (sum : T.status_summary) =
let approval_suffix = if sum.has_expected_output then "" else "*" in
match sum.passing_status with
| PASS -> "PASS" ^ approval_suffix
| FAIL _ -> "FAIL" ^ approval_suffix
| XFAIL _ -> "XFAIL" ^ approval_suffix
| XPASS -> "XPASS" ^ approval_suffix
| MISS _ -> "MISS"
let success_of_status_summary (sum : T.status_summary) =
match sum.passing_status with
| PASS -> if sum.has_expected_output then OK else OK_but_new
| XFAIL _ ->
OK
| FAIL fail_reason -> Not_OK (Some fail_reason)
| XPASS -> Not_OK None
| MISS _ -> OK_but_new
let color_of_status_summary (sum : T.status_summary) : Style.color =
match success_of_status_summary sum with
| OK -> Green
| OK_but_new -> Yellow
| Not_OK _ -> Red
let brackets s = sprintf "[%s]" s
let format_status_summary (sum : T.status_summary) =
let style = color_of_status_summary sum in
let displayed_string = sum |> string_of_status_summary |> brackets in
Style.left_col displayed_string |> Style.color style
let count_flaky_tests tests_with_status =
tests_with_status
|> List.fold_left
(fun n ((test : T.test), _, _) ->
match test.flaky with
| None -> n
| Some _reason -> n + 1)
0
let stats_of_tests tests tests_with_status =
let stats =
{
total_tests = List.length tests;
selected_tests = List.length tests_with_status;
flaky_tests = count_flaky_tests tests_with_status;
skipped_tests = ref 0;
pass = ref 0;
fail = ref 0;
xfail = ref 0;
xpass = ref 0;
miss = ref 0;
needs_approval = ref 0;
}
in
tests_with_status
|> List.iter (fun ((test : T.test), _status, (sum : T.status_summary)) ->
match test.skipped with
| Some _reason -> incr stats.skipped_tests
| None ->
(match sum.passing_status with
| MISS _
| XFAIL _ ->
()
| _ ->
if not sum.has_expected_output then incr stats.needs_approval);
incr
(match sum.passing_status with
| PASS -> stats.pass
| FAIL _ -> stats.fail
| XFAIL _ -> stats.xfail
| XPASS -> stats.xpass
| MISS _ -> stats.miss));
stats
let format_tags (test : T.test) =
match test.tags with
| [] -> ""
| tags ->
let tags =
List.sort Tag.compare tags
|> Helpers.list_map (fun tag -> Style.color Bold (Tag.to_string tag))
in
sprintf " (%s)" (String.concat " " tags)
let format_title (test : T.test) : string =
sprintf "%s%s %s" test.id (format_tags test)
(test.category @ [ test.name ]
|> Helpers.list_map (Style.color Cyan)
|> String.concat " > ")
let group_by_key key_value_list =
let tbl = Hashtbl.create 100 in
key_value_list
|> List.iteri (fun pos (k, v) ->
let tbl_v =
match Hashtbl.find_opt tbl k with
| None -> (pos, [ v ])
| Some (pos, vl) -> (pos, v :: vl)
in
Hashtbl.replace tbl k tbl_v);
let clusters =
Hashtbl.fold (fun k (pos, vl) acc -> (pos, (k, List.rev vl)) :: acc) tbl []
in
clusters
|> List.sort (fun (pos1, _) (pos2, _) -> compare pos1 pos2)
|> Helpers.list_map snd
let chdir_error (test : T.test) =
if test.tolerate_chdir then None
else
Some
(fun old new_ ->
sprintf "Current working directory (cwd) wasn't restored: %s -> %s" old
new_)
let protect_globals (test : T.test) (func : unit -> 'promise) : unit -> 'promise
=
let protect_global ?error_if_changed get set func () =
let original_value = get () in
P.protect
~finally:(fun () ->
let current_value = get () in
set original_value;
(match error_if_changed with
| Some err_msg_func when current_value <> original_value ->
Error.fail_test (err_msg_func original_value current_value)
| _ -> ());
P.return ())
func
in
func
|> protect_global Sys.getcwd Sys.chdir ?error_if_changed:(chdir_error test)
|> protect_global Printexc.backtrace_status Printexc.record_backtrace
let to_alcotest_gen ~(alcotest_skip : unit -> _)
~(wrap_test_function :
T.test -> (unit -> unit Promise.t) -> unit -> test_result)
(tests : T.test list) : _ list =
tests
|> Helpers.list_map (fun (test : T.test) ->
let suite_name =
match test.category with
| [] -> test.name
| path -> String.concat " > " path
in
let xfail_note =
match test.expected_outcome with
| Should_succeed -> ""
| Should_fail _reason -> " [xfail]"
in
let suite_name =
sprintf "%s%s%s %s" test.id xfail_note (format_tags test) suite_name
in
let func =
match test.skipped with
| Some _reason ->
fun () ->
alcotest_skip () |> ignore;
Error.user_error
"The function 'alcotest_skip' passed to 'Testo.to_alcotest' \
didn't raise\n\
an exception as expected. 'Testo.to_alcotest' should be \
called with\n\
'~alcotest_skip:Alcotest.skip'."
| None ->
let func = wrap_test_function test test.func in
let func () =
func () >>= function
| Ok () -> P.return ()
| Error e -> Printexc.raise_with_backtrace e.exn e.trace
in
func
in
(suite_name, (test.name, `Quick, func)))
|> group_by_key
let catch_user_exception ~with_storage ~flip_xfail_outcome test func () :
(unit, caught_exn) result Promise.t =
let func =
if with_storage then Store.with_result_capture test func else func
in
let flip_outcome =
match test.expected_outcome with
| Should_succeed -> false
| Should_fail _reason -> flip_xfail_outcome
in
P.catch
(fun () ->
func () >>= fun () -> P.return (Ok ()))
(fun exn trace ->
let opt_trace =
match exn with
| Error.Test_failure _ -> ""
| _ -> Printexc.raw_backtrace_to_string trace
in
let exn_txt = sprintf "%s\n%s" (Printexc.to_string exn) opt_trace in
P.return (Error { exn_txt; exn; trace }))
>>= function
| Ok () ->
if with_storage then Store.store_exception test None;
let res =
if flip_outcome then
Error
{
exn_txt = "Test failed to raise an exception";
exn = Error.Test_failure "failed to raise an exception";
trace = Printexc.get_raw_backtrace ();
}
else Ok ()
in
P.return res
| Error e ->
if with_storage then Store.store_exception test (Some e.exn_txt);
let res =
if flip_outcome then (
eprintf "XFAIL: As expected, an exception was raised: %s\n" e.exn_txt;
Ok ())
else Error e
in
P.return res
let current_test : T.test option ref = ref None
let get_current_test () = !current_test
let with_current_test_ref test func =
fun () ->
P.protect
(fun () ->
current_test := Some test;
func ())
~finally:(fun () ->
current_test := None;
P.return ())
let wrap_test_function_internal ~with_storage ~flip_xfail_outcome test
(func : unit -> unit Promise.t) :
unit -> (unit, caught_exn) result Promise.t =
fun () ->
Store.init_test_workspace test;
Store.remove_stashed_output_files test;
(func
|> catch_user_exception ~with_storage ~flip_xfail_outcome test
|> protect_globals test |> with_current_test_ref test)
()
let wrap_test_function test (func : unit -> unit Promise.t) :
unit -> unit Promise.t =
fun () ->
wrap_test_function_internal ~with_storage:true ~flip_xfail_outcome:false test
func ()
>>= fun _res -> P.return ()
let to_alcotest ~alcotest_skip tests =
to_alcotest_gen ~alcotest_skip
~wrap_test_function:
(wrap_test_function_internal ~with_storage:false ~flip_xfail_outcome:true)
tests
let filter ~filter_by_substring ~filter_by_tag tests =
let filter_sub =
match filter_by_substring with
| None -> None
| Some subs ->
let contains_sub str =
List.exists (fun sub -> Helpers.contains_substring ~sub str) subs
in
Some
(fun (test : T.test) ->
contains_sub test.internal_full_name || contains_sub test.id)
in
let filter_tag =
match filter_by_tag with
| None -> None
| Some tag_query ->
Some (fun (test : T.test) -> Tag_query.match_ test.tags tag_query)
in
let filters = [ filter_sub; filter_tag ] |> List.filter_map (fun x -> x) in
match filters with
| [] -> tests
| _ ->
tests
|> List.filter (fun test ->
List.for_all (fun filter -> filter test) filters)
let print_error (msg : Error.msg) =
match msg with
| Error msg -> eprintf "%s%s\n" (Style.color Red "Error: ") msg
| Warning msg -> eprintf "%s%s\n" (Style.color Yellow "Warning: ") msg
let print_errors (xs : (Store.changed, Error.msg) Result.t list) : int =
let changed = ref 0 in
let error_messages = ref [] in
xs
|> List.iter (function
| Ok Store.Changed -> incr changed
| Ok Store.Unchanged -> ()
| Error msg -> error_messages := msg :: !error_messages);
let changed = !changed in
let error_messages = List.rev !error_messages in
printf "Expected output changed for %i test%s.\n%!" changed
(if_plural changed "s");
match error_messages with
| [] -> Error.Exit_code.success
| xs ->
List.iter print_error xs;
flush stderr;
Error.Exit_code.test_failure
let is_important_status ((test : T.test), _status, (sum : T.status_summary)) =
test.skipped = None
&&
match success_of_status_summary sum with
| OK -> false
| OK_but_new
| Not_OK _ ->
true
let show_diff (output_kind : string) path_to_expected_output path_to_output =
if
Sys.file_exists !!path_to_output
&& Sys.file_exists !!path_to_expected_output
then
match Diff.files path_to_expected_output path_to_output with
| None -> ()
| Some diffs ->
printf "%sCaptured %s differs from expectation:\n%s" bullet output_kind
diffs
let show_output_details (test : T.test) (sum : T.status_summary)
(capture_paths : Store.capture_paths list) =
let success = success_of_status_summary sum in
capture_paths
|> List.iter
(fun
({ kind; short_name; path_to_expected_output; path_to_output } :
Store.capture_paths)
->
flush stdout;
flush stderr;
(match path_to_expected_output with
| None -> ()
| Some path_to_expected_output ->
(match success with
| OK
| OK_but_new ->
()
| Not_OK _ ->
show_diff short_name path_to_expected_output path_to_output);
if success <> OK_but_new then
printf "%sPath to expected %s: %s\n" bullet short_name
!!path_to_expected_output);
printf "%sPath to captured %s: %s%s\n" bullet short_name
!!path_to_output
(match (Store.get_orig_output_suffix test, kind) with
| Some suffix, Std -> sprintf " [%s]" suffix
| None, _
| Some _, (Log | File) ->
""))
let print_error text = printf "%s%s\n" bullet (Style.color Red text)
let print_hint text = printf "%s%s\n" bullet (Style.color Faint text)
let format_one_line_status ((test : T.test), (_status : T.status), sum) =
sprintf "%s%s" (format_status_summary sum) (format_title test)
let print_one_line_status test_with_status =
printf "%s\n" (format_one_line_status test_with_status)
let with_highlight_test ~highlight_test ~title func =
if highlight_test then printf "%s" (Style.frame title)
else printf "%s\n" title;
func ();
if highlight_test then print_string (Style.horizontal_line ())
let ends_with_newline str =
str <> "" && str.[String.length str - 1] = '\n'
let with_cwd paths =
if List.exists Fpath.is_rel paths then sprintf " [cwd: %s]" (Sys.getcwd ())
else ""
let print_status ~highlight_test
~always_show_unchecked_output:
(always_show_unchecked_output, max_inline_log_bytes)
(((test : T.test), (status : T.status), sum) as test_with_status) =
let title = format_one_line_status test_with_status in
with_highlight_test ~highlight_test ~title (fun () ->
match test.skipped with
| Some _reason -> printf "%sAlways skipped\n" bullet
| None -> (
(match test.solo with
| None -> ()
| Some reason ->
printf
"%sThis is a solo test, set to not run concurrently with other \
tests. Reason: %s\n"
bullet reason);
(match test.flaky with
| None -> ()
| Some reason ->
printf "%sThis test was marked as flaky by the programmer: %s\n"
bullet reason);
(match test.tracking_url with
| None -> ()
| Some url -> printf "%sTracking URL: %s\n" bullet url);
(match status.expectation.expected_outcome with
| Should_succeed -> ()
| Should_fail reason ->
printf "%sExpected to fail: %s\n" bullet reason);
(match test.checked_output with
| Ignore_output -> ()
| _ ->
let text =
match test.checked_output with
| Ignore_output -> Error.assert_false ~__LOC__ ()
| Stdout _ -> "stdout"
| Stderr _ -> "stderr"
| Stdxxx _ -> "merged stdout and stderr"
| Split_stdout_stderr _ -> "separate stdout and stderr"
in
printf "%sChecked output: %s\n" bullet text);
(match test.checked_output_files with
| [] -> ()
| xs ->
let names =
Helpers.list_map (fun (x : T.checked_output_file) -> x.name) xs
in
printf "%sChecked output file%s: %s\n" bullet
(if_plural (List.length names) "s")
(String.concat ", " names));
(match status.expectation.expected_output with
| Error (Missing_files [ path ]) ->
print_error
(sprintf "Missing file containing the expected output: %s%s"
!!path (with_cwd [ path ]))
| Error (Missing_files paths) ->
print_error
(sprintf "Missing files containing the expected output: %s%s"
(String.concat ", " (Fpath_.to_string_list paths))
(with_cwd paths))
| Ok _expected_output -> (
match status.result with
| Error (Missing_files [ path ]) ->
print_error
(sprintf "Missing file containing the test output: %s%s"
!!path (with_cwd [ path ]))
| Error (Missing_files paths) ->
print_error
(sprintf "Missing files containing the test output: %s%s"
(String.concat ", " (Fpath_.to_string_list paths))
(with_cwd paths))
| Ok result -> (
match result.missing_output_files with
| [] -> ()
| missing_files ->
print_error
(sprintf "Missing captured output file%s: %s%s"
(if List.length missing_files > 1 then "s" else "")
(String.concat ", "
(Fpath_.to_string_list missing_files))
(with_cwd missing_files));
print_hint
"If you ran the test already, you may have forgotten \
to call 'Testo.stash_output_file' in the test \
function.")));
status.expectation.expected_output_files
|> List.iter (function
| Error missing_file ->
print_error
(sprintf
"Missing file containing the expected output: %s%s"
!!missing_file
(with_cwd [ missing_file ]))
| Ok _ -> ());
let capture_paths = Store.all_capture_paths_of_test test in
show_output_details test sum capture_paths;
let success = success_of_status_summary sum in
(match (test.max_duration, success) with
| None, (OK | OK_but_new) -> ()
| Some max_duration, (OK | OK_but_new) ->
printf "%sTime limit: %g seconds%s\n" bullet max_duration
(match test.solo with
| None -> ""
| Some _reason -> " (unenforceable due to solo setting)")
| _, Not_OK (Some Timeout) ->
let current_max_duration =
match test.max_duration with
| None ->
"none"
| Some max_duration -> sprintf "%g seconds" max_duration
in
printf "%s%s. Current time limit: %s\n" bullet
(Style.color Red "Timed out")
current_max_duration
| ( _,
Not_OK
( None
| Some
(Raised_exception | Missing_output_file | Incorrect_output)
) ) ->
());
let show_unchecked_output =
match test.inline_logs with
| On -> true
| Off -> false
| Auto -> (
always_show_unchecked_output
||
match success with
| OK -> false
| OK_but_new -> true
| Not_OK _ -> true)
in
let show_ok_exceptions =
match test.inline_logs with
| On -> true
| Off -> false
| Auto -> always_show_unchecked_output
in
(if show_unchecked_output then
match Store.get_unchecked_output test with
| None -> (
match success with
| OK -> ()
| OK_but_new -> ()
| Not_OK (Some Raised_exception) ->
printf
"%sFailed due to an exception. See captured output.\n"
bullet
| Not_OK (Some Missing_output_file) ->
printf
"%sFailed due to one or more missing output files.\n"
bullet
| Not_OK (Some Incorrect_output) ->
printf "%sFailed due to wrong output.\n" bullet
| Not_OK (Some Timeout) ->
printf "%sFailed due to timeout (%gs).\n" bullet
(match test.max_duration with
| None -> infinity
| Some dur -> dur)
| Not_OK None ->
printf
"%sSucceded when it should have failed. See captured \
output.\n"
bullet)
| Some (log_description, data) -> (
match data with
| "" -> printf "%sLog (%s) is empty.\n" bullet log_description
| _ ->
printf "%sLog (%s):\n%s" bullet log_description
(Style.quote_multiline_text
?max_bytes:max_inline_log_bytes data);
if not (ends_with_newline data) then print_char '\n'));
if show_unchecked_output then
match success with
| Not_OK (Some Raised_exception) -> (
match Store.get_exception test with
| Some msg ->
printf "%sException raised by the test:\n%s" bullet
(Style.quote_multiline_text
~decorate_data_fragment:(Style.color Red) msg)
| None ->
())
| OK
| OK_but_new
when show_ok_exceptions -> (
match Store.get_exception test with
| Some msg ->
printf "%sException raised by the test:\n%s" bullet
(Style.quote_multiline_text
~decorate_data_fragment:(Style.color Green) msg)
| None -> ())
| OK
| OK_but_new
| Not_OK
(Some (Missing_output_file | Incorrect_output | Timeout) | None)
->
()));
flush stdout
let print_statuses ~highlight_test ~always_show_unchecked_output
tests_with_status =
tests_with_status
|> List.iter (print_status ~highlight_test ~always_show_unchecked_output)
let is_overall_success ~strict statuses =
statuses
|> List.for_all (fun ((test : T.test), _status, sum) ->
test.skipped <> None
|| ((not strict) && test.flaky <> None)
||
match sum |> success_of_status_summary with
| OK -> true
| OK_but_new -> false
| Not_OK _ -> false)
let print_introduction intro =
print_string intro;
if not (intro = "" || intro.[String.length intro - 1] = '\n') then
print_char '\n';
flush stdout
let print_compact_status ?(important = false) ~strict tests_with_status =
let tests_with_status =
if important then List.filter is_important_status tests_with_status
else tests_with_status
in
List.iter print_one_line_status tests_with_status;
if is_overall_success ~strict tests_with_status then Error.Exit_code.success
else Error.Exit_code.test_failure
let print_short_status ~always_show_unchecked_output tests_with_status =
let tests_with_status = List.filter is_important_status tests_with_status in
match tests_with_status with
| [] -> ()
| _ ->
print_statuses ~highlight_test:true ~always_show_unchecked_output
tests_with_status
let print_long_status ~always_show_unchecked_output tests_with_status =
match tests_with_status with
| [] -> ()
| _ ->
print_statuses ~highlight_test:false ~always_show_unchecked_output
tests_with_status
let report_dead_snapshots ~autoclean all_tests =
let dead_snapshots = Store.find_dead_snapshots all_tests in
let n = List.length dead_snapshots in
if n > 0 then (
if autoclean then
printf
"%i folder%s no longer belong%s to the test suite and %s being removed:\n"
n (if_plural n "s") (if_singular n "s")
(if n < 2 then "is" else "are")
else
printf
"%i folder%s no longer belong%s to the test suite and can be removed \
manually or with '--autoclean':\n"
n (if_plural n "s") (if_singular n "s");
List.iter
(fun (x : Store.dead_snapshot) ->
let msg =
match x.test_name with
| None -> "??"
| Some name -> name
in
printf " %s %s\n" !!(x.dir_or_junk_file) msg;
if autoclean then Store.remove_dead_snapshot x)
dead_snapshots)
let print_status_summary ~autoclean ~strict tests tests_with_status : int =
report_dead_snapshots ~autoclean tests;
let stats = stats_of_tests tests tests_with_status in
let overall_success = is_overall_success ~strict tests_with_status in
printf "%i/%i selected test%s:\n" stats.selected_tests stats.total_tests
(if_plural stats.total_tests "s");
if !(stats.skipped_tests) > 0 then
printf " %i skipped\n" !(stats.skipped_tests);
printf " %i successful (%i pass, %i xfail)\n"
(!(stats.pass) + !(stats.xfail))
!(stats.pass) !(stats.xfail);
printf " %i unsuccessful (%i fail, %i xpass)\n"
(!(stats.fail) + !(stats.xpass))
!(stats.fail) !(stats.xpass);
if !(stats.miss) > 0 then
printf "%i new test%s\n" !(stats.miss) (if_plural !(stats.miss) "s");
if !(stats.needs_approval) > 0 then
printf "%i test%s whose output needs first-time approval\n"
!(stats.needs_approval)
(if_plural !(stats.needs_approval) "s");
printf "overall status: %s\n"
(if overall_success then Style.color Green "success"
else Style.color Red "failure");
if stats.flaky_tests > 0 && not strict then
printf "%s\n"
(Style.color Yellow
(sprintf
"The status of %i flaky test%s was ignored! Use '--strict' to \
override."
stats.flaky_tests
(if_plural stats.flaky_tests "s")));
if overall_success then Error.Exit_code.success
else Error.Exit_code.test_failure
let print_all_statuses ~always_show_unchecked_output ~autoclean ~intro tests
tests_with_status =
print_introduction intro;
print_newline ();
print_long_status ~always_show_unchecked_output tests_with_status;
print_newline ();
print_short_status ~always_show_unchecked_output tests_with_status;
print_status_summary ~autoclean tests tests_with_status
let print_important_statuses ~always_show_unchecked_output ~autoclean ~strict
tests tests_with_status : int =
print_short_status ~always_show_unchecked_output tests_with_status;
print_status_summary ~autoclean ~strict tests tests_with_status
let get_test_with_status test =
let status = Store.get_status test in
(test, status, Store.status_summary_of_status status)
let get_tests_with_status tests = tests |> Helpers.list_map get_test_with_status
let cmd_status ~always_show_unchecked_output ~autoclean ~filter_by_substring
~filter_by_tag ~intro ~output_style ~strict tests =
check_test_definitions tests;
let selected_tests = filter ~filter_by_substring ~filter_by_tag tests in
let tests_with_status = get_tests_with_status selected_tests in
let exit_code =
match output_style with
| Long_all ->
print_all_statuses ~always_show_unchecked_output ~autoclean ~intro
~strict tests tests_with_status
| Long_important ->
print_important_statuses ~always_show_unchecked_output ~autoclean
~strict tests tests_with_status
| Compact_all -> print_compact_status ~strict tests_with_status
| Compact_important ->
print_compact_status ~important:true ~strict tests_with_status
in
(exit_code, tests_with_status)
let report_start_test (timers : Timers.t option)
(worker : Multiprocess.Client.worker option) (test : T.test) =
let run_label =
match test.solo with
| None -> "[RUN]"
| Some reason -> sprintf "[RUN SOLO: %s]" reason
in
printf "%s%s\n%!"
(Style.left_col (Style.color Yellow run_label))
(format_title test);
match (timers, worker) with
| None, None -> ()
| Some timers, Some worker -> Timers.add_test timers test worker
| _ -> Error.assert_false ~__LOC__ ()
let report_end_sequential_test ~always_show_unchecked_output test =
get_test_with_status test
|> print_status ~highlight_test:false ~always_show_unchecked_output
let report_skip_test test reason =
printf "%s%s\n%!"
(Style.left_col (Style.color Yellow (sprintf "[SKIP: %s]" reason)))
(format_title test)
let report_timeout test max_duration =
printf "%s%s\n%!"
(Style.left_col (Style.color Red (sprintf "[TIMEOUT: %gs]" max_duration)))
(format_title test)
let get_timed_out_workers timers =
let timed_out = Timers.remove_timeouts timers in
List.iter
(fun (test, max_duration, _worker) -> report_timeout test max_duration)
timed_out;
Helpers.list_map
(fun (test, _max_duration, worker) ->
let on_worker_termination () = Store.mark_test_as_timed_out test in
(worker, on_worker_termination))
timed_out
let run_tests_sequentially ~always_show_unchecked_output (tests : T.test list) :
'unit_promise =
List.fold_left
(fun previous (test : T.test) ->
P.catch
(fun () ->
let test_func : unit -> unit Promise.t =
wrap_test_function test test.func
in
previous >>= fun () ->
match test.skipped with
| Some reason ->
report_skip_test test reason;
P.return ()
| None ->
report_start_test None None test;
test_func () >>= fun () ->
report_end_sequential_test ~always_show_unchecked_output test;
P.return ())
(fun exn trace ->
eprintf
"Internal error encountered in the master process while processing \
test %s:\n\
%s\n\
%s\n\
%!"
test.name (Printexc.to_string exn)
(Printexc.raw_backtrace_to_string trace);
exit Error.Exit_code.internal_error))
(P.return ()) tests
let run_tests_requested_by_master (tests : T.test list) : unit Promise.t =
let get_test =
let tbl = Hashtbl.create (2 * List.length tests) in
List.iter (fun (test : T.test) -> Hashtbl.add tbl test.id test) tests;
fun test_id ->
try Hashtbl.find tbl test_id with
| Not_found ->
failwith (sprintf "Invalid test ID received by worker: %S" test_id)
in
let rec loop previous =
previous >>= fun () ->
match Multiprocess.Server.read () with
| None -> exit Error.Exit_code.success
| Some (Start_test test_id) ->
let test = get_test test_id in
let test_func : unit -> 'unit_promise =
wrap_test_function test test.func
in
let job =
match test.skipped with
| Some _reason ->
Multiprocess.Server.write (End_test test_id);
P.return ()
| None ->
P.catch test_func (fun exn trace ->
let msg =
sprintf "Uncaught exception: %s %s" (Printexc.to_string exn)
(Printexc.raw_backtrace_to_string trace)
in
Multiprocess.Server.fatal_error msg)
>>= fun () ->
Multiprocess.Server.write (End_test test_id);
P.return ()
in
loop job
in
loop (P.return ())
let select_tests ~filter_by_substring ~filter_by_tag ~lazy_ ~slice tests =
let tests =
match lazy_ with
| false -> tests
| true ->
let tests_with_status = get_tests_with_status tests in
List.filter is_important_status tests_with_status
|> Helpers.list_map (fun (test, _, _) -> test)
in
filter ~filter_by_substring ~filter_by_tag tests |> Slice.apply_slices slice
let before_run ~filter_by_substring ~filter_by_tag ~intro ~lazy_ ~slice tests =
Store.init_workspace ();
check_test_definitions tests;
let selected_tests =
select_tests ~filter_by_substring ~filter_by_tag ~lazy_ ~slice tests
in
print_introduction intro;
selected_tests
let after_run ~always_show_unchecked_output ~autoclean ~strict tests
selected_tests =
let tests_with_status = get_tests_with_status selected_tests in
let exit_code =
print_short_status ~always_show_unchecked_output tests_with_status;
print_compact_status ~important:true ~strict tests_with_status |> ignore;
print_status_summary ~autoclean ~strict tests tests_with_status
in
(exit_code, tests_with_status)
let ignore_broken_pipe () =
if not Sys.win32 then
Sys.set_signal Sys.sigpipe
(Signal_handle (fun _signal -> exit Error.Exit_code.success))
let cmd_run ~always_show_unchecked_output ~argv ~autoclean ~filter_by_substring
~filter_by_tag ~intro ~is_worker ~jobs ~lazy_ ~orig_cwd ~slice ~strict
~test_list_checksum:expected_checksum tests cont =
if is_worker then (
ignore_broken_pipe ();
check_checksum_or_abort ~expected_checksum tests;
let selected_tests =
select_tests ~filter_by_substring ~filter_by_tag ~lazy_ ~slice tests
in
run_tests_requested_by_master selected_tests >>= fun () ->
exit Error.Exit_code.success)
else
let num_workers =
match jobs with
| Some n -> max 0 n
| None -> (
match CPU.get_count () with
| None -> 0
| Some n -> n)
in
let selected_tests =
before_run ~filter_by_substring ~filter_by_tag ~intro ~lazy_ ~slice tests
in
let all_sequential = num_workers = 0 in
let sequential_tests, parallel_tests =
selected_tests
|> List.partition (fun (test : T.test) ->
all_sequential || test.solo <> None)
in
let timers = Timers.create () in
let on_end_test (test : T.test) =
Timers.remove_test timers test;
match test.skipped with
| Some reason -> report_skip_test test reason
| None ->
get_test_with_status test
|> print_status ~highlight_test:false ~always_show_unchecked_output
in
run_tests_sequentially ~always_show_unchecked_output sequential_tests
>>= fun () ->
(if num_workers > 0 then
match
Multiprocess.Client.run_tests_in_workers ~argv
~get_test_id:(fun (x : T.test) -> x.id)
~get_timed_out_workers:(fun () -> get_timed_out_workers timers)
~num_workers
~on_start_test:(report_start_test (Some timers))
~on_end_test ~orig_cwd ~test_list_checksum:(get_checksum tests)
parallel_tests
with
| Ok () -> ()
| Error msg ->
eprintf "Internal error: %s\n%!" msg;
exit Error.Exit_code.internal_error);
P.return () >>= fun () ->
let exit_code, tests_with_status =
after_run ~always_show_unchecked_output ~autoclean ~strict tests
selected_tests
in
cont exit_code tests_with_status |> ignore;
exit exit_code
let cmd_approve ~filter_by_substring ~filter_by_tag tests =
Store.init_workspace ();
check_test_definitions tests;
tests
|> filter ~filter_by_substring ~filter_by_tag
|> Helpers.list_map Store.approve_new_output
|> print_errors
let introduction_text =
sprintf
{|Legend:
%s[PASS]: a successful test that was expected to succeed (good);
%s[FAIL]: a failing test that was expected to succeed (needs fixing);
%s[XFAIL]: a failing test that was expected to fail (tolerated failure);
%s[XPASS]: a successful test that was expected to fail (progress?).
%s[MISS]: a test that never ran;
%s[SKIP]: a test that is always skipped but kept around for some reason;
%s[xxxx*]: a new test for which there's no expected output yet.
In this case, you should review the test output and run the 'approve'
subcommand once you're satisfied with the output.
Try '--help' for options.
|}
bullet bullet bullet bullet bullet bullet bullet