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 TestResult result = null;
\r
101 string savedDirectory = Environment.CurrentDirectory;
\r
102 TextWriter savedOut = Console.Out;
\r
103 TextWriter savedError = Console.Error;
\r
107 result = testRunner.Run( collector, testFilter );
\r
112 errorWriter.Flush();
\r
114 if ( redirectOutput )
\r
116 if ( redirectError )
\r
117 errorWriter.Close();
\r
119 Environment.CurrentDirectory = savedDirectory;
\r
120 Console.SetOut( savedOut );
\r
121 Console.SetError( savedError );
\r
124 Console.WriteLine();
\r
126 if (result != null) {
\r
127 string xmlOutput = CreateXmlOutput( result );
\r
129 if (options.xmlConsole)
\r
131 Console.WriteLine(xmlOutput);
\r
137 //CreateSummaryDocument(xmlOutput, transformReader );
\r
138 XmlResultTransform xform = new XmlResultTransform( transformReader );
\r
139 xform.Transform( new StringReader( xmlOutput ), Console.Out );
\r
141 catch( Exception ex )
\r
143 Console.WriteLine( "Error: {0}", ex.Message );
\r
144 return TRANSFORM_ERROR;
\r
148 // Write xml output here
\r
149 string xmlResultFile = options.xml == null || options.xml == string.Empty
\r
150 ? "TestResult.xml" : options.xml;
\r
152 using ( StreamWriter writer = new StreamWriter( xmlResultFile ) )
\r
154 writer.Write(xmlOutput);
\r
158 //if ( testRunner != null )
\r
159 // testRunner.Unload();
\r
161 if ( result == null || collector.HasExceptions )
\r
163 collector.WriteExceptions();
\r
164 return UNEXPECTED_ERROR;
\r
167 if ( !result.IsFailure ) return OK;
\r
169 ResultSummarizer summ = new ResultSummarizer( result );
\r
170 return summ.FailureCount;
\r
174 testRunner.Unload();
\r
178 #region Helper Methods
\r
179 private static XmlTextReader GetTransformReader(ConsoleOptions parser)
\r
181 XmlTextReader reader = null;
\r
182 if(parser.transform == null || parser.transform == string.Empty)
\r
184 Assembly assembly = Assembly.GetAssembly(typeof(XmlResultVisitor));
\r
185 ResourceManager resourceManager = new ResourceManager("NUnit.Util.Transform",assembly);
\r
186 string xmlData = (string)resourceManager.GetObject("Summary.xslt");
\r
188 reader = new XmlTextReader(new StringReader(xmlData));
\r
192 FileInfo xsltInfo = new FileInfo(parser.transform);
\r
193 if(!xsltInfo.Exists)
\r
195 Console.Error.WriteLine("Transform file: {0} does not exist", xsltInfo.FullName);
\r
200 reader = new XmlTextReader(xsltInfo.FullName);
\r
207 private static TestRunner MakeRunnerFromCommandLine( ConsoleOptions options )
\r
209 TestPackage package;
\r
210 ConsoleOptions.DomainUsage domainUsage = ConsoleOptions.DomainUsage.Default;
\r
212 if (options.IsTestProject)
\r
214 NUnitProject project = NUnitProject.LoadProject((string)options.Parameters[0]);
\r
215 string configName = options.config;
\r
216 if (configName != null)
\r
217 project.SetActiveConfig(configName);
\r
219 package = project.ActiveConfig.MakeTestPackage();
\r
220 package.TestName = options.fixture;
\r
222 domainUsage = ConsoleOptions.DomainUsage.Single;
\r
224 else if (options.Parameters.Count == 1)
\r
226 package = new TestPackage((string)options.Parameters[0]);
\r
227 domainUsage = ConsoleOptions.DomainUsage.Single;
\r
231 package = new TestPackage("UNNAMED", options.Parameters);
\r
232 domainUsage = ConsoleOptions.DomainUsage.Multiple;
\r
235 if (options.domain != ConsoleOptions.DomainUsage.Default)
\r
236 domainUsage = options.domain;
\r
238 TestRunner testRunner = null;
\r
240 switch( domainUsage )
\r
242 case ConsoleOptions.DomainUsage.None:
\r
243 testRunner = new NUnit.Core.RemoteTestRunner();
\r
244 // Make sure that addins are available
\r
245 CoreExtensions.Host.AddinRegistry = Services.AddinRegistry;
\r
248 case ConsoleOptions.DomainUsage.Single:
\r
249 testRunner = new TestDomain();
\r
252 case ConsoleOptions.DomainUsage.Multiple:
\r
253 testRunner = new MultipleTestDomainRunner();
\r
257 package.TestName = options.fixture;
\r
258 package.Settings["ShadowCopyFiles"] = !options.noshadow;
\r
259 package.Settings["UseThreadedRunner"] = !options.nothread;
\r
260 testRunner.Load( package );
\r
265 private static string CreateXmlOutput( TestResult result )
\r
267 StringBuilder builder = new StringBuilder();
\r
268 XmlResultVisitor resultVisitor = new XmlResultVisitor(new StringWriter( builder ), result);
\r
269 result.Accept(resultVisitor);
\r
270 resultVisitor.Write();
\r
272 return builder.ToString();
\r