private TestCase CreateTestCase(TestCaseDescriptor descriptor, TestCaseResolver resolver)
        {
            TestCaseLocation location =
                resolver.FindTestCaseLocation(_signatureCreator.GetTestMethodSignatures(descriptor).ToList());

            return(CreateTestCase(descriptor, location));
        }
        private List<TestCaseLocation> GetTestCaseLocations(IList<TestCaseDescriptor> testCaseDescriptors, string pathExtension)
        {
            var testMethodSignatures = new List<string>();
            foreach (TestCaseDescriptor descriptor in testCaseDescriptors)
            {
                testMethodSignatures.AddRange(_signatureCreator.GetTestMethodSignatures(descriptor));
            }

            string filterString = "*" + GoogleTestConstants.TestBodySignature;
            var resolver = new TestCaseResolver(_diaResolverFactory, _testEnvironment);
            return resolver.ResolveAllTestCases(_executable, testMethodSignatures, filterString, pathExtension);
        }
        private List <TestCaseLocation> GetTestCaseLocations(IList <TestCaseDescriptor> testCaseDescriptors, string pathExtension)
        {
            var testMethodSignatures = new List <string>();

            foreach (TestCaseDescriptor descriptor in testCaseDescriptors)
            {
                testMethodSignatures.AddRange(_signatureCreator.GetTestMethodSignatures(descriptor));
            }

            string filterString = "*" + GoogleTestConstants.TestBodySignature;
            var    resolver     = new TestCaseResolver(_diaResolverFactory, _testEnvironment);

            return(resolver.ResolveAllTestCases(_executable, testMethodSignatures, filterString, pathExtension));
        }
        private Dictionary <string, TestCaseLocation> GetTestCaseLocations(IList <TestCaseDescriptor> testCaseDescriptors, string pathExtension)
        {
            var testMethodSignatures = new HashSet <string>();

            foreach (var descriptor in testCaseDescriptors)
            {
                foreach (var signature in _signatureCreator.GetTestMethodSignatures(descriptor))
                {
                    testMethodSignatures.Add(signature);
                }
            }

            string filterString = "*" + GoogleTestConstants.TestBodySignature;
            var    resolver     = new TestCaseResolver(_diaResolverFactory, _logger);

            return(resolver.ResolveAllTestCases(_executable, testMethodSignatures, filterString, pathExtension));
        }
示例#5
0
        private void AssertCorrectTestLocationIsFound(string suite, uint line)
        {
            var descriptor = new TestCaseDescriptor(
                suite,
                "Test",
                $"{suite}.Test",
                $"{suite}.Test",
                TestCaseDescriptor.TestTypes.Simple);
            var signatures = new MethodSignatureCreator().GetTestMethodSignatures(descriptor);
            var resolver   = new TestCaseResolver(TestResources.Tests_ReleaseX64,
                                                  new DefaultDiaResolverFactory(), MockOptions.Object, _fakeLogger);

            var testCaseLocation = resolver.FindTestCaseLocation(signatures.ToList());

            _fakeLogger.Errors.Should().BeEmpty();
            testCaseLocation.Should().NotBeNull();
            testCaseLocation.Sourcefile.Should().EndWithEquivalent(@"sampletests\tests\namespacetests.cpp");
            testCaseLocation.Line.Should().Be(line);
        }
