forked from OCamlPro/autofonce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_gen.ml
More file actions
178 lines (147 loc) · 6.37 KB
/
command_gen.ml
File metadata and controls
178 lines (147 loc) · 6.37 KB
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
(**************************************************************************)
(* *)
(* Copyright (c) 2023 OCamlPro SAS *)
(* *)
(* All rights reserved. *)
(* This file is distributed under the terms of the GNU General Public *)
(* License version 3.0, as described in the LICENSE.md file in the root *)
(* directory of this source tree. *)
(* *)
(* *)
(**************************************************************************)
(*
open Ezcmd.V2
open EZCMD.TYPES
open Ez_file.V1
module Misc = Autofonce_misc.Misc
module Parser = Autofonce_core.Parser
let cmd =
let name = ref "testsuite" in
let build = ref None in
let run = ref None in
let files = ref [] in
let data = ref [] in
let args =
[
[ "name" ], Arg.String (fun s -> name := s),
EZCMD.info ~docv:"NAME" "Store name in AT_INIT(...)";
[ "f"; "file" ], Arg.String (fun s -> data := s :: !data),
EZCMD.info ~docv:"FILE" "Add file to each test AT_DATA";
[ "build" ], Arg.String (fun s -> build := Some s),
EZCMD.info ~docv:"FILE" "Build command";
[ "run" ], Arg.String (fun s -> run := Some s),
EZCMD.info ~docv:"FILE" "Run command";
[], Arg.Anons (fun list -> files := list),
EZCMD.info "List of files" ;
]
in
EZCMD.sub
"gen"
(fun () ->
begin match !build, !run with
None, None -> Misc.error "Missing either --build or --run argument"
| _ -> ()
end;
let top_basename = "tests" in
let sub_basename = "testsuite.src" in
let gen_basename = "testsuite.gen" in
let shared_basename = "shared" in
let files_basename = "files" in
let dir = top_basename in
let sub_dir = Filename.concat dir sub_basename in
let gen_dir = Filename.concat dir gen_basename in
let shared_dir = Filename.concat gen_dir shared_basename in
let files_dir = Filename.concat gen_dir files_basename in
if not (Sys.file_exists dir) then Unix.mkdir dir 0o755;
if not (Sys.file_exists sub_dir) then Unix.mkdir sub_dir 0o755;
if not (Sys.file_exists gen_dir) then Unix.mkdir gen_dir 0o755;
if not (Sys.file_exists shared_dir) then Unix.mkdir shared_dir 0o755;
if not (Sys.file_exists files_dir) then Unix.mkdir files_dir 0o755;
let files = List.map (fun file ->
let basename = Filename.basename file in
let content = EzFile.read_file file in
EzFile.write_file (Filename.concat files_dir basename) content;
basename
) !files
in
let data = List.map (fun file ->
let basename = Filename.basename file in
let content = EzFile.read_file file in
EzFile.write_file (Filename.concat shared_dir basename) content;
basename
) (List.rev !data)
in
let contents =
let b = Buffer.create 10000 in
Printf.bprintf b "# File generated with:\n";
Printf.bprintf b "#";
Array.iter (fun s -> Printf.bprintf b " '%s'" s) Sys.argv;
Printf.bprintf b "\n\n";
Printf.bprintf b "AT_COPYRIGHT(No copyright given)\n\n";
Printf.bprintf b "# Name of testsuite\n";
Printf.bprintf b "AT_INIT(%s)\n\n" (Parser.m4_escape !name);
Printf.bprintf b "AT_COLOR_TESTS\n\n";
Printf.bprintf b "# Tools being tested\n";
Printf.bprintf b "# AT_TESTED([ ... tools ... ])\n\n";
Printf.bprintf b "# First section\n";
Printf.bprintf b "AT_BANNER([Main tests])\n\n";
List.iter (fun basename ->
Printf.bprintf b "m4_include(%s)\n\n"
(Parser.m4_escape @@
(Filename.concat sub_basename @@
(Filename.chop_extension basename ^ ".at")))
) files;
Buffer.contents b
in
let output = Filename.concat dir "testsuite.at" in
EzFile.write_file output contents;
let script = Buffer.create 10000 in
List.iter (fun basename ->
Printf.bprintf script "\n# autofonce script to regen %s.at\n\n"
basename;
Printf.bprintf script "test: %s\n"
(Filename.chop_extension basename);
Printf.bprintf script "# keywords: \n";
List.iter (fun file ->
Printf.bprintf script "data: %s/%s\n" shared_basename file
) data;
Printf.bprintf script "data: %s/%s\n" files_basename basename;
Printf.bprintf script "target: %s\n" basename;
begin
match !build with
| None -> ()
| Some command ->
Printf.bprintf script "command: %s\n" command;
Printf.bprintf script "#stdout: file.stdout\n";
Printf.bprintf script "#stderr: file.stderr\n";
Printf.bprintf script "run: 0\n";
end;
begin
match !run with
| None -> ()
| Some command ->
Printf.bprintf script "command: %s\n" command;
Printf.bprintf script "#stdout: file.stdout\n";
Printf.bprintf script "#stderr: file.stderr\n";
Printf.bprintf script "run: 0\n";
end;
Printf.bprintf script "save: ../%s/%s.at\n\n\n"
sub_basename (Filename.chop_extension basename) ;
) files ;
let script_file = Filename.concat gen_dir "testsuite.atscript" in
EzFile.write_file script_file (Buffer.contents script);
Printf.eprintf "File %S generated\n%!" output;
Command_regen.regen ();
Printf.eprintf "1. Use `autofonce init` to initialize autofonce.toml file\n";
Printf.eprintf "2. Use `autofonce run` to run the generated tests afterwards.\n";
Printf.eprintf "3. Fix the tests and run `autofonce run --auto-promote 2` to run and promote tests.\n";
)
~args
~doc: "Generate a testsuite"
~man:[
`S "DESCRIPTION";
`Blocks [
`P {|Generates a full testsuite in directory tests/ from a set of data files.|}
];
]
*)