5 // Zoltan Varga (vargaz@gmail.com)
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Threading;
31 using System.Diagnostics;
32 using System.Collections.Generic;
35 // This is a simple test runner with support for parallel execution
38 public class TestRunner
42 public StreamWriter stdout, stderr;
45 public static int Main (String[] args) {
48 int timeout = 2 * 60; // in seconds
50 // FIXME: Add support for runtime arguments + env variables
52 string disabled_tests = null;
53 string runtime = "mono";
57 while (i < args.Length) {
58 if (args [i].StartsWith ("-")) {
59 if (args [i] == "-j") {
60 if (i + i >= args.Length) {
61 Console.WriteLine ("Missing argument to -j command line option.");
64 if (args [i + 1] == "a")
65 concurrency = Environment.ProcessorCount;
67 concurrency = Int32.Parse (args [i + 1]);
69 } else if (args [i] == "--timeout") {
70 if (i + i >= args.Length) {
71 Console.WriteLine ("Missing argument to --timeout command line option.");
74 timeout = Int32.Parse (args [i + 1]);
76 } else if (args [i] == "--disabled") {
77 if (i + i >= args.Length) {
78 Console.WriteLine ("Missing argument to --disabled command line option.");
81 disabled_tests = args [i + 1];
83 } else if (args [i] == "--runtime") {
84 if (i + i >= args.Length) {
85 Console.WriteLine ("Missing argument to --runtime command line option.");
88 runtime = args [i + 1];
91 Console.WriteLine ("Unknown command line option: '" + args [i] + "'.");
99 var disabled = new Dictionary <string, string> ();
101 if (disabled_tests != null) {
102 foreach (string test in disabled_tests.Split ())
103 disabled [test] = test;
106 // The remaining arguments are the tests
107 var tests = new List<string> ();
108 for (int j = i; j < args.Length; ++j)
109 if (!disabled.ContainsKey (args [j]))
110 tests.Add (args [j]);
115 var processes = new List<Process> ();
116 var failed = new List<string> ();
117 var process_data = new Dictionary<Process, ProcessData> ();
119 object monitor = new object ();
121 if (concurrency != 1)
122 Console.WriteLine ("Running tests: ");
124 foreach (string test in tests) {
126 while (processes.Count == concurrency) {
127 /* Wait for one process to terminate */
128 Monitor.Wait (monitor);
132 if (concurrency == 1)
133 Console.Write ("Testing " + test + "... ");
135 /* Spawn a new process */
136 ProcessStartInfo info = new ProcessStartInfo (runtime, test);
137 info.UseShellExecute = false;
138 info.RedirectStandardOutput = true;
139 info.RedirectStandardError = true;
140 Process p = new Process ();
142 p.EnableRaisingEvents = true;
144 ProcessData data = new ProcessData ();
147 p.Exited += delegate (object sender, EventArgs e) {
148 // Anon methods share some of their state, so we can't use
149 // variables which change during the loop (test, p)
150 Process dead = (Process)sender;
153 if (dead.ExitCode == 0) {
154 if (concurrency == 1)
155 Console.WriteLine ("passed.");
160 if (concurrency == 1)
161 Console.WriteLine ("failed.");
164 failed.Add (process_data [dead].test);
167 processes.Remove (dead);
168 process_data [dead].stdout.Close ();
169 process_data [dead].stderr.Close ();
170 // This is needed to avoid CreateProcess failed errors :(
172 Monitor.Pulse (monitor);
176 data.stdout = new StreamWriter (new FileStream (test + ".stdout", FileMode.Create));
178 data.stderr = new StreamWriter (new FileStream (test + ".stderr", FileMode.Create));
180 p.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e) {
181 Process p2 = (Process)sender;
186 fs = process_data [p2].stdout;
189 if (String.IsNullOrEmpty (e.Data))
192 fs.WriteLine (e.Data);
195 p.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs e) {
196 Process p2 = (Process)sender;
201 fs = process_data [p2].stderr;
204 if (String.IsNullOrEmpty (e.Data))
207 fs.WriteLine (e.Data);
212 process_data [p] = data;
216 p.BeginOutputReadLine ();
217 p.BeginErrorReadLine ();
220 bool timed_out = false;
222 /* Wait for all processes to terminate */
225 int nprocesses = processes.Count;
230 bool res = Monitor.Wait (monitor, 1000 * timeout);
238 Console.WriteLine ();
241 Console.WriteLine ("\nrunning tests timed out:\n");
242 Console.WriteLine (npassed + nfailed);
244 foreach (Process p in processes) {
245 Console.WriteLine (process_data [p].test);
251 Console.WriteLine ("" + npassed + " test(s) passed. " + nfailed + " test(s) did not pass.");
253 Console.WriteLine ("\nFailed tests:\n");
254 foreach (string s in failed)
255 Console.WriteLine (s);