示例#6
0
        private List <TestCaseLocation> GetTestCaseLocations(IList <TestCaseDescriptor> testCaseDescriptors, string pathExtension)
        {
            var testMethodSignatures = new List <string>();

            foreach (TestCaseDescriptor descriptor in testCaseDescriptors)
            {
                testMethodSignatures.AddRange(SignatureCreator.GetTestMethodSignatures(descriptor));
            }

            string filterString  = "*" + GoogleTestConstants.TestBodySignature;
            var    errorMessages = new List <string>();

            var resolver = new TestCaseResolver();
            List <TestCaseLocation> testCaseLocations = resolver.ResolveAllTestCases(Executable, testMethodSignatures, filterString, pathExtension, errorMessages);

            foreach (string errorMessage in errorMessages)
            {
                TestEnvironment.LogWarning(errorMessage);
            }

            return(testCaseLocations);
        }
        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)
        {
            if (_settings.UseNewTestExecutionFramework)
            {
                return(NewCreateTestcases(reportTestCase));
            }

            string        workingDir     = _settings.GetWorkingDirForDiscovery(_executable);
            string        finalParams    = GetDiscoveryParams();
            List <string> standardOutput = new List <string>();

            try
            {
                int             processExitCode = 0;
                ProcessLauncher launcher        = null;
                var             listTestsTask   = new Task(() =>
                {
                    launcher       = new ProcessLauncher(_logger, _settings.GetPathExtension(_executable), null);
                    standardOutput = launcher.GetOutputOfCommand(workingDir, _executable, finalParams,
                                                                 false, false, out processExitCode);
                }, TaskCreationOptions.AttachedToParent);
                listTestsTask.Start();

                if (!listTestsTask.Wait(TimeSpan.FromSeconds(_settings.TestDiscoveryTimeoutInSeconds)))
                {
                    launcher?.Cancel();
                    LogTimeoutError(workingDir, finalParams);
                    return(new List <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>());
            }

            IList <TestCaseDescriptor> testCaseDescriptors = new ListTestsParser(_settings.TestNameSeparator).ParseListTestsOutput(standardOutput);
            var resolver = new TestCaseResolver(_executable, _settings.GetPathExtension(_executable), _settings.GetAdditionalPdbs(_executable), _diaResolverFactory, _settings.ParseSymbolInformation, _logger);

            IList <TestCase> testCases = new List <TestCase>();
            IDictionary <string, ISet <TestCase> > suite2TestCases = new Dictionary <string, ISet <TestCase> >();

            foreach (var descriptor in testCaseDescriptors)
            {
                var testCase = _settings.ParseSymbolInformation
                    ? CreateTestCase(descriptor, resolver)
                    : CreateTestCase(descriptor);
                ISet <TestCase> testCasesInSuite;
                if (!suite2TestCases.TryGetValue(descriptor.Suite, out testCasesInSuite))
                {
                    suite2TestCases.Add(descriptor.Suite, testCasesInSuite = new HashSet <TestCase>());
                }
                testCasesInSuite.Add(testCase);
                testCases.Add(testCase);
            }

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

            if (reportTestCase != null)
            {
                foreach (var testCase in testCases)
                {
                    reportTestCase(testCase);
                }
            }

            return(testCases);
        }
        private IList <TestCase> NewCreateTestcases(Action <TestCase> reportTestCase)
        {
            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);
            };

            Action <string> lineAction = s =>
            {
                standardOutput.Add(s);
                parser.ReportLine(s);
            };

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

            try
            {
                int             processExitCode       = ProcessExecutor.ExecutionFailed;
                ProcessExecutor executor              = null;
                var             listAndParseTestsTask = new Task(() =>
                {
                    executor        = new ProcessExecutor(null, _logger);
                    processExitCode = executor.ExecuteCommandBlocking(
                        _executable,
                        finalParams,
                        workingDir,
                        _settings.GetPathExtension(_executable),
                        lineAction);
                }, TaskCreationOptions.AttachedToParent);
                listAndParseTestsTask.Start();

                if (!listAndParseTestsTask.Wait(TimeSpan.FromSeconds(_settings.TestDiscoveryTimeoutInSeconds)))
                {
                    executor?.Cancel();
                    LogTimeoutError(workingDir, finalParams);
                    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);
        }
