forked from ocaml/oasis
/
TestQuickstart.ml
468 lines (449 loc) · 14.7 KB
/
TestQuickstart.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
(******************************************************************************)
(* OASIS: architecture for building OCaml libraries and applications *)
(* *)
(* Copyright (C) 2008-2010, OCamlCore SARL *)
(* *)
(* This library is free software; you can redistribute it and/or modify it *)
(* under the terms of the GNU Lesser General Public License as published by *)
(* the Free Software Foundation; either version 2.1 of the License, or (at *)
(* your option) any later version, with the OCaml static compilation *)
(* exception. *)
(* *)
(* This library is distributed in the hope that it will be useful, but *)
(* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *)
(* or FITNESS FOR A PARTICULAR PURPOSE. See the file COPYING for more *)
(* details. *)
(* *)
(* You should have received a copy of the GNU Lesser General Public License *)
(* along with this library; if not, write to the Free Software Foundation, *)
(* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *)
(******************************************************************************)
(** Test quickstart subcommand
@author Sylvain Le Gall
*)
open TestCommon
open Expect
open ExpectPcre
open OUnit
open OASISTypes
let () =
METAPlugin.init ()
module MapString = Map.Make(String)
let tests =
let assert_exit_code_equal exp rel =
assert_equal
~msg:"exit code"
~printer:(function
| Unix.WEXITED i
| Unix.WSIGNALED i
| Unix.WSTOPPED i ->
string_of_int i)
exp
rel
in
let with_quickstart_spawn args f exp_exit_code =
let args =
["quickstart"; "-machine"] @ args
in
let args =
if not !dbug then
"-quiet" :: args
else
"-debug" :: args
in
let args =
!oasis_args @ args
in
let () =
if !dbug then
Printf.eprintf
"Quickstart command line: %s\n%!"
(String.concat " " (oasis () :: args))
in
let _, exit_code =
try
with_spawn
~verbose:!dbug
~timeout:(Some 0.1)
(oasis ())
(Array.of_list args)
(fun t () -> f t)
()
with e ->
Printexc.print_backtrace stderr;
raise e
in
assert_exit_code_equal
exp_exit_code
exit_code
in
let run_quickstart args qa =
with_quickstart_spawn
args
(fun t ->
let rec continue =
function
| [] ->
begin
()
end
| qa ->
begin
let expectations =
let rec expectations' prev next =
match next with
| (q, a) :: tl ->
(* next QA if we chose this answer *)
let qa' =
List.rev_append prev tl
in
let q_pat =
match q with
| "create_section" ->
"create a(nother)? section\\? "
| "end" ->
"What do you want to do now\\?"
| "name" ->
"'?name'?\\? "
| q ->
Printf.sprintf
"value for field '%s'\\? "
q
in
let q_rex =
Pcre.regexp ~flags:[`CASELESS] q_pat
in
(`Rex q_rex, Some (a, qa'))
::
(`Prefix ("???"^q^" "), Some (a, qa'))
::
(expectations' ((q, a) :: prev) tl)
| [] ->
[]
in
expectations' [] qa
in
let exp_q =
expect t expectations None
in
match exp_q with
| Some (a, qa) ->
send t (a^"\n");
continue qa
| None ->
let assert_msg =
Printf.sprintf "expecting questions: %s"
(String.concat ", "
(List.map
(fun (q, _) -> Printf.sprintf "%S" q)
qa))
in
assert_failure assert_msg
end
in
continue qa;
assert_bool
"wait for eof"
(expect t [`Eof, true] false))
(Unix.WEXITED 0)
in
let test_of_vector (nm, args, qa, post) =
nm >::
bracket
(fun () ->
let pwd = FileUtil.pwd () in
let tmp = temp_dir () in
Sys.chdir tmp;
pwd, tmp)
(fun _ ->
run_quickstart args qa;
if !dbug then
begin
let chn = open_in "_oasis" in
let () =
try
while true do
prerr_endline (input_line chn)
done
with End_of_file ->
()
in
close_in chn
end;
assert_oasis_cli ["check"];
begin
try
assert_oasis_cli ["setup"];
with e ->
failwith "'oasis setup' failed but 'oasis check' succeed"
end;
let pkg =
OASISParse.from_file ~ctxt:!oasis_ctxt "_oasis"
in
post pkg)
(fun (pwd, tmp) ->
Sys.chdir pwd;
FileUtil.rm ~recurse:true [tmp])
in
let test_simple_qa =
[
"name", "test";
"version", "0.0.1";
"synopsis", "test";
"authors", "me";
"license", "GPL-2+";
"plugins", "";
"create_section", "e";
"name", "test";
"path", "./";
"mainis", "test.ml";
"create_section", "n";
"end", "w";
]
in
"Quickstart" >:::
(List.map test_of_vector
[
"simple",
[],
test_simple_qa,
(fun pkg ->
let () =
assert_equal
~msg:"field name"
~printer:(fun s -> s)
"test" pkg.name
in
let sct =
try
OASISSection.section_find
(`Executable, "test")
pkg.sections
with Not_found ->
failwith "Cannot find executable section 'test'"
in
match sct with
| Executable (cs, bs, exec) ->
assert_equal
~msg:"mainis of test"
~printer:(fun s -> s)
"test.ml" exec.exec_main_is
| _ ->
assert false);
"custom",
["-level"; "expert"],
[
"name", "test";
"version", "0.1";
"synopsis", "test";
"description", "test";
"licensefile", "";
"authors", "me";
"copyrights", "(C) 2010 Me";
"maintainers", "";
"license", "GPL-3";
"ocamlversion", "";
"findlibversion", "";
"conftype", "custom";
"preconfcommand", "";
"postconfcommand", "";
"buildtype", "custom";
"prebuildcommand", "";
"postbuildcommand", "";
"installtype", "";
"preinstallcommand", "";
"postinstallcommand", "";
"preuninstallcommand", "";
"postuninstallcommand", "";
"precleancommand", "";
"postcleancommand", "";
"predistcleancommand", "";
"postdistcleancommand", "";
"homepage", "";
"categories", "";
"filesab", "";
"plugins", "stdfiles, devfiles, meta";
"builddepends", "";
"buildtools", "";
"create_section", "n";
"end", "w";
"xcustomconf", "./configure";
"xcustomconfclean", "";
"xcustomconfdistclean", "$rm config.log";
"xcustombuild", "make all";
"xcustombuildclean", "make clean";
"xcustombuilddistclean", "make distclean";
"xstdfilesreadme", "";
"xstdfilesreadmefilename", "";
"xstdfilesinstallfilename", "";
"xstdfilesinstall", "";
"xstdfilesauthorsfilename", "";
"xstdfilesauthors", "";
"xdevfilesmakefilenotargets", "";
"xdevfilesenablemakefile", "";
"xdevfilesenableconfigure", "";
],
(fun pkg ->
assert_equal
~msg:"field name"
~printer:(fun s -> s)
"test" pkg.name);
"meta",
["-level"; "expert"],
[
"name", "test";
"version", "0.1";
"synopsis", "test";
"description", "test";
"licensefile", "";
"authors", "me";
"copyrights", "(C) 2010 Me";
"maintainers", "";
"license", "GPL-3";
"ocamlversion", "";
"findlibversion", "";
"conftype", "";
"preconfcommand", "";
"postconfcommand", "";
"buildtype", "";
"prebuildcommand", "";
"postbuildcommand", "";
"installtype", "";
"preinstallcommand", "";
"postinstallcommand", "";
"preuninstallcommand", "";
"postuninstallcommand", "";
"precleancommand", "";
"postcleancommand", "";
"predistcleancommand", "";
"postdistcleancommand", "";
"homepage", "";
"categories", "";
"filesab", "";
"plugins", "stdfiles, meta";
"builddepends", "";
"buildtools", "";
"create_section", "l";
"name", "libtest";
"path", ".";
"build", "";
"install", "";
"datafiles", "";
"builddepends", "";
"buildtools", "";
"compiledobject", "";
"csources", "";
"ccopt", "";
"cclib", "";
"dlllib", "";
"dllpath", "";
"byteopt", "";
"nativeopt", "";
"modules", "";
"internalmodules", "";
"pack", "";
"findlibparent", "";
"findlibname", "";
"findlibcontainers", "";
"xmetaenable", "";
"xmetaextralines", "";
"xmetadescription", "this is a test";
"xmetatype", "syntax";
"xmetarequires", "test.syntax";
"create_section", "l";
"name", "libtest2";
"path", ".";
"build", "";
"install", "";
"datafiles", "";
"builddepends", "";
"buildtools", "";
"compiledobject", "";
"csources", "";
"ccopt", "";
"cclib", "";
"dlllib", "";
"dllpath", "";
"byteopt", "";
"nativeopt", "";
"modules", "";
"internalmodules", "";
"pack", "";
"findlibparent", "libtest";
"findlibname", "";
"findlibcontainers", "";
"xmetaenable", "";
"xmetaextralines", "";
"xmetadescription", "this is a test v2";
"xmetatype", "";
"xmetarequires", "test.syntax2";
"create_section", "n";
"end", "w";
"xstdfilesreadme", "";
"xstdfilesreadmefilename", "foo";
"xstdfilesinstall", "";
"xstdfilesinstallfilename", "bar";
"xstdfilesauthors", "";
"xstdfilesauthorsfilename", "baz";
],
(fun pkg ->
let libtest =
let res =
OASISSection.section_find
(`Library, "libtest")
pkg.sections
in
OASISSection.section_common res
in
let meta =
METAPlugin.generator libtest.cs_data
in
assert_equal
~msg:"field name"
~printer:(fun s -> s)
"test" pkg.name;
assert_equal
~msg:"field XMETAType"
~printer:(function
| METAPlugin.METASyntax -> "syntax"
| METAPlugin.METALibrary -> "library")
METAPlugin.METASyntax
meta.METAPlugin.meta_type;
assert_equal
~msg:"field XMETADescription"
~printer:(function
| Some s -> s
| None -> "<none>")
(Some "this is a test")
meta.METAPlugin.description;
assert_equal
~msg:"field XMETARequires"
~printer:(function
| Some lst -> String.concat ", " lst
| None -> "<none>")
(Some ["test.syntax"])
meta.METAPlugin.requires;
);
])
@
[
"error" >::
(fun () ->
with_quickstart_spawn
[]
(fun t ->
let q = `Prefix ("???name "), true in
assert_equal
~msg:"First question"
true
(expect t [q] false);
send t "\n";
assert_equal
~msg:"Second question"
true
(expect t [q] false))
(if Sys.os_type = "Win32" then
(Unix.WEXITED 2)
else
(Unix.WSIGNALED ~-8)));
]