示例#1
0
        public int EntryPoint(string[] args)
        {
            commandLine = CommandLine.Parse(args);

            try
            {
                var reporters = GetAvailableRunnerReporters();

                if (args.Length == 0 || args[0] == "-?" || args[0] == "/?" || args[0] == "-h" || args[0] == "--help")
                {
                    PrintHeader();
                    PrintUsage(reporters);
                    return(2);
                }

                if (commandLine.Project.Assemblies.Count == 0)
                {
                    throw new ArgumentException("must specify at least one assembly");
                }

#if NET452
                AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
#endif

                Console.CancelKeyPress += (sender, e) =>
                {
                    if (!cancel)
                    {
                        Console.WriteLine("Canceling... (Press Ctrl+C again to terminate)");
                        cancel   = true;
                        e.Cancel = true;
                    }
                };

                var defaultDirectory = Directory.GetCurrentDirectory();
                if (!defaultDirectory.EndsWith(new string(new[] { Path.DirectorySeparatorChar }), StringComparison.Ordinal))
                {
                    defaultDirectory += Path.DirectorySeparatorChar;
                }

                var reporter = commandLine.ChooseReporter(reporters);

#if DEBUG
                if (commandLine.Pause)
                {
                    Console.Write("Press any key to start execution...");
                    Console.ReadKey(true);
                    Console.WriteLine();
                }
#endif

                if (commandLine.Debug)
                {
                    Debugger.Launch();
                }

                logger = new ConsoleRunnerLogger(!commandLine.NoColor, consoleLock);
                reporterMessageHandler = MessageSinkWithTypesAdapter.Wrap(reporter.CreateMessageHandler(logger));

                if (!commandLine.NoLogo)
                {
                    PrintHeader();
                }

                var failCount = RunProject(commandLine.Project, commandLine.Serialize, commandLine.ParallelizeAssemblies,
                                           commandLine.ParallelizeTestCollections, commandLine.MaxParallelThreads,
                                           commandLine.DiagnosticMessages, commandLine.NoColor, commandLine.AppDomains,
                                           commandLine.FailSkips, commandLine.StopOnFail, commandLine.InternalDiagnosticMessages);

                if (cancel)
                {
                    return(-1073741510);    // 0xC000013A: The application terminated as a result of a CTRL+C
                }
                if (commandLine.Wait)
                {
                    Console.WriteLine();
                    Console.Write("Press any key to continue...");
                    Console.ReadKey();
                    Console.WriteLine();
                }

                return(failCount > 0 ? 1 : 0);
            }
            catch (Exception ex)
            {
                if (!commandLine.NoColor)
                {
                    ConsoleHelper.SetForegroundColor(ConsoleColor.Red);
                }

                Console.WriteLine($"error: {ex.Message}");

                if (commandLine.InternalDiagnosticMessages)
                {
                    if (!commandLine.NoColor)
                    {
                        ConsoleHelper.SetForegroundColor(ConsoleColor.DarkGray);
                    }

                    Console.WriteLine(ex.StackTrace);
                }

                return(ex is ArgumentException ? 3 : 4);
            }
            finally
            {
                if (!commandLine.NoColor)
                {
                    ConsoleHelper.ResetColor();
                }
            }
        }
示例#2
0
        public int EntryPoint(string[] args)
        {
            var assemblyUnderTest = Assembly.GetEntryAssembly();

            commandLine = CommandLine.Parse(assemblyUnderTest.GetLocalCodeBase(), args);

            try
            {
                var reporters = GetAvailableRunnerReporters();

                if (args.Length > 0 && (args[0] == "-?" || args[0] == "/?" || args[0] == "-h" || args[0] == "--help"))
                {
                    PrintHeader();
                    PrintUsage(reporters);
                    return(2);
                }

                // TODO: What is the portable version of this?
#if NETFRAMEWORK
                AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
#endif

                Console.CancelKeyPress += (sender, e) =>
                {
                    if (!cancel)
                    {
                        Console.WriteLine("Canceling... (Press Ctrl+C again to terminate)");
                        cancel   = true;
                        e.Cancel = true;
                    }
                };

                var defaultDirectory = Directory.GetCurrentDirectory();
                if (!defaultDirectory.EndsWith(new string(new[] { Path.DirectorySeparatorChar }), StringComparison.Ordinal))
                {
                    defaultDirectory += Path.DirectorySeparatorChar;
                }

                var reporter = commandLine.ChooseReporter(reporters);

                if (commandLine.Pause)
                {
                    Console.Write("Press any key to start execution...");
                    Console.ReadKey(true);
                    Console.WriteLine();
                }

                if (commandLine.Debug)
                {
                    Debugger.Launch();
                }

                logger = new ConsoleRunnerLogger(!commandLine.NoColor, consoleLock);
                reporterMessageHandler = MessageSinkWithTypesAdapter.Wrap(reporter.CreateMessageHandler(logger));

                if (!commandLine.NoLogo)
                {
                    PrintHeader();
                }

                // TODO: Will need more things here, like filters and output transform, when they're back
                var failCount = RunProject(commandLine.Project,
                                           commandLine.ParallelizeTestCollections, commandLine.MaxParallelThreads,
                                           commandLine.DiagnosticMessages, commandLine.NoColor,
                                           commandLine.FailSkips, commandLine.StopOnFail, commandLine.InternalDiagnosticMessages);

                if (cancel)
                {
                    return(-1073741510);    // 0xC000013A: The application terminated as a result of a CTRL+C
                }
                if (commandLine.Wait)
                {
                    Console.WriteLine();
                    Console.Write("Press any key to continue...");
                    Console.ReadKey();
                    Console.WriteLine();
                }

                return(failCount > 0 ? 1 : 0);
            }
            catch (Exception ex)
            {
                if (!commandLine.NoColor)
                {
                    ConsoleHelper.SetForegroundColor(ConsoleColor.Red);
                }

                Console.WriteLine($"error: {ex.Message}");

                if (commandLine.InternalDiagnosticMessages)
                {
                    if (!commandLine.NoColor)
                    {
                        ConsoleHelper.SetForegroundColor(ConsoleColor.DarkGray);
                    }

                    Console.WriteLine(ex.StackTrace);
                }

                return(ex is ArgumentException ? 3 : 4);
            }
            finally
            {
                if (!commandLine.NoColor)
                {
                    ConsoleHelper.ResetColor();
                }
            }
        }