示例#10
0
        public IList <TestCase> CreateTestCases(Action <TestCase> reportTestCase = null)
        {
            List <string> standardOutput = new List <string>();

            if (_settings.UseNewTestExecutionFramework)
            {
                return(NewCreateTestcases(reportTestCase, standardOutput));
            }

            try
            {
                int             processExitCode = 0;
                ProcessLauncher launcher        = null;
                var             listTestsTask   = new Task(() =>
                {
                    launcher       = new ProcessLauncher(_logger, _settings.GetPathExtension(_executable), null);
                    standardOutput = launcher.GetOutputOfCommand("", _executable, GoogleTestConstants.ListTestsOption,
                                                                 false, false, out processExitCode);
                }, TaskCreationOptions.AttachedToParent);
                listTestsTask.Start();

                if (!listTestsTask.Wait(TimeSpan.FromSeconds(_settings.TestDiscoveryTimeoutInSeconds)))
                {
                    launcher?.Cancel();

                    string dir              = Path.GetDirectoryName(_executable);
                    string file             = Path.GetFileName(_executable);
                    string cdToWorkingDir   = $@"cd ""{dir}""";
                    string listTestsCommand = $"{file} {GoogleTestConstants.ListTestsOption}";

                    _logger.LogError($"Test discovery was cancelled after {_settings.TestDiscoveryTimeoutInSeconds}s for executable {_executable}");
                    _logger.DebugError($"Test whether the following commands can be executed sucessfully on the command line (make sure all required binaries are on the PATH):{Environment.NewLine}{cdToWorkingDir}{Environment.NewLine}{listTestsCommand}");

                    return(new List <TestCase>());
                }

                if (!CheckProcessExitCode(processExitCode, standardOutput))
                {
                    return(new List <TestCase>());
                }
            }
            catch (Exception e)
            {
                SequentialTestRunner.LogExecutionError(_logger, _executable, Path.GetFullPath(""),
                                                       GoogleTestConstants.ListTestsOption, e);
                return(new List <TestCase>());
            }

            IList <TestCaseDescriptor> testCaseDescriptors = new ListTestsParser(_settings.TestNameSeparator).ParseListTestsOutput(standardOutput);
            var resolver = new TestCaseResolver(_executable, _settings.GetPathExtension(_executable), _diaResolverFactory, _settings.ParseSymbolInformation, _logger);

            IList <TestCase> testCases = new List <TestCase>();
            IDictionary <string, ISet <TestCase> > suite2TestCases = new Dictionary <string, ISet <TestCase> >();

            foreach (var descriptor in testCaseDescriptors)
            {
                var testCase = _settings.ParseSymbolInformation
                    ? CreateTestCase(descriptor, resolver)
                    : CreateTestCase(descriptor);
                ISet <TestCase> testCasesInSuite;
                if (!suite2TestCases.TryGetValue(descriptor.Suite, out testCasesInSuite))
                {
                    suite2TestCases.Add(descriptor.Suite, testCasesInSuite = new HashSet <TestCase>());
                }
                testCasesInSuite.Add(testCase);
                testCases.Add(testCase);
            }

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

            if (reportTestCase != null)
            {
                foreach (var testCase in testCases)
                {
                    reportTestCase(testCase);
                }
            }

            return(testCases);
        }
示例#11
0
        private IList <TestCase> NewCreateTestcases(Action <TestCase> reportTestCase, List <string> standardOutput)
        {
            var testCases = new List <TestCase>();

            var resolver = new TestCaseResolver(
                _executable,
                _settings.GetPathExtension(_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);
            };

            Action <string> lineAction = s =>
            {
                standardOutput.Add(s);
                parser.ReportLine(s);
            };

            try
            {
                int             processExitCode       = ProcessExecutor.ExecutionFailed;
                ProcessExecutor executor              = null;
                var             listAndParseTestsTask = new Task(() =>
                {
                    executor        = new ProcessExecutor(null, _logger);
                    processExitCode = executor.ExecuteCommandBlocking(
                        _executable,
                        GoogleTestConstants.ListTestsOption,
                        "",
                        _settings.GetPathExtension(_executable),
                        lineAction);
                }, TaskCreationOptions.AttachedToParent);
                listAndParseTestsTask.Start();

                if (!listAndParseTestsTask.Wait(TimeSpan.FromSeconds(_settings.TestDiscoveryTimeoutInSeconds)))
                {
                    executor?.Cancel();

                    string dir              = Path.GetDirectoryName(_executable);
                    string file             = Path.GetFileName(_executable);
                    string cdToWorkingDir   = $@"cd ""{dir}""";
                    string listTestsCommand = $"{file} {GoogleTestConstants.ListTestsOption}";

                    _logger.LogError($"Test discovery was cancelled after {_settings.TestDiscoveryTimeoutInSeconds}s for executable {_executable}");
                    _logger.DebugError($"Test whether the following commands can be executed sucessfully on the command line (make sure all required binaries are on the PATH):{Environment.NewLine}{cdToWorkingDir}{Environment.NewLine}{listTestsCommand}");

                    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))
                {
                    return(new List <TestCase>());
                }
            }
            catch (Exception e)
            {
                SequentialTestRunner.LogExecutionError(_logger, _executable, Path.GetFullPath(""),
                                                       GoogleTestConstants.ListTestsOption, e);
                return(new List <TestCase>());
            }
            return(testCases);
        }