Source file oUnitLoggerStd.ml
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
open OUnitLogger
open OUnitTest
open OUnitResultSummary
let ocaml_position pos =
Printf.sprintf
"File \"%s\", line %d, characters 1-1:"
pos.filename pos.line
let multiline f str =
if String.length str > 0 then
let buf = Buffer.create 80 in
let flush () = f (Buffer.contents buf); Buffer.clear buf in
String.iter
(function '\n' -> flush () | c -> Buffer.add_char buf c)
str;
flush ()
let count results f =
List.fold_left
(fun count (_, test_result, _) ->
if f test_result then count + 1 else count)
0 results
let results_style_1_X =
OUnitConf.make_bool
"results_style_1_X"
false
"Use OUnit 1.X results printer (will be deprecated in 2.1.0+)."
let format_display_event conf log_event =
match log_event.event with
| GlobalEvent e ->
begin
match e with
| GConf (_, _) | GLog _ | GStart | GEnd -> ""
| GResults (running_time, results, test_case_count) ->
let separator1 = String.make (Format.get_margin ()) '=' in
let separator2 = String.make (Format.get_margin ()) '-' in
let buf = Buffer.create 1024 in
let bprintf fmt = Printf.bprintf buf fmt in
let print_results =
List.iter
(fun (path, test_result, pos_opt) ->
bprintf "%s\n" separator1;
if results_style_1_X conf then begin
bprintf "%s: %s\n\n"
(result_flavour test_result)
(string_of_path path);
end else begin
bprintf "Error: %s.\n\n" (string_of_path path);
begin
match pos_opt with
| Some pos ->
bprintf "%s\nError: %s (in the log).\n\n"
(ocaml_position pos)
(string_of_path path)
| None ->
()
end;
begin
match test_result with
| RError (_, Some backtrace) ->
bprintf "%s\n" backtrace
| RFailure (_, Some pos, _) ->
bprintf "%s\nError: %s (in the code).\n\n"
(ocaml_position pos)
(string_of_path path)
| RFailure (_, _, Some backtrace) ->
bprintf "%s\n" backtrace
| _ ->
()
end;
end;
bprintf "%s\n" (result_msg test_result);
bprintf "%s\n" separator2)
in
let filter f =
let lst =
List.filter
(fun (_, test_result, _) -> f test_result)
results
in
lst, List.length lst
in
let errors, nerrors = filter is_error in
let failures, nfailures = filter is_failure in
let skips, nskips = filter is_skip in
let _, ntodos = filter is_todo in
let timeouts, ntimeouts = filter is_timeout in
bprintf "\n";
print_results errors;
print_results failures;
print_results timeouts;
bprintf "Ran: %d tests in: %.2f seconds.\n"
(List.length results) running_time;
if was_successful results then
begin
if skips = [] then
bprintf "OK"
else
bprintf "OK: Cases: %d Skip: %d"
test_case_count nskips
end
else
begin
bprintf
"FAILED: Cases: %d Tried: %d Errors: %d \
Failures: %d Skip: %d Todo: %d \
Timeouts: %d."
test_case_count
(List.length results)
nerrors
nfailures
nskips
ntodos
ntimeouts;
end;
bprintf "\n";
Buffer.contents buf
end
| TestEvent (_, e) ->
begin
match e with
| EStart | EEnd | ELog _ | ELogRaw _ -> ""
| EResult RSuccess -> "."
| EResult (RFailure _) -> "F"
| EResult (RError _) -> "E"
| EResult (RSkip _) -> "S"
| EResult (RTodo _) -> "T"
| EResult (RTimeout _) -> "~"
end
let format_log_event ev =
let rlst = ref [] in
let timestamp_str = OUnitUtils.date_iso8601 ev.timestamp in
let spf pre fmt =
Printf.ksprintf
(multiline
(fun l ->
rlst := (timestamp_str^" "^ev.shard^" "^pre^": "^l) :: !rlst))
fmt
in
let ispf fmt = spf "I" fmt in
let wspf fmt = spf "W" fmt in
let espf fmt = spf "E" fmt in
let format_result path result =
let path_str = string_of_path path in
match result with
| RTimeout test_length ->
espf "Test %s timed out after %.1fs"
path_str (delay_of_length test_length)
| RError (msg, backtrace_opt) ->
espf "Test %s exited with an error." path_str;
espf "%s in test %s." msg path_str;
OUnitUtils.opt (espf "%s") backtrace_opt
| RFailure (msg, _, backtrace_opt) ->
espf "Test %s has failed." path_str;
espf "%s in test %s." msg path_str;
OUnitUtils.opt (espf "%s") backtrace_opt
| RTodo msg -> wspf "TODO test %s: %s." path_str msg
| RSkip msg -> wspf "Skip test %s: %s." path_str msg
| RSuccess -> ispf "Test %s is successful." path_str
in
begin
match ev.event with
| GlobalEvent e ->
begin
match e with
| GConf (k, v) -> ispf "Configuration %s = %S" k v
| GLog (`Error, str) -> espf "%s" str
| GLog (`Warning, str) -> wspf "%s" str
| GLog (`Info, str) -> ispf "%s" str
| GStart -> ispf "Start testing."
| GEnd -> ispf "End testing."
| GResults (running_time, results, test_case_count) ->
let counter = count results in
ispf "==============";
ispf "Summary:";
List.iter
(fun (path, test_result, _) ->
format_result path test_result)
results;
ispf "Ran: %d tests in: %.2f seconds."
(List.length results) running_time;
ispf "Cases: %d." test_case_count;
ispf "Tried: %d." (List.length results);
ispf "Errors: %d." (counter is_error);
ispf "Failures: %d." (counter is_failure);
ispf "Skip: %d." (counter is_skip);
ispf "Todo: %d." (counter is_todo);
ispf "Timeout: %d." (counter is_timeout)
end
| TestEvent (path, e) ->
begin
let path_str = string_of_path path in
match e with
| EStart -> ispf "Start test %s." path_str
| EEnd -> ispf "End test %s." path_str
| EResult result -> format_result path result
| ELog (`Error, str) -> espf "%s" str
| ELog (`Warning, str) -> wspf "%s" str
| ELog (`Info, str) -> ispf "%s" str
| ELogRaw str -> ispf "%s" str
end
end;
List.rev !rlst
let file_logger _ shard_id fn =
let chn = open_out fn in
let line = ref 1 in
let fwrite ev =
List.iter
(fun l -> output_string chn l; output_char chn '\n'; incr line)
(format_log_event ev);
flush chn
in
let fpos () =
Some { filename = fn; line = !line }
in
let fclose () =
close_out chn
in
{
lshard = shard_id;
fwrite = fwrite;
fpos = fpos;
fclose = fclose;
}
let verbose =
OUnitConf.make_bool
"verbose"
false
"Run test in verbose mode."
let display =
OUnitConf.make_bool
"display"
true
"Output logs on screen."
let std_logger conf shard_id =
if display conf then
let verbose = verbose conf in
let fwrite log_ev =
if verbose then
List.iter print_endline (format_log_event log_ev)
else
print_string (format_display_event conf log_ev);
flush stdout
in
{
lshard = shard_id;
fwrite = fwrite;
fpos = (fun () -> None);
fclose = ignore;
}
else
null_logger
let output_file =
OUnitConf.make_string_subst_opt
"output_file"
(Some (Filename.concat
OUnitUtils.buildir
"oUnit-$(suite_name)-$(shard_id).log"))
"Output verbose log in the given file."
let is_output_file_shard_dependent conf =
let fn1 = output_file ~extra_subst:["shard_id", "foo"] conf in
let fn2 = output_file ~extra_subst:["shard_id", "bar"] conf in
fn1 <> fn2
let create_file_logger conf shard_id =
match output_file ~extra_subst:["shard_id", shard_id] conf with
| Some fn ->
file_logger conf shard_id fn
| None ->
null_logger
let create conf shard_id =
let std_logger = std_logger conf shard_id in
let file_logger = create_file_logger conf shard_id in
combine [std_logger; file_logger]