예제 #1
0
        int MainBody(string[] args)
        {
            if (args.Length == 0)
            {
                Help();
                return(-1);
            }

            _isUnix = Environment.OSVersion.Platform == PlatformID.Unix;

            // try and define DLR_ROOT if not already defined (makes it easier to debug TestRunner in an IDE)
            string dlrRoot = Environment.GetEnvironmentVariable("DLR_ROOT");

            if (dlrRoot == null)
            {
                dlrRoot = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "../../../../..");
                Environment.SetEnvironmentVariable("DLR_ROOT", dlrRoot);
            }

            // Parse the options
            List <string> inputFiles      = new List <string>();
            List <string> categories      = new List <string>();
            List <string> tests           = new List <string>();
            bool          runAll          = false;
            string        binPath         = Path.Combine(dlrRoot, "bin", "Debug"); // Default to debug binaries
            string        nunitOutputPath = null;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].StartsWith("/category:"))
                {
                    categories.Add(args[i].Substring("/category:".Length));
                }
                else if (args[i].StartsWith("/test:"))
                {
                    tests.Add(args[i].Substring("/test:".Length));
                }
                else if (args[i].StartsWith("/binpath:"))
                {
                    binPath = Path.Combine(dlrRoot, args[i].Substring("/binpath:".Length));
                }
                else if (args[i].StartsWith("/nunitoutput:"))
                {
                    nunitOutputPath = args[i].Substring("/nunitoutput:".Length);
                }
                else if (args[i] == "/verbose")
                {
                    _verbose = true;
                }
                else if (args[i] == "/runlong")
                {
                    _runLongRunning = true;
                }
                else if (args[i] == "/rundisabled")
                {
                    _runDisabled = true;
                }
                else if (args[i] == "/admin")
                {
                    _admin = true;
                }
                else if (args[i] == "/quiet")
                {
                    _quiet = true;
                }
                else if (args[i].StartsWith("/threads:"))
                {
                    int threadCount;
                    if (!Int32.TryParse(args[i].Substring("/threads:".Length), out threadCount) || threadCount <= 0)
                    {
                        Console.WriteLine("Bad thread count: {0}", args[i].Substring("/threads:".Length));
                        return(-1);
                    }
                    _threadCount = threadCount;
                }
                else if (args[i] == "/all")
                {
                    runAll = true;
                }
                else if (File.Exists(args[i]))
                {
                    inputFiles.Add(args[i]);
                }
                else
                {
                    Console.WriteLine("Unknown option: {0}", args[i]);
                    Help();
                    return(-1);
                }
            }

            // Read the test list
            XmlSerializer   serializer = new XmlSerializer(typeof(TestList));
            List <TestList> testLists  = new List <TestList>();

            foreach (var file in inputFiles)
            {
                using (var fs = new FileStream(args[0], FileMode.Open, FileAccess.Read)) {
                    testLists.Add((TestList)serializer.Deserialize(fs));
                }
            }

            if (!runAll && categories.Count == 0 && tests.Count == 0)
            {
                Console.WriteLine("Available categories: ");
                foreach (var list in testLists)
                {
                    foreach (var cat in list.Categories)
                    {
                        Console.WriteLine("    " + cat.Name);
                    }

                    Console.WriteLine("Use /all to run all tests, /category:pattern to run categories, /test:pattern");
                    Console.WriteLine("to run individual tests. Multiple category and test options can be combined.");
                }
                return(-1);
            }

            // Filter the test list
            var originalColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Gray;

            var testNamesRegex     = CompileGlobPatternsToRegex(tests);
            var categoryNamesRegex = CompileGlobPatternsToRegex(categories, startsWith: true);

            List <Test> testList        = new List <Test>();
            List <Test> notParallelSafe = new List <Test>();

            foreach (var list in testLists)
            {
                foreach (var cat in list.Categories)
                {
                    if (runAll || categoryNamesRegex.IsMatch(cat.Name))
                    {
                        foreach (var test in cat.Tests)
                        {
                            test.Category = cat;

                            if (runAll || testNamesRegex.IsMatch(test.Name))
                            {
                                if (test.NotParallelSafe)
                                {
                                    notParallelSafe.Add(test);
                                }
                                else
                                {
                                    testList.Add(test);
                                }
                            }
                        }
                    }
                }
            }

            // Set DLR_BIN, some tests expect this to be defined.
            Environment.SetEnvironmentVariable("DLR_BIN", binPath);

            // start the test running threads
            DateTime start = DateTime.Now;

            List <Thread> threads = new List <Thread>();

            for (int i = 0; i < _threadCount; i++)
            {
                Thread t = new Thread(() => {
                    Test curTest;

                    for (;;)
                    {
                        lock (testList) {
                            if (testList.Count == 0)
                            {
                                break;
                            }

                            curTest = testList[testList.Count - 1];
                            testList.RemoveAt(testList.Count - 1);
                        }

                        RunTestForConsole(curTest);
                    }
                });
                t.Start();
                threads.Add(t);
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }

            foreach (var test in notParallelSafe)
            {
                RunTestForConsole(test);
            }

            var failures = _results.Where(r => r.IsFailure).ToList();

            if (failures.Count > 0)
            {
                if (!_quiet)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Failed test output:");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    foreach (var failedTest in failures)
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.WriteLine(failedTest.Test.Name);
                        Console.ForegroundColor = ConsoleColor.Gray;

                        foreach (var outputLine in failedTest.Output)
                        {
                            Console.WriteLine(outputLine);
                        }
                    }
                }

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Failed test summary:");
                Console.ForegroundColor = ConsoleColor.Gray;
                foreach (var failedTest in failures)
                {
                    Console.WriteLine(failedTest.Test.Name);
                }
            }

            var elapsedTime = (DateTime.Now - start);

            if (!string.IsNullOrWhiteSpace(nunitOutputPath))
            {
                var resultsWriter = new NUnitResultsWriter(_results, inputFiles.First(), elapsedTime, allMessages: _verbose);
                resultsWriter.Save(nunitOutputPath);
            }

            Console.WriteLine("Total time: {0} seconds", elapsedTime.TotalSeconds);
            Console.ForegroundColor = originalColor;

            return(failures.Count);
        }
