コード例 #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();
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: toomanypeople/nunit
        public static int Main(string[] args)
        {
            try
            {
                Options.Parse(args);
            }
            catch (OptionException ex)
            {
                WriteHeader();
                OutWriter.WriteLine(ColorStyle.Error, string.Format(ex.Message, ex.OptionName));
                return(ConsoleRunner.INVALID_ARG);
            }

            //ColorConsole.Enabled = !Options.NoColor;

            // Create SettingsService early so we know the trace level right at the start
            //SettingsService settingsService = new SettingsService();
            //InternalTraceLevel level = (InternalTraceLevel)settingsService.GetSetting("Options.InternalTraceLevel", InternalTraceLevel.Default);
            //if (options.trace != InternalTraceLevel.Default)
            //    level = options.trace;

            //InternalTrace.Initialize("nunit3-console_%p.log", level);

            //log.Info("NUnit3-console.exe starting");
            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.Validate())
                {
                    using (new ColorConsole(ColorStyle.Error))
                    {
                        foreach (string message in Options.ErrorMessages)
                        {
                            Console.Error.WriteLine(message);
                        }
                    }

                    return(ConsoleRunner.INVALID_ARG);
                }

                if (Options.InputFiles.Count == 0)
                {
                    using (new ColorConsole(ColorStyle.Error))
                        Console.Error.WriteLine("Error: no inputs specified");
                    return(ConsoleRunner.OK);
                }

                using (ITestEngine engine = TestEngineActivator.CreateInstance(false))
                {
                    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 (NUnitEngineException ex)
                    {
                        OutWriter.WriteLine(ColorStyle.Error, ex.Message);
                        return(ConsoleRunner.INVALID_ARG);
                    }
                    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, ex.ToString());
                        return(ConsoleRunner.UNEXPECTED_ERROR);
                    }
                    finally
                    {
                        if (Options.WaitBeforeExit)
                        {
                            using (new ColorConsole(ColorStyle.Warning))
                            {
                                Console.Out.WriteLine("\nPress any key to continue . . .");
                                Console.ReadKey(true);
                            }
                        }

                        //    log.Info( "NUnit3-console.exe terminating" );
                    }
                }
            }
            finally
            {
                Console.ResetColor();
            }
        }
コード例 #3
0
        public static int Main(string[] args)
        {
            ConsoleOptions options = new ConsoleOptions();

            try
            {
                options.Parse(args);
            }
            catch (OptionException ex)
            {
                WriteHeader();
                Console.WriteLine(ex.Message, ex.OptionName);
                return(ConsoleRunner.INVALID_ARG);
            }

            ColorConsole.Options = options;

            // Create SettingsService early so we know the trace level right at the start
            //SettingsService settingsService = new SettingsService();
            //InternalTraceLevel level = (InternalTraceLevel)settingsService.GetSetting("Options.InternalTraceLevel", InternalTraceLevel.Default);
            //if (options.trace != InternalTraceLevel.Default)
            //    level = options.trace;

            //InternalTrace.Initialize("nunit-console_%p.log", level);

            //log.Info("NUnit-console.exe starting");
            try
            {
                if (options.PauseBeforeRun)
                {
                    ColorConsole.WriteLine(ColorStyle.Warning, "Press any key to continue . . .");
                    Console.ReadKey(true);
                }

                if (!options.NoHeader)
                {
                    WriteHeader();
                }

                if (options.ShowHelp)
                {
                    WriteHelpText(options);
                    return(ConsoleRunner.OK);
                }

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

                    return(ConsoleRunner.INVALID_ARG);
                }

                if (options.InputFiles.Count == 0)
                {
                    using (new ColorConsole(ColorStyle.Error))
                        Console.Error.WriteLine("Error: no inputs specified");
                    return(ConsoleRunner.OK);
                }

                // TODO: Move this to engine
                foreach (string file in options.InputFiles)
                {
                    //if (!Services.ProjectService.CanLoadProject(file) && !PathUtils.IsAssemblyFileType(file))
                    string ext = Path.GetExtension(file);
                    if (ext != ".dll" && ext != ".exe" && ext != ".nunit")
                    {
                        ColorConsole.WriteLine(ColorStyle.Warning, "File type not known: " + file);
                        return(ConsoleRunner.INVALID_ARG);
                    }
                }

                string workDirectory = options.WorkDirectory ?? Environment.CurrentDirectory;
                var    traceLevel    = options.InternalTraceLevel != null
                    ? (InternalTraceLevel)Enum.Parse(typeof(InternalTraceLevel), options.InternalTraceLevel)
                    : InternalTraceLevel.Off;

                using (ITestEngine engine = TestEngineActivator.CreateInstance(workDirectory, traceLevel))
                {
                    try
                    {
                        return(new ConsoleRunner(engine, options).Execute());
                    }
                    catch (NUnitEngineException ex)
                    {
                        ColorConsole.WriteLine(ColorStyle.Error, ex.Message);
                        return(ConsoleRunner.INVALID_ARG);
                    }
                    catch (FileNotFoundException ex)
                    {
                        ColorConsole.WriteLine(ColorStyle.Error, ex.Message);
#if DEBUG
                        ColorConsole.WriteLine(ColorStyle.Error, ex.StackTrace);
#endif
                        return(ConsoleRunner.FILE_NOT_FOUND);
                    }
                    catch (DirectoryNotFoundException ex)
                    {
                        ColorConsole.WriteLine(ColorStyle.Error, ex.Message);
                        return(ConsoleRunner.FILE_NOT_FOUND);
                    }
                    catch (Exception ex)
                    {
                        ColorConsole.WriteLine(ColorStyle.Error, ex.Message);
                        return(ConsoleRunner.UNEXPECTED_ERROR);
                    }
                    finally
                    {
                        if (options.WaitBeforeExit)
                        {
                            using (new ColorConsole(ColorStyle.Warning))
                            {
                                Console.Out.WriteLine("\nPress any key to continue . . .");
                                Console.ReadKey(true);
                            }
                        }

                        //    log.Info( "NUnit-console.exe terminating" );
                    }
                }
            }
            finally
            {
                Console.ResetColor();
            }
        }