-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathmain.ml
86 lines (76 loc) · 2.39 KB
/
main.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
let test_directory = "cases"
let package_directory = "../../../../install/default/lib"
let _read_file name =
let buffer = Buffer.create 4096 in
let channel = open_in name in
try
let rec read () =
try input_char channel |> Buffer.add_char buffer; read ()
with End_of_file -> ()
in
read ();
close_in channel;
Buffer.contents buffer
with exn ->
close_in_noerr channel;
raise exn
let _command_failed ?status command =
match status with
| None -> Printf.sprintf "'%s' did not exit" command |> failwith
| Some v -> Printf.sprintf "'%s' failed with status %i" command v |> failwith
let _run_int command =
match Unix.system command with
| Unix.WEXITED v -> v
| _ -> _command_failed command
let run command =
let v = _run_int command in
if v <> 0 then _command_failed command ~status:v
let diff reference result =
let command = Printf.sprintf "diff -au %s %s" reference result in
let status = _run_int (command ^ " > /dev/null") in
match status with
| 0 -> ()
| 1 ->
let _ : int =_run_int (command ^ " > delta") in
let delta = _read_file "delta" in
Printf.eprintf "> %s:\n\n%s" command delta;
failwith "Output does not match expected"
| _ -> _command_failed command ~status
let run_test name =
let ml_name = name ^ ".ml" in
let expect_name = name ^ ".expect" in
let fixed_name = name ^ ".fixed" in
let command =
Printf.sprintf
"OCAMLPATH=%s ocamlfind opt %s -linkpkg -package lwt,lwt_ppx %s > %s 2>&1"
package_directory "-color=never" ml_name fixed_name
in
let ocaml_return_code = _run_int command in
begin if ocaml_return_code = 0 then
failwith
(Printf.sprintf "Unexpected compiler return code: %d" ocaml_return_code)
end;
diff expect_name fixed_name
let () =
let test_cases =
Sys.readdir test_directory
|> Array.to_list
|> List.filter (fun file -> Filename.check_suffix file ".ml")
|> List.map Filename.chop_extension
in
Sys.chdir test_directory;
let only_if () =
Sys.cygwin = false && Sys.win32 = false &&
(* 4.02.3 prints file paths differently *)
Scanf.sscanf Sys.ocaml_version "%u.%u"
(fun major minor -> (major, minor) >= (4, 4))
in
let suite = Test.suite "ppx_expect" (
List.map (fun test_case ->
Test.test_direct test_case ~only_if (fun () ->
run_test test_case;
true
)
) test_cases)
in
Test.run "ppx_expect" [suite]