Inheritance: GoogleTestAdapter.DiaResolver.SourceFileLocation
 private TestCaseLocation ToTestCaseLocation(SourceFileLocation location, IEnumerable<SourceFileLocation> allTraitSymbols)
 {
     List<Trait> traits = GetTraits(location, allTraitSymbols);
     var testCaseLocation = new TestCaseLocation(location.Symbol, location.Sourcefile, location.Line);
     testCaseLocation.Traits.AddRange(traits);
     return testCaseLocation;
 }
        private TestCase CreateTestCase(TestCaseDescriptor descriptor, List <TestCaseLocation> testCaseLocations)
        {
            TestCaseLocation location = testCaseLocations.FirstOrDefault(
                l => l != null && _signatureCreator.GetTestMethodSignatures(descriptor).Any(s => Regex.IsMatch(l.Symbol, s)));

            if (location != null)
            {
                var testCase = new TestCase(
                    descriptor.FullyQualifiedName, _executable, descriptor.DisplayName, location.Sourcefile, (int)location.Line);
                testCase.Traits.AddRange(GetFinalTraits(descriptor.DisplayName, location.Traits));
                return(testCase);
            }

            _logger.LogWarning($"Could not find source location for test {descriptor.FullyQualifiedName}");
            return(new TestCase(
                       descriptor.FullyQualifiedName, _executable, descriptor.DisplayName, "", 0));
        }
        public TestCaseLocation FindTestCaseLocation(List <MethodSignature> testMethodSignatures)
        {
            TestCaseLocation result = DoFindTestCaseLocation(testMethodSignatures);

            if (result == null && !_loadedSymbolsFromAdditionalPdbs)
            {
                LoadSymbolsFromAdditionalPdbs();
                _loadedSymbolsFromAdditionalPdbs = true;
                result = DoFindTestCaseLocation(testMethodSignatures);
            }
            if (result == null && !_loadedSymbolsFromImports)
            {
                LoadSymbolsFromImports();
                _loadedSymbolsFromImports = true;
                result = DoFindTestCaseLocation(testMethodSignatures);
            }
            return(result);
        }
Example #4
0
        private TestCase FinilizeTestCase(TestCase testcase, TestCaseLocation location)
        {
            if (testcase.CodeFilePath != "")
            {
                return(testcase);
            }

            if (location != null)
            {
                testcase.CodeFilePath = location.Sourcefile;
                testcase.LineNumber   = (int)location.Line;
                testcase.Traits.AddRange(GetFinalTraits(testcase.DisplayName, location.Traits));
                return(testcase);
            }

            _logger.LogWarning($"Could not find source location for test {testcase.FullyQualifiedName}, executable: {_executable}");
            return(FinilizeTestCase(testcase));
        }
        private void AddSymbolsFromBinary(string binary, string pdb, bool resolveMainMethod = false)
        {
            using (IDiaResolver diaResolver = _diaResolverFactory.Create(binary, pdb, _logger))
            {
                try
                {
                    _allTestMethodSymbols.AddRange(diaResolver.GetFunctions("*" + GoogleTestConstants.TestBodySignature));
                    _allTraitSymbols.AddRange(diaResolver.GetFunctions("*" + TraitAppendix));
                    _logger.DebugInfo($"Found {_allTestMethodSymbols.Count} test method symbols and {_allTraitSymbols.Count} trait symbols in binary {binary}, pdb {pdb}");

                    if (resolveMainMethod)
                    {
                        MainMethodLocation = ResolveMainMethod(diaResolver);
                    }
                }
                catch (Exception e)
                {
                    _logger.DebugError($"Exception while resolving test locations and traits in '{binary}':{Environment.NewLine}{e}");
                }
            }
        }
        private void AddSymbolsFromBinary(string binary, string pdb, bool resolveMainMethod = false)
        {
            using (IDiaResolver diaResolver = _diaResolverFactory.Create(binary, pdb, _logger))
            {
                try
                {
                    _allTestMethodSymbols.AddRange(diaResolver.GetFunctions("*" + GoogleTestConstants.TestBodySignature));
                    _allTraitSymbols.AddRange(diaResolver.GetFunctions("*" + TraitAppendix));
                    _logger.DebugInfo(String.Format(Resources.FoundTestMethod, _allTestMethodSymbols.Count, _allTraitSymbols.Count, binary, pdb));

                    if (resolveMainMethod)
                    {
                        MainMethodLocation = ResolveMainMethod(diaResolver);
                    }
                }
                catch (Exception e)
                {
                    _logger.DebugError(String.Format(Resources.ExceptionResolving, binary, e));
                }
            }
        }
        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);
        }
        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);
        }
        private IList <TestCase> NewCreateTestcases(Action <TestCase> reportTestCase, List <string> standardOutput)
        {
            var testCases = new List <TestCase>();

            var resolver = new NewTestCaseResolver(
                _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 command = $@"cd ""{dir}""{Environment.NewLine}{file} {GoogleTestConstants.ListTestsOption}";

                    _logger.LogError(String.Format(Resources.TestDiscoveryCancelled, _settings.TestDiscoveryTimeoutInSeconds, _executable));
                    _logger.DebugError(String.Format(Resources.TestCommandCanBeRun, Environment.NewLine, command));

                    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);
        }