private List <string> RunTestExecutableWithNewFramework(string executable, string workingDir, CommandLineGenerator.Args arguments, IProcessExecutor executor,
                                                                StreamingStandardOutputTestResultParser streamingParser)
        {
            string pathExtension   = _settings.GetPathExtension(executable);
            bool   printTestOutput = _settings.PrintTestOutput &&
                                     !_settings.ParallelTestExecution;

            if (printTestOutput)
            {
                _logger.LogInfo(
                    $"{_threadName}>>>>>>>>>>>>>>> Output of command '" + executable + " " + arguments.CommandLine + "'");
            }

            Action <string> reportOutputAction = line =>
            {
                try
                {
                    if (!_canceled)
                    {
                        streamingParser.ReportLine(line);
                    }

                    if (printTestOutput)
                    {
                        _logger.LogInfo(line);
                    }
                }
                catch (TestRunCanceledException e)
                {
                    _logger.DebugInfo($"{_threadName}Execution has been canceled: {e.InnerException?.Message ?? e.Message}");
                    Cancel();
                }
            };

            _processExecutor = executor;
            _processExecutor.ExecuteCommandBlocking(
                executable, arguments.CommandLine, workingDir, pathExtension,
                reportOutputAction);
            streamingParser.Flush();

            if (printTestOutput)
            {
                _logger.LogInfo($"{_threadName}<<<<<<<<<<<<<<< End of Output");
            }

            var consoleOutput = new List <string>();

            new TestDurationSerializer().UpdateTestDurations(streamingParser.TestResults);
            _logger.DebugInfo(
                $"{_threadName}Reported {streamingParser.TestResults.Count} test results to VS during test execution, executable: '{executable}'");
            foreach (TestResult result in streamingParser.TestResults)
            {
                if (!_schedulingAnalyzer.AddActualDuration(result.TestCase, (int)result.Duration.TotalMilliseconds))
                {
                    _logger.LogWarning($"{_threadName}TestCase already in analyzer: {result.TestCase.FullyQualifiedName}");
                }
            }
            return(consoleOutput);
        }
