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
|
using System;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using System.Collections;
namespace TestRunner {
interface ITester
{
string Output { get; }
bool Invoke (string[] args);
}
class ReflectionTester: ITester {
MethodInfo ep;
object[] method_arg;
StringWriter output;
public ReflectionTester (Assembly a)
{
ep = a.GetType ("Mono.CSharp.CompilerCallableEntryPoint").GetMethod ("InvokeCompiler",
BindingFlags.Static | BindingFlags.Public);
if (ep == null)
throw new MissingMethodException ("static InvokeCompiler");
method_arg = new object [2];
}
public string Output {
get {
return output.GetStringBuilder ().ToString ();
}
}
public bool Invoke(string[] args)
{
output = new StringWriter ();
method_arg [0] = args;
method_arg [1] = output;
return (bool)ep.Invoke (null, method_arg);
}
}
class ProcessTester: ITester
{
ProcessStartInfo pi;
string output;
public ProcessTester (string p_path)
{
pi = new ProcessStartInfo ();
pi.FileName = p_path;
pi.CreateNoWindow = true;
pi.WindowStyle = ProcessWindowStyle.Hidden;
pi.RedirectStandardOutput = true;
pi.RedirectStandardError = true;
pi.UseShellExecute = false;
}
public string Output {
get {
return output;
}
}
public bool Invoke(string[] args)
{
StringBuilder sb = new StringBuilder ();
foreach (string s in args) {
sb.Append (s);
sb.Append (" ");
}
pi.Arguments = sb.ToString ();
Process p = Process.Start (pi);
output = p.StandardError.ReadToEnd ();
if (output.Length == 0)
output = p.StandardOutput.ReadToEnd ();
p.WaitForExit ();
return p.ExitCode == 0;
}
}
class Checker: IDisposable
{
protected ITester tester;
protected int success;
int total;
string issue_file;
StreamWriter log_file;
protected ArrayList regression = new ArrayList ();
protected ArrayList know_issues = new ArrayList ();
protected ArrayList ignore_list = new ArrayList ();
protected ArrayList no_error_list = new ArrayList ();
protected Checker (ITester tester, string log_file, string issue_file)
{
this.tester = tester;
this.issue_file = issue_file;
ReadWrongErrors (issue_file);
this.log_file = new StreamWriter (log_file, false);
}
protected virtual string[] GetExtraOptions (string file)
{
const string options = "// Compiler options:";
int row = 0;
using (StreamReader sr = new StreamReader (file)) {
String line;
while (row++ < 3 && (line = sr.ReadLine()) != null) {
int index = line.IndexOf (options);
if (index != -1) {
string[] o = line.Substring (index + options.Length).Trim().Split (' ');
for (int i = 0; i < o.Length; i++)
o [i] = o[i].TrimStart ();
return o;
}
}
}
return null;
}
public void Do (string filename)
{
Log (filename);
Log ("...\t");
if (ignore_list.Contains (filename)) {
LogLine ("NOT TESTED");
return;
}
++total;
Check (filename);
}
protected virtual bool Check (string filename)
{
string[] extra = GetExtraOptions (filename);
string[] test_args;
if (extra != null) {
test_args = new string [1 + extra.Length];
extra.CopyTo (test_args, 0);
} else {
test_args = new string [1];
}
test_args [test_args.Length - 1] = filename;
return tester.Invoke (test_args);
}
void ReadWrongErrors (string file)
{
const string ignored = "IGNORE";
const string no_error = "NO ERROR";
using (StreamReader sr = new StreamReader (file)) {
string line;
while ((line = sr.ReadLine()) != null) {
if (line.StartsWith ("#"))
continue;
ArrayList active_cont = know_issues;
if (line.IndexOf (ignored) > 0)
active_cont = ignore_list;
else if (line.IndexOf (no_error) > 0)
active_cont = no_error_list;
string file_name = line.Split (' ')[0];
if (file_name.Length == 0)
continue;
active_cont.Add (file_name);
}
}
}
public void PrintSummary ()
{
LogLine ("Done" + Environment.NewLine);
LogLine ("{0} test cases passed ({1:.##%})", success, (float) (success) / (float)total);
know_issues.AddRange (no_error_list);
if (know_issues.Count > 0) {
LogLine ("");
LogLine (issue_file + " contains {0} already fixed issues. Please remove", know_issues.Count);
foreach (string s in know_issues)
LogLine (s);
}
if (regression.Count > 0) {
LogLine ("");
LogLine ("The latest changes caused regression in {0} file(s)", regression.Count);
foreach (string s in regression)
LogLine (s);
}
}
public int ResultCode
{
get {
return regression.Count == 0 ? 0 : 1;
}
}
protected void Log (string msg, params object [] rest)
{
Console.Write (msg, rest);
log_file.Write (msg, rest);
}
protected void LogLine (string msg, params object [] rest)
{
Console.WriteLine (msg, rest);
log_file.WriteLine (msg, rest);
}
#region IDisposable Members
public void Dispose()
{
log_file.Close ();
}
#endregion
}
class PositiveChecker: Checker {
readonly string files_folder;
readonly static object[] default_args = new object[1] { new string[] {} };
string doc_output;
// When we cannot load assembly to domain we are automaticaly switching to process calling
bool appdomain_limit_reached;
ProcessStartInfo pi;
readonly string mono;
// This is really pain
// This number is highly experimental on my box I am not able to load more than 1000 files to domain
// Every files is there twice and we need some space for tests which reference assemblies
const int MAX_TESTS_IN_DOMAIN = 420;
int test_counter;
protected enum TestResult {
CompileError,
ExecError,
LoadError,
XmlError,
Success
}
public PositiveChecker (ITester tester, string log_file, string issue_file):
base (tester, log_file, issue_file)
{
files_folder = Directory.GetCurrentDirectory ();
pi = new ProcessStartInfo ();
pi.CreateNoWindow = true;
pi.WindowStyle = ProcessWindowStyle.Hidden;
pi.RedirectStandardOutput = true;
pi.RedirectStandardError = true;
pi.UseShellExecute = false;
mono = Environment.GetEnvironmentVariable ("MONO_RUNTIME");
if (mono != null) {
pi.FileName = mono;
}
}
protected override string[] GetExtraOptions(string file)
{
doc_output = null;
string[] opt = base.GetExtraOptions (file);
if (opt == null)
return null;
foreach (string one_opt in opt) {
if (one_opt.StartsWith ("-doc:")) {
doc_output = one_opt.Split (':')[1];
}
}
return opt;
}
protected override bool Check(string filename)
{
try {
if (!base.Check (filename)) {
HandleFailure (filename, TestResult.CompileError, null);
return false;
}
}
catch (Exception e) {
HandleFailure (filename, TestResult.CompileError, e.ToString ());
return false;
}
MethodInfo mi = null;
string file = Path.Combine (files_folder, Path.GetFileNameWithoutExtension (filename) + ".exe");
if (!appdomain_limit_reached) {
try {
mi = Assembly.LoadFile (file).EntryPoint;
if (test_counter++ > MAX_TESTS_IN_DOMAIN)
appdomain_limit_reached = true;
}
catch (FileNotFoundException) {
if (File.Exists (file)) {
Console.WriteLine ("APPDOMAIN LIMIT REACHED");
appdomain_limit_reached = true;
}
}
catch (Exception e) {
HandleFailure (filename, TestResult.LoadError, e.ToString ());
return false;
}
}
if (appdomain_limit_reached) {
if (!ExecuteFile (file, filename))
return false;
} else {
if (!ExecuteFile (mi, filename))
return false;
}
if (doc_output != null) {
string ref_file = filename.Replace (".cs", "-ref.xml");
try {
XmlComparer.Compare (ref_file, doc_output);
}
catch (Exception e) {
HandleFailure (filename, TestResult.XmlError, e.Message);
return false;
}
}
HandleFailure (filename, TestResult.Success, null);
return true;
}
bool ExecuteFile (string exe_name, string filename)
{
if (mono == null)
pi.FileName = exe_name;
else
pi.Arguments = exe_name;
Process p = Process.Start (pi);
p.WaitForExit ();
// TODO: How can I recognize return type void ?
if (p.ExitCode == 0)
return true;
HandleFailure (filename, TestResult.ExecError, "Wrong return code: " + p.ExitCode.ToString ());
return false;
}
bool ExecuteFile (MethodInfo entry_point, string filename)
{
TextWriter standart_ouput = Console.Out;
TextWriter standart_error = Console.Error;
Console.SetOut (TextWriter.Null);
Console.SetError (TextWriter.Null);
ParameterInfo[] pi = entry_point.GetParameters ();
object[] args = pi.Length == 0 ? null : default_args;
object result = null;
try {
result = entry_point.Invoke (null, args);
Console.SetOut (standart_ouput);
Console.SetError (standart_error);
}
catch (Exception e) {
Console.SetOut (standart_ouput);
Console.SetError (standart_error);
HandleFailure (filename, TestResult.ExecError, e.ToString ());
return false;
}
if (result is int && (int)result != 0) {
HandleFailure (filename, TestResult.ExecError, "Wrong return code: " + result.ToString ());
return false;
}
return true;
}
void HandleFailure (string file, TestResult status, string extra)
{
switch (status) {
case TestResult.Success:
success++;
if (know_issues.Contains (file)) {
LogLine ("FIXED ISSUE");
return;
}
LogLine ("OK");
return;
case TestResult.CompileError:
if (know_issues.Contains (file)) {
LogLine ("KNOWN ISSUE (Compilation error)");
know_issues.Remove (file);
return;
}
LogLine ("REGRESSION (SUCCESS -> COMPILATION ERROR)");
break;
case TestResult.ExecError:
if (know_issues.Contains (file)) {
LogLine ("KNOWN ISSUE (Execution error)");
know_issues.Remove (file);
return;
}
LogLine ("REGRESSION (SUCCESS -> EXECUTE ERROR)");
break;
case TestResult.XmlError:
if (know_issues.Contains (file)) {
LogLine ("KNOWN ISSUE (Xml comparision error)");
know_issues.Remove (file);
return;
}
LogLine ("REGRESSION (SUCCESS -> DOCUMENTATION ERROR)");
break;
case TestResult.LoadError:
LogLine ("REGRESSION (SUCCESS -> LOAD ERROR)");
break;
}
if (extra != null)
LogLine (extra);
regression.Add (file);
}
}
class NegativeChecker: Checker
{
protected enum CompilerError {
Expected,
Wrong,
Missing
}
public NegativeChecker (ITester tester, string log_file, string issue_file):
base (tester, log_file, issue_file)
{
}
protected override bool Check (string filename)
{
int start_char = 0;
while (Char.IsLetter (filename, start_char))
++start_char;
int end_char = filename.IndexOfAny (new char [] { '-', '.' } );
string expected = filename.Substring (start_char, end_char - start_char);
try {
if (base.Check (filename)) {
HandleFailure (filename, CompilerError.Missing);
return false;
}
}
catch (Exception e) {
HandleFailure (filename, CompilerError.Missing);
Log (e.ToString ());
return false;
}
CompilerError result_code = GetCompilerError (expected, tester.Output);
if (HandleFailure (filename, result_code)) {
success++;
return true;
}
LogLine (tester.Output);
return false;
}
CompilerError GetCompilerError (string expected, string buffer)
{
const string error_prefix = "CS";
const string ignored_error = "error CS5001";
string tested_text = "error " + error_prefix + expected;
StringReader sr = new StringReader (buffer);
string line = sr.ReadLine ();
bool any_error = false;
while (line != null) {
if (line.IndexOf (tested_text) != -1)
return CompilerError.Expected;
if (line.IndexOf (error_prefix) != -1 &&
line.IndexOf (ignored_error) == -1)
any_error = true;
line = sr.ReadLine ();
}
return any_error ? CompilerError.Wrong : CompilerError.Missing;
}
bool HandleFailure (string file, CompilerError status)
{
switch (status) {
case CompilerError.Expected:
if (know_issues.Contains (file) || no_error_list.Contains (file)) {
LogLine ("FIXED ISSUE");
return true;
}
LogLine ("OK");
return true;
case CompilerError.Wrong:
if (know_issues.Contains (file)) {
LogLine ("KNOWN ISSUE (Wrong error reported)");
know_issues.Remove (file);
return false;
}
if (no_error_list.Contains (file)) {
LogLine ("REGRESSION (NO ERROR -> WRONG ERROR)");
no_error_list.Remove (file);
}
else {
LogLine ("REGRESSION (CORRECT ERROR -> WRONG ERROR)");
}
break;
case CompilerError.Missing:
if (no_error_list.Contains (file)) {
LogLine ("KNOWN ISSUE (No error reported)");
no_error_list.Remove (file);
return false;
}
if (know_issues.Contains (file)) {
LogLine ("REGRESSION (WRONG ERROR -> NO ERROR)");
know_issues.Remove (file);
}
else {
LogLine ("REGRESSION (CORRECT ERROR -> NO ERROR)");
}
break;
}
regression.Add (file);
return false;
}
}
class Tester {
static int Main(string[] args) {
if (args.Length != 5) {
Console.Error.WriteLine ("Usage: TestRunner [negative|positive] test-pattern compiler know-issues log-file");
return 1;
}
string mode = args[0].ToLower ();
string test_pattern = args [1];
string mcs = args [2];
string issue_file = args [3];
string log_fname = args [4];
string[] files = Directory.GetFiles (".", test_pattern);
ITester tester;
try {
Console.WriteLine ("Loading: " + mcs);
tester = new ReflectionTester (Assembly.LoadFile (mcs));
}
catch (Exception) {
Console.Error.WriteLine ("Switching to command line mode (compiler entry point was not found)");
if (!File.Exists (mcs)) {
Console.Error.WriteLine ("ERROR: Tested compiler was not found");
return 1;
}
tester = new ProcessTester (mcs);
}
Checker checker;
switch (mode) {
case "negative":
checker = new NegativeChecker (tester, log_fname, issue_file);
break;
case "positive":
checker = new PositiveChecker (tester, log_fname, issue_file);
break;
default:
Console.Error.WriteLine ("You must specify testing mode (positive or negative)");
return 1;
}
foreach (string s in files) {
string filename = Path.GetFileName (s);
if (Char.IsUpper (filename, 0)) { // Windows hack
continue;
}
if (filename.EndsWith ("-p2.cs") || filename.EndsWith ("-lib.cs") || filename.EndsWith ("-mod.cs"))
continue;
checker.Do (filename);
}
checker.PrintSummary ();
checker.Dispose ();
return checker.ResultCode;
}
}
}
|