[tests] Rework how we AOT compile tests + General cleanup of make targets (#4639)
[mono.git] / mono / tests / test-runner.cs
index baf0609a3bfe3a23963411bd055ca82f2887e6dd..af932e2f2907c355b91b4c6d3f5bf5edc190776d 100644 (file)
 using System;
 using System.IO;
 using System.Threading;
-using System.Text;
 using System.Diagnostics;
 using System.Collections.Generic;
 using System.Globalization;
 using System.Xml;
+using System.Text;
 using System.Text.RegularExpressions;
+
+#if !FULL_AOT_DESKTOP && !MOBILE
 using Mono.Unix.Native;
+#endif
 
 //
 // This is a simple test runner with support for parallel execution
@@ -27,10 +30,13 @@ public class TestRunner
 {
        const string TEST_TIME_FORMAT = "mm\\:ss\\.fff";
        const string ENV_TIMEOUT = "TEST_DRIVER_TIMEOUT_SEC";
+       const string MONO_PATH = "MONO_PATH";
+       const string MONO_GAC_PREFIX = "MONO_GAC_PREFIX";
 
        class ProcessData {
                public string test;
                public StringBuilder stdout, stderr;
+               public object stdoutLock = new object (), stderrLock = new object ();
                public string stdoutName, stderrName;
        }
 
@@ -43,13 +49,16 @@ public class TestRunner
                int concurrency = 1;
                int timeout = 2 * 60; // in seconds
                int expectedExitCode = 0;
+               bool verbose = false;
                string testsuiteName = null;
                string inputFile = null;
 
-               // FIXME: Add support for runtime arguments + env variables
-
                string disabled_tests = null;
                string runtime = "mono";
+               string config = null;
+               string mono_path = null;
+               string runtime_args = null;
+               string mono_gac_prefix = null;
                var opt_sets = new List<string> ();
 
                // Process options
@@ -87,6 +96,20 @@ public class TestRunner
                                        }
                                        runtime = args [i + 1];
                                        i += 2;
+                               } else if (args [i] == "--runtime-args") {
+                                       if (i + 1 >= args.Length) {
+                                               Console.WriteLine ("Missing argument to --runtime-args command line option.");
+                                               return 1;
+                                       }
+                                       runtime_args = (runtime_args ?? "") + " " + args [i + 1];
+                                       i += 2;
+                               } else if (args [i] == "--config") {
+                                       if (i + 1 >= args.Length) {
+                                               Console.WriteLine ("Missing argument to --config command line option.");
+                                               return 1;
+                                       }
+                                       config = args [i + 1];
+                                       i += 2;
                                } else if (args [i] == "--opt-sets") {
                                        if (i + 1 >= args.Length) {
                                                Console.WriteLine ("Missing argument to --opt-sets command line option.");
@@ -116,6 +139,24 @@ public class TestRunner
                                        }
                                        inputFile = args [i + 1];
                                        i += 2;
+                               } else if (args [i] == "--mono-path") {
+                                       if (i + 1 >= args.Length) {
+                                               Console.WriteLine ("Missing argument to --mono-path command line option.");
+                                               return 1;
+                                       }
+                                       mono_path = args [i + 1].Substring(0, args [i + 1].Length);
+
+                                       i += 2;
+                               } else if (args [i] == "--mono-gac-prefix") {
+                                       if (i + 1 >= args.Length) {
+                                               Console.WriteLine ("Missing argument to --mono-gac-prefix command line option.");
+                                               return 1;
+                                       }
+                                       mono_gac_prefix = args[i + 1];
+                                       i += 2;
+                               } else if (args [i] == "--verbose") {
+                                       verbose = true;
+                                       i ++;
                                } else {
                                        Console.WriteLine ("Unknown command line option: '" + args [i] + "'.");
                                        return 1;
@@ -194,19 +235,34 @@ public class TestRunner
                                        string test = ti.test;
                                        string opt_set = ti.opt_set;
 
-                                       output.Write (String.Format ("{{0,-{0}}} ", output_width), test);
+                                       if (verbose) {
+                                               output.Write (String.Format ("{{0,-{0}}} ", output_width), test);
+                                       } else {
+                                               Console.Write (".");
+                                       }
 
                                        /* Spawn a new process */
-                                       string process_args;
-                                       if (opt_set == null)
-                                               process_args = test;
-                                       else
-                                               process_args = "-O=" + opt_set + " " + test;
+
+                                       string process_args = "";
+
+                                       if (opt_set != null)
+                                               process_args += " -O=" + opt_set;
+                                       if (runtime_args != null)
+                                               process_args += " " + runtime_args;
+
+                                       process_args += " " + test;
+
                                        ProcessStartInfo info = new ProcessStartInfo (runtime, process_args);
                                        info.UseShellExecute = false;
                                        info.RedirectStandardOutput = true;
                                        info.RedirectStandardError = true;
                                        info.EnvironmentVariables[ENV_TIMEOUT] = timeout.ToString();
+                                       if (config != null)
+                                               info.EnvironmentVariables["MONO_CONFIG"] = config;
+                                       if (mono_path != null)
+                                               info.EnvironmentVariables[MONO_PATH] = mono_path;
+                                       if (mono_gac_prefix != null)
+                                               info.EnvironmentVariables[MONO_GAC_PREFIX] = mono_gac_prefix;
                                        Process p = new Process ();
                                        p.StartInfo = info;
 
@@ -224,14 +280,16 @@ public class TestRunner
                                        data.stderr = new StringBuilder ();
 
                                        p.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e) {
-                                               if (e.Data != null) {
-                                                       data.stdout.AppendLine (e.Data);
+                                               lock (data.stdoutLock) {
+                                                       if (e.Data != null)
+                                                               data.stdout.AppendLine (e.Data);
                                                }
                                        };
 
                                        p.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs e) {
-                                               if (e.Data != null) {
-                                                       data.stderr.AppendLine (e.Data);
+                                               lock (data.stderrLock) {
+                                                       if (e.Data != null)
+                                                               data.stderr.AppendLine (e.Data);
                                                }
                                        };
 
@@ -248,13 +306,11 @@ public class TestRunner
                                                }
 
                                                // Force the process to print a thread dump
