5 // Zoltan Varga (vargaz@gmail.com)
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
9 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 using System.Threading;
15 using System.Diagnostics;
16 using System.Collections.Generic;
17 using System.Globalization;
20 using System.Text.RegularExpressions;
23 using Mono.Unix.Native;
27 // This is a simple test runner with support for parallel execution
30 public class TestRunner
32 const string TEST_TIME_FORMAT = "mm\\:ss\\.fff";
33 const string ENV_TIMEOUT = "TEST_DRIVER_TIMEOUT_SEC";
34 const string MONO_PATH = "MONO_PATH";
38 public StringBuilder stdout, stderr;
39 public string stdoutName, stderrName;
43 public string test, opt_set;
46 public static int Main (String[] args) {
49 int timeout = 2 * 60; // in seconds
50 int expectedExitCode = 0;
51 string testsuiteName = null;
52 string inputFile = null;
54 string disabled_tests = null;
55 string runtime = "mono";
57 string mono_path = null;
58 var opt_sets = new List<string> ();
60 string aot_run_flags = null;
61 string aot_build_flags = null;
65 while (i < args.Length) {
66 if (args [i].StartsWith ("-")) {
67 if (args [i] == "-j") {
68 if (i + 1 >= args.Length) {
69 Console.WriteLine ("Missing argument to -j command line option.");
72 if (args [i + 1] == "a")
73 concurrency = Environment.ProcessorCount;
75 concurrency = Int32.Parse (args [i + 1]);
77 } else if (args [i] == "--timeout") {
78 if (i + 1 >= args.Length) {
79 Console.WriteLine ("Missing argument to --timeout command line option.");
82 timeout = Int32.Parse (args [i + 1]);
84 } else if (args [i] == "--disabled") {
85 if (i + 1 >= args.Length) {
86 Console.WriteLine ("Missing argument to --disabled command line option.");
89 disabled_tests = args [i + 1];
91 } else if (args [i] == "--runtime") {
92 if (i + 1 >= args.Length) {
93 Console.WriteLine ("Missing argument to --runtime command line option.");
96 runtime = args [i + 1];
98 } else if (args [i] == "--config") {
99 if (i + 1 >= args.Length) {
100 Console.WriteLine ("Missing argument to --config command line option.");
103 config = args [i + 1];
105 } else if (args [i] == "--opt-sets") {
106 if (i + 1 >= args.Length) {
107 Console.WriteLine ("Missing argument to --opt-sets command line option.");
110 foreach (var s in args [i + 1].Split ())
113 } else if (args [i] == "--expected-exit-code") {
114 if (i + 1 >= args.Length) {
115 Console.WriteLine ("Missing argument to --expected-exit-code command line option.");
118 expectedExitCode = Int32.Parse (args [i + 1]);
120 } else if (args [i] == "--testsuite-name") {
121 if (i + 1 >= args.Length) {
122 Console.WriteLine ("Missing argument to --testsuite-name command line option.");
125 testsuiteName = args [i + 1];
127 } else if (args [i] == "--input-file") {
128 if (i + 1 >= args.Length) {
129 Console.WriteLine ("Missing argument to --input-file command line option.");
132 inputFile = args [i + 1];
134 } else if (args [i] == "--runtime") {
135 if (i + 1 >= args.Length) {
136 Console.WriteLine ("Missing argument to --runtime command line option.");
139 runtime = args [i + 1];
141 } else if (args [i] == "--mono-path") {
142 if (i + 1 >= args.Length) {
143 Console.WriteLine ("Missing argument to --mono-path command line option.");
146 mono_path = args [i + 1].Substring(0, args [i + 1].Length);
149 } else if (args [i] == "--aot-run-flags") {
150 if (i + 1 >= args.Length) {
151 Console.WriteLine ("Missing argument to --aot-run-flags command line option.");
154 aot_run_flags = args [i + 1].Substring(0, args [i + 1].Length);
156 } else if (args [i] == "--aot-build-flags") {
157 if (i + 1 >= args.Length) {
158 Console.WriteLine ("Missing argument to --aot-build-flags command line option.");
161 aot_build_flags = args [i + 1].Substring(0, args [i + 1].Length);
164 Console.WriteLine ("Unknown command line option: '" + args [i] + "'.");
172 if (String.IsNullOrEmpty (testsuiteName)) {
173 Console.WriteLine ("Missing the required --testsuite-name command line option.");
177 var disabled = new Dictionary <string, string> ();
179 if (disabled_tests != null) {
180 foreach (string test in disabled_tests.Split ())
181 disabled [test] = test;
184 var tests = new List<string> ();
186 if (!String.IsNullOrEmpty (inputFile)) {
187 tests.AddRange (File.ReadAllLines (inputFile));
189 // The remaining arguments are the tests
190 for (int j = i; j < args.Length; ++j)
191 if (!disabled.ContainsKey (args [j]))
192 tests.Add (args [j]);
195 var passed = new List<ProcessData> ();
196 var failed = new List<ProcessData> ();
197 var timedout = new List<ProcessData> ();
199 object monitor = new object ();
201 Console.WriteLine ("Running tests: ");
203 var test_info = new Queue<TestInfo> ();
204 if (opt_sets.Count == 0) {
205 foreach (string s in tests)
206 test_info.Enqueue (new TestInfo { test = s });
208 foreach (string opt in opt_sets) {
209 foreach (string s in tests)
210 test_info.Enqueue (new TestInfo { test = s, opt_set = opt });
214 /* compute the max length of test names, to have an optimal output width */
215 int output_width = -1;
216 foreach (TestInfo ti in test_info) {
217 if (ti.test.Length > output_width)
218 output_width = Math.Min (120, ti.test.Length);
221 if (aot_build_flags != null) {
222 Console.WriteLine("AOT compiling tests");
224 object aot_monitor = new object ();
225 var aot_queue = new Queue<String> (tests);
227 List<Thread> build_threads = new List<Thread> (concurrency);
229 for (int j = 0; j < concurrency; ++j) {
230 Thread thread = new Thread (() => {
235 if (aot_queue.Count == 0)
237 test_name = aot_queue.Dequeue ();
240 string test_bitcode_output = test_name + "_bitcode_tmp";
241 string test_bitcode_arg = ",temp-path=" + test_bitcode_output;
242 string aot_args = aot_build_flags + test_bitcode_arg + " " + test_name;
244 Directory.CreateDirectory(test_bitcode_output);
246 ProcessStartInfo job = new ProcessStartInfo (runtime, aot_args);
247 job.UseShellExecute = false;
248 job.EnvironmentVariables[ENV_TIMEOUT] = timeout.ToString();
249 job.EnvironmentVariables[MONO_PATH] = mono_path;
250 Process compiler = new Process ();
251 compiler.StartInfo = job;
255 if (!compiler.WaitForExit (timeout * 1000)) {
260 throw new Exception(String.Format("Timeout AOT compiling tests, output in {0}", test_bitcode_output));
261 } else if (compiler.ExitCode != 0) {
262 throw new Exception(String.Format("Error AOT compiling tests, output in {0}", test_bitcode_output));
264 Directory.Delete (test_bitcode_output, true);
271 build_threads.Add (thread);
274 for (int j = 0; j < build_threads.Count; ++j)
275 build_threads [j].Join ();
277 Console.WriteLine("Done compiling");
280 List<Thread> threads = new List<Thread> (concurrency);
282 DateTime test_start_time = DateTime.UtcNow;
284 for (int j = 0; j < concurrency; ++j) {
285 Thread thread = new Thread (() => {
290 if (test_info.Count == 0)
292 ti = test_info.Dequeue ();
295 var output = new StringWriter ();
297 string test = ti.test;
298 string opt_set = ti.opt_set;
300 output.Write (String.Format ("{{0,-{0}}} ", output_width), test);
304 if (aot_run_flags != null)
305 test_invoke = aot_run_flags + " " + test;
309 /* Spawn a new process */
312 process_args = test_invoke;
314 process_args = "-O=" + opt_set + " " + test_invoke;
316 ProcessStartInfo info = new ProcessStartInfo (runtime, process_args);
317 info.UseShellExecute = false;
318 info.RedirectStandardOutput = true;
319 info.RedirectStandardError = true;
320 info.EnvironmentVariables[ENV_TIMEOUT] = timeout.ToString();
322 info.EnvironmentVariables["MONO_CONFIG"] = config;
323 if (mono_path != null)
324 info.EnvironmentVariables[MONO_PATH] = mono_path;
325 Process p = new Process ();
328 ProcessData data = new ProcessData ();
331 string log_prefix = "";
333 log_prefix = "." + opt_set.Replace ("-", "no").Replace (",", "_");
335 data.stdoutName = test + log_prefix + ".stdout";
336 data.stdout = new StringBuilder ();
338 data.stderrName = test + log_prefix + ".stderr";
339 data.stderr = new StringBuilder ();
341 p.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e) {
342 if (e.Data != null) {
343 data.stdout.AppendLine (e.Data);
347 p.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs e) {
348 if (e.Data != null) {
349 data.stderr.AppendLine (e.Data);
353 var start = DateTime.UtcNow;
357 p.BeginOutputReadLine ();
358 p.BeginErrorReadLine ();
360 if (!p.WaitForExit (timeout * 1000)) {
366 // Force the process to print a thread dump
368 Syscall.kill (p.Id, Signum.SIGQUIT);
374 output.Write ($"timed out ({timeout}s)");
380 } else if (p.ExitCode != expectedExitCode) {
381 var end = DateTime.UtcNow;
387 output.Write ("failed, time: {0}, exit code: {1}", (end - start).ToString (TEST_TIME_FORMAT), p.ExitCode);
389 var end = DateTime.UtcNow;
395 output.Write ("passed, time: {0}", (end - start).ToString (TEST_TIME_FORMAT));
401 Console.WriteLine (output.ToString ());
408 threads.Add (thread);
411 for (int j = 0; j < threads.Count; ++j)
414 TimeSpan test_time = DateTime.UtcNow - test_start_time;
416 int npassed = passed.Count;
417 int nfailed = failed.Count;
418 int ntimedout = timedout.Count;
420 XmlWriterSettings xmlWriterSettings = new XmlWriterSettings ();
421 xmlWriterSettings.NewLineOnAttributes = true;
422 xmlWriterSettings.Indent = true;
423 using (XmlWriter writer = XmlWriter.Create (String.Format ("TestResult-{0}.xml", testsuiteName), xmlWriterSettings)) {
424 // <?xml version="1.0" encoding="utf-8" standalone="no"?>
425 writer.WriteStartDocument ();
426 // <!--This file represents the results of running a test suite-->
427 writer.WriteComment ("This file represents the results of running a test suite");
428 // <test-results name="/home/charlie/Dev/NUnit/nunit-2.5/work/src/bin/Debug/tests/mock-assembly.dll" total="21" errors="1" failures="1" not-run="7" inconclusive="1" ignored="4" skipped="0" invalid="3" date="2010-10-18" time="13:23:35">
429 writer.WriteStartElement ("test-results");
430 writer.WriteAttributeString ("name", String.Format ("{0}-tests.dummy", testsuiteName));
431 writer.WriteAttributeString ("total", (npassed + nfailed + ntimedout).ToString());
432 writer.WriteAttributeString ("failures", (nfailed + ntimedout).ToString());
433 writer.WriteAttributeString ("not-run", "0");
434 writer.WriteAttributeString ("date", DateTime.Now.ToString ("yyyy-MM-dd"));
435 writer.WriteAttributeString ("time", DateTime.Now.ToString ("HH:mm:ss"));
436 // <environment nunit-version="2.4.8.0" clr-version="4.0.30319.17020" os-version="Unix 3.13.0.45" platform="Unix" cwd="/home/directhex/Projects/mono/mcs/class/corlib" machine-name="marceline" user="directhex" user-domain="marceline" />
437 writer.WriteStartElement ("environment");
438 writer.WriteAttributeString ("nunit-version", "2.4.8.0" );
439 writer.WriteAttributeString ("clr-version", Environment.Version.ToString() );
440 writer.WriteAttributeString ("os-version", Environment.OSVersion.ToString() );
441 writer.WriteAttributeString ("platform", Environment.OSVersion.Platform.ToString() );
442 writer.WriteAttributeString ("cwd", Environment.CurrentDirectory );
443 writer.WriteAttributeString ("machine-name", Environment.MachineName );
444 writer.WriteAttributeString ("user", Environment.UserName );
445 writer.WriteAttributeString ("user-domain", Environment.UserDomainName );
446 writer.WriteEndElement ();
447 // <culture-info current-culture="en-GB" current-uiculture="en-GB" />
448 writer.WriteStartElement ("culture-info");
449 writer.WriteAttributeString ("current-culture", CultureInfo.CurrentCulture.Name );
450 writer.WriteAttributeString ("current-uiculture", CultureInfo.CurrentUICulture.Name );
451 writer.WriteEndElement ();
452 // <test-suite name="corlib_test_net_4_5.dll" success="True" time="114.318" asserts="0">
453 writer.WriteStartElement ("test-suite");
454 writer.WriteAttributeString ("name", String.Format ("{0}-tests.dummy", testsuiteName));
455 writer.WriteAttributeString ("success", (nfailed + ntimedout == 0).ToString());
456 writer.WriteAttributeString ("time", test_time.Seconds.ToString());
457 writer.WriteAttributeString ("asserts", (nfailed + ntimedout).ToString());
459 writer.WriteStartElement ("results");
460 // <test-suite name="MonoTests" success="True" time="114.318" asserts="0">
461 writer.WriteStartElement ("test-suite");
462 writer.WriteAttributeString ("name","MonoTests");
463 writer.WriteAttributeString ("success", (nfailed + ntimedout == 0).ToString());
464 writer.WriteAttributeString ("time", test_time.Seconds.ToString());
465 writer.WriteAttributeString ("asserts", (nfailed + ntimedout).ToString());
467 writer.WriteStartElement ("results");
468 // <test-suite name="MonoTests" success="True" time="114.318" asserts="0">
469 writer.WriteStartElement ("test-suite");
470 writer.WriteAttributeString ("name", testsuiteName);
471 writer.WriteAttributeString ("success", (nfailed + ntimedout == 0).ToString());
472 writer.WriteAttributeString ("time", test_time.Seconds.ToString());
473 writer.WriteAttributeString ("asserts", (nfailed + ntimedout).ToString());
475 writer.WriteStartElement ("results");
476 // Dump all passing tests first
477 foreach (ProcessData pd in passed) {
478 // <test-case name="MonoTests.Microsoft.Win32.RegistryKeyTest.bug79051" executed="True" success="True" time="0.063" asserts="0" />
479 writer.WriteStartElement ("test-case");
480 writer.WriteAttributeString ("name", String.Format ("MonoTests.{0}.{1}", testsuiteName, pd.test));
481 writer.WriteAttributeString ("executed", "True");
482 writer.WriteAttributeString ("success", "True");
483 writer.WriteAttributeString ("time", "0");
484 writer.WriteAttributeString ("asserts", "0");
485 writer.WriteEndElement ();
487 // Now dump all failing tests
488 foreach (ProcessData pd in failed) {
489 // <test-case name="MonoTests.Microsoft.Win32.RegistryKeyTest.bug79051" executed="True" success="True" time="0.063" asserts="0" />
490 writer.WriteStartElement ("test-case");
491 writer.WriteAttributeString ("name", String.Format ("MonoTests.{0}.{1}", testsuiteName, pd.test));
492 writer.WriteAttributeString ("executed", "True");
493 writer.WriteAttributeString ("success", "False");
494 writer.WriteAttributeString ("time", "0");
495 writer.WriteAttributeString ("asserts", "1");
496 writer.WriteStartElement ("failure");
497 writer.WriteStartElement ("message");
498 writer.WriteCData (FilterInvalidXmlChars (pd.stdout.ToString ()));
499 writer.WriteEndElement ();
500 writer.WriteStartElement ("stack-trace");
501 writer.WriteCData (FilterInvalidXmlChars (pd.stderr.ToString ()));
502 writer.WriteEndElement ();
503 writer.WriteEndElement ();
504 writer.WriteEndElement ();
506 // Then dump all timing out tests
507 foreach (ProcessData pd in timedout) {
508 // <test-case name="MonoTests.Microsoft.Win32.RegistryKeyTest.bug79051" executed="True" success="True" time="0.063" asserts="0" />
509 writer.WriteStartElement ("test-case");
510 writer.WriteAttributeString ("name", String.Format ("MonoTests.{0}.{1}_timedout", testsuiteName, pd.test));
511 writer.WriteAttributeString ("executed", "True");
512 writer.WriteAttributeString ("success", "False");
513 writer.WriteAttributeString ("time", "0");
514 writer.WriteAttributeString ("asserts", "1");
515 writer.WriteStartElement ("failure");
516 writer.WriteStartElement ("message");
517 writer.WriteCData (FilterInvalidXmlChars (pd.stdout.ToString ()));
518 writer.WriteEndElement ();
519 writer.WriteStartElement ("stack-trace");
520 writer.WriteCData (FilterInvalidXmlChars (pd.stderr.ToString ()));
521 writer.WriteEndElement ();
522 writer.WriteEndElement ();
523 writer.WriteEndElement ();
526 writer.WriteEndElement ();
528 writer.WriteEndElement ();
530 writer.WriteEndElement ();
532 writer.WriteEndElement ();
534 writer.WriteEndElement ();
536 writer.WriteEndElement ();
538 writer.WriteEndElement ();
539 writer.WriteEndDocument ();
542 Console.WriteLine ();
543 Console.WriteLine ("Time: {0}", test_time.ToString (TEST_TIME_FORMAT));
544 Console.WriteLine ();
545 Console.WriteLine ("{0,4} test(s) passed", npassed);
546 Console.WriteLine ("{0,4} test(s) failed", nfailed);
547 Console.WriteLine ("{0,4} test(s) timed out", ntimedout);
550 Console.WriteLine ();
551 Console.WriteLine ("Failed test(s):");
552 foreach (ProcessData pd in failed) {
553 Console.WriteLine ();
554 Console.WriteLine (pd.test);
555 DumpFile (pd.stdoutName, pd.stdout.ToString ());
556 DumpFile (pd.stderrName, pd.stderr.ToString ());
561 Console.WriteLine ();
562 Console.WriteLine ("Timed out test(s):");
563 foreach (ProcessData pd in timedout) {
564 Console.WriteLine ();
565 Console.WriteLine (pd.test);
566 DumpFile (pd.stdoutName, pd.stdout.ToString ());
567 DumpFile (pd.stderrName, pd.stderr.ToString ());
571 return (ntimedout == 0 && nfailed == 0) ? 0 : 1;
574 static void DumpFile (string filename, string text) {
575 Console.WriteLine ("=============== {0} ===============", filename);
576 Console.WriteLine (text);
577 Console.WriteLine ("=============== EOF ===============");
580 static string FilterInvalidXmlChars (string text) {
581 // Spec at http://www.w3.org/TR/2008/REC-xml-20081126/#charsets says only the following chars are valid in XML:
582 // Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. */
583 return Regex.Replace (text, @"[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u10000-\u10FFFF]", "");