-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.ml
253 lines (209 loc) · 7.35 KB
/
main_test.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
(*
* Please imagine a long and boring gnu-style copyright notice
* appearing just here.
*)
open Common
open OUnit
(*****************************************************************************)
(* Purpose *)
(*****************************************************************************)
(* Unit tests runner (and a few adhoc actions) *)
(*****************************************************************************)
(* Flags *)
(*****************************************************************************)
(* In addition to flags that can be tweaked via -xxx options (cf the
* full list of options in the "the options" section below), this
* program also depends on external files ?
*)
let verbose = ref false
(* action mode *)
let action = ref ""
(*****************************************************************************)
(* Main action *)
(*****************************************************************************)
(*---------------------------------------------------------------------------*)
(* regression testing *)
(*---------------------------------------------------------------------------*)
let test regexp =
(* There is no reflection in OCaml so the unit test framework OUnit requires
* us to explicitely build the test suites (which is not that bad).
*)
let tests =
"all" >::: [
(* PHP related tests *)
Unit_parsing_php.unittest;
Unit_pretty_print_php.unittest;
Unit_matcher_php.unittest;
Unit_foundation_php.unittest;
Unit_static_analysis_php.unittest;
Unit_typeinfer_php.unittest;
(* Unit_analyze_db_php.unittest; *)
(* Unit_static_analysis_simple_php.unittest;*)
Unit_prolog_php.unittest;
Unit_checker_php.unittest;
(* this one needs xdebug to work *)
Unit_coverage_php.unittest;
(* non PHP related tests *)
Unit_parsing_ml.unittest;
Unit_analyze_ml.unittest;
Unit_parsing_java.unittest;
Unit_analyze_java.unittest;
Unit_analyze_bytecode.unittest;
Unit_parsing_js.unittest;
Unit_analyze_js.unittest;
Unit_parsing_html.unittest;
Unit_parsing_opa.unittest;
Unit_parsing_cpp.unittest;
Unit_parsing_objc.unittest;
Unit_version_control.unittest;
Unit_program_lang.unittest;
]
in
let suite =
if regexp = "all"
then tests
else
let paths =
OUnit.test_case_paths tests +> List.map OUnit.string_of_path in
let keep = paths
+> List.filter (fun path ->
pr2 path;
path =~ (".*" ^ regexp))
in
Common2.some (OUnit.test_filter keep tests)
in
let results = OUnit.run_test_tt ~verbose:!verbose suite in
let has_an_error =
results +> List.exists (function
| OUnit.RSuccess _ | OUnit.RSkip _ | OUnit.RTodo _ -> false
| OUnit.RFailure _ | OUnit.RError _ -> true
)
in
raise (Common.UnixExit (if has_an_error then 1 else 0))
let main_action x =
test x
(*****************************************************************************)
(* Extra Actions *)
(*****************************************************************************)
type x = Foo of int
let action1 () =
let x = Foo 1 in
pr2_gen x;
let x = 1 in
pr2_gen x;
()
let test_json_pretty_printer file =
let json = Json_in.load_json file in
let s = Json_io.string_of_json json in
pr s
let test_json_bench file =
Common.profile_code "json_bench" (fun () ->
pr2 (Common2.memory_stat ());
let _json = Json_in.load_json file in
pr2 (Common2.memory_stat ());
)
(* ---------------------------------------------------------------------- *)
let pfff_extra_actions () = [
"-json_pp", " <file>",
Common.mk_action_1_arg test_json_pretty_printer;
"-json_bench", " <file>",
Common.mk_action_1_arg test_json_bench;
"-check_overlay", " <dir_orig> <dir_overlay>",
Common.mk_action_2_arg (fun dir_orig dir_overlay ->
Overlay_code.check_overlay ~dir_orig ~dir_overlay;
);
"-gen_overlay", " <dir_orig> <dir_overlay> <output>",
Common.mk_action_3_arg (fun dir_orig dir_overlay output ->
Overlay_code.gen_overlay ~dir_orig ~dir_overlay ~output;
);
"-adapt_layers_overlay", " <overlay> <dir_layers> <dir_ayers_overlay>",
Common.mk_action_3_arg
(fun overlay dir_layers_orig dir_layers_overlay ->
Overlay_code.adapt_layers
~overlay:(Overlay_code.load_overlay overlay)
~dir_layers_orig
~dir_layers_overlay
;
);
"-adapt_database_overlay", "<overlay> <file> <output>",
Common.mk_action_3_arg (fun overlay orig output ->
let db =
Database_code.load_database orig in
let db' =
Overlay_code.adapt_database db (Overlay_code.load_overlay overlay) in
Database_code.save_database db' output
);
"-action1", "", Common.mk_action_0_arg action1;
]
(*****************************************************************************)
(* The options *)
(*****************************************************************************)
let all_actions () =
pfff_extra_actions() ++
Test_parsing_php.actions()++
Test_analyze_php.actions()++
Test_analyze_ml.actions()++
Test_analyze_clang.actions()++
Test_program_lang.actions()++
Builtins_php.actions()++
[]
let options () = [
"-verbose", Arg.Set verbose,
" ";
] ++
Common.options_of_actions action (all_actions()) ++
Common2.cmdline_flags_devel () ++
Common2.cmdline_flags_other () ++
[
"-version", Arg.Unit (fun () ->
pr2 (spf "pfff (test) version: %s" Config_pfff.version);
exit 0;
),
" guess what";
(* this can not be factorized in Common *)
"-date", Arg.Unit (fun () ->
pr2 "version: $Date: 2008/10/26 00:44:57 $";
raise (Common.UnixExit 0)
),
" guess what";
]
(*****************************************************************************)
(* Main entry point *)
(*****************************************************************************)
let main () =
Gc.set {(Gc.get ()) with Gc.stack_limit = 1000 * 1024 * 1024};
Common_extra.set_link();
let usage_msg =
"Usage: " ^ Common2.basename Sys.argv.(0) ^
" [options] <file or dir> " ^ "\n" ^ "Options are:"
in
(* does side effect on many global flags *)
let args = Common.parse_options (options()) usage_msg Sys.argv in
(* must be done after Arg.parse, because Common.profile is set by it *)
Common.profile_code "Main total" (fun () ->
(match args with
(* --------------------------------------------------------- *)
(* actions, useful to debug subpart *)
(* --------------------------------------------------------- *)
| xs when List.mem !action (Common.action_list (all_actions())) ->
Common.do_action !action xs (all_actions())
| _ when not (Common.null_string !action) ->
failwith ("unrecognized action or wrong params: " ^ !action)
(* --------------------------------------------------------- *)
(* main entry *)
(* --------------------------------------------------------- *)
| [x] ->
main_action x
(* --------------------------------------------------------- *)
(* empty entry *)
(* --------------------------------------------------------- *)
| _ ->
Common.usage usage_msg (options());
failwith "too few or too many arguments"
)
)
(*****************************************************************************)
let _ =
Common.main_boilerplate (fun () ->
main ();
)