5 // Marek Safar (marek.safar@gmail.com)
9 // Copyright (C) 2008, 2009 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Diagnostics;
34 using System.Reflection;
36 using System.Collections;
39 namespace TestRunner {
43 string Output { get; }
44 bool Invoke (string[] args);
45 bool IsWarning (int warningNumber);
48 class ReflectionTester: ITester {
54 public ReflectionTester (Assembly a)
56 Type t = a.GetType ("Mono.CSharp.CompilerCallableEntryPoint");
59 Console.Error.WriteLine ("null, huh?");
61 ep = t.GetMethod ("InvokeCompiler",
62 BindingFlags.Static | BindingFlags.Public);
64 throw new MissingMethodException ("static InvokeCompiler");
65 method_arg = new object [2];
67 PropertyInfo pi = t.GetProperty ("AllWarningNumbers");
68 all_warnings = (int[])pi.GetValue (null, null);
69 Array.Sort (all_warnings);
72 public string Output {
74 return output.GetStringBuilder ().ToString ();
78 public bool Invoke(string[] args)
80 output = new StringWriter ();
81 method_arg [0] = args;
82 method_arg [1] = output;
83 return (bool)ep.Invoke (null, method_arg);
86 public bool IsWarning (int warningNumber)
88 return Array.BinarySearch (all_warnings, warningNumber) >= 0;
93 class ProcessTester: ITester
98 public ProcessTester (string p_path)
100 pi = new ProcessStartInfo ();
101 pi.FileName = p_path;
102 pi.CreateNoWindow = true;
103 pi.WindowStyle = ProcessWindowStyle.Hidden;
104 pi.RedirectStandardOutput = true;
105 pi.RedirectStandardError = true;
106 pi.UseShellExecute = false;
109 public string Output {
115 public bool Invoke(string[] args)
117 StringBuilder sb = new StringBuilder ("/nologo ");
118 foreach (string s in args) {
122 pi.Arguments = sb.ToString ();
123 Process p = Process.Start (pi);
124 output = p.StandardError.ReadToEnd ();
125 if (output.Length == 0)
126 output = p.StandardOutput.ReadToEnd ();
128 return p.ExitCode == 0;
131 public bool IsWarning (int warningNumber)
133 throw new NotImplementedException ();
138 class TestCase : MarshalByRefObject
140 public readonly string FileName;
141 public readonly string[] CompilerOptions;
142 public readonly string[] Dependencies;
144 public TestCase (string filename, string[] options, string[] deps)
146 this.FileName = filename;
147 this.CompilerOptions = options;
148 this.Dependencies = deps;
152 class PositiveTestCase : TestCase
154 public class VerificationData : MarshalByRefObject
156 public class MethodData : MarshalByRefObject
158 public MethodData (MethodBase mi, int il_size)
160 this.Type = mi.DeclaringType.ToString ();
161 this.MethodName = mi.ToString ();
162 this.ILSize = il_size;
165 public MethodData (string type_name, string method_name, int il_size)
167 this.Type = type_name;
168 this.MethodName = method_name;
169 this.ILSize = il_size;
173 public string MethodName;
179 public bool IsNewSet;
181 public VerificationData (string test_file)
184 this.test_file = test_file;
191 public static VerificationData FromFile (string name, XmlReader r)
193 VerificationData tc = new VerificationData (name);
194 ArrayList methods = new ArrayList ();
196 while (r.ReadToNextSibling ("type")) {
197 string type_name = r ["name"];
199 while (r.ReadToNextSibling ("method")) {
200 string m_name = r ["name"];
202 r.ReadToDescendant ("size");
203 int il_size = r.ReadElementContentAsInt ();
204 methods.Add (new MethodData (type_name, m_name, il_size));
210 tc.methods = methods;
214 public void WriteCodeInfoTo (XmlWriter w)
216 w.WriteStartElement ("test");
217 w.WriteAttributeString ("name", test_file);
220 foreach (MethodData data in methods) {
224 if (type != data.Type) {
226 w.WriteEndElement ();
229 w.WriteStartElement ("type");
230 w.WriteAttributeString ("name", type);
233 w.WriteStartElement ("method");
234 w.WriteAttributeString ("name", data.MethodName);
235 w.WriteStartElement ("size");
236 w.WriteValue (data.ILSize);
237 w.WriteEndElement ();
238 w.WriteEndElement ();
242 w.WriteEndElement ();
244 w.WriteEndElement ();
248 public MethodData FindMethodData (string method_name, string declaring_type)
253 foreach (MethodData md in methods) {
254 if (md.MethodName == method_name && md.Type == declaring_type)
261 public void AddNewMethod (MethodBase mb, int il_size)
264 methods = new ArrayList ();
266 MethodData md = new MethodData (mb, il_size);
272 VerificationData verif_data;
274 public PositiveTestCase (string filename, string [] options, string [] deps)
275 : base (filename, options, deps)
279 public void CreateNewTest ()
281 verif_data = new VerificationData (FileName);
282 verif_data.IsNewSet = true;
285 public VerificationData VerificationProvider {
295 class Checker: MarshalByRefObject, IDisposable
297 protected ITester tester;
298 protected int success;
300 protected int ignored;
301 protected int syntax_errors;
303 StreamWriter log_file;
304 protected string[] extra_compiler_options;
305 // protected string[] compiler_options;
306 // protected string[] dependencies;
308 protected ArrayList tests = new ArrayList ();
309 protected Hashtable test_hash = new Hashtable ();
310 protected ArrayList regression = new ArrayList ();
311 protected ArrayList know_issues = new ArrayList ();
312 protected ArrayList ignore_list = new ArrayList ();
313 protected ArrayList no_error_list = new ArrayList ();
315 protected bool verbose;
316 protected bool safe_execution;
318 int total_known_issues;
320 protected Checker (ITester tester)
322 this.tester = tester;
325 public string IssueFile {
327 this.issue_file = value;
328 ReadWrongErrors (issue_file);
332 public string LogFile {
334 this.log_file = new StreamWriter (value, false);
338 public bool Verbose {
344 public bool SafeExecution {
346 safe_execution = value;
350 public string[] ExtraCompilerOptions {
352 extra_compiler_options = value;
356 protected virtual bool GetExtraOptions (string file, out string[] compiler_options,
357 out string[] dependencies)
360 compiler_options = null;
363 using (StreamReader sr = new StreamReader (file)) {
365 while (row++ < 3 && (line = sr.ReadLine()) != null) {
366 if (!AnalyzeTestFile (file, ref row, line, ref compiler_options,
377 protected virtual bool AnalyzeTestFile (string file, ref int row, string line,
378 ref string[] compiler_options,
379 ref string[] dependencies)
381 const string options = "// Compiler options:";
382 const string depends = "// Dependencies:";
385 compiler_options = null;
389 int index = line.IndexOf (options);
391 compiler_options = line.Substring (index + options.Length).Trim().Split (' ');
392 for (int i = 0; i < compiler_options.Length; i++)
393 compiler_options[i] = compiler_options[i].TrimStart ();
395 index = line.IndexOf (depends);
397 dependencies = line.Substring (index + depends.Length).Trim().Split (' ');
398 for (int i = 0; i < dependencies.Length; i++)
399 dependencies[i] = dependencies[i].TrimStart ();
405 public bool Do (string filename)
407 if (test_hash.Contains (filename))
411 Log (filename + "...\t");
413 if (ignore_list.Contains (filename)) {
415 LogFileLine (filename, "NOT TESTED");
419 string[] compiler_options, dependencies;
420 if (!GetExtraOptions (filename, out compiler_options, out dependencies)) {
421 LogFileLine (filename, "ERROR");
425 if (extra_compiler_options != null) {
426 if (compiler_options == null)
427 compiler_options = extra_compiler_options;
429 string[] new_options = new string [compiler_options.Length + extra_compiler_options.Length];
430 extra_compiler_options.CopyTo (new_options, 0);
431 compiler_options.CopyTo (new_options, extra_compiler_options.Length);
432 compiler_options = new_options;
436 TestCase test = CreateTestCase (filename, compiler_options, dependencies);
437 test_hash.Add (filename, test);
440 if (dependencies != null) {
441 foreach (string dependency in dependencies) {
442 if (!Do (dependency)) {
443 LogFileLine (filename, "DEPENDENCY FAILED");
454 protected virtual bool Check (TestCase test)
458 if (test.CompilerOptions != null) {
459 test_args = new string [2 + test.CompilerOptions.Length];
460 test.CompilerOptions.CopyTo (test_args, 0);
462 test_args = new string [2];
464 test_args [test_args.Length - 2] = test.FileName;
465 test_args [test_args.Length - 1] = "-debug";
467 return tester.Invoke (test_args);
470 protected virtual TestCase CreateTestCase (string filename, string [] options, string [] deps)
472 return new TestCase (filename, options, deps);
475 void ReadWrongErrors (string file)
477 const string ignored = "IGNORE";
478 const string no_error = "NO ERROR";
480 using (StreamReader sr = new StreamReader (file)) {
482 while ((line = sr.ReadLine()) != null) {
483 if (line.StartsWith ("#"))
486 ArrayList active_cont = know_issues;
488 if (line.IndexOf (ignored) > 0)
489 active_cont = ignore_list;
490 else if (line.IndexOf (no_error) > 0)
491 active_cont = no_error_list;
493 string file_name = line.Split (' ')[0];
494 if (file_name.Length == 0)
497 active_cont.Add (file_name);
500 total_known_issues = know_issues.Count;
503 protected virtual void PrintSummary ()
505 LogLine ("Done" + Environment.NewLine);
508 rate = (float) (success) / (float)total;
509 LogLine ("{0} test cases passed ({1:0.##%})", success, rate);
511 if (syntax_errors > 0)
512 LogLine ("{0} test(s) ignored because of wrong syntax !", syntax_errors);
515 LogLine ("{0} test(s) ignored", ignored);
517 if (total_known_issues - know_issues.Count > 0)
518 LogLine ("{0} known issue(s)", total_known_issues - know_issues.Count);
520 know_issues.AddRange (no_error_list);
521 if (know_issues.Count > 0) {
523 LogLine (issue_file + " contains {0} already fixed issues. Please remove", know_issues.Count);
524 foreach (string s in know_issues)
527 if (regression.Count > 0) {
529 LogLine ("The latest changes caused regression in {0} file(s)", regression.Count);
530 foreach (string s in regression)
535 public int ResultCode
538 return regression.Count == 0 ? 0 : 1;
542 protected void Log (string msg, params object [] rest)
544 Console.Write (msg, rest);
545 if (log_file != null)
546 log_file.Write (msg, rest);
549 protected void LogLine (string msg)
551 Console.WriteLine (msg);
552 if (log_file != null)
553 log_file.WriteLine (msg);
556 protected void LogLine (string msg, params object [] rest)
558 Console.WriteLine (msg, rest);
559 if (log_file != null)
560 log_file.WriteLine (msg, rest);
563 public void LogFileLine (string file, string msg, params object [] args)
566 string.Format (msg, args) :
567 file + "...\t" + string.Format (msg, args);
569 Console.WriteLine (s);
570 if (log_file != null)
571 log_file.WriteLine (s);
574 #region IDisposable Members
576 public void Dispose()
578 if (log_file != null)
584 public virtual void Initialize ()
588 public virtual void CleanUp ()
594 class PositiveChecker: Checker
596 readonly string files_folder;
597 readonly static object[] default_args = new object[1] { new string[] {} };
600 bool update_verif_file;
601 Hashtable verif_data;
606 readonly string mono;
608 public enum TestResult {
617 public PositiveChecker (ITester tester, string verif_file):
620 files_folder = Directory.GetCurrentDirectory ();
621 this.verif_file = verif_file;
624 pi = new ProcessStartInfo ();
625 pi.CreateNoWindow = true;
626 pi.WindowStyle = ProcessWindowStyle.Hidden;
627 pi.RedirectStandardOutput = true;
628 pi.RedirectStandardError = true;
629 pi.UseShellExecute = false;
631 mono = Environment.GetEnvironmentVariable ("MONO_RUNTIME");
638 public bool UpdateVerificationDataFile {
640 update_verif_file = value;
643 return update_verif_file;
647 protected override bool GetExtraOptions(string file, out string[] compiler_options,
648 out string[] dependencies) {
649 if (!base.GetExtraOptions (file, out compiler_options, out dependencies))
653 if (compiler_options == null)
656 foreach (string one_opt in compiler_options) {
657 if (one_opt.StartsWith ("-doc:")) {
658 doc_output = one_opt.Split (':', '/')[1];
664 class DomainTester : MarshalByRefObject
666 public bool CheckILSize (PositiveTestCase test, PositiveChecker checker, string file)
668 Assembly assembly = Assembly.LoadFile (file);
671 Type[] types = assembly.GetTypes ();
672 foreach (Type t in types) {
675 if (!t.IsClass && !t.IsValueType)
678 if (test.VerificationProvider == null) {
679 if (!checker.UpdateVerificationDataFile)
680 checker.LogFileLine (test.FileName, "Missing IL verification data");
681 test.CreateNewTest ();
684 foreach (MemberInfo m in t.GetMembers (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
685 MethodBase mi = m as MethodBase;
689 if ((mi.Attributes & (MethodAttributes.PinvokeImpl)) != 0)
692 success &= CompareIL (mi, test, checker);
699 bool CompareIL (MethodBase mi, PositiveTestCase test, PositiveChecker checker)
701 string m_name = mi.ToString ();
702 string decl_type = mi.DeclaringType.ToString ();
703 PositiveTestCase.VerificationData data_provider = test.VerificationProvider;
705 PositiveTestCase.VerificationData.MethodData md = data_provider.FindMethodData (m_name, decl_type);
707 data_provider.AddNewMethod (mi, GetILSize (mi));
708 if (!data_provider.IsNewSet) {
709 checker.HandleFailure (test.FileName, PositiveChecker.TestResult.ILError, decl_type + ": " + m_name + " (new method?)");
717 checker.HandleFailure (test.FileName, PositiveChecker.TestResult.ILError, decl_type + ": " + m_name + " has a duplicate");
723 int il_size = GetILSize (mi);
724 if (md.ILSize == il_size)
727 if (md.ILSize > il_size) {
728 checker.LogFileLine (test.FileName, "{0} (code size reduction {1} -> {2})", m_name, md.ILSize, il_size);
733 checker.HandleFailure (test.FileName, PositiveChecker.TestResult.ILError,
734 string.Format ("{0} (code size {1} -> {2})", m_name, md.ILSize, il_size));
741 static int GetILSize (MethodBase mi)
744 MethodBody body = mi.GetMethodBody ();
746 return body.GetILAsByteArray ().Length;
751 bool ExecuteFile (MethodInfo entry_point, string filename)
753 TextWriter stdout = Console.Out;
754 TextWriter stderr = Console.Error;
755 Console.SetOut (TextWriter.Null);
756 Console.SetError (TextWriter.Null);
757 ParameterInfo[] pi = entry_point.GetParameters ();
758 object[] args = pi.Length == 0 ? null : default_args;
760 object result = null;
763 result = entry_point.Invoke (null, args);
765 Console.SetOut (stdout);
766 Console.SetError (stderr);
768 } catch (Exception e) {
769 throw new ApplicationException (e.ToString ());
772 if (result is int && (int) result != 0)
773 throw new ApplicationException ("Wrong return code: " + result.ToString ());
778 public bool Test (string file)
780 Assembly assembly = Assembly.LoadFile (file);
781 return ExecuteFile (assembly.EntryPoint, file);
785 protected override bool Check(TestCase test)
787 string filename = test.FileName;
789 if (!base.Check (test)) {
790 HandleFailure (filename, TestResult.CompileError, tester.Output);
794 catch (Exception e) {
795 if (e.InnerException != null)
796 e = e.InnerException;
798 HandleFailure (filename, TestResult.CompileError, e.ToString ());
803 if (filename.EndsWith ("-lib.cs") || filename.EndsWith ("-mod.cs")) {
805 LogFileLine (filename, "OK");
810 string file = Path.Combine (files_folder, Path.GetFileNameWithoutExtension (filename) + ".exe");
812 // Enable .dll only tests (no execution required)
813 if (!File.Exists(file)) {
814 HandleFailure (filename, TestResult.Success, null);
818 AppDomain domain = null;
819 if (safe_execution) {
820 // Create a new AppDomain, with the current directory as the base.
821 AppDomainSetup setupInfo = new AppDomainSetup ();
822 setupInfo.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
823 setupInfo.LoaderOptimization = LoaderOptimization.SingleDomain;
824 domain = AppDomain.CreateDomain (Path.GetFileNameWithoutExtension (file), null, setupInfo);
831 tester = (DomainTester) domain.CreateInstanceAndUnwrap (typeof (PositiveChecker).Assembly.FullName, typeof (DomainTester).FullName);
833 tester = new DomainTester ();
835 if (!tester.Test (file))
838 } catch (ApplicationException e) {
839 HandleFailure (filename, TestResult.ExecError, e.Message);
841 } catch (Exception e) {
842 HandleFailure (filename, TestResult.LoadError, e.ToString ());
846 if (doc_output != null) {
847 string ref_file = filename.Replace (".cs", "-ref.xml");
850 XmlComparer.Compare (ref_file, doc_output);
852 } catch (Exception e) {
853 HandleFailure (filename, TestResult.XmlError, e.Message);
857 if (verif_file != null) {
858 PositiveTestCase pt = (PositiveTestCase) test;
859 pt.VerificationProvider = (PositiveTestCase.VerificationData) verif_data[filename];
861 if (!tester.CheckILSize (pt, this, file))
867 AppDomain.Unload (domain);
870 HandleFailure (filename, TestResult.Success, null);
874 protected override TestCase CreateTestCase (string filename, string [] options, string [] deps)
876 return new PositiveTestCase (filename, options, deps);
879 public void HandleFailure (string file, TestResult status, string extra)
882 case TestResult.Success:
884 if (know_issues.Contains (file)) {
885 LogFileLine (file, "FIXED ISSUE");
889 LogFileLine (file, "OK");
892 case TestResult.CompileError:
893 if (know_issues.Contains (file)) {
894 LogFileLine (file, "KNOWN ISSUE (Compilation error)");
895 know_issues.Remove (file);
898 LogFileLine (file, "REGRESSION (SUCCESS -> COMPILATION ERROR)");
901 case TestResult.ExecError:
902 if (know_issues.Contains (file)) {
903 LogFileLine (file, "KNOWN ISSUE (Execution error)");
904 know_issues.Remove (file);
907 LogFileLine (file, "REGRESSION (SUCCESS -> EXECUTION ERROR)");
910 case TestResult.XmlError:
911 if (know_issues.Contains (file)) {
912 LogFileLine (file, "KNOWN ISSUE (Xml comparision error)");
913 know_issues.Remove (file);
916 LogFileLine (file, "REGRESSION (SUCCESS -> DOCUMENTATION ERROR)");
919 case TestResult.LoadError:
920 LogFileLine (file, "REGRESSION (SUCCESS -> LOAD ERROR)");
923 case TestResult.ILError:
924 if (!update_verif_file) {
925 LogFileLine (file, "IL REGRESSION: " + extra);
932 LogLine ("{0}", extra);
934 if (!regression.Contains (file))
935 regression.Add (file);
938 public override void Initialize ()
940 if (verif_file != null) {
942 LoadVerificationData (verif_file);
944 throw new NotSupportedException ();
951 public override void CleanUp ()
955 if (update_verif_file) {
957 UpdateVerificationData (verif_file);
959 throw new NotSupportedException ();
965 void LoadVerificationData (string file)
967 LogLine ("Loading verification data from `{0}' ...", file);
969 using (XmlReader r = XmlReader.Create (file)) {
970 r.ReadStartElement ("tests");
971 verif_data = new Hashtable ();
974 if (r.Name != "test")
977 string name = r.GetAttribute ("name");
978 PositiveTestCase.VerificationData tc = PositiveTestCase.VerificationData.FromFile (name, r);
979 verif_data.Add (name, tc);
984 void UpdateVerificationData (string file)
986 LogLine ("Updating verification data `{0}' ...", file);
988 XmlWriterSettings s = new XmlWriterSettings ();
990 using (XmlWriter w = XmlWriter.Create (new StreamWriter (file, false, Encoding.UTF8), s)) {
991 w.WriteStartDocument ();
992 w.WriteComment ("This file contains expected IL and metadata produced by compiler for each test");
993 w.WriteStartElement ("tests");
994 foreach (PositiveTestCase tc in tests) {
995 if (tc.VerificationProvider != null)
996 tc.VerificationProvider.WriteCodeInfoTo (w);
998 w.WriteEndElement ();
1004 class NegativeChecker: Checker
1006 string expected_message;
1007 string error_message;
1009 bool check_error_line;
1011 IDictionary wrong_warning;
1013 protected enum CompilerError {
1022 public NegativeChecker (ITester tester, bool check_msg):
1025 this.check_msg = check_msg;
1026 wrong_warning = new Hashtable ();
1029 protected override bool AnalyzeTestFile (string file, ref int row, string line,
1030 ref string[] compiler_options,
1031 ref string[] dependencies)
1034 expected_message = null;
1036 int index = line.IndexOf (':');
1037 if (index == -1 || index > 15) {
1038 LogFileLine (file, "IGNORING: Wrong test file syntax (missing error mesage text)");
1040 base.AnalyzeTestFile (file, ref row, line, ref compiler_options,
1045 expected_message = line.Substring (index + 1).Trim ();
1049 string filtered = line.Replace(" ", "");
1051 // Some error tests require to have different error text for different runtimes.
1052 if (filtered.StartsWith ("//GMCS")) {
1057 return AnalyzeTestFile(file, ref row, line, ref compiler_options, ref dependencies);
1061 check_error_line = !filtered.StartsWith ("//Line:0");
1063 if (!filtered.StartsWith ("//Line:")) {
1064 LogFileLine (file, "IGNORING: Wrong test syntax (following line after an error messsage must have `// Line: xx' syntax");
1070 if (!base.AnalyzeTestFile (file, ref row, line, ref compiler_options, ref dependencies))
1074 if (compiler_options != null) {
1075 foreach (string s in compiler_options) {
1076 if (s.StartsWith ("-warnaserror") || s.StartsWith ("/warnaserror"))
1084 protected override bool Check (TestCase test)
1086 string filename = test.FileName;
1089 while (Char.IsLetter (filename, start_char))
1092 int end_char = filename.IndexOfAny (new char [] { '-', '.' } );
1093 string expected = filename.Substring (start_char, end_char - start_char);
1096 if (base.Check (test)) {
1097 HandleFailure (filename, CompilerError.Missing);
1101 catch (Exception e) {
1102 HandleFailure (filename, CompilerError.Missing);
1103 if (e.InnerException != null)
1104 e = e.InnerException;
1106 Log (e.ToString ());
1110 int err_id = int.Parse (expected, System.Globalization.CultureInfo.InvariantCulture);
1111 if (tester.IsWarning (err_id)) {
1113 wrong_warning [err_id] = true;
1116 wrong_warning [err_id] = false;
1119 CompilerError result_code = GetCompilerError (expected, tester.Output);
1120 if (HandleFailure (filename, result_code)) {
1125 if (result_code == CompilerError.Wrong)
1126 LogLine (tester.Output);
1131 CompilerError GetCompilerError (string expected, string buffer)
1133 const string error_prefix = "CS";
1134 const string ignored_error = "error CS5001";
1135 string tested_text = "error " + error_prefix + expected;
1136 StringReader sr = new StringReader (buffer);
1137 string line = sr.ReadLine ();
1138 ArrayList ld = new ArrayList ();
1139 CompilerError result = CompilerError.Missing;
1140 while (line != null) {
1141 if (ld.Contains (line)) {
1142 if (line.IndexOf ("Location of the symbol related to previous") == -1)
1143 return CompilerError.Duplicate;
1147 if (result != CompilerError.Expected) {
1148 if (line.IndexOf (tested_text) != -1) {
1150 int first = line.IndexOf (':');
1151 int second = line.IndexOf (':', first + 1);
1152 if (second == -1 || !check_error_line)
1155 string msg = line.Substring (second + 1).TrimEnd ('.').Trim ();
1156 if (msg != expected_message && msg != expected_message.Replace ('`', '\'')) {
1157 error_message = msg;
1158 return CompilerError.WrongMessage;
1161 if (check_error_line && line.IndexOf (".cs(") == -1)
1162 return CompilerError.MissingLocation;
1164 result = CompilerError.Expected;
1165 } else if (line.IndexOf (error_prefix) != -1 &&
1166 line.IndexOf (ignored_error) == -1)
1167 result = CompilerError.Wrong;
1170 line = sr.ReadLine ();
1176 bool HandleFailure (string file, CompilerError status)
1179 case CompilerError.Expected:
1180 if (know_issues.Contains (file) || no_error_list.Contains (file)) {
1181 LogFileLine (file, "FIXED ISSUE");
1186 LogFileLine (file, "OK");
1189 case CompilerError.Wrong:
1190 if (know_issues.Contains (file)) {
1191 LogFileLine (file, "KNOWN ISSUE (Wrong error reported)");
1192 know_issues.Remove (file);
1195 if (no_error_list.Contains (file)) {
1196 LogFileLine (file, "REGRESSION (NO ERROR -> WRONG ERROR CODE)");
1197 no_error_list.Remove (file);
1200 LogFileLine (file, "REGRESSION (CORRECT ERROR -> WRONG ERROR CODE)");
1204 case CompilerError.WrongMessage:
1205 if (know_issues.Contains (file)) {
1206 LogFileLine (file, "KNOWN ISSUE (Wrong error message reported)");
1207 know_issues.Remove (file);
1210 if (no_error_list.Contains (file)) {
1211 LogFileLine (file, "REGRESSION (NO ERROR -> WRONG ERROR MESSAGE)");
1212 no_error_list.Remove (file);
1215 LogFileLine (file, "REGRESSION (CORRECT ERROR -> WRONG ERROR MESSAGE)");
1216 LogLine ("Exp: {0}", expected_message);
1217 LogLine ("Was: {0}", error_message);
1221 case CompilerError.Missing:
1222 if (no_error_list.Contains (file)) {
1223 LogFileLine (file, "KNOWN ISSUE (No error reported)");
1224 no_error_list.Remove (file);
1228 if (know_issues.Contains (file)) {
1229 LogFileLine (file, "REGRESSION (WRONG ERROR -> NO ERROR)");
1230 know_issues.Remove (file);
1233 LogFileLine (file, "REGRESSION (CORRECT ERROR -> NO ERROR)");
1238 case CompilerError.MissingLocation:
1239 if (know_issues.Contains (file)) {
1240 LogFileLine (file, "KNOWN ISSUE (Missing error location)");
1241 know_issues.Remove (file);
1244 if (no_error_list.Contains (file)) {
1245 LogFileLine (file, "REGRESSION (NO ERROR -> MISSING ERROR LOCATION)");
1246 no_error_list.Remove (file);
1249 LogFileLine (file, "REGRESSION (CORRECT ERROR -> MISSING ERROR LOCATION)");
1253 case CompilerError.Duplicate:
1254 // Will become an error soon
1255 LogFileLine (file, "WARNING: EXACTLY SAME ERROR HAS BEEN ISSUED MULTIPLE TIMES");
1259 regression.Add (file);
1263 protected override void PrintSummary()
1265 base.PrintSummary ();
1267 if (wrong_warning.Count > 0) {
1269 LogLine ("List of incorectly defined warnings (they should be either defined in the compiler as a warning or a test-case has redundant `warnaserror' option)");
1271 foreach (DictionaryEntry de in wrong_warning)
1272 LogLine ("CS{0:0000} : {1}", de.Key, (bool)de.Value ? "incorrect warning definition" : "missing warning definition");
1280 static int Main(string[] args)
1284 if (GetOption ("help", args, false, out temp)) {
1290 if (!GetOption ("compiler", args, true, out compiler)) {
1297 Console.WriteLine ("Loading " + compiler + " ...");
1298 tester = new ReflectionTester (Assembly.LoadFile (compiler));
1304 Console.Error.WriteLine ("Switching to command line mode (compiler entry point was not found)");
1305 if (!File.Exists (compiler)) {
1306 Console.Error.WriteLine ("ERROR: Tested compiler was not found");
1309 tester = new ProcessTester (compiler);
1314 if (!GetOption ("mode", args, true, out mode)) {
1322 checker = new NegativeChecker (tester, true);
1326 GetOption ("il", args, false, out iltest);
1327 checker = new PositiveChecker (tester, iltest);
1329 if (iltest != null && GetOption ("update-il", args, false, out temp)) {
1330 ((PositiveChecker) checker).UpdateVerificationDataFile = true;
1335 Console.Error.WriteLine ("Invalid -mode argument");
1340 if (GetOption ("issues", args, true, out temp))
1341 checker.IssueFile = temp;
1342 if (GetOption ("log", args, true, out temp))
1343 checker.LogFile = temp;
1344 if (GetOption ("verbose", args, false, out temp))
1345 checker.Verbose = true;
1346 if (GetOption ("safe-execution", args, false, out temp))
1347 checker.SafeExecution = true;
1348 if (GetOption ("compiler-options", args, true, out temp)) {
1349 string[] extra = temp.Split (' ');
1350 checker.ExtraCompilerOptions = extra;
1353 string test_pattern;
1354 if (!GetOption ("files", args, true, out test_pattern)) {
1359 ArrayList files = new ArrayList ();
1360 switch (test_pattern) {
1362 files.AddRange (Directory.GetFiles (".", "test*.cs"));
1365 files.AddRange (Directory.GetFiles (".", "gtest*.cs"));
1368 files.AddRange (Directory.GetFiles (".", "dtest*.cs"));
1371 files.AddRange (Directory.GetFiles (".", test_pattern));
1375 if (files.Count == 0) {
1376 Console.Error.WriteLine ("No files matching `{0}' found", test_pattern);
1380 checker.Initialize ();
1382 foreach (string s in files) {
1383 string filename = Path.GetFileName (s);
1384 if (Char.IsUpper (filename, 0)) { // Windows hack
1388 if (filename.EndsWith ("-p2.cs"))
1391 checker.Do (filename);
1398 return checker.ResultCode;
1401 static bool GetOption (string opt, string[] args, bool req_arg, out string value)
1404 foreach (string a in args) {
1405 if (a.StartsWith (opt)) {
1406 int sep = a.IndexOf (':');
1408 value = a.Substring (sep + 1);
1412 Console.Error.WriteLine ("Missing argument in option " + opt);
1425 static void Usage ()
1428 "Mono compiler tester, (C) 2009 Novell, Inc.\n" +
1429 "compiler-tester -mode:[pos|neg] -compiler:FILE -files:file-list [options]\n" +
1431 " -compiler:FILE The file which will be used to compiler tests\n" +
1432 " -compiler-options:OPTIONS Add global compiler options\n" +
1433 " -il:IL-FILE XML file with expected IL details for each test\n" +
1434 " -issues:FILE The list of expected failures\n" +
1435 " -log:FILE Writes any output also to the file\n" +
1436 " -help Lists all options\n" +
1437 " -mode:[pos|neg] Specifies compiler test mode\n" +
1438 " -safe-execution Runs compiled executables in separate app-domain\n" +
1439 " -update-il Updates IL-FILE to match compiler output\n" +
1440 " -verbose Prints more details during testing\n"