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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699 | -----------------------------------------------------------------------
-- util-tests - Helper for writing unit tests
-- Copyright (C) 2009, 2010, 2011, 2012, 2013, 2017, 2019, 2021, 2022, 2023 Stephane Carrez
-- Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-----------------------------------------------------------------------
with GNAT.Command_Line;
with GNAT.Regpat;
with GNAT.Traceback.Symbolic;
with Ada.Command_Line;
with Ada.Directories;
with Ada.IO_Exceptions;
with Ada.Text_IO;
with Ada.Calendar.Formatting;
with Ada.Exceptions;
with Ada.Containers;
with Util.Strings;
with Util.Measures;
with Util.Files;
with Util.Log.Loggers;
with Util.Processes;
with Util.Streams.Buffered;
with Util.Streams.Pipes;
package body Util.Tests is
Test_Properties : Util.Properties.Manager;
Log : constant Util.Log.Loggers.Logger := Util.Log.Loggers.Create ("Util.Tests");
-- When a test uses external test files to match a result against a well
-- defined content, it can be difficult to maintain those external files.
-- The <b>Assert_Equal_Files</b> can automatically maintain the reference
-- file by updating it with the lastest test result.
--
-- Of course, using this mode means the test does not validate anything.
Update_Test_Files : Boolean := False;
-- The default timeout for a test case execution.
Default_Timeout : Duration := 60.0;
-- A prefix that is added to the test class names. Adding a prefix is useful when
-- the same testsuite is executed several times with different configurations. It allows
-- to track and identify the tests in different environments and have a global view
-- in Jenkins. See option '-p prefix'.
Harness_Prefix : Unbounded_String;
-- Verbose flag activated by the '-v' option.
Verbose_Flag : Boolean := False;
-- When not empty, defines the name of the test that is enabled. Other tests are disabled.
-- This is initialized by the -r test option.
Enabled_Test : Unbounded_String;
-- ------------------------------
-- Execute the command and get the output in a string.
-- ------------------------------
procedure Execute (T : in out Test;
Command : in String;
Input : in String;
Output : in String;
Result : out Ada.Strings.Unbounded.Unbounded_String;
Working_Directory : in String;
Status : in Natural := 0;
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
P : aliased Util.Streams.Pipes.Pipe_Stream;
Buffer : Util.Streams.Buffered.Input_Buffer_Stream;
begin
if Input'Length > 0 then
Log.Info ("Execute: {0} < {1}", Command, Input);
elsif Output'Length > 0 then
Log.Info ("Execute: {0} > {1}", Command, Output);
else
Log.Info ("Execute: {0}", Command);
end if;
P.Set_Input_Stream (Input);
P.Set_Output_Stream (Output);
if Working_Directory'Length > 0 then
P.Set_Working_Directory (Working_Directory);
end if;
P.Open (Command, Util.Processes.READ_ALL);
-- Write on the process input stream.
Result := Ada.Strings.Unbounded.Null_Unbounded_String;
Buffer.Initialize (P'Unchecked_Access, 8192);
Buffer.Read (Result);
P.Close;
Ada.Text_IO.Put_Line (Ada.Strings.Unbounded.To_String (Result));
Log.Info ("Command result: {0}", Result);
Util.Tests.Assert_Equals (T, Status, P.Get_Exit_Status, "Command '" & Command & "' failed",
Source, Line);
end Execute;
procedure Execute (T : in out Test;
Command : in String;
Input : in String;
Output : in String;
Result : out Ada.Strings.Unbounded.Unbounded_String;
Status : in Natural := 0;
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
begin
T.Execute (Command, Input, Output, Result, "", Status, Source, Line);
end Execute;
procedure Execute (T : in out Test;
Command : in String;
Result : out Ada.Strings.Unbounded.Unbounded_String;
Status : in Natural := 0;
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
begin
T.Execute (Command, "", "", Result, "", Status, Source, Line);
end Execute;
procedure Execute (T : in out Test;
Command : in String;
Expect : in String;
Status : in Natural := 0;
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
Path : constant String := Util.Tests.Get_Path ("regtests/expect/" & Expect);
Output : constant String := Util.Tests.Get_Test_Path (Expect);
Result : Ada.Strings.Unbounded.Unbounded_String;
begin
T.Execute (Command, "", Output, Result, "", Status, Source, Line);
Util.Tests.Assert_Equal_Files (T, Path, Output, "Command '" & Command & "' invalid output",
Source, Line);
end Execute;
-- ------------------------------
-- Get a path to access a test file.
-- ------------------------------
function Get_Path (File : String) return String is
Dir : constant String := Get_Parameter ("test.dir", ".");
begin
return Dir & "/" & File;
end Get_Path;
-- ------------------------------
-- Get a path to create a test file.
-- ------------------------------
function Get_Test_Path (File : String) return String is
Dir : constant String := Get_Parameter ("test.result.dir", "regtests/results");
begin
return Dir & "/" & File;
end Get_Test_Path;
-- ------------------------------
-- Get the timeout for the test execution.
-- ------------------------------
function Get_Test_Timeout (Name : in String) return Duration is
Prop_Name : constant String := "test.timeout." & Name;
Value : constant String := Test_Properties.Get (Prop_Name,
Duration'Image (Default_Timeout));
begin
return Duration'Value (Value);
exception
when Constraint_Error =>
return Default_Timeout;
end Get_Test_Timeout;
-- ------------------------------
-- Get the testsuite harness prefix. This prefix is added to the test class name.
-- By default it is empty. It is allows to execute the test harness on different
-- environment (ex: MySQL or SQLlite) and be able to merge and collect the two result
-- sets together.
-- ------------------------------
function Get_Harness_Prefix return String is
begin
return To_String (Harness_Prefix);
end Get_Harness_Prefix;
-- ------------------------------
-- Get a test configuration parameter.
-- ------------------------------
function Get_Parameter (Name : String;
Default : String := "") return String is
begin
return Test_Properties.Get (Name, Default);
end Get_Parameter;
-- ------------------------------
-- Get the test configuration properties.
-- ------------------------------
function Get_Properties return Util.Properties.Manager is
begin
return Test_Properties;
end Get_Properties;
-- ------------------------------
-- Get a new unique string
-- ------------------------------
function Get_Uuid return String is
Time : constant Ada.Calendar.Time := Ada.Calendar.Clock;
Year : Ada.Calendar.Year_Number;
Month : Ada.Calendar.Month_Number;
Day : Ada.Calendar.Day_Number;
T : Ada.Calendar.Day_Duration;
V : Long_Long_Integer;
begin
Ada.Calendar.Split (Date => Time,
Year => Year,
Month => Month,
Day => Day,
Seconds => T);
V := (Long_Long_Integer (Year) * 365 * 24 * 3600 * 1000)
+ (Long_Long_Integer (Month) * 31 * 24 * 3600 * 1000)
+ (Long_Long_Integer (Day) * 24 * 3600 * 1000)
+ (Long_Long_Integer (T * 1000));
return "U" & Util.Strings.Image (V);
end Get_Uuid;
-- ------------------------------
-- Get the verbose flag that can be activated with the <tt>-v</tt> option.
-- ------------------------------
function Verbose return Boolean is
begin
return Verbose_Flag;
end Verbose;
-- ------------------------------
-- Returns True if the test with the given name is enabled.
-- By default all the tests are enabled. When the -r test option is passed
-- all the tests are disabled except the test specified by the -r option.
-- ------------------------------
function Is_Test_Enabled (Name : in String) return Boolean is
begin
return Length (Enabled_Test) = 0 or else Enabled_Test = Name;
end Is_Test_Enabled;
-- ------------------------------
-- Check that the value matches what we expect.
-- ------------------------------
procedure Assert_Equals (T : in Test'Class;
Expect, Value : in Ada.Calendar.Time;
Message : in String := "Test failed";
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
use Ada.Calendar.Formatting;
use Ada.Calendar;
begin
T.Assert (Condition => Image (Expect) = Image (Value),
Message => Message & ": expecting '" & Image (Expect) & "'"
& " value was '" & Image (Value) & "'",
Source => Source,
Line => Line);
end Assert_Equals;
-- ------------------------------
-- Check that the value matches what we expect.
-- ------------------------------
procedure Assert_Equals (T : in Test'Class;
Expect, Value : in String;
Message : in String := "Test failed";
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
begin
T.Assert (Condition => Expect = Value,
Message => Message & ": expecting '" & Expect & "'"
& " value was '" & Value & "'",
Source => Source,
Line => Line);
end Assert_Equals;
-- ------------------------------
-- Check that the value matches what we expect.
-- ------------------------------
procedure Assert_Equals (T : in Test'Class;
Expect : in String;
Value : in Unbounded_String;
Message : in String := "Test failed";
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
begin
Assert_Equals (T => T,
Expect => Expect,
Value => To_String (Value),
Message => Message,
Source => Source,
Line => Line);
end Assert_Equals;
-- ------------------------------
-- Check that the value matches the regular expression
-- ------------------------------
procedure Assert_Matches (T : in Test'Class;
Pattern : in String;
Value : in Unbounded_String;
Message : in String := "Test failed";
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
begin
Assert_Matches (T => T,
Pattern => Pattern,
Value => To_String (Value),
Message => Message,
Source => Source,
Line => Line);
end Assert_Matches;
-- ------------------------------
-- Check that the value matches the regular expression
-- ------------------------------
procedure Assert_Matches (T : in Test'Class;
Pattern : in String;
Value : in String;
Message : in String := "Test failed";
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
use GNAT.Regpat;
Regexp : constant Pattern_Matcher := Compile (Expression => Pattern,
Flags => Multiple_Lines);
begin
T.Assert (Condition => Match (Regexp, Value),
Message => Message & ". Value '" & Value & "': Does not Match '"
& Pattern & "'",
Source => Source,
Line => Line);
end Assert_Matches;
-- ------------------------------
-- Check that the file exists.
-- ------------------------------
procedure Assert_Exists (T : in Test'Class;
File : in String;
Message : in String := "Test failed";
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
begin
T.Assert (Condition => Ada.Directories.Exists (File),
Message => Message & ": file '" & File & "' does not exist",
Source => Source,
Line => Line);
end Assert_Exists;
-- ------------------------------
-- Check that two files are equal. This is intended to be used by
-- tests that create files that are then checked against patterns.
-- ------------------------------
procedure Assert_Equal_Files (T : in Test_Case'Class;
Expect : in String;
Test : in String;
Message : in String := "Test failed";
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
use Util.Files;
use type Ada.Containers.Count_Type;
use type Util.Strings.Vectors.Vector;
Expect_File : Util.Strings.Vectors.Vector;
Test_File : Util.Strings.Vectors.Vector;
Same : Boolean;
begin
begin
if not Ada.Directories.Exists (Expect) then
T.Assert (Condition => False,
Message => "Expect file '" & Expect & "' does not exist",
Source => Source, Line => Line);
end if;
Read_File (Path => Expect,
Into => Expect_File);
Read_File (Path => Test,
Into => Test_File);
exception
when others =>
if Update_Test_Files then
Ada.Directories.Copy_File (Source_Name => Test,
Target_Name => Expect);
else
raise;
end if;
end;
if Expect_File.Length /= Test_File.Length then
if Update_Test_Files then
Ada.Directories.Copy_File (Source_Name => Test,
Target_Name => Expect);
end if;
-- Check file sizes
Assert_Equals (T => T,
Expect => Natural (Expect_File.Length),
Value => Natural (Test_File.Length),
Message => Message & ": Invalid number of lines",
Source => Source,
Line => Line);
end if;
Same := Expect_File = Test_File;
if Same then
return;
end if;
if Update_Test_Files then
Ada.Directories.Copy_File (Source_Name => Test,
Target_Name => Expect);
end if;
T.Assert (Condition => False,
Message => Message & ": Content is different on some lines",
Source => Source,
Line => Line);
end Assert_Equal_Files;
-- ------------------------------
-- Check that two files are equal. This is intended to be used by
-- tests that create files that are then checked against patterns.
-- ------------------------------
procedure Assert_Equal_Files (T : in Test'Class;
Expect : in String;
Test : in String;
Message : in String := "Test failed";
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
use Util.Files;
use type Ada.Containers.Count_Type;
use type Util.Strings.Vectors.Vector;
Expect_File : Util.Strings.Vectors.Vector;
Test_File : Util.Strings.Vectors.Vector;
Same : Boolean;
begin
begin
if not Ada.Directories.Exists (Expect) then
T.Assert (Condition => False,
Message => "Expect file '" & Expect & "' does not exist",
Source => Source, Line => Line);
end if;
Read_File (Path => Expect,
Into => Expect_File);
Read_File (Path => Test,
Into => Test_File);
exception
when others =>
if Update_Test_Files then
Ada.Directories.Copy_File (Source_Name => Test,
Target_Name => Expect);
else
raise;
end if;
end;
if Expect_File.Length /= Test_File.Length then
if Update_Test_Files then
Ada.Directories.Copy_File (Source_Name => Test,
Target_Name => Expect);
end if;
-- Check file sizes
Assert_Equals (T => T,
Expect => Natural (Expect_File.Length),
Value => Natural (Test_File.Length),
Message => Message & ": Invalid number of lines",
Source => Source,
Line => Line);
end if;
Same := Expect_File = Test_File;
if Same then
return;
end if;
if Update_Test_Files then
Ada.Directories.Copy_File (Source_Name => Test,
Target_Name => Expect);
end if;
Fail (T => T,
Message => Message & ": Content is different on some lines",
Source => Source,
Line => Line);
end Assert_Equal_Files;
-- ------------------------------
-- Report a test failed.
-- ------------------------------
procedure Fail (T : in Test'Class;
Message : in String := "Test failed";
Source : in String := GNAT.Source_Info.File;
Line : in Natural := GNAT.Source_Info.Line) is
begin
T.Assert (False, Message, Source, Line);
end Fail;
procedure Assert_Equal_Vectors (T : in Test'Class;
Expect : in Util.Strings.Vectors.Vector;
List : in Util.Strings.Vectors.Vector;
Message : in String := "Test failed";
Source : String := GNAT.Source_Info.File;
Line : Natural := GNAT.Source_Info.Line) is
begin
Assert_Equals (T => T,
Expect => Natural (Expect.Length),
Value => Natural (List.Length),
Message => "Invalid length:" & Message,
Source => Source,
Line => Line);
for I in 1 .. Natural (Expect.Length) loop
Assert_Equals (T => T,
Expect => Expect.Element (I),
Value => List.Element (I),
Message => "Bad value" & I'Image & ":" & Message,
Source => Source,
Line => Line);
end loop;
end Assert_Equal_Vectors;
-- ------------------------------
-- Default initialization procedure.
-- ------------------------------
procedure Initialize_Test (Props : in Util.Properties.Manager) is
begin
null;
end Initialize_Test;
-- ------------------------------
-- The main testsuite program. This launches the tests, collects the
-- results, create performance logs and set the program exit status
-- according to the testsuite execution status.
--
-- The <b>Initialize</b> procedure is called before launching the unit tests. It is intended
-- to configure the tests according to some external environment (paths, database access).
--
-- The <b>Finish</b> procedure is called after the test suite has executed.
-- ------------------------------
procedure Harness (Name : in String) is
use GNAT.Command_Line;
use Ada.Text_IO;
use type Util.XUnit.Status;
procedure Help;
procedure Help is
begin
Put_Line ("Test harness: " & Name);
Put ("Usage: harness [-l label] [-xml result.xml] [-t timeout] [-p prefix] [-v]"
& "[-config file.properties] [-d dir] [-r testname]");
Put_Line ("[-update]");
Put_Line ("-l label Print the label in the test summary result");
Put_Line ("-xml file Produce an XML test report");
Put_Line ("-config file Specify a test configuration file");
Put_Line ("-d dir Change the current directory to <dir>");
Put_Line ("-t timeout Test execution timeout in seconds");
Put_Line ("-v Activate the verbose test flag");
Put_Line ("-p prefix Add the prefix to the test class names");
Put_Line ("-r testname Run only the tests for the given testsuite name");
Put_Line ("-update Update the test reference files if a file");
Put_Line (" is missing or the test generates another output");
Put_Line (" (See Assert_Equals_File)");
Ada.Command_Line.Set_Exit_Status (2);
end Help;
Perf : aliased Util.Measures.Measure_Set;
Result : Util.XUnit.Status;
XML : Boolean := False;
Output : Ada.Strings.Unbounded.Unbounded_String;
Chdir : Ada.Strings.Unbounded.Unbounded_String;
Label : String (1 .. 16) := (others => ' ');
begin
loop
case Getopt ("h u v l: x: t: p: c: config: d: r: update help xml: timeout:") is
when ASCII.NUL =>
exit;
when 'c' =>
declare
Name : constant String := Parameter;
begin
Test_Properties.Load_Properties (Name);
Default_Timeout := Get_Test_Timeout ("default");
exception
when Ada.IO_Exceptions.Name_Error =>
Ada.Text_IO.Put_Line ("Cannot find configuration file: " & Name);
Ada.Command_Line.Set_Exit_Status (2);
return;
end;
when 'd' =>
Chdir := To_Unbounded_String (Parameter);
when 'l' =>
if Parameter'Length > Label'Length then
Label := Parameter (Parameter'First .. Parameter'First + Label'Length - 1);
else
Label := (others => ' ');
Label (Label'First .. Label'First + Parameter'Length - 1) := Parameter;
end if;
when 'u' =>
Update_Test_Files := True;
when 't' =>
begin
Default_Timeout := Duration'Value (Parameter);
exception
when Constraint_Error =>
Ada.Text_IO.Put_Line ("Invalid timeout: " & Parameter);
Ada.Command_Line.Set_Exit_Status (2);
return;
end;
when 'r' =>
Enabled_Test := To_Unbounded_String (Parameter);
when 'p' =>
Harness_Prefix := To_Unbounded_String (Parameter & " ");
when 'v' =>
Verbose_Flag := True;
when 'x' =>
XML := True;
Output := To_Unbounded_String (Parameter);
when others =>
Help;
return;
end case;
end loop;
-- Initialization is optional. Get the log configuration by reading the property
-- file 'samples/log4j.properties'. The 'log.util' logger will use a DEBUG level
-- and write the message in 'result.log'.
Util.Log.Loggers.Initialize (Test_Properties);
Initialize (Test_Properties);
if Length (Chdir) /= 0 then
begin
Ada.Directories.Set_Directory (To_String (Chdir));
exception
when Ada.IO_Exceptions.Name_Error =>
Put_Line ("Invalid directory " & To_String (Chdir));
Ada.Command_Line.Set_Exit_Status (1);
return;
end;
end if;
declare
procedure Runner is new Util.XUnit.Harness (Suite);
S : Util.Measures.Stamp;
begin
Util.Measures.Set_Current (Perf'Unchecked_Access);
Runner (To_String (Output), XML,
(if (for all C of Label => C = ' ') then "" else Label), Result);
Util.Measures.Report (Perf, S, "Testsuite execution");
Util.Measures.Write (Perf, "Test measures", Name);
end;
Finish (Result);
-- Program exit status reflects the testsuite result
if Result /= Util.XUnit.Success then
Ada.Command_Line.Set_Exit_Status (1);
else
Ada.Command_Line.Set_Exit_Status (0);
end if;
exception
when Invalid_Switch =>
Put_Line ("Invalid Switch " & Full_Switch);
Help;
return;
when Invalid_Parameter =>
Put_Line ("No parameter for " & Full_Switch);
Help;
return;
when E : others =>
Put_Line ("Exception: " & Ada.Exceptions.Exception_Name (E));
Put_Line ("Message: " & Ada.Exceptions.Exception_Message (E));
Put_Line ("Stacktrace:");
Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
Ada.Command_Line.Set_Exit_Status (4);
end Harness;
end Util.Tests;
|