private void runTest(ITestRunner runner, PreProcessedTesRuns preProcessed, RunReport report)
        {
            var testRunInfos = new List <TestRunInfo>();

            foreach (var runInfo in preProcessed.RunInfos)
            {
                if (_testAssemblyValidator.ShouldNotTestAssembly(runInfo.Assembly))
                {
                    return;
                }
                if (runner.CanHandleTestFor(runInfo.Assembly))
                {
                    testRunInfos.Add(runInfo.CloneToTestRunInfo());
                    _bus.Publish(new RunInformationMessage(InformationType.TestRun, "", runInfo.Assembly, runner.GetType()));
                }
            }
            if (testRunInfos.Count == 0)
            {
                return;
            }
            var results = runner.RunTests(testRunInfos.ToArray(), preProcessed.ProcessWrapper, () => { return(_exit); });

            if (_exit)
            {
                return;
            }
            mergeReport(results, report, testRunInfos.ToArray());
            reRunTests(runner, report, testRunInfos, preProcessed.ProcessWrapper);
        }
        public void Consume(AssemblyChangeMessage message)
        {
            _isRunning = true;
            var runReport = new RunReport();

            try
            {
                informParticipants(message);
                var runInfos     = getRunInfos(message);
                var preProcessed = preProcess(runInfos);
                preProcessed = new PreProcessedTesRuns(preProcessed.ProcessWrapper, new TestRunInfoMerger(preProcessed.RunInfos).MergeWith(_abortedTestRuns).ToArray());
                foreach (var runner in _testRunners)
                {
                    runTest(runner, preProcessed, runReport);
                    if (_exit)
                    {
                        _abortedTestRuns.Clear();
                        _abortedTestRuns.AddRange(preProcessed.RunInfos);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                var result = new TestRunResults("", "", false, TestRunner.Any, new TestResult[] { new TestResult(TestRunner.Any, TestRunStatus.Failed, "AutoTest.Net internal error", ex.ToString()) });
                _bus.Publish(new TestRunMessage(result));
            }
            _bus.Publish(new RunFinishedMessage(runReport));
            if (!_exit)
            {
                _abortedTestRuns.Clear();
            }
            _exit      = false;
            _isRunning = false;
        }
        private void testAll(RunInfo[] projectList, RunReport runReport)
        {
            var preProcessed = preProcessTestRun(projectList);

            preProcessed = new PreProcessedTesRuns(preProcessed.ProcessWrapper, new TestRunInfoMerger(preProcessed.RunInfos).MergeWith(_abortedTestRuns).ToArray());
            runPreProcessedTestRun(preProcessed, runReport);
        }
 private PreProcessedTesRuns preProcess(RunInfo[] runInfos)
 {
     var preProcessed = new PreProcessedTesRuns(null, runInfos);
     foreach (var preProcessor in _preProcessors)
         preProcessed = preProcessor.PreProcess(preProcessed);
     return preProcessed;
 }
        private void runPreProcessedTestRun(PreProcessedTesRuns preProcessed, RunReport runReport)
        {
            foreach (var runner in _testRunners)
            {
                Debug.WriteDebug("Preparing runner " + runner.GetType().ToString());
                var runInfos = new List <TestRunInfo>();
                foreach (var file in preProcessed.RunInfos)
                {
                    var project = file.Project;
                    if (hasInvalidAssembly(file))
                    {
                        continue;
                    }
                    var assembly = file.Assembly;
                    if (_testAssemblyValidator.ShouldNotTestAssembly(assembly))
                    {
                        continue;
                    }
                    if (runner.CanHandleTestFor(assembly))
                    {
                        runInfos.Add(file.CloneToTestRunInfo());
                        _bus.Publish(new RunInformationMessage(InformationType.TestRun, project.Key, assembly, runner.GetType()));
                    }
                }
                if (runInfos.Count > 0)
                {
                    Debug.WriteDebug("Running tests for runner " + runner.GetType().ToString());
                    runTests(runner, runInfos.ToArray(), preProcessed.ProcessWrapper, runReport);
                    if (_exit)
                    {
                        _abortedTestRuns.Clear();
                        _abortedTestRuns.AddRange(preProcessed.RunInfos);
                        return;
                    }

                    var rerunInfos = new List <TestRunInfo>();
                    foreach (var info in runInfos)
                    {
                        if (info.RerunAllTestWhenFinishedForAny())
                        {
                            rerunInfos.Add(new TestRunInfo(info.Project, info.Assembly));
                        }
                    }
                    if (rerunInfos.Count > 0)
                    {
                        Debug.WriteDebug("Rerunning all tests for runner " + runner.GetType().ToString());
                        runTests(runner, rerunInfos.ToArray(), preProcessed.ProcessWrapper, runReport);
                        if (_exit)
                        {
                            _abortedTestRuns.Clear();
                            _abortedTestRuns.AddRange(preProcessed.RunInfos);
                            return;
                        }
                    }
                }
            }
            _abortedTestRuns.Clear();
        }
        private void testAll(RunInfo[] projectList, RunReport runReport)
        {
            Debug.WriteDebug("Running test preprosessor");
            var preProcessed = preProcessTestRun(projectList);

            preProcessed = new PreProcessedTesRuns(preProcessed.ProcessWrapper, new TestRunInfoMerger(preProcessed.RunInfos).MergeByAssembly(_abortedTestRuns).ToArray());
            Debug.WriteDebug("Done - Running test preprosessor");
            runPreProcessedTestRun(preProcessed, runReport);
        }
        public PreProcessedTesRuns PreProcess(PreProcessedTesRuns preProcessed)
		{
            var details = preProcessed.RunInfos;
			foreach (var info in details)
			{
				info.AddTestsToRun(getTestsFor(info, _resultCache.Failed));
                info.AddTestsToRun(getTestsFor(info, _resultCache.Ignored));
				info.ShouldOnlyRunSpcifiedTestsFor(TestRunner.Any);
                info.ShouldRerunAllTestWhenFinishedFor(TestRunner.Any);
			}
            return new PreProcessedTesRuns(preProcessed.ProcessWrapper, details);
		}
 PreProcessedTesRuns IPreProcessTestruns.PreProcess(PreProcessedTesRuns preProcessed)
 {
     if (!_isActive)
         return preProcessed;
     var runDetails = new List<RunInfo>();
     foreach (var run in _addToNextRun)
         Logger.WriteDebug("On demand run for preprocessing " + run.Project);
     foreach (var detail in preProcessed.RunInfos)
     {
         var project = "Invalid project";
         if (detail.Project != null)
             project = detail.Project.Key;
         Logger.WriteDebug(string.Format("On demand preprocessor handling {0} ({1})", detail.Assembly, project));
         var runs = _addToNextRun.Where(x => detail.Project != null && x.Project.Equals(detail.Project.Key));
         foreach (var run in runs)
         {
             Logger.WriteDebug(string.Format("Detail matched to run {0} for runner {1}", run.Project, run.Runner));
             if (!run.RunAllTestsInProject)
             {
                 Logger.WriteDebug(string.Format("Should run only specified tests for {0}", run.Runner));
                 detail.ShouldOnlyRunSpcifiedTestsFor(run.Runner);
                 var tests = getList(run.Runner, run.Tests);
                 Logger.WriteDebug(string.Format("Containins {0} tests", tests.Length));
                 detail.AddTestsToRun(tests);
                 var members = getList(run.Runner, run.Members);
                 Logger.WriteDebug(string.Format("Containins {0} members", members.Length));
                 detail.AddMembersToRun(members);
                 var namespaces = getList(run.Runner, run.Namespaces);
                 Logger.WriteDebug(string.Format("Containins {0} namespaces", namespaces.Length));
                 detail.AddNamespacesToRun(namespaces);
             }
             else
             {
                 Logger.WriteDebug(string.Format("Should run all tests for {0}", run.Runner));
             }
         }
         if (runs.Count() > 0)
             runDetails.Add(detail);
     }
     _addToNextRun.Clear();
     return new PreProcessedTesRuns(preProcessed.ProcessWrapper, runDetails.ToArray());
 }
        public PreProcessedTesRuns PreProcess(PreProcessedTesRuns preProcessed)
        {
            // Clean up profiler logs before we start
            var logFiles = getProfilerOutput();
            cleanUpFiles(logFiles);

            var runMinimized = !(_isFullRun || minimizerIsDisabled() || _minimizerPaused);
            Logger.WriteDebug("Run minimized is " + runMinimized.ToString());
            if (runMinimized)
                _bus.Publish(new RunInformationMessage(InformationType.PreProcessing, "", "", typeof(MinimizingPreProcessor)));
            var finalDetails = new List<RunInfo>();
            var details = preProcessed.RunInfos;
            if (!minimizerGraphsAndRiskIsOff())
            {
                try
                {
                    var assemblies = GetAssemblies();
                    //var hash = details.ToDictionary(current => current.Assembly);
                    if (!runMinimized || _runCount % 10 == 0)
                        _minimizer.LoadOldCachedFiles(assemblies);
                    _runCount++;
                    var tests = _minimizer.GetTestsFor(assemblies);
                    Logger.WriteDebug("minimizer returns " + tests.Count + " tests");

                    if (runMinimized)
                    {
                        var profiled = GetProfiledEntries();
                        Logger.WriteDebug("profiler returns " + profiled.Count() + " tests");
                        var all = Combine(tests, profiled);

                        //TODO THIS IS A HACK TO ENRICH PROFILED TESTS, REFACTOR ME
                        foreach (var t in all)
                        {
                            var original = t.TestAssembly
                                .Replace(".mm.dll", ".dll")
                                .Replace(".mm.exe", ".exe");
                            var testAssembly = original;
                            testAssembly = new PathTranslator(_configuration.WatchToken).TranslateFrom(testAssembly);
                            if (testAssembly == null)
                                testAssembly = original;
                            Logger.WriteDebug("Translated TestAssembly is: " + testAssembly + " original is: " + t.TestAssembly);
                            var current = details.FirstOrDefault(x => x.Assembly.Equals(testAssembly));
                            if (current == null)
                            {
                                current = finalDetails.FirstOrDefault(x => x.Assembly.Equals(testAssembly));
                                if (current == null)
                                    current = GetRunInfoFromAssembly(testAssembly);
                                if (current == null)
                                    throw new Exception("unable to match assembly for test. - assembly is " + testAssembly);
                                if (!finalDetails.Exists(x => x.Assembly.Equals(testAssembly)))
                                    finalDetails.Add(current);
                            }

                            foreach (var s in t.TestRunners)
                            {
                                var runner = ConvertStringToTestRunnerEnum(s);
                                var test = getTestSignature(t, runner);
                                Logger.WriteDetails(string.Format("Adding test {0} to runner {1} on run info {2}", test,
                                                                  runner, current.Assembly));
                                current.AddTestsToRun(runner, test);
                            }
                        }

                        if (runMinimized)
                            addCurrentBrokenTests(details);

                        foreach (var detail in details)
                        {
                            detail.ShouldOnlyRunSpcifiedTestsFor(TestRunner.Any);
                        }
                        Logger.WritePreprocessor(string.Format("Found {0} affected tests", tests.Count));
                        _bus.Publish(new AssembliesMinimizedMessage());
                        finalDetails.AddRange(details);
                    }

                }
                catch (Exception ex)
                {
                    Logger.WriteError(ex.ToString());
                    var newDetails = new List<RunInfo>();
                    foreach (var detail in details)
                    {
                        var newDetail = new RunInfo(detail.Project);
                        newDetail.SetAssembly(detail.Assembly);
                        if (detail.ShouldBeBuilt)
                            newDetail.ShouldBuild();
                        newDetails.Add(newDetail);
                    }
                }

                Logger.WriteDebug("Running 2nd generation garbage collection");
                GC.Collect(2);
                Logger.WriteDebug("Waiting for finalizers");
                GC.WaitForPendingFinalizers();
                Logger.WriteDebug("GC done");
            }

            Logger.WriteDebug("Getting profiler wrapper");
            _profilerWrapper = getProfilerWrapper();
            if (!runMinimized)
            {
                var wrapper = _profilerWrapper;
                _profilerWrapper = null;
                Logger.WriteDebug("Returning original runinfos");
                return new PreProcessedTesRuns(wrapper, preProcessed.RunInfos); // return original runInfos
            }
            finalDetails.ForEach(x => _runsToProfile.Add(x.CloneToTestRunInfo()));
            Logger.WriteDebug("Returning modified details");
            return new PreProcessedTesRuns(_profilerWrapper, finalDetails.ToArray());
        }
        public PreProcessedTesRuns PreProcess(PreProcessedTesRuns preProcessed)
		{
            return preProcessed;
		}