-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathoUnitAssert.ml
354 lines (331 loc) · 11.4 KB
/
oUnitAssert.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
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
(**************************************************************************)
(* The OUnit library *)
(* *)
(* Copyright (C) 2002-2008 Maas-Maarten Zeeman. *)
(* Copyright (C) 2010 OCamlCore SARL *)
(* Copyright (C) 2013 Sylvain Le Gall *)
(* *)
(* The package OUnit is copyright by Maas-Maarten Zeeman, OCamlCore SARL *)
(* and Sylvain Le Gall. *)
(* *)
(* Permission is hereby granted, free of charge, to any person obtaining *)
(* a copy of this document and the OUnit software ("the Software"), to *)
(* deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, *)
(* sublicense, and/or sell copies of the Software, and to permit persons *)
(* to whom the Software is furnished to do so, subject to the following *)
(* conditions: *)
(* *)
(* The above copyright notice and this permission notice shall be *)
(* included in all copies or substantial portions of the Software. *)
(* *)
(* The Software is provided ``as is'', without warranty of any kind, *)
(* express or implied, including but not limited to the warranties of *)
(* merchantability, fitness for a particular purpose and noninfringement. *)
(* In no event shall Maas-Maarten Zeeman be liable for any claim, damages *)
(* or other liability, whether in an action of contract, tort or *)
(* otherwise, arising from, out of or in connection with the Software or *)
(* the use or other dealings in the software. *)
(* *)
(* See LICENSE.txt for details. *)
(**************************************************************************)
open OUnitUtils
open OUnitBracket
open OUnitTest
let skip_if b msg =
if b then
raise (Skip msg)
let todo msg =
raise (Todo msg)
let assert_failure msg =
raise (OUnit_failure msg)
let assert_bool msg b =
if not b then assert_failure msg
let assert_string str =
if not (str = "") then assert_failure str
let rec seq_of_channel channel () =
match input_char channel with
| exception End_of_file -> Seq.Nil
| char -> Seq.Cons (char, seq_of_channel channel)
let assert_equal ?ctxt ?(cmp = ( = )) ?printer ?pp_diff ?msg expected actual =
let get_error_string () =
let res =
buff_format_printf
(fun fmt ->
Format.pp_open_vbox fmt 0;
begin
match msg with
| Some s ->
Format.pp_open_box fmt 0;
Format.pp_print_string fmt s;
Format.pp_close_box fmt ();
Format.pp_print_cut fmt ()
| None ->
()
end;
begin
match printer with
| Some p ->
Format.fprintf fmt
"@[expected: @[%s@]@ but got: @[%s@]@]@,"
(p expected)
(p actual)
| None ->
Format.fprintf fmt "@[not equal@]@,"
end;
begin
match pp_diff with
| Some d ->
Format.fprintf fmt
"@[differences: %a@]@,"
d (expected, actual)
| None ->
()
end;
Format.pp_close_box fmt ())
in
let len =
String.length res
in
if len > 0 && res.[len - 1] = '\n' then
String.sub res 0 (len - 1)
else
res
in
let logf fmt =
match ctxt with
| Some ctxt ->
OUnitLogger.Test.logf ctxt.test_logger `Info fmt
| None ->
Printf.ksprintf ignore fmt
in
begin
match msg with
| Some str ->
logf "%s" str;
| _ ->
()
end;
begin
match printer with
| Some p ->
logf "Expected: %s" (p expected);
logf "Actual: %s" (p actual)
| _ ->
()
end;
if not (cmp expected actual) then
assert_failure (get_error_string ())
let assert_command
?(exit_code=Unix.WEXITED 0)
?(sinput=Seq.empty)
?(foutput=ignore)
?(use_stderr=true)
?(backtrace=true)
?chdir
?env
~ctxt
prg args =
let log_environment_diff () =
let module SetString = Set.Make(struct
type t = string
let compare = String.compare
end)
in
let set_of_array a =
let ss = ref SetString.empty in
for i = 0 to (Array.length a) - 1 do
ss := SetString.add (Array.get a i) !ss
done;
!ss
in
let current_environment = set_of_array (Unix.environment ()) in
let initial_environment = set_of_array ctxt.initial_environment in
if SetString.equal current_environment initial_environment then begin
OUnitLogger.Test.logf ctxt.test_logger `Info
"Environment is the same as original environment.";
end else begin
OUnitLogger.Test.logf ctxt.test_logger `Info
"Environment (diff with original environment):";
SetString.iter
(fun s -> OUnitLogger.Test.logf ctxt.test_logger `Info "+%s" s)
(SetString.diff current_environment initial_environment);
SetString.iter
(fun s -> OUnitLogger.Test.logf ctxt.test_logger `Info "-%s" s)
(SetString.diff current_environment initial_environment);
end
in
begin
match env with
| Some a when Array.length a = 0 && Sys.os_type = "Win32" ->
OUnitLogger.Test.logf ctxt.test_logger `Info "%s"
("Using an empty environment on Windows could cause "^
"failure when running command.")
| _ -> ()
end;
OUnitTest.section_ctxt ctxt
(fun ctxt ->
let (fn_out, chn_out) = bracket_tmpfile ctxt in
let cmd_print fmt =
Format.pp_print_string fmt prg;
List.iter (Format.fprintf fmt "@ %s") args
in
(* Start the process *)
let in_write =
Unix.dup (Unix.descr_of_out_channel chn_out)
in
let (out_read, out_write) =
Unix.pipe ()
in
let err =
if use_stderr then
in_write
else
Unix.stderr
in
let args =
Array.of_list (prg :: args)
in
let env =
let param = "OCAMLRUNPARAM" in
let analyse_and_fix env =
let arr = Array.copy env in
let fixed = ref false in
let new_var = ref "" in
for i = 0 to (Array.length arr) - 1 do
let really_starts, current_value =
OUnitUtils.start_substr ~prefix:(param^"=") arr.(i)
in
if really_starts then begin
(* Rewrite the params. *)
if not (String.contains current_value 'b') then begin
arr.(i) <- param^"="^current_value^"b"
end;
new_var := arr.(i);
fixed := true
end
done;
if !fixed then
arr
else
Array.append arr [|param^"=b"|]
in
if backtrace then begin
(* Analyse of the provided environment. *)
match env with
| Some env -> Some (analyse_and_fix env)
| None -> Some (analyse_and_fix (Unix.environment ()))
end else begin
env
end
in
let command_chdir, in_chdir =
match chdir with
| Some dn ->
dn,
fun f ->
with_bracket ctxt (bracket_chdir dn)
(fun _ _ -> f ())
| None ->
Sys.getcwd (), fun f -> f ()
in
let pid =
OUnitLogger.Test.logf ctxt.test_logger `Info "%s"
(buff_format_printf
(fun fmt ->
Format.fprintf fmt "Starting command '%t'." cmd_print));
OUnitLogger.Test.logf ctxt.test_logger `Info "Working directory: %S"
command_chdir;
log_environment_diff ();
Unix.set_close_on_exec out_write;
match env with
| Some e ->
in_chdir
(fun () ->
Unix.create_process_env prg args e out_read in_write err)
| None ->
in_chdir
(fun () ->
Unix.create_process prg args out_read in_write err)
in
let () =
Unix.close out_read;
Unix.close in_write
in
let () =
(* Dump sinput into the process stdin *)
let buff = Bytes.make 1 ' ' in
Seq.iter
(fun c ->
let _i : int =
Bytes.set buff 0 c;
Unix.write out_write buff 0 1
in
())
sinput;
Unix.close out_write
in
let _, real_exit_code =
let rec wait_intr () =
try
Unix.waitpid [] pid
with Unix.Unix_error (Unix.EINTR, _, _) ->
wait_intr ()
in
wait_intr ()
in
(* Dump process output to stderr *)
begin
let chn = open_in_bin fn_out in
let buff = Bytes.make 4096 'X' in
let len = ref (-1) in
while !len <> 0 do
len := input chn buff 0 (Bytes.length buff);
OUnitLogger.Test.raw_printf
ctxt.test_logger "%s" Bytes.(to_string (sub buff 0 !len));
done;
close_in chn
end;
(* Check process status *)
assert_equal
~msg:(buff_format_printf
(fun fmt ->
Format.fprintf fmt
"@[Exit status of command '%t'@]" cmd_print))
~printer:string_of_process_status
exit_code
real_exit_code;
begin
let chn = open_in_bin fn_out in
try
foutput (seq_of_channel chn)
with e ->
close_in chn;
raise e
end)
let raises f =
try
let _ = f () in None
with e ->
Some e
let assert_raises ?msg exn (f: unit -> 'a) =
let pexn =
Printexc.to_string
in
let get_error_string () =
let str =
Format.sprintf
"expected exception %s, but no exception was raised."
(pexn exn)
in
match msg with
| None ->
assert_failure str
| Some s ->
assert_failure (s^"\n"^str)
in
match raises f with
| None ->
assert_failure (get_error_string ())
| Some e ->
assert_equal ?msg ~printer:pexn exn e