Beispiel #2
0
        private List <string> RunTestExecutableWithNewFramework(string executable, string workingDir, IDictionary <string, string> envVars,
                                                                CommandLineGenerator.Args arguments, IProcessExecutor executor,
                                                                StreamingStandardOutputTestResultParser streamingParser)
        {
            string pathExtension   = _settings.GetPathExtension(executable);
            bool   printTestOutput = _settings.PrintTestOutput &&
                                     !_settings.ParallelTestExecution;

            if (printTestOutput)
            {
                _logger.LogInfo(String.Format(Resources.OutputOfCommandMessage, _threadName, executable, arguments.CommandLine));
            }

            Action <string> reportOutputAction = line =>
            {
                try
                {
                    if (!_canceled)
                    {
                        streamingParser.ReportLine(line);
                    }

                    if (printTestOutput)
                    {
                        _logger.LogInfo(line);
                    }
                }
                catch (TestRunCanceledException e)
                {
                    _logger.DebugInfo(String.Format(Resources.ExecutionCancelled, _threadName, e.InnerException?.Message ?? e.Message));
                    Cancel();
                }
            };

            _processExecutor = executor;
            _processExecutor.ExecuteCommandBlocking(
                executable, arguments.CommandLine, workingDir, envVars, pathExtension,
                reportOutputAction);
            streamingParser.Flush();

            if (printTestOutput)
            {
                _logger.LogInfo(String.Format(Resources.EndOfOutputMessage, _threadName));
            }

            var consoleOutput = new List <string>();

            new TestDurationSerializer().UpdateTestDurations(streamingParser.TestResults);
            _logger.DebugInfo(String.Format(Resources.ReportedResultsToVS, _threadName, streamingParser.TestResults.Count, executable));

            foreach (TestResult result in streamingParser.TestResults)
            {
                if (!_schedulingAnalyzer.AddActualDuration(result.TestCase, (int)result.Duration.TotalMilliseconds))
                {
                    _logger.LogWarning(String.Format(Resources.AlreadyInAnalyzer, _threadName, result.TestCase.FullyQualifiedName));
                }
            }
            return(consoleOutput);
        }
        private List <string> RunTestExecutable(string executable, string workingDir, CommandLineGenerator.Args arguments, bool isBeingDebugged, IDebuggedProcessExecutorFactory processExecutorFactory,
                                                StreamingStandardOutputTestResultParser streamingParser)
        {
            string pathExtension         = _settings.GetPathExtension(executable);
            bool   isTestOutputAvailable = !isBeingDebugged || _settings.DebuggerKind > DebuggerKind.VsTestFramework;
            bool   printTestOutput       = _settings.PrintTestOutput &&
                                           !_settings.ParallelTestExecution &&
                                           isTestOutputAvailable;

            void OnNewOutputLine(string line)
            {
                try
                {
                    if (!_canceled)
                    {
                        streamingParser.ReportLine(line);
                    }
                }
                catch (TestRunCanceledException e)
                {
                    _logger.DebugInfo($"{_threadName}Execution has been canceled: {e.InnerException?.Message ?? e.Message}");
                    Cancel();
                }
            }

            _processExecutor = isBeingDebugged
                ? _settings.DebuggerKind == DebuggerKind.VsTestFramework
                    ? processExecutorFactory.CreateFrameworkDebuggingExecutor(printTestOutput, _logger)
                    : processExecutorFactory.CreateNativeDebuggingExecutor(
                _settings.DebuggerKind == DebuggerKind.Native ? DebuggerEngine.Native : DebuggerEngine.ManagedAndNative,
                printTestOutput, _logger)
                : processExecutorFactory.CreateExecutor(printTestOutput, _logger);
            int exitCode = _processExecutor.ExecuteCommandBlocking(
                executable, arguments.CommandLine, workingDir, pathExtension,
                isTestOutputAvailable ? (Action <string>)OnNewOutputLine : null);

            streamingParser.Flush();

            ExecutableResults.Add(new ExecutableResult(executable, exitCode, streamingParser.ExitCodeOutput,
                                                       streamingParser.ExitCodeSkip));

            var consoleOutput = new List <string>();

            new TestDurationSerializer().UpdateTestDurations(streamingParser.TestResults);
            _logger.DebugInfo(
                $"{_threadName}Reported {streamingParser.TestResults.Count} test results to VS during test execution, executable: '{executable}'");
            foreach (TestResult result in streamingParser.TestResults)
            {
                if (!_schedulingAnalyzer.AddActualDuration(result.TestCase, (int)result.Duration.TotalMilliseconds))
                {
                    _logger.DebugWarning($"{_threadName}TestCase already in analyzer: {result.TestCase.FullyQualifiedName}");
                }
            }
            return(consoleOutput);
        }
        public static int ExecuteBatchFileBlocking(this IProcessExecutor executor, string batchFile, string parameters, string workingDir, string pathExtension, Action <string> reportOutputLine)
        {
            if (!File.Exists(batchFile))
            {
                throw new FileNotFoundException("File not found", batchFile);
            }

            string command = Path.Combine(Environment.SystemDirectory, "cmd.exe");

            return(executor.ExecuteCommandBlocking(command, $"/C \"{batchFile}\" {parameters}", workingDir, pathExtension, new Dictionary <string, string>(),
                                                   reportOutputLine));
        }
        public IList <TestCase> CreateTestCases(Action <TestCase> reportTestCase = null)
        {
            var standardOutput = new List <string>();
            var testCases      = new List <TestCase>();

            var resolver = new TestCaseResolver(_executable, _diaResolverFactory, _settings, _logger);

            var suite2TestCases = new Dictionary <string, ISet <TestCase> >();
            var parser          = new StreamingListTestsParser(_settings.TestNameSeparator);

            parser.TestCaseDescriptorCreated += (sender, args) =>
            {
                TestCase testCase;
                if (_settings.ParseSymbolInformation)
                {
                    TestCaseLocation testCaseLocation =
                        resolver.FindTestCaseLocation(
                            _signatureCreator.GetTestMethodSignatures(args.TestCaseDescriptor).ToList());
                    testCase = CreateTestCase(args.TestCaseDescriptor, testCaseLocation);
                }
                else
                {
                    testCase = CreateTestCase(args.TestCaseDescriptor);
                }
                testCases.Add(testCase);

                if (!suite2TestCases.TryGetValue(args.TestCaseDescriptor.Suite, out var testCasesOfSuite))
                {
                    suite2TestCases.Add(args.TestCaseDescriptor.Suite, testCasesOfSuite = new HashSet <TestCase>());
                }
                testCasesOfSuite.Add(testCase);
            };

            string workingDir  = _settings.GetWorkingDirForDiscovery(_executable);
            var    finalParams = GetDiscoveryParams();

            try
            {
                int processExitCode       = ExecutionFailed;
                IProcessExecutor executor = null;

                void OnReportOutputLine(string line)
                {
                    standardOutput.Add(line);
                    parser.ReportLine(line);
                }

                var listAndParseTestsTask = Task.Run(() =>
                {
                    _logger.VerboseInfo($"Starting test discovery for {_executable}");
                    executor        = _processExecutorFactory.CreateExecutor(false, _logger);
                    processExitCode = executor.ExecuteCommandBlocking(
                        _executable,
                        finalParams,
                        workingDir,
                        _settings.GetPathExtension(_executable),
                        OnReportOutputLine);
                    _logger.VerboseInfo($"Finished execution of {_executable}");
                });
                _logger.VerboseInfo($"Scheduled test discovery for {_executable}");

                if (!listAndParseTestsTask.Wait(TimeSpan.FromSeconds(_settings.TestDiscoveryTimeoutInSeconds)))
                {
                    executor?.Cancel();
                    LogTimeoutError(workingDir, finalParams, standardOutput);
                    return(new List <TestCase>());
                }

                foreach (var suiteTestCasesPair in suite2TestCases)
                {
                    foreach (var testCase in suiteTestCasesPair.Value)
                    {
                        testCase.Properties.Add(new TestCaseMetaDataProperty(suiteTestCasesPair.Value.Count, testCases.Count));
                        reportTestCase?.Invoke(testCase);
                    }
                }

                if (!string.IsNullOrWhiteSpace(_settings.ExitCodeTestCase))
                {
                    var exitCodeTestCase = ExitCodeTestsReporter.CreateExitCodeTestCase(_settings, _executable, resolver.MainMethodLocation);
                    testCases.Add(exitCodeTestCase);
                    reportTestCase?.Invoke(exitCodeTestCase);
                    _logger.DebugInfo($"Exit code of executable '{_executable}' is ignored for test discovery because option '{SettingsWrapper.OptionExitCodeTestCase}' is set");
                }
                else if (!CheckProcessExitCode(processExitCode, standardOutput, workingDir, finalParams))
                {
                    return(new List <TestCase>());
                }
            }
            catch (Exception e)
            {
                SequentialTestRunner.LogExecutionError(_logger, _executable, workingDir, finalParams, e);
                return(new List <TestCase>());
            }
            return(testCases);
        }
        public IList <TestCase> CreateTestCases(Action <TestCase> reportTestCase = null)
        {
            var standardOutput = new List <string>();
            var testCases      = new List <TestCase>();

            var resolver = new TestCaseResolver(
                _executable,
                _settings.GetPathExtension(_executable),
                _settings.GetAdditionalPdbs(_executable),
                _diaResolverFactory,
                _settings.ParseSymbolInformation,
                _logger);

            var suite2TestCases = new Dictionary <string, ISet <TestCase> >();
            var parser          = new StreamingListTestsParser(_settings.TestNameSeparator);

            parser.TestCaseDescriptorCreated += (sender, args) =>
            {
                TestCase testCase;
                if (_settings.ParseSymbolInformation)
                {
                    TestCaseLocation testCaseLocation =
                        resolver.FindTestCaseLocation(
                            _signatureCreator.GetTestMethodSignatures(args.TestCaseDescriptor).ToList());
                    testCase = CreateTestCase(args.TestCaseDescriptor, testCaseLocation);
                }
                else
                {
                    testCase = CreateTestCase(args.TestCaseDescriptor);
                }
                testCases.Add(testCase);

                ISet <TestCase> testCasesOfSuite;
                if (!suite2TestCases.TryGetValue(args.TestCaseDescriptor.Suite, out testCasesOfSuite))
                {
                    suite2TestCases.Add(args.TestCaseDescriptor.Suite, testCasesOfSuite = new HashSet <TestCase>());
                }
                testCasesOfSuite.Add(testCase);
            };

            string workingDir  = _settings.GetWorkingDirForDiscovery(_executable);
            var    finalParams = GetDiscoveryParams();

            try
            {
                int processExitCode       = ExecutionFailed;
                IProcessExecutor executor = null;

                void OnReportOutputLine(string line)
                {
                    standardOutput.Add(line);
                    parser.ReportLine(line);
                }

                var listAndParseTestsTask = new Task(() =>
                {
                    executor        = _processExecutorFactory.CreateExecutor(false, _logger);
                    processExitCode = executor.ExecuteCommandBlocking(
                        _executable,
                        finalParams,
                        workingDir,
                        _settings.GetPathExtension(_executable),
                        OnReportOutputLine);
                }, TaskCreationOptions.AttachedToParent);
                listAndParseTestsTask.Start();

                if (!listAndParseTestsTask.Wait(TimeSpan.FromSeconds(_settings.TestDiscoveryTimeoutInSeconds)))
                {
                    executor?.Cancel();
                    LogTimeoutError(workingDir, finalParams, standardOutput);
                    return(new List <TestCase>());
                }

                foreach (var suiteTestCasesPair in suite2TestCases)
                {
                    foreach (var testCase in suiteTestCasesPair.Value)
                    {
                        testCase.Properties.Add(new TestCaseMetaDataProperty(suiteTestCasesPair.Value.Count, testCases.Count));
                        reportTestCase?.Invoke(testCase);
                    }
                }

                if (!CheckProcessExitCode(processExitCode, standardOutput, workingDir, finalParams))
                {
                    return(new List <TestCase>());
                }
            }
            catch (Exception e)
            {
                SequentialTestRunner.LogExecutionError(_logger, _executable, workingDir, finalParams, e);
                return(new List <TestCase>());
            }
            return(testCases);
        }
 public static int ExecuteBatchFileBlocking(this IProcessExecutor executor, string batchFile, string parameters, string workingDir, string pathExtension, Action <string> reportOutputLine)
 {
     return(executor.ExecuteCommandBlocking($"cmd.exe /C {batchFile}", parameters, workingDir, pathExtension,
                                            reportOutputLine));
 }