-                                               try {
-                                                       Syscall.kill (p.Id, Signum.SIGQUIT);
-                                                       Thread.Sleep (1000);
-                                               } catch {
-                                               }
+                                               TryThreadDump (p.Id, data);
 
-                                               output.Write ($"timed out ({timeout}s)");
+                                               if (verbose) {
+                                                       output.Write ($"timed out ({timeout}s)");
+                                               }
 
                                                try {
                                                        p.Kill ();
@@ -267,7 +323,8 @@ public class TestRunner
                                                        failed.Add (data);
                                                }
 
-                                               output.Write ("failed, time: {0}, exit code: {1}", (end - start).ToString (TEST_TIME_FORMAT), p.ExitCode);
+                                               if (verbose)
+                                                       output.Write ("failed, time: {0}, exit code: {1}", (end - start).ToString (TEST_TIME_FORMAT), p.ExitCode);
                                        } else {
                                                var end = DateTime.UtcNow;
 
@@ -275,13 +332,15 @@ public class TestRunner
                                                        passed.Add (data);
                                                }
 
-                                               output.Write ("passed, time: {0}", (end - start).ToString (TEST_TIME_FORMAT));
+                                               if (verbose)
+                                                       output.Write ("passed, time: {0}", (end - start).ToString (TEST_TIME_FORMAT));
                                        }
 
                                        p.Close ();
 
                                        lock (monitor) {
-                                               Console.WriteLine (output.ToString ());
+                                               if (verbose)
+                                                       Console.WriteLine (output.ToString ());
                                        }
                                }
                        });
@@ -303,7 +362,9 @@ public class TestRunner
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings ();
                xmlWriterSettings.NewLineOnAttributes = true;
                xmlWriterSettings.Indent = true;
