1 // ****************************************************************
\r
2 // This is free software licensed under the NUnit license. You
\r
3 // may obtain a copy of the license as well as information regarding
\r
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.
\r
5 // ****************************************************************
\r
7 namespace NUnit.ConsoleRunner
\r
11 using System.Reflection;
\r
13 using System.Resources;
\r
16 using NUnit.Core.Filters;
\r
20 /// Summary description for ConsoleUi.
\r
22 public class ConsoleUi
\r
24 public static readonly int OK = 0;
\r
25 public static readonly int INVALID_ARG = -1;
\r
26 public static readonly int FILE_NOT_FOUND = -2;
\r
27 public static readonly int FIXTURE_NOT_FOUND = -3;
\r
28 public static readonly int TRANSFORM_ERROR = -4;
\r
29 public static readonly int UNEXPECTED_ERROR = -100;
\r
35 public int Execute( ConsoleOptions options )
\r
37 XmlTextReader transformReader = GetTransformReader(options);
\r
38 if(transformReader == null) return FILE_NOT_FOUND;
\r
40 TextWriter outWriter = Console.Out;
\r
41 bool redirectOutput = options.output != null && options.output != string.Empty;
\r
42 if ( redirectOutput )
\r
44 StreamWriter outStreamWriter = new StreamWriter( options.output );
\r
45 outStreamWriter.AutoFlush = true;
\r
46 outWriter = outStreamWriter;
\r
49 TextWriter errorWriter = Console.Error;
\r
50 bool redirectError = options.err != null && options.err != string.Empty;
\r
51 if ( redirectError )
\r
53 StreamWriter errorStreamWriter = new StreamWriter( options.err );
\r
54 errorStreamWriter.AutoFlush = true;
\r
55 errorWriter = errorStreamWriter;
\r
58 TestRunner testRunner = MakeRunnerFromCommandLine( options );
\r
62 if (testRunner.Test == null)
\r
64 testRunner.Unload();
\r
65 Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
\r
66 return FIXTURE_NOT_FOUND;
\r
69 EventCollector collector = new EventCollector( options, outWriter, errorWriter );
\r
71 TestFilter testFilter = TestFilter.Empty;
\r
72 if ( options.run != null && options.run != string.Empty )
\r
74 Console.WriteLine( "Selected test: " + options.run );
\r
75 testFilter = new SimpleNameFilter( options.run );
\r
78 if ( options.include != null && options.include != string.Empty )
\r
80 Console.WriteLine( "Included categories: " + options.include );
\r
81 TestFilter includeFilter = new CategoryExpression( options.include ).Filter;
\r
82 if ( testFilter.IsEmpty )
\r
83 testFilter = includeFilter;
\r
85 testFilter = new AndFilter( testFilter, includeFilter );
\r
88 if ( options.exclude != null && options.exclude != string.Empty )
\r
90 Console.WriteLine( "Excluded categories: " + options.exclude );
\r
91 TestFilter excludeFilter = new NotFilter( new CategoryExpression( options.exclude ).Filter );
\r
92 if ( testFilter.IsEmpty )
\r
93 testFilter = excludeFilter;
\r
94 else if ( testFilter is AndFilter )
\r
95 ((AndFilter)testFilter).Add( excludeFilter );
\r
97 testFilter = new AndFilter( testFilter, excludeFilter );
\r
100 testFilter = BabysitterSupport.AddBabysitterFilter(testFilter);
\r
102 TestResult result = null;
\r
103 string savedDirectory = Environment.CurrentDirectory;
\r
104 TextWriter savedOut = Console.Out;
\r
105 TextWriter savedError = Console.Error;
\r
109 result = testRunner.Run( collector, testFilter );
\r
114 errorWriter.Flush();
\r
116 if ( redirectOutput )
\r
118 if ( redirectError )
\r
119 errorWriter.Close();
\r
121 Environment.CurrentDirectory = savedDirectory;
\r
122 Console.SetOut( savedOut );
\r
123 Console.SetError( savedError );
\r
126 Console.WriteLine();
\r
128 if (result != null) {
\r
129 string xmlOutput = CreateXmlOutput( result );
\r
131 if (options.xmlConsole)
\r
133 Console.WriteLine(xmlOutput);
\r
139 //CreateSummaryDocument(xmlOutput, transformReader );
\r
140 XmlResultTransform xform = new XmlResultTransform( transformReader );
\r
141 xform.Transform( new StringReader( xmlOutput ), Console.Out );
\r
143 catch( Exception ex )
\r
145 Console.WriteLine( "Error: {0}", ex.Message );
\r
146 return TRANSFORM_ERROR;
\r
150 // Write xml output here
\r
151 string xmlResultFile = options.xml == null || options.xml == string.Empty
\r
152 ? "TestResult.xml" : options.xml;
\r
154 using ( StreamWriter writer = new StreamWriter( xmlResultFile ) )
\r
156 writer.Write(xmlOutput);
\r
160 //if ( testRunner != null )
\r
161 // testRunner.Unload();
\r
163 if ( result == null || collector.HasExceptions )
\r
165 collector.WriteExceptions();
\r
166 return UNEXPECTED_ERROR;
\r
169 if ( !result.IsFailure ) return OK;
\r
171 ResultSummarizer summ = new ResultSummarizer( result );
\r
172 return summ.FailureCount;
\r
176 testRunner.Unload();
\r
180 #region Helper Methods
\r
181 private static XmlTextReader GetTransformReader(ConsoleOptions parser)
\r
183 XmlTextReader reader = null;
\r
184 if(parser.transform == null || parser.transform == string.Empty)
\r
186 Assembly assembly = Assembly.GetAssembly(typeof(XmlResultVisitor));
\r
187 ResourceManager resourceManager = new ResourceManager("NUnit.Util.Transform",assembly);
\r
188 string xmlData = (string)resourceManager.GetObject("Summary.xslt");
\r
190 reader = new XmlTextReader(new StringReader(xmlData));
\r
194 FileInfo xsltInfo = new FileInfo(parser.transform);
\r
195 if(!xsltInfo.Exists)
\r
197 Console.Error.WriteLine("Transform file: {0} does not exist", xsltInfo.FullName);
\r
202 reader = new XmlTextReader(xsltInfo.FullName);
\r
209 private static TestRunner MakeRunnerFromCommandLine( ConsoleOptions options )
\r
211 TestPackage package;
\r
212 ConsoleOptions.DomainUsage domainUsage = ConsoleOptions.DomainUsage.Default;
\r
214 if (options.IsTestProject)
\r
216 NUnitProject project = NUnitProject.LoadProject((string)options.Parameters[0]);
\r
217 string configName = options.config;
\r
218 if (configName != null)
\r
219 project.SetActiveConfig(configName);
\r
221 package = project.ActiveConfig.MakeTestPackage();
\r
222 package.TestName = options.fixture;
\r
224 domainUsage = ConsoleOptions.DomainUsage.Single;
\r
226 else if (options.Parameters.Count == 1)
\r
228 package = new TestPackage((string)options.Parameters[0]);
\r
229 domainUsage = ConsoleOptions.DomainUsage.Single;
\r
233 package = new TestPackage("UNNAMED", options.Parameters);
\r
234 domainUsage = ConsoleOptions.DomainUsage.Multiple;
\r
237 if (options.domain != ConsoleOptions.DomainUsage.Default)
\r
238 domainUsage = options.domain;
\r
240 TestRunner testRunner = null;
\r
242 switch( domainUsage )
\r
244 case ConsoleOptions.DomainUsage.None:
\r
245 testRunner = new NUnit.Core.RemoteTestRunner();
\r
246 // Make sure that addins are available
\r
247 CoreExtensions.Host.AddinRegistry = Services.AddinRegistry;
\r
250 case ConsoleOptions.DomainUsage.Single:
\r
251 testRunner = new TestDomain();
\r
254 case ConsoleOptions.DomainUsage.Multiple:
\r
255 testRunner = new MultipleTestDomainRunner();
\r
259 package.TestName = options.fixture;
\r
260 package.Settings["ShadowCopyFiles"] = !options.noshadow;
\r
261 package.Settings["UseThreadedRunner"] = !options.nothread;
\r
262 testRunner.Load( package );
\r
267 private static string CreateXmlOutput( TestResult result )
\r
269 StringBuilder builder = new StringBuilder();
\r
270 XmlResultVisitor resultVisitor = new XmlResultVisitor(new StringWriter( builder ), result);
\r
271 result.Accept(resultVisitor);
\r
272 resultVisitor.Write();
\r
274 return builder.ToString();
\r