예제 #2
0
파일: Program.cs 프로젝트: kashano/main
        int MainBody(string[] args) {
            if (args.Length == 0) {
                Help();
                return -1;
            }

            List<string> inputFiles = new List<string>();
            List<string> categories = new List<string>();
            List<string> tests = new List<string>();

            // try and define DLR_ROOT if not already defined (makes it easier to debug TestRunner in an IDE)
            string dlrRoot = Environment.GetEnvironmentVariable("DLR_ROOT");
            if (dlrRoot == null) {
                dlrRoot = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..\\..\\..\\..\\..");
                Environment.SetEnvironmentVariable("DLR_ROOT", dlrRoot);
            }

            // Default to debug binaries
            string binPath = Path.Combine(dlrRoot, "bin\\Debug");
            bool runAll = false;
            string nunitOutputPath = null;

            // parse the options
            for (int i = 0; i < args.Length; i++) {
                if (args[i].StartsWith("/category:")) {
                    categories.Add(args[i].Substring("/category:".Length));
                } else if (args[i].StartsWith("/test:")) {
                    tests.Add(args[i].Substring("/test:".Length));
                } else if (args[i].StartsWith("/binpath:")) {
                    binPath = Path.Combine(dlrRoot, args[i].Substring("/binpath:".Length));
                }
                else if (args[i].StartsWith("/nunitoutput:"))
                {
                    nunitOutputPath = args[i].Substring("/nunitoutput:".Length);
                } else if (args[i] == "/verbose") {
                    _verbose = true;
                } else if (args[i] == "/runlong") {
                    _runLongRunning = true;
                } else if (args[i] == "/admin") {
                    _admin = true;
                } else if (args[i] == "/quiet") {
                    _quiet = true;
                } else if (args[i].StartsWith("/threads:")) {
                    int threadCount;
                    if (!Int32.TryParse(args[i].Substring("/threads:".Length), out threadCount) || threadCount <= 0) {
                        Console.WriteLine("Bad thread count: {0}", args[i].Substring("/threads:".Length));
                        return -1;
                    }
                    _threadCount = threadCount; 
                } else if (args[i] == "/all") {
                    runAll = true;
                } else if(File.Exists(args[i])) {
                    inputFiles.Add(args[i]);
                } else {
                    Console.WriteLine("Unknown option: {0}", args[i]);
                    Help();
                    return -1;
                }
            }

            // Read the test list
            XmlSerializer serializer = new XmlSerializer(typeof(TestList));
            List<TestList> testLists = new List<TestList>();
            foreach (var file in inputFiles) {
                using (var fs = new FileStream(args[0], FileMode.Open, FileAccess.Read)) {
                    testLists.Add((TestList)serializer.Deserialize(fs));
                }
            }

            if (!runAll && categories.Count == 0 && tests.Count == 0) {
                Console.WriteLine("Available categories: ");
                foreach (var list in testLists) {
                    foreach (var cat in list.Categories) {
                        Console.WriteLine("    " + cat.Name);
                    }

                    Console.WriteLine("Use /all to run all tests, /category:Catname to run a category, ");
                    Console.WriteLine("or /test: to run an individual test.  Multiple category and options");
                    Console.WriteLine("can be used.");
                }
                return -1;
            }

            // Set DLR_BIN, some tests expect this to be defined.
            Environment.SetEnvironmentVariable("DLR_BIN", binPath);

            DateTime start = DateTime.Now;
            // Run the tests
            var originalColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Gray;

            List<Test> testList = new List<Test>();
            List<Test> notParallelSafe = new List<Test>();
            foreach (var list in testLists) {
                foreach (var cat in list.Categories) {
                    if (runAll || ShouldRunCategory(cat.Name, categories)) {
                        foreach (var test in cat.Tests) {
                            test.Category = cat;

                            if (runAll || ShouldRunTest(test, tests)) {
                                if (test.NotParallelSafe) {
                                    notParallelSafe.Add(test);
                                } else {
                                    testList.Add(test);
                                }
                            }
                        }
                    }
                }
            }

            // start the test running threads
            List<Thread> threads = new List<Thread>();
            for (int i = 0; i < _threadCount; i++) {
                Thread t = new Thread(() => {
                    Test curTest;

                    for(;;) {
                        lock (testList) {
                            if (testList.Count == 0) {
                                break;
                            }

                            curTest = testList[testList.Count - 1];
                            testList.RemoveAt(testList.Count - 1);
                        }

                        RunTestForConsole(curTest);
                    }
                });
                t.Start();
                threads.Add(t);
            }
            
            foreach (var thread in threads) {
                thread.Join();
            }

            foreach (var test in notParallelSafe) {
                RunTestForConsole(test);
            }

            var failures = _results.Where(r => r.IsFailure).ToList();

            if (failures.Count > 0) {
                if (_verbose) {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Failed test output:");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    foreach (var failedTest in failures) {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.WriteLine(failedTest.Test.Name);
                        Console.ForegroundColor = ConsoleColor.Gray;

                        foreach (var outputLine in failedTest.Output) {
                            Console.WriteLine(outputLine);
                        }
                    }
                }

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Failed test summary:");
                Console.ForegroundColor = ConsoleColor.Gray;
                foreach (var failedTest in failures) {
                    Console.WriteLine(failedTest.Test.Name);
                }
            }

            var elapsedTime = (DateTime.Now - start);
            if (!string.IsNullOrWhiteSpace(nunitOutputPath))
            {
                var resultsWriter = new NUnitResultsWriter(_results, inputFiles.First(), elapsedTime);
                resultsWriter.Save(nunitOutputPath);
            }


            Console.WriteLine("Total time: {0} seconds", elapsedTime.TotalSeconds);

            Console.ForegroundColor = originalColor;

            return failures.Count;
        }