コード例 #1
0
        public static int Main(string[] args)
        {
            Console.CancelKeyPress += new ConsoleCancelEventHandler(CancelHandler);

            try
            {
                Options.Parse(Options.PreParse(args));
            }
            catch (OptionException ex)
            {
                WriteHeader();
                OutWriter.WriteLine(ColorStyle.Error, string.Format(ex.Message, ex.OptionName));
                return(ConsoleRunner.INVALID_ARG);
            }

            if (!string.IsNullOrEmpty(Options.ConsoleEncoding))
            {
                try
                {
                    Console.OutputEncoding = Encoding.GetEncoding(Options.ConsoleEncoding);
                }
                catch (Exception error)
                {
                    WriteHeader();
                    OutWriter.WriteLine(ColorStyle.Error, string.Format("Unsupported Encoding, {0}", error.Message));
                    return(ConsoleRunner.INVALID_ARG);
                }
            }

            try
            {
                if (Options.ShowVersion || !Options.NoHeader)
                {
                    WriteHeader();
                }

                if (Options.ShowHelp || args.Length == 0)
                {
                    WriteHelpText();
                    return(ConsoleRunner.OK);
                }

                // We already showed version as a part of the header
                if (Options.ShowVersion)
                {
                    return(ConsoleRunner.OK);
                }

                if (Options.WarningMessages.Count != 0)
                {
                    foreach (string message in Options.WarningMessages)
                    {
                        OutWriter.WriteLine(ColorStyle.Warning, message);
                    }

                    OutWriter.WriteLine();
                }

                if (!Options.Validate())
                {
                    using (new ColorConsole(ColorStyle.Error))
                    {
                        foreach (string message in Options.ErrorMessages)
                        {
                            Console.Error.WriteLine(message);
                        }
                    }

                    return(ConsoleRunner.INVALID_ARG);
                }

                using (ITestEngine engine = TestEngineActivator.CreateInstance())
                {
                    if (Options.WorkDirectory != null)
                    {
                        engine.WorkDirectory = Options.WorkDirectory;
                    }

                    if (Options.InternalTraceLevel != null)
                    {
                        engine.InternalTraceLevel = (InternalTraceLevel)Enum.Parse(typeof(InternalTraceLevel), Options.InternalTraceLevel);
                    }

                    try
                    {
                        return(new ConsoleRunner(engine, Options, OutWriter).Execute());
                    }
                    catch (TestSelectionParserException ex)
                    {
                        OutWriter.WriteLine(ColorStyle.Error, ex.Message);
                        return(ConsoleRunner.INVALID_ARG);
                    }
                    catch (FileNotFoundException ex)
                    {
                        OutWriter.WriteLine(ColorStyle.Error, ex.Message);
                        return(ConsoleRunner.INVALID_ASSEMBLY);
                    }
                    catch (DirectoryNotFoundException ex)
                    {
                        OutWriter.WriteLine(ColorStyle.Error, ex.Message);
                        return(ConsoleRunner.INVALID_ASSEMBLY);
                    }
                    catch (Exception ex)
                    {
                        OutWriter.WriteLine(ColorStyle.Error, ExceptionHelper.BuildMessage(ex));
                        OutWriter.WriteLine();
                        OutWriter.WriteLine(ColorStyle.Error, ExceptionHelper.BuildMessageAndStackTrace(ex));
                        return(ConsoleRunner.UNEXPECTED_ERROR);
                    }
                    finally
                    {
                        if (Options.WaitBeforeExit)
                        {
                            using (new ColorConsole(ColorStyle.Warning))
                            {
                                Console.Out.WriteLine("\nPress any key to continue . . .");
                                Console.ReadKey(true);
                            }
                        }
                    }
                }
            }
            finally
            {
                Console.ResetColor();
            }
        }