-               using (XmlWriter writer = XmlWriter.Create (String.Format ("TestResult-{0}.xml", testsuiteName), xmlWriterSettings)) {
+
+               string xmlPath = String.Format ("TestResult-{0}.xml", testsuiteName);
+               using (XmlWriter writer = XmlWriter.Create (xmlPath, xmlWriterSettings)) {
                        // <?xml version="1.0" encoding="utf-8" standalone="no"?>
                        writer.WriteStartDocument ();
                        // <!--This file represents the results of running a test suite-->
@@ -420,14 +481,29 @@ public class TestRunner
                        // </test-results>
                        writer.WriteEndElement ();
                        writer.WriteEndDocument ();
+
+                       string babysitterXmlList = Environment.GetEnvironmentVariable("MONO_BABYSITTER_NUNIT_XML_LIST_FILE");
+                       if (!String.IsNullOrEmpty(babysitterXmlList)) {
+                               try {
+                                       string fullXmlPath = Path.GetFullPath(xmlPath);
+                                       File.AppendAllText(babysitterXmlList, fullXmlPath + Environment.NewLine);
+                               } catch (Exception e) {
+                                       Console.WriteLine("Attempted to record XML path to file {0} but failed.", babysitterXmlList);
+                               }
+                       }
                }
 
-               Console.WriteLine ();
-               Console.WriteLine ("Time: {0}", test_time.ToString (TEST_TIME_FORMAT));
-               Console.WriteLine ();
-               Console.WriteLine ("{0,4} test(s) passed", npassed);
-               Console.WriteLine ("{0,4} test(s) failed", nfailed);
-               Console.WriteLine ("{0,4} test(s) timed out", ntimedout);
+               if (verbose) {
+                       Console.WriteLine ();
+                       Console.WriteLine ("Time: {0}", test_time.ToString (TEST_TIME_FORMAT));
+                       Console.WriteLine ();
+                       Console.WriteLine ("{0,4} test(s) passed", npassed);
+                       Console.WriteLine ("{0,4} test(s) failed", nfailed);
+                       Console.WriteLine ("{0,4} test(s) timed out", ntimedout);
+               } else {
+                       Console.WriteLine ();
+                       Console.WriteLine (String.Format ("{0} test(s) passed, {1} test(s) did not pass.", npassed, nfailed));
+               }
 
                if (nfailed > 0) {
                        Console.WriteLine ();
@@ -465,4 +541,119 @@ public class TestRunner
                // Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]      /* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. */
                return Regex.Replace (text, @"[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u10000-\u10FFFF]", "");
        }
+
+       static void TryThreadDump (int pid, ProcessData data)
+       {
+               try {
+                       TryGDB (pid, data);
+                       return;
+               } catch {
+               }
+
+#if !FULL_AOT_DESKTOP && !MOBILE
+               /* LLDB cannot produce managed stacktraces for all the threads */
+               try {
+                       Syscall.kill (pid, Signum.SIGQUIT);
+                       Thread.Sleep (1000);
+               } catch {
+               }
+#endif
+
+               try {
+                       TryLLDB (pid, data);
+                       return;
+               } catch {
+               }
+       }
+
+       static void TryLLDB (int pid, ProcessData data)
+       {
+               string filename = Path.GetTempFileName ();
+
+               using (StreamWriter sw = new StreamWriter (new FileStream (filename, FileMode.Open, FileAccess.Write)))
+               {
+                       sw.WriteLine ("process attach --pid " + pid);
+                       sw.WriteLine ("thread list");
+                       sw.WriteLine ("thread backtrace all");
+                       sw.WriteLine ("detach");
+                       sw.WriteLine ("quit");
+                       sw.Flush ();
+
+                       ProcessStartInfo psi = new ProcessStartInfo {
+                               FileName = "lldb",
+                               Arguments = "--batch --source \"" + filename + "\" --no-lldbinit",
+                               UseShellExecute = false,
+                               RedirectStandardError = true,
+                               RedirectStandardOutput = true,
+                       };
+
+                       using (Process process = new Process { StartInfo = psi })
+                       {
+                               process.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e) {
+                                       lock (data.stdoutLock) {
+                                               if (e.Data != null)
+                                                       data.stdout.AppendLine (e.Data);
+                                       }
+                               };
+
+                               process.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs e) {
+                                       lock (data.stderrLock) {
+                                               if (e.Data != null)
+                                                       data.stderr.AppendLine (e.Data);
+                                       }
+                               };
+
+                               process.Start ();
+                               process.BeginOutputReadLine ();
+                               process.BeginErrorReadLine ();
+                               if (!process.WaitForExit (60 * 1000))
+                                       process.Kill ();
+                       }
+               }
+       }
+
+       static void TryGDB (int pid, ProcessData data)
+       {
+               string filename = Path.GetTempFileName ();
+
+               using (StreamWriter sw = new StreamWriter (new FileStream (filename, FileMode.Open, FileAccess.Write)))
+               {
+                       sw.WriteLine ("attach " + pid);
+                       sw.WriteLine ("info threads");
+                       sw.WriteLine ("thread apply all p mono_print_thread_dump(0)");
+                       sw.WriteLine ("thread apply all backtrace");
+                       sw.Flush ();
+
+                       ProcessStartInfo psi = new ProcessStartInfo {
+                               FileName = "gdb",
+                               Arguments = "-batch -x \"" + filename + "\" -nx",
+                               UseShellExecute = false,
+                               RedirectStandardError = true,
+                               RedirectStandardOutput = true,
+                       };
+
+                       using (Process process = new Process { StartInfo = psi })
+                       {
+                               process.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e) {
+                                       lock (data.stdoutLock) {
+                                               if (e.Data != null)
+                                                       data.stdout.AppendLine (e.Data);
+                                       }
+                               };
+
+                               process.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs e) {
+                                       lock (data.stderrLock) {
+                                               if (e.Data != null)
+                                                       data.stderr.AppendLine (e.Data);
+                                       }
+                               };
+
+                               process.Start ();
+                               process.BeginOutputReadLine ();
+                               process.BeginErrorReadLine ();
+                               if (!process.WaitForExit (60 * 1000))
+                                       process.Kill ();
+                       }
+               }
+       }
 }