Skip to content

Commit f9e0ce4

Browse files
authored
Add unit tests for CLI parsing (#243)
Signed-off-by: Andrew Helwer <[email protected]>
1 parent 80088ef commit f9e0ce4

File tree

8 files changed

+668
-56
lines changed

8 files changed

+668
-56
lines changed

src/params.ml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type exec = executable ref
102102
let path_prefix =
103103
sprintf "PATH='%s';" Paths.backend_path_string
104104

105-
let get_exec e =
105+
let get_exec err e =
106106
match !e with
107107
| Unchecked (exec, cmd, vers) ->
108108
(*
@@ -131,7 +131,7 @@ let get_exec e =
131131
else "."
132132
in
133133
let msg = msg1 ^ msg2 in
134-
eprintf "%s" msg;
134+
Format.fprintf err "%s" msg;
135135
e := NotFound msg;
136136
failwith msg;
137137
end;
@@ -450,14 +450,14 @@ let stats = ref false
450450

451451
let solve_cmd cmd file =
452452
if Sys.os_type = "Cygwin" then
453-
sprintf "file=%s; winfile=\"`cygpath -a -w \"%s\"`\"; %s" file file (get_exec cmd)
453+
sprintf "file=%s; winfile=\"`cygpath -a -w \"%s\"`\"; %s" file file (get_exec Format.err_formatter cmd)
454454
else
455-
sprintf "file=%s; %s" file (get_exec cmd)
455+
sprintf "file=%s; %s" file (get_exec Format.err_formatter cmd)
456456

457457

458-
let external_tool_config force (name, tool) =
458+
let external_tool_config (err : Format.formatter) force (name, tool) =
459459
if force then begin
460-
try ignore (get_exec tool) with Failure _ -> ()
460+
try ignore (get_exec err tool) with Failure _ -> ()
461461
end;
462462
match !tool with
463463
| Checked (cmd, []) ->
@@ -470,7 +470,7 @@ let external_tool_config force (name, tool) =
470470
| _ -> []
471471

472472

473-
let configuration toolbox force =
473+
let configuration err toolbox force =
474474
let library_path = (match stdlib_path with
475475
| Some path -> "\"" ^ (String.escaped path) ^ "\"";
476476
| None -> "N/A")
@@ -494,7 +494,7 @@ let configuration toolbox force =
494494
first_line :: mid_lines @ [last_line]
495495
end
496496
end
497-
@ List.flatten (List.map (external_tool_config force)
497+
@ List.flatten (List.map (external_tool_config err force)
498498
[("Isabelle", isabelle);
499499
("zenon", zenon);
500500
("CVC4", cvc4);
@@ -517,17 +517,17 @@ let configuration toolbox force =
517517
header @ lines @ footer
518518

519519

520-
let printconfig force =
521-
String.concat "\n" (configuration false force)
520+
let printconfig err force =
521+
String.concat "\n" (configuration err false force)
522522

523523
let print_config_toolbox force =
524-
String.concat "\n" (configuration true force)
524+
String.concat "\n" (configuration Format.err_formatter true force)
525525

526526

527527
let zenon_version = ref None
528528

529529
let check_zenon_ver () =
530-
let zen = get_exec zenon in
530+
let zen = get_exec Format.err_formatter zenon in
531531
match get_version zenon with
532532
| [] -> ()
533533
| ret :: _ ->
@@ -543,7 +543,7 @@ let get_zenon_verfp () = if !zenon_version = None then check_zenon_ver ();
543543
let isabelle_version = ref None
544544

545545
let check_isabelle_ver () =
546-
(try ignore (get_exec isabelle) with Failure _ -> ());
546+
(try ignore (get_exec Format.err_formatter isabelle) with Failure _ -> ());
547547
match get_version isabelle with
548548
| [] -> ()
549549
| ret :: _ ->

src/params.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ val safefp: bool ref
6666
val fp_deb: bool ref
6767

6868
(* util/util.ml *)
69-
val configuration: bool -> bool -> string list
69+
val configuration: Format.formatter -> bool -> bool -> string list
7070

7171
(* module/save.ml *)
7272
val stdlib_search_paths : string list
@@ -97,7 +97,7 @@ val suppress_all: bool ref
9797
val set_smt_logic: string -> unit
9898
val set_smt_solver: string -> unit
9999
val set_tlapm_cache_dir: string -> unit
100-
val printconfig: bool -> string
100+
val printconfig: Format.formatter -> bool -> string
101101
val print_config_toolbox: bool -> string
102102
val check_zenon_ver: unit -> unit
103103
val fpf_out: string option ref

src/tlapm_args.ml

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ open Ext
66
open Params
77

88

9-
let show_config = ref false
10-
let show_version () =
11-
print_endline (rawversion ()) ;
12-
exit 0
13-
9+
let show_version formatter terminate =
10+
Format.fprintf formatter "%s\n" (rawversion ());
11+
terminate 0
1412

1513
let set_debug_flags flgs =
1614
let flgs = Ext.split flgs ',' in
@@ -52,24 +50,23 @@ let set_default_method meth =
5250
with Failure msg -> raise (Arg.Bad ("--method: " ^ msg))
5351

5452

55-
(* FIXME use Arg.parse instead *)
56-
let parse_args args opts mods usage_fmt =
53+
let parse_args executable_name args opts mods usage_fmt err terminate =
5754
try
58-
Arg.parse_argv (Array.of_list args) opts (fun mfile -> mods := mfile :: !mods)
59-
(Printf.sprintf usage_fmt (Filename.basename Sys.executable_name))
55+
Arg.current := 0;
56+
Arg.parse_argv args opts (fun mfile -> mods := mfile :: !mods)
57+
(Printf.sprintf usage_fmt (Filename.basename executable_name))
6058
with Arg.Bad msg ->
61-
print_endline msg ;
62-
flush stdout ;
63-
exit 2
59+
Format.fprintf err "%s\n" msg;
60+
terminate 2
6461

65-
let show_where () =
62+
let show_where out terminate =
6663
match stdlib_path with
6764
| Some path ->
68-
Printf.printf "%s\n" path;
69-
exit 0
65+
Format.fprintf out "%s\n" path;
66+
terminate 0
7067
| None ->
71-
Printf.printf "N/A\n";
72-
exit 1
68+
Format.fprintf out "N/A\n";
69+
terminate 1
7370

7471
let set_nofp_start s =
7572
nofp_sl := s
@@ -134,7 +131,8 @@ let quote_if_needed s =
134131
end
135132

136133

137-
let init () =
134+
let init ?(out=Format.std_formatter) ?(err=Format.err_formatter) ?(terminate=exit) (executable_name : string) (args : string array) =
135+
let show_config = ref false in
138136
let mods = ref [] in
139137
let helpfn = ref (fun () -> ()) in
140138
let show_help () = !helpfn () in
@@ -147,11 +145,11 @@ let init () =
147145
blank;
148146
"--help", Arg.Unit show_help, " show this help message and exit" ;
149147
"-help", Arg.Unit show_help, " (same as --help)" ;
150-
"--version", Arg.Unit show_version, " show version number and exit" ;
148+
"--version", Arg.Unit (fun () -> show_version out terminate), " show version number and exit" ;
151149
"--verbose", Arg.Set verbose, " produce verbose messages" ;
152150
"-v", Arg.Set verbose, " (same as --verbose)" ;
153151
blank;
154-
"--where", Arg.Unit show_where,
152+
"--where", Arg.Unit (fun () -> show_where out terminate),
155153
" show location of standard library and exit" ;
156154
"--config", Arg.Set show_config, " show configuration and exit" ;
157155
"--summary", Arg.Set summary,
@@ -245,44 +243,44 @@ let init () =
245243
format_of_string "Usage: %s <options> FILE ...\noptions are:"
246244
in
247245
helpfn := begin fun () ->
248-
Arg.usage opts
249-
(Printf.sprintf usage_fmt (Filename.basename Sys.executable_name)) ;
250-
exit 0
246+
Arg.usage_string opts
247+
(Printf.sprintf usage_fmt (Filename.basename executable_name))
248+
|> Format.fprintf err "%s";
249+
terminate 0
251250
end ;
252-
let args = Array.to_list Sys.argv in
253-
parse_args args opts mods usage_fmt ;
251+
parse_args executable_name args opts mods usage_fmt err terminate;
254252
if !show_config || !verbose then begin
255-
print_endline (printconfig true) ;
256-
flush stdout
253+
Format.fprintf out "%s\n" (printconfig err true);
257254
end ;
258-
if !show_config then exit 0 ;
255+
if !show_config then terminate 0 ;
259256
if !mods = [] then begin
260-
Arg.usage opts
257+
Arg.usage_string opts
261258
(Printf.sprintf "Need at least one module file.\n\n\
262259
Usage: %s <options> FILE ...\noptions are:"
263-
(Filename.basename Sys.executable_name)) ;
264-
exit 2
260+
(Filename.basename executable_name))
261+
|> Format.fprintf err "%s";
262+
terminate 2
265263
end ;
266264
if !summary then begin
267265
suppress_all := true ;
268266
check := false ;
269267
end ;
270268
check_zenon_ver () ;
271269
if !Params.toolbox then begin
272-
Printf.printf "\n\\* TLAPM version %s\n"
270+
Format.fprintf out "\n\\* TLAPM version %s\n"
273271
(Params.rawversion ());
274272
let tm = Unix.localtime (Unix.gettimeofday ()) in
275-
Printf.printf "\\* launched at %04d-%02d-%02d %02d:%02d:%02d"
273+
Format.fprintf out "\\* launched at %04d-%02d-%02d %02d:%02d:%02d"
276274
(tm.Unix.tm_year + 1900) (tm.Unix.tm_mon + 1) tm.Unix.tm_mday
277275
tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec;
278-
Printf.printf " with command line:\n\\*";
279-
Array.iter (fun s -> Printf.printf " %s" (quote_if_needed s)) Sys.argv;
280-
Printf.printf "\n\n%!"
276+
Format.fprintf out " with command line:\n\\*";
277+
Array.iter (fun s -> Format.fprintf out " %s" (quote_if_needed s)) args;
278+
Format.fprintf out "\n\n%!"
281279
end;
282280
if !use_stdin && (List.length !mods) <> 1 then begin
283281
Arg.usage opts
284282
"Exactly 1 module has to be specified if TLAPM is invoked with\
285283
the --stdin option." ;
286-
exit 2
284+
terminate 2
287285
end;
288286
!mods

src/tlapm_args.mli

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@
22
33
Copyright (C) 2011 INRIA and Microsoft Corporation
44
*)
5-
val init: unit -> string list
5+
6+
(** Given the executable name and an array of command-line arguments, parses
7+
those arguments then either sets associated values in the Params module
8+
or directly takes action such as printing out file contents or deleting
9+
directories. Returns a list of files for proof checking. The optional
10+
arguments are used for unit tests.
11+
*)
12+
val init: ?out:Format.formatter -> ?err:Format.formatter -> ?terminate:(int -> unit) -> string -> string array -> string list

src/tlapm_lib.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,9 @@ let init () =
586586
Printexc.record_backtrace true;
587587
Format.pp_set_max_indent Format.err_formatter 35;
588588
if Params.debugging "main" then
589-
main (Tlapm_args.init ())
589+
main (Tlapm_args.init Sys.executable_name Sys.argv)
590590
else
591-
try main (Tlapm_args.init ()) with
591+
try main (Tlapm_args.init Sys.executable_name Sys.argv) with
592592
| Errors.Fatal ->
593593
Util.eprintf "tlapm: Exiting because of the above error.";
594594
exit 0;

src/util/util.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ let bug ?at msg =
117117
"Please file a bug report, including as much information" ;
118118
"as you can. Mention the following in your report: " ;
119119
"" ]
120-
@ Params.configuration false false
120+
@ Params.configuration Format.err_formatter false false
121121
in
122122
String.concat "\n" lines ^ "\n"
123123
in

0 commit comments

Comments
 (0)