Пример #1
0
        /// <summary>
        /// Deserializes the results.xml file to CoverageSession
        /// </summary>
        /// <returns>OpenCover execution results</returns>
        internal CoverageSession GetExecutionResults()
        {
            CoverageSession coverageSession = null;

            try
            {
                var serializer = new XmlSerializer(typeof(CoverageSession), new[] { typeof(Module), typeof(OpenCover.Framework.Model.File), typeof(Class) });
                using (var stream = System.IO.File.Open(_openCoverResultsFile, FileMode.Open))
                {
                    coverageSession = serializer.Deserialize(stream) as CoverageSession;
                }

                if (_commandLineParameterReader.ReadConfiguration(_currentWorkingDirectory))
                {
                    ExecuteTestResultPostProcessor(_testResultsFile);
                    ExecuteCoverageResultPostProcessor(_openCoverResultsFile);
                }

                if (!System.Diagnostics.Debugger.IsAttached)
                {
                    System.IO.File.Delete(_openCoverResultsFile);
                }
            }
            catch (Exception ex)
            {
                IDEHelper.WriteToOutputWindow(ex.Message);
                IDEHelper.WriteToOutputWindow(ex.StackTrace);
            }

            return(coverageSession);
        }
Пример #2
0
        private async Task ProcessCoverageSessionResults(ProjectInfo projectInfo, QueuedTest testQueueItem, string resultFilename, string fileToRead)
        {
            var sw = Stopwatch.StartNew();

            _queries.RemoveFromQueue(testQueueItem);
            var coverageSession = new CoverageSession();
            var testOutput      = new resultType();

            await Task.Run(() =>
            {
                coverageSession = GetCoverageSessionFile(fileToRead);

                TestOutputFileReader testOutputFileReader = new TestOutputFileReader();

                testOutput = testOutputFileReader.ReadTestResultFile(GetOutputFolder() + resultFilename);
            });

            Log.DebugFormat("Coverage and Test Result Files Read Elapsed Time = {0}", sw.ElapsedMilliseconds);
            sw.Reset();


            Log.DebugFormat("SaveUnitTestResults Elapsed Time = {0}", sw.ElapsedMilliseconds);
            sw.Reset();
            await _queries.SaveCoverageSessionResults(coverageSession, testOutput, projectInfo, testQueueItem.IndividualTests);

            Log.DebugFormat("SaveCoverageSessionResults Elapsed Time = {0}", sw.ElapsedMilliseconds);

            Log.DebugFormat("ProcessCoverageSessionResults Completed, Name: {0}, Individual Test Count: {1}, Time from Build-to-Complete {2}",

                            testQueueItem.ProjectName, testQueueItem.IndividualTests == null ? 0: testQueueItem.IndividualTests.Count(), DateTime.Now - testQueueItem.TestStartTime);

            System.IO.File.Delete(fileToRead);
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="session"></param>
        protected void ReassignCoverageSession(CoverageSession session)
        {
            _moduleMethodMap.Clear();
            CoverageSession         = session;
            CoverageSession.Summary = new Summary();
            foreach (var module in CoverageSession.Modules)
            {
                BuildMethodMapForModule(module);
                module.Summary = new Summary();
                foreach (var @class in module.Classes)
                {
                    @class.Summary = new Summary();
                    foreach (var method in @class.Methods)
                    {
                        method.Summary = new Summary();
                        if (method.SequencePoints.Any() && method.SequencePoints[0].Offset == method.MethodPoint.Offset)
                        {
                            var point = new[] { method.SequencePoints[0], (SequencePoint)method.MethodPoint }
                            .OrderBy(x => x.OrigSequencePoint)
                            .First();

                            method.MethodPoint       = point;
                            method.SequencePoints[0] = point;
                        }
                    }
                }
            }

            InstrumentationPoint.ResetAfterLoading();
            File.ResetAfterLoading();
        }
Пример #4
0
        private void GenerateReport(CoverageSession coverageSession, string reportPath)
        {
            var serializer = new XmlSerializer(typeof(CoverageSession),
                                               new[] { typeof(OpenCover.Framework.Model.Module), typeof(OpenCover.Framework.Model.File), typeof(Class) });

            ExportReport(reportPath, serializer, coverageSession);
        }
Пример #5
0
 /// <summary>
 /// constructor
 /// </summary>
 /// <param name="commandLine"></param>
 /// <param name="logger"></param>
 protected BasePersistance(ICommandLine commandLine, ILog logger)
 {
     CommandLine      = commandLine;
     _logger          = logger ?? DebugLogger;
     CoverageSession  = new CoverageSession();
     _trackedMethodId = 0;
 }
Пример #6
0
        private static void CalculateAndDisplayResults(CoverageSession coverageSession, CommandLineParser parser)
        {
            if (!Logger.IsInfoEnabled)
            {
                return;
            }

            var results = new Results();

            if (coverageSession.Modules != null)
            {
                CalculateResults(coverageSession, results);

                if (parser.LogModules)
                {
                    foreach (var module in coverageSession.Modules)
                    {
                        Logger.InfoFormat("A: {0}", module.ModuleName);
                        foreach (var @class in module.Classes)
                        {
                            Logger.InfoFormat("  C: {0}", @class.FullName);
                            foreach (var method in @class.Methods)
                            {
                                Logger.InfoFormat("    M: {0}", method.FullName);
                            }
                        }
                    }
                }
            }

            DisplayResults(coverageSession, parser, results);
        }
Пример #7
0
        public CoverageSession ReadCoverageFile(string path)
        {
            StreamReader file;

            var summary = new CoverageSession();

            try
            {
                _log.DebugFormat("ReadCoverageFile for file name: {0}", path);
                file = new StreamReader(path);

                var reader = new XmlSerializer(typeof(CoverageSession));

                summary = (CoverageSession)reader.Deserialize(file);
                _log.DebugFormat("BranchCoverage: {0}", summary.Summary.BranchCoverage);
                _log.DebugFormat("SequenceCoverage: {0}", summary.Summary.SequenceCoverage);
                _log.DebugFormat("VisitedBranchPoints: {0}", summary.Summary.VisitedBranchPoints);
                _log.DebugFormat("VisitedSequencePoints: {0}", summary.Summary.VisitedSequencePoints);
            }
            catch (Exception ex)
            {
                _log.ErrorFormat("Error ReadCoverageFile: {0} Message{1}", path, ex.Message);
                throw;
            }

            file.Close();
            return(summary);
        }
Пример #8
0
 private void ExportReport(string reportPath, XmlSerializer serializer, CoverageSession openCoverReport)
 {
     using (var stream = new FileStream(reportPath, FileMode.Create))
         using (var writer = new StreamWriter(stream, new UTF8Encoding()))
         {
             serializer.Serialize(writer, openCoverReport);
         }
 }
        private void ProcessGenericMethods(CoverageSession coverageSession)
        {
            CoveredMethodStats[] coveredMethodStats = Coverage.GetStatsForAllCoveredMethods();
            foreach (CoveredMethodStats coveredMethodStat in coveredMethodStats)
            {
                MethodBase method = coveredMethodStat.method;

                ResultsLogger.LogSessionItem($"Processing generic method: {method.Name}", LogVerbosityLevel.Verbose);

                Type   declaringType = method.DeclaringType;
                string assemblyName  = declaringType.Assembly.GetName().Name.ToLower();
                if (!m_ReporterFilter.ShouldProcessAssembly(assemblyName))
                {
                    ResultsLogger.LogSessionItem($"Excluded assembly from generic (Assembly Filtering): {assemblyName}", LogVerbosityLevel.Verbose);
                    continue;
                }

                if (!(declaringType.IsGenericType || method.IsGenericMethod))
                {
                    continue;
                }

                Module module = Array.Find(coverageSession.Modules, element => element.ModuleName.ToLower() == assemblyName);
                if (module != null)
                {
                    string className = string.Empty;
                    if (declaringType.IsGenericType)
                    {
                        Type genericTypeDefinition = declaringType.GetGenericTypeDefinition();
                        className = GenerateTypeName(genericTypeDefinition);
                    }
                    else if (method.IsGenericMethod)
                    {
                        className = GenerateTypeName(declaringType);
                    }

                    Class klass = Array.Find(module.Classes, element => element.FullName == className);
                    if (klass != null)
                    {
                        Method targetMethod = Array.Find(klass.Methods, element => element.MetadataToken == method.MetadataToken);
                        if (targetMethod != null)
                        {
                            ResultsLogger.LogSessionItem($"Processing included generic method: {method.Name}", LogVerbosityLevel.Verbose);

                            CoveredSequencePoint[] coveredSequencePoints = Coverage.GetSequencePointsFor(method);
                            foreach (CoveredSequencePoint coveredSequencePoint in coveredSequencePoints)
                            {
                                SequencePoint targetSequencePoint = Array.Find(targetMethod.SequencePoints, element => (element.StartLine == coveredSequencePoint.line && element.Offset == coveredSequencePoint.ilOffset));
                                if (targetSequencePoint != null)
                                {
                                    targetSequencePoint.VisitCount += (int)coveredSequencePoint.hitCount;
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #10
0
        private static void DisplayResults(CoverageSession coverageSession, ICommandLine parser, Results results)
        {
            if (coverageSession.Summary.NumClasses > 0)
            {
                Logger.InfoFormat("Visited Classes {0} of {1} ({2})", coverageSession.Summary.VisitedClasses,
                                  coverageSession.Summary.NumClasses, Math.Round(coverageSession.Summary.VisitedClasses * 100.0 / coverageSession.Summary.NumClasses, 2));
                Logger.InfoFormat("Visited Methods {0} of {1} ({2})", coverageSession.Summary.VisitedMethods,
                                  coverageSession.Summary.NumMethods, Math.Round(coverageSession.Summary.VisitedMethods * 100.0 / coverageSession.Summary.NumMethods, 2));
                Logger.InfoFormat("Visited Points {0} of {1} ({2})", coverageSession.Summary.VisitedSequencePoints,
                                  coverageSession.Summary.NumSequencePoints, coverageSession.Summary.SequenceCoverage);
                Logger.InfoFormat("Visited Branches {0} of {1} ({2})", coverageSession.Summary.VisitedBranchPoints,
                                  coverageSession.Summary.NumBranchPoints, coverageSession.Summary.BranchCoverage);

                Logger.InfoFormat("");
                Logger.InfoFormat(
                    "==== Alternative Results (includes all methods including those without corresponding source) ====");
                Logger.InfoFormat("Alternative Visited Classes {0} of {1} ({2})", results.altVisitedClasses,
                                  results.altTotalClasses, results.altTotalClasses == 0 ? 0 : Math.Round(results.altVisitedClasses * 100.0 / results.altTotalClasses, 2));
                Logger.InfoFormat("Alternative Visited Methods {0} of {1} ({2})", results.altVisitedMethods,
                                  results.altTotalMethods, results.altTotalMethods == 0 ? 0 : Math.Round(results.altVisitedMethods * 100.0 / results.altTotalMethods, 2));

                if (parser.ShowUnvisited)
                {
                    Logger.InfoFormat("");
                    Logger.InfoFormat("====Unvisited Classes====");
                    foreach (var unvisitedClass in results.unvisitedClasses)
                    {
                        Logger.InfoFormat(unvisitedClass);
                    }

                    Logger.InfoFormat("");
                    Logger.InfoFormat("====Unvisited Methods====");
                    foreach (var unvisitedMethod in results.unvisitedMethods)
                    {
                        Logger.InfoFormat(unvisitedMethod);
                    }
                }
            }
            else
            {
                Logger.InfoFormat("No results, this could be for a number of reasons. The most common reasons are:");
                Logger.InfoFormat("    1) missing PDBs for the assemblies that match the filter please review the");
                Logger.InfoFormat("    output file and refer to the Usage guide (Usage.rtf) about filters.");
                Logger.InfoFormat("    2) the profiler may not be registered correctly, please refer to the Usage");
                Logger.InfoFormat("    guide and the -register switch.");
                Logger.InfoFormat("    3) your assemblies under test were not be loaded, refer to the output xml");
                Logger.InfoFormat("    and check your filters/code.");
                Logger.InfoFormat("    4) you are targetting .net core and your assemblies under test were not");
                Logger.InfoFormat("    instrumented, try using the -oldstyle switch.");
                Logger.InfoFormat("    5) you are targetting 4.8 or .net core and your assemblies under test were");
                Logger.InfoFormat("    not instrumented, try using the -register:path32 or -register:path64 switch.");
            }
        }
Пример #11
0
        /// <summary>
        /// Updates the coverage results for current OpenCover run on the UI thread.
        /// </summary>
        /// <param name="data">The CoverageSession data.</param>
        public void UpdateCoverageResults(CoverageSession data)
        {
            CoverageSession = data;

            if (CoverageSession != null)
            {
                Dispatcher.Invoke(() =>
                {
                    CodeCoverageResultsTreeView.Root = new CoverageNode(data);
                });
            }
        }
Пример #12
0
        /// <summary>
        /// Updates the coverage results for current OpenCover run on the UI thread.
        /// </summary>
        /// <param name="data">The CoverageSession data.</param>
        public void UpdateCoverageResults(CoverageSession data)
        {
            CoverageSession = data;

            if (CoverageSession != null)
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    CodeCoverageResultsTreeView.Root = new CoverageNode(data);
                    IsLoading = false;
                }), null);
            }
        }
Пример #13
0
 public bool TryOpenCoverageReport(string reportPath)
 {
     try
     {
         _report = GenericExtensions.ParseXml <CoverageSession>(reportPath);
         CoverageUpdated?.Invoke(this, EventArgs.Empty);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Пример #14
0
        /// <summary>
        /// Deserializes the results.xml file to CoverageSession
        /// </summary>
        /// <returns>OpenCover execution results</returns>
        public CoverageSession GetExecutionResults()
        {
            var             serializer      = new XmlSerializer(typeof(CoverageSession), new[] { typeof(Module), typeof(OpenCover.Framework.Model.File), typeof(Class) });
            CoverageSession coverageSession = null;

            using (var stream = System.IO.File.Open(this._openCoverResultsFile, FileMode.Open))
            {
                coverageSession = serializer.Deserialize(stream) as CoverageSession;
            }

            System.IO.File.Delete(this._openCoverResultsFile);

            return(coverageSession);
        }
Пример #15
0
        private static CoverageSession ConvertTo(CoverageContext coverageContext)
        {
            var coverageSession = new CoverageSession();

            coverageSession.Modules = coverageContext.Modules.Select(x => new OpenCover.Framework.Model.Module
            {
                ModuleHash = x.ModuleHash,
                ModuleName = x.ModuleName,
                ModulePath = x.ModulePath,
                ModuleTime = x.ModuleTime,
                Files      = x.FileReferences.Select(f => new OpenCover.Framework.Model.File {
                    FullPath = f.FilePath,
                    UniqueId = Convert.ToUInt32(f.UniqueId)
                }).ToArray(),
                Classes = x.Types.Select(t => new Class
                {
                    FullName = t.FullName,
                    Methods  = t.Methods.Select(m => new OpenCover.Framework.Model.Method
                    {
                        FullName       = m.FullName,
                        IsGetter       = m.IsGetter,
                        IsConstructor  = m.IsConstructor,
                        IsSetter       = m.IsSetter,
                        IsStatic       = m.IsStatic,
                        Visited        = m.Executed,
                        SequencePoints = m.SequencePoints == null ? new SequencePoint[] { } : m.SequencePoints.Select(s => new OpenCover.Framework.Model.SequencePoint
                        {
                            EndLine     = s.EndLine,
                            Offset      = s.Offset,
                            Ordinal     = s.Ordinal,
                            StartLine   = s.StartLine,
                            StartColumn = s.StartColumn,
                            EndColumn   = s.EndColumn,
                            VisitCount  = s.ExecutionCount,
                            FileId      = Convert.ToUInt32(s.FileReference.UniqueId)
                        }).ToArray(),
                        BranchPoints = m.BranchPoints == null ? new BranchPoint [] {} : m.BranchPoints.Select(b => new OpenCover.Framework.Model.BranchPoint
                        {
                            Ordinal      = b.Ordinal,
                            Offset       = b.Offset,
                            EndOffset    = b.EndOffset,
                            OffsetPoints = b.OffsetPoints,
                            FileId       = Convert.ToUInt32(b.FileReference.UniqueId)
                        }).ToArray()
                    }).ToArray()
                }).ToArray()
            })
                                      .ToArray();
            return(coverageSession);
        }
Пример #16
0
        private static void DisplayResults(CoverageSession coverageSession, ICommandLine parser, Results results)
        {
            if (coverageSession.Summary.NumClasses > 0)
            {
                Logger.InfoFormat("Visited Classes {0} of {1} ({2})", coverageSession.Summary.VisitedClasses,
                                  coverageSession.Summary.NumClasses, Math.Round(coverageSession.Summary.VisitedClasses * 100.0 / coverageSession.Summary.NumClasses, 2));
                Logger.InfoFormat("Visited Methods {0} of {1} ({2})", coverageSession.Summary.VisitedMethods,
                                  coverageSession.Summary.NumMethods, Math.Round(coverageSession.Summary.VisitedMethods * 100.0 / coverageSession.Summary.NumMethods, 2));
                Logger.InfoFormat("Visited Points {0} of {1} ({2})", coverageSession.Summary.VisitedSequencePoints,
                                  coverageSession.Summary.NumSequencePoints, coverageSession.Summary.SequenceCoverage);
                Logger.InfoFormat("Visited Branches {0} of {1} ({2})", coverageSession.Summary.VisitedBranchPoints,
                                  coverageSession.Summary.NumBranchPoints, coverageSession.Summary.BranchCoverage);

                Logger.Info("");
                Logger.Info(
                    "==== Alternative Results (includes all methods including those without corresponding source) ====");
                Logger.InfoFormat("Alternative Visited Classes {0} of {1} ({2})", results.AltVisitedClasses,
                                  results.AltTotalClasses, results.AltTotalClasses == 0 ? 0 : Math.Round(results.AltVisitedClasses * 100.0 / results.AltTotalClasses, 2));
                Logger.InfoFormat("Alternative Visited Methods {0} of {1} ({2})", results.AltVisitedMethods,
                                  results.AltTotalMethods, results.AltTotalMethods == 0 ? 0 : Math.Round(results.AltVisitedMethods * 100.0 / results.AltTotalMethods, 2));

                if (parser.ShowUnvisited)
                {
                    Logger.Info("");
                    Logger.Info("====Unvisited Classes====");
                    foreach (var unvisitedClass in results.UnvisitedClasses)
                    {
                        Logger.Info(unvisitedClass);
                    }

                    Logger.Info("");
                    Logger.Info("====Unvisited Methods====");
                    foreach (var unvisitedMethod in results.UnvisitedMethods)
                    {
                        Logger.Info(unvisitedMethod);
                    }
                }
            }
            else
            {
                Logger.Info("No results, this could be for a number of reasons. The most common reasons are:");
                Logger.Info("    1) missing PDBs for the assemblies that match the filter please review the");
                Logger.Info("    output file and refer to the Usage guide (Usage.rtf) about filters.");
                Logger.Info("    2) the profiler may not be registered correctly, please refer to the Usage");
                Logger.Info("    guide and the -register switch.");
                Logger.Info("    3) the user account for the process under test (e.g., app pool account) may");
                Logger.Info("    not have access to the registered profiler DLL.");
            }
        }
Пример #17
0
        private static void CalculateAndDisplayResults(CoverageSession coverageSession, ICommandLine parser)
        {
            if (!Logger.IsInfoEnabled)
            {
                return;
            }

            var results = new Results();

            if (coverageSession.Modules != null)
            {
                CalculateResults(coverageSession, results);
            }

            DisplayResults(coverageSession, parser, results);
        }
Пример #18
0
        public void Consolidate(CoverageSession coverageSession)
        {
            foreach (var module in coverageSession.Modules)
            {
                foreach (var moduleClass in module.Classes)
                {
                    foreach (var method in moduleClass.Methods)
                    {
                        ProcessMethod(method);
                    }

                    ProcessClass(moduleClass);
                }

                ProcessModule(module);
            }
        }
Пример #19
0
        /// <summary>
        /// Updates the coverage results for current OpenCover run on the UI thread.
        /// </summary>
        /// <param name="data">The CoverageSession data.</param>
        public void UpdateCoverageResults(CoverageSession data)
        {
            CoverageSession = data;

            if (CoverageSession != null)
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    CodeCoverageResultsTreeView.Root = new CoverageNode(data);
                    IsLoading = false;

                    if (NewCoverageDataAvailable != null)
                    {
                        NewCoverageDataAvailable(this, EventArgs.Empty);
                    }
                }), null);
            }
        }
Пример #20
0
        private static void CalculateResults(CoverageSession coverageSession, Results results)
        {
            foreach (var @class in
                     from module in coverageSession.Modules.Where(x => x.Classes != null)
                     from @class in module.Classes.Where(c => !c.ShouldSerializeSkippedDueTo())
                     select @class)
            {
                if (@class.Methods == null)
                {
                    continue;
                }

                if ([email protected](x => !x.ShouldSerializeSkippedDueTo() && x.SequencePoints.Any(y => y.VisitCount > 0)) &&
                    @class.Methods.Any(x => x.FileRef != null))
                {
                    results.unvisitedClasses.Add(@class.FullName);
                }

                if (@class.Methods.Any(x => x.Visited))
                {
                    results.altVisitedClasses += 1;
                    results.altTotalClasses   += 1;
                }
                else if (@class.Methods.Any())
                {
                    results.altTotalClasses += 1;
                }

                foreach (var method in @class.Methods.Where(x => !x.ShouldSerializeSkippedDueTo()))
                {
                    if (method.FileRef != null && !method.SequencePoints.Any(x => x.VisitCount > 0))
                    {
                        results.unvisitedMethods.Add(string.Format("{0}", method.FullName));
                    }

                    results.altTotalMethods += 1;
                    if (method.Visited)
                    {
                        results.altVisitedMethods += 1;
                    }
                }
            }
        }
Пример #21
0
        private void OnTestsFinished(object sender, EventArgs <TestReport> e)
        {
            if (e.Value.CoverageReport != null)
            {
                _report = e.Value.CoverageReport;

                var testMethods = e.Value.TestResults
                                  .Select(p => p.Method)
                                  .GroupBy(p => (p.Kind == CodeItemKind.Data ? p.Parent.FullName : p.FullName).CleanPath(true))
                                  .ToDictionary(p => p.Key, p => p.ToArray());

                _trackedMethods = _report.Modules
                                  .SelectMany(p => p.TrackedMethods)
                                  .Select(p => new { Id = p.Id, NameMatch = _visitorNameRegex.Match(p.Name), Name = p.Name })
                                  .DoIf(p => !p.NameMatch.Success, p => _telemetryManager.UploadExceptionAsync(new Exception("Could not parse tracked method name: " + p.Name)))
                                  .Where(p => p.NameMatch.Success)
                                  .ToDictionary(p => p.Id, p => testMethods.TryGetValue(p.NameMatch.Groups["visitorName"].Value.Replace("::", ".")) ?? new TestMethod[0]);

                CoverageUpdated?.Invoke(this, EventArgs.Empty);
            }
        }
        public void OutputCoverageReport(ITestResultAdaptor testResults = null, bool clearProgressBar = true)
        {
            if (!CommandLineManager.instance.batchmode)
            {
                EditorUtility.DisplayProgressBar(Styles.ProgressTitle.text, Styles.ProgressWritingFile.text, 0.95f);
            }

            CoverageSession coverageSession = GenerateOpenCoverSession();

            if (coverageSession != null && m_Writer != null)
            {
                m_Writer.CoverageSession = coverageSession;
                m_Writer.WriteCoverageSession();
            }
            else
            {
                ResultsLogger.Log(ResultID.Warning_NoCoverageResultsSaved);
            }

            if (clearProgressBar)
            {
                EditorUtility.ClearProgressBar();
            }
        }
Пример #23
0
 /// <summary>
 /// Clear the current coverage session data
 /// </summary>
 protected void ClearCoverageSession()
 {
     _moduleMethodMap.Clear();
     CoverageSession = new CoverageSession();
     InstrumentationPoint.Clear();
 }
Пример #24
0
 private void OnSolutionClosing(object sender, EventArgs e)
 {
     _report         = null;
     _trackedMethods = new Dictionary <int, TestMethod[]>();
 }
Пример #25
0
        private static FileCoverage GetFileCoverage(CoverageSession report, Dictionary <int, TestMethod[]> trackedMethods, string filePath)
        {
            if (report != null)
            {
                foreach (var module in report.Modules)
                {
                    var file = module.Files
                               .FirstOrDefault(p => StringComparer.OrdinalIgnoreCase.Equals(p.FullPath, filePath));

                    if (file == null)
                    {
                        continue;
                    }

                    var methods = module.Classes
                                  .SelectMany(p => p.Methods)
                                  .Where(p => p.FileRef != null && p.FileRef.Id == file.Id)
                                  .ToArray();

                    var sequenceGroups = methods
                                         .SelectMany(p => p.SequencePoints)
                                         .SelectMany(p => Enumerable
                                                     .Range(p.StartLine, p.EndLine - p.StartLine + 1)
                                                     .Select(q => new
                    {
                        LineNumber = q - 1,
                        VisitCount = p.VisitCount,
                        Start      = q == p.StartLine ? p.StartColumn - 1 : 0,
                        End        = q == p.EndLine ? p.EndColumn - 1 : int.MaxValue,
                        Visitors   = p.TrackedMethodRefs
                    }))
                                         .GroupBy(p => p.LineNumber)
                                         .ToDictionary(p => p.Key);

                    var branchGroups = methods
                                       .SelectMany(p => p.BranchPoints)
                                       .GroupBy(p => p.StartLine)
                                       .ToDictionary(p => p.Key - 1);

                    var affectedLines = sequenceGroups
                                        .Select(p => p.Key)
                                        .Concat(branchGroups.Select(p => p.Key))
                                        .Distinct();

                    var lineCoverages = new Dictionary <int, LineCoverage>();
                    foreach (var affectedLine in affectedLines)
                    {
                        var sequenceGroup = sequenceGroups.TryGetValue(affectedLine);
                        var branchGroup   = branchGroups.TryGetValue(affectedLine);

                        var visitCount    = sequenceGroup.Max(p => p.VisitCount);
                        var sequenceState =
                            sequenceGroup.All(p => p.VisitCount > 0) ? CoverageState.Covered :
                            (sequenceGroup.All(p => p.VisitCount == 0) ? CoverageState.Uncovered :
                             CoverageState.Mixed);
                        var unvisitedSections = sequenceGroup
                                                .Where(p => p.VisitCount == 0)
                                                .Select(p => new LineSection(p.Start, p.End))
                                                .ToArray();

                        var branchesVisited = branchGroup?
                                              .GroupBy(p => p.Offset)
                                              .Select(p => p
                                                      .OrderBy(q => q.Path)
                                                      .Select(q => q.VisitCount > 0)
                                                      .ToArray())
                                              .ToArray() ?? new bool[0][];
                        var branchPoints = branchesVisited.SelectMany(p => p).ToArray();
                        var branchState  =
                            branchPoints.All(p => p) ? CoverageState.Covered :
                            (branchPoints.All(p => !p) ? CoverageState.Uncovered :
                             CoverageState.Mixed);

                        var lineVisitors = new HashSet <TestMethod>(sequenceGroup
                                                                    .SelectMany(p => p.Visitors)
                                                                    .Select(p => p.Id)
                                                                    .Distinct()
                                                                    .Where(p => trackedMethods.ContainsKey(p))
                                                                    .SelectMany(p => trackedMethods[p]));

                        var branchVisitors = branchGroup?
                                             .GroupBy(p => p.Offset)
                                             .Select(p => p
                                                     .OrderBy(q => q.Path)
                                                     .Select(q => new HashSet <TestMethod>(q.TrackedMethodRefs
                                                                                           .Where(r => trackedMethods.ContainsKey(r.Id))
                                                                                           .SelectMany(r => trackedMethods[r.Id])))
                                                     .ToArray())
                                             .ToArray() ?? new HashSet <TestMethod> [0][];

                        var lineCoverage = new LineCoverage(visitCount, sequenceState, branchState, branchesVisited, unvisitedSections, lineVisitors, branchVisitors);
                        lineCoverages.Add(affectedLine, lineCoverage);
                    }

                    return(new FileCoverage(lineCoverages));
                }
            }

            return(FileCoverage.Empty);
        }
Пример #26
0
        public List <LineCoverageInfo> GetCoveredLinesFromCoverageSession(CoverageSession codeCoverage,
                                                                          string projectAssemblyName)
        {
            var coveredLines = new List <LineCoverageInfo>();

            List <Module> sessionModules = codeCoverage.Modules;

            _log.DebugFormat("GetCoveredLines for project: {0}", projectAssemblyName);
            _log.DebugFormat("Summary.NumSequencePoints: {0}", codeCoverage.Summary.NumSequencePoints);
            _log.DebugFormat("Summary.SequenceCoverage: {0}", codeCoverage.Summary.SequenceCoverage);
            _log.DebugFormat("Summary.VisitedSequencePoints: {0}", codeCoverage.Summary.VisitedSequencePoints);
            _log.DebugFormat("Summary.SequenceCoverage: {0}", codeCoverage.Summary.SequenceCoverage);
            _log.DebugFormat("Number of Modules: {0}", sessionModules.Count());

            foreach (Module sessionModule in sessionModules)
            {
                _log.DebugFormat("Module Name: {0}", sessionModule.ModuleName);

                IEnumerable <TrackedMethod> tests =
                    sessionModules.Where(x => x.TrackedMethods.Any()).SelectMany(y => y.TrackedMethods);

                if (sessionModule != null)
                {
                    List <Class> classes = sessionModule.Classes;

                    _log.DebugFormat("First Module Name: {0}", sessionModule.ModuleName);
                    _log.DebugFormat("Number of Classes: {0}", classes.Count());

                    foreach (var codeClass in classes)
                    {
                        List <Method> methods = codeClass.Methods;

                        foreach (var method in methods)
                        {
                            if (!method.Name.ToString().Contains("__") && !method.IsGetter && !method.IsSetter)
                            {
                                var fileNames = new List <File>();
                                if (method.FileRef != null)
                                {
                                    fileNames = sessionModule.Files.Where(x => x.UniqueId == method.FileRef.UniqueId).ToList();
                                }

                                string fileName;
                                if (fileNames.Any())
                                {
                                    fileName = fileNames.FirstOrDefault().FullPath;


                                    // remove closing paren
                                    // modifiedMethodName = modifiedMethodName.Substring(0, modifiedMethodName.Length - 1);
                                    // Raw: System.Void Quad.QuadMed.WebPortal.Domain.App_LocalResources.AssessmentQuestions::.ctor()
                                    // modified:        Quad.QuadMed.WebPortal.Domain.App_LocalResources.AssessmentQuestions..ctor
                                    //Needed:           Quad.QuadMed.WebPortal.Domain.App_LocalResources.AssessmentQuestions..ctor


                                    CodeMethodInfo methodInfo = UpdateMethodLocation(method, fileName);

                                    Queries.UpdateCodeMethodPath(methodInfo);

                                    ProcessSequencePoints(coveredLines, sessionModule, tests, codeClass, method, fileName);
                                }
                            }
                        }
                    }
                }
            }
            return(coveredLines);
        }
 private void UpdateSessionSummary(CoverageSession coverageSession)
 {
     UpdateSummary(coverageSession.Summary, coverageSession.Modules);
 }
        internal CoverageSession GenerateOpenCoverSession()
        {
            ResultsLogger.LogSessionItem("Started OpenCover Session", LogVerbosityLevel.Info);
            CoverageSession coverageSession = null;

            UInt32        fileUID    = 0;
            List <Module> moduleList = new List <Module>();

            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            float progressInterval = 0.9f / assemblies.Length;
            float currentProgress  = 0.0f;

            bool shouldGenerateAdditionalMetrics = m_ReporterFilter.ShouldGenerateAdditionalMetrics();

            foreach (Assembly assembly in assemblies)
            {
                string assemblyName = assembly.GetName().Name.ToLower();

                ResultsLogger.LogSessionItem($"Processing assembly: {assemblyName}", LogVerbosityLevel.Verbose);

                try
                {
                    if (assembly.GetCustomAttribute <ExcludeFromCoverageAttribute>() != null ||
                        assembly.GetCustomAttribute <ExcludeFromCodeCoverageAttribute>() != null)
                    {
                        ResultsLogger.LogSessionItem($"Excluded assembly (ExcludeFromCoverage): {assemblyName}", LogVerbosityLevel.Verbose);
                        continue;
                    }
                }
                catch
                {
                    ResultsLogger.Log(ResultID.Warning_ExcludeAttributeAssembly, assemblyName);
                }

                if (!CommandLineManager.instance.batchmode)
                {
                    EditorUtility.DisplayProgressBar(Styles.ProgressTitle.text, Styles.ProgressGatheringResults.text, currentProgress);
                }
                currentProgress += progressInterval;

                if (!m_ReporterFilter.ShouldProcessAssembly(assemblyName))
                {
                    ResultsLogger.LogSessionItem($"Excluded assembly (Assembly Filtering): {assemblyName}", LogVerbosityLevel.Verbose);
                    continue;
                }

                List <Class>  coveredClasses         = new List <Class>();
                List <string> filesNotFound          = new List <string>();
                Dictionary <string, UInt32> fileList = new Dictionary <string, UInt32>();
                Type[] assemblyTypes = null;
                m_ExcludedMethods = null;
                m_ExcludedTypes   = null;

                try
                {
                    assemblyTypes = assembly.GetTypes();
                }
                catch (ReflectionTypeLoadException ex)
                {
                    // This exception can be thrown if some of the types from this assembly can't be loaded. If this
                    // happens, the Types property array contains a Type for all loaded types and null for each
                    // type that couldn't be loaded.
                    assemblyTypes = ex.Types;
                    m_ReporterFilter.ShouldProcessAssembly(assemblyName);
                }

                // Debug.Assert(assemblyTypes != null)
                ResultsLogger.Log(ResultID.Assert_NullAssemblyTypes, assemblyTypes == null ? "0" : "1");

                ResultsLogger.LogSessionItem($"Processing included assembly: {assemblyName}", LogVerbosityLevel.Info);

                foreach (Type type in assemblyTypes)
                {
                    // The type can be null if the ReflectionTypeLoadException has been thrown previously.
                    if (type == null)
                    {
                        continue;
                    }

                    string className = type.FullName;
                    ResultsLogger.LogSessionItem($"Processing class: {className}", LogVerbosityLevel.Verbose);

                    try
                    {
                        if (type.GetCustomAttribute <ExcludeFromCoverageAttribute>() != null ||
                            type.GetCustomAttribute <ExcludeFromCodeCoverageAttribute>() != null ||
                            CheckIfParentMemberIsExcluded(type))
                        {
                            ResultsLogger.LogSessionItem($"Excluded class (ExcludeFromCoverage): {className}", LogVerbosityLevel.Verbose);
                            if (m_ExcludedTypes == null)
                            {
                                m_ExcludedTypes = new List <string>();
                            }
                            m_ExcludedTypes.Add(type.FullName);
                            continue;
                        }
                    }
                    catch
                    {
                        ResultsLogger.Log(ResultID.Warning_ExcludeAttributeClass, className, assemblyName);
                    }

                    ResultsLogger.LogSessionItem($"Processing included class: {className}", LogVerbosityLevel.Info);

                    CoveredMethodStats[] classMethodStatsArray = Coverage.GetStatsFor(type);
                    if (classMethodStatsArray.Length > 0)
                    {
                        List <Method> coveredMethods = new List <Method>();

                        foreach (CoveredMethodStats classMethodStats in classMethodStatsArray)
                        {
                            MethodBase method = classMethodStats.method;

                            if (method == null)
                            {
                                continue;
                            }

                            string methodName = method.Name;

                            ResultsLogger.LogSessionItem($"Processing method: {methodName}", LogVerbosityLevel.Verbose);

                            try
                            {
                                if (method.GetCustomAttribute <ExcludeFromCoverageAttribute>() != null ||
                                    method.GetCustomAttribute <ExcludeFromCodeCoverageAttribute>() != null ||
                                    CheckIfParentMemberIsExcluded(method))
                                {
                                    ResultsLogger.LogSessionItem($"Excluded method (ExcludeFromCoverage): {methodName}", LogVerbosityLevel.Verbose);
                                    if (m_ExcludedMethods == null)
                                    {
                                        m_ExcludedMethods = new List <MethodBase>();
                                    }
                                    m_ExcludedMethods.Add(method);
                                    continue;
                                }
                            }
                            catch
                            {
                                ResultsLogger.Log(ResultID.Warning_ExcludeAttributeMethod, methodName, className, assemblyName);
                            }

                            if (IsGetterSetterPropertyExcluded(method, type))
                            {
                                ResultsLogger.LogSessionItem($"Excluded method (ExcludeFromCoverage): {methodName}", LogVerbosityLevel.Verbose);
                                continue;
                            }

                            if (classMethodStats.totalSequencePoints > 0)
                            {
                                List <SequencePoint> coveredSequencePoints = new List <SequencePoint>();

                                uint fileId = 0;
                                CoveredSequencePoint[] classMethodSequencePointsArray = Coverage.GetSequencePointsFor(method);
                                foreach (CoveredSequencePoint classMethodSequencePoint in classMethodSequencePointsArray)
                                {
                                    string filename = classMethodSequencePoint.filename;
                                    if (filesNotFound.Contains(filename) || !m_ReporterFilter.ShouldProcessFile(filename))
                                    {
                                        ResultsLogger.LogSessionItem($"Excluded method (Path Filtering): {methodName}", LogVerbosityLevel.Verbose);
                                        continue;
                                    }

                                    if (!fileList.TryGetValue(filename, out fileId))
                                    {
                                        if (!File.Exists(filename))
                                        {
                                            filesNotFound.Add(filename);
                                            continue;
                                        }
                                        else
                                        {
                                            fileId = ++fileUID;
                                            fileList.Add(filename, fileId);
                                        }
                                    }

                                    SequencePoint coveredSequencePoint = new SequencePoint();
                                    coveredSequencePoint.FileId      = fileId;
                                    coveredSequencePoint.StartLine   = (int)classMethodSequencePoint.line;
                                    coveredSequencePoint.StartColumn = (int)classMethodSequencePoint.column;
                                    coveredSequencePoint.EndLine     = (int)classMethodSequencePoint.line;
                                    coveredSequencePoint.EndColumn   = (int)classMethodSequencePoint.column;
                                    coveredSequencePoint.VisitCount  = (int)classMethodSequencePoint.hitCount;
                                    coveredSequencePoint.Offset      = (int)classMethodSequencePoint.ilOffset;
                                    coveredSequencePoints.Add(coveredSequencePoint);
                                }

                                if (coveredSequencePoints.Count > 0)
                                {
                                    Method coveredMethod = new Method();
                                    coveredMethod.MetadataToken = method.MetadataToken;
                                    coveredMethod.FullName      = GenerateMethodName(method);
                                    coveredMethod.FileRef       = new FileRef()
                                    {
                                        UniqueId = fileId
                                    };
                                    coveredMethod.IsConstructor  = IsConstructor(method) || IsStaticConstructor(method);
                                    coveredMethod.IsStatic       = method.IsStatic;
                                    coveredMethod.IsSetter       = IsPropertySetter(method);
                                    coveredMethod.IsGetter       = IsPropertyGetter(method);
                                    coveredMethod.SequencePoints = coveredSequencePoints.ToArray();
                                    if (shouldGenerateAdditionalMetrics)
                                    {
                                        coveredMethod.CyclomaticComplexity = method.CalculateCyclomaticComplexity();
                                    }

                                    ResultsLogger.LogSessionItem($"Processing included method: {coveredMethod.FullName}", LogVerbosityLevel.Verbose);

                                    coveredMethods.Add(coveredMethod);
                                }
                            }
                        }

                        if (coveredMethods.Count > 0)
                        {
                            Class coveredClass = new Class();
                            coveredClass.FullName = GenerateTypeName(type);
                            coveredClass.Methods  = coveredMethods.ToArray();
                            coveredClasses.Add(coveredClass);
                        }
                    }
                }

                if (coveredClasses.Count != 0)
                {
                    Module module = new Module();
                    module.ModuleName = assembly.GetName().Name;
                    List <ModelFile> coveredFileList = new List <ModelFile>();
                    foreach (KeyValuePair <string, UInt32> fileEntry in fileList)
                    {
                        ModelFile coveredFile = new ModelFile();
                        coveredFile.FullPath = CoverageUtils.NormaliseFolderSeparators(fileEntry.Key);
                        if (CommandLineManager.instance.pathStrippingSpecified)
                        {
                            coveredFile.FullPath = CommandLineManager.instance.pathStripping.StripPath(coveredFile.FullPath);
                        }
                        coveredFile.UniqueId = fileEntry.Value;

                        coveredFileList.Add(coveredFile);
                    }
                    module.Files   = coveredFileList.ToArray();
                    module.Classes = coveredClasses.ToArray();
                    moduleList.Add(module);
                }
            }

            if (moduleList.Count > 0)
            {
                coverageSession         = new CoverageSession();
                coverageSession.Modules = moduleList.ToArray();
                ProcessGenericMethods(coverageSession);

                foreach (Module coveredModule in moduleList)
                {
                    foreach (Class coveredClass in coveredModule.Classes)
                    {
                        foreach (Method coveredMethod in coveredClass.Methods)
                        {
                            UpdateMethodSummary(coveredMethod);
                        }
                        UpdateClassSummary(coveredClass);
                    }
                    UpdateModuleSummary(coveredModule);
                }

                UpdateSessionSummary(coverageSession);
            }

            ResultsLogger.LogSessionItem("Finished OpenCover Session", LogVerbosityLevel.Info);

            return(coverageSession);
        }
Пример #29
0
        public List <LineCoverageInfo> GetRetestedLinesFromCoverageSession(CoverageSession coverageSession,
                                                                           string projectAssemblyName, List <int> uniqueIds)
        {
            /// todo need to figure out how to remove a CoveredLine if an Edit has made it uncovered,
            /// without removing all lines that were not covered by the subset of tests that were run
            /// One option: check all previously covered lines from this group of tests with the current covered lines list from this group.
            ///
            //todo Handle case where a line that was "Code" and was covered is now not "Code"
            var coveredLines = new List <LineCoverageInfo>();

            List <Module> sessionModules      = coverageSession.Modules;
            Module        module              = sessionModules.FirstOrDefault(x => x.ModuleName.Equals(projectAssemblyName));
            Module        testModule          = sessionModules.FirstOrDefault(x => x.ModuleName.Equals(projectAssemblyName + ".Test"));
            IEnumerable <TrackedMethod> tests =
                sessionModules.Where(x => x.TrackedMethods.Any()).SelectMany(y => y.TrackedMethods);
            var testsRun      = tests.Where(x => uniqueIds.Contains((int)x.UniqueId));
            var methodsTested = module.Classes.SelectMany(m => m.Methods).Where(x => x.SequenceCoverage > 0);

            if (module != null)
            {
                var codeModule = new CodeModule
                {
                    Name    = module.FullName,
                    Summary = new Summary(module.Summary)
                };

                List <Class> classes = module.Classes;

                _log.DebugFormat("First Module Name: {0}", module.ModuleName);
                _log.DebugFormat("Number of Classes: {0}", classes.Count());


                foreach (Method method in methodsTested)
                {
                    string methodName = method.Name.ToString();
                    if (!methodName.Contains("_") &&
                        !methodName.StartsWith("get_") &&
                        !methodName.StartsWith("set_"))
                    {
                        var codeMethod = new CodeMethod
                        {
                            Name    = method.Name,
                            Summary = new Summary(method.Summary)
                        };
                        string modifiedMethodName = methodName;
                        if (method.IsConstructor)
                        {
                            modifiedMethodName = ConvertTrackedMethodFormatToUnitTestFormat(methodName);
                            modifiedMethodName = modifiedMethodName.Replace("..", "::.");
                        }



                        var modelClass = module.Classes.FirstOrDefault(x => x.Methods.Any(y => y.Name.Contains(modifiedMethodName)));



                        var codeClass = new CodeClass
                        {
                            Name    = modelClass.FullName,
                            Summary = new Summary(modelClass.Summary)
                        };

                        List <SequencePoint> sequencePoints = method.SequencePoints;
                        foreach (SequencePoint sequencePoint in sequencePoints)
                        {
                            if (testsRun.Any())
                            {
                                var coveredLine = new LineCoverageInfo
                                {
                                    IsCode     = true,
                                    LineNumber = sequencePoint.StartLine,
                                    IsCovered  = (sequencePoint.VisitCount > 0),
                                    Module     = codeModule,
                                    Class      = codeClass,
                                    ClassName  = modelClass.FullName,
                                    Method     = codeMethod,
                                    FileName   = module.Files.FirstOrDefault(x => x.UniqueId == method.FileRef.UniqueId).FullPath,
                                    MethodName = method.Name
                                                 //   UnitTests = testsRun.ToList()
                                };

                                foreach (TrackedMethodRef trackedMethodRef in sequencePoint.TrackedMethodRefs)
                                {
                                    IEnumerable <TrackedMethod> testsThatCoverLine =
                                        testsRun.Where(y => y.UniqueId.Equals(trackedMethodRef.UniqueId));
                                    var fileNames = module.Files.Where(x => x.UniqueId == method.FileRef.UniqueId).ToList();
                                    foreach (TrackedMethod test in testsThatCoverLine)
                                    {
                                        coveredLine.IsCode = true;

                                        coveredLine.IsCovered = (sequencePoint.VisitCount > 0);

                                        coveredLine.TrackedMethods.Add(new Poco.TrackedMethod
                                        {
                                            UniqueId      = (int)test.UniqueId,
                                            MetadataToken = method.MetadataToken,
                                            Strategy      = test.Strategy,
                                            Name          = test.Name
                                        });
                                    }
                                }
                                if (coveredLine.Class == null)
                                {
                                    _log.ErrorFormat("CoveredLine.Class is null for method:{0}", method.Name);
                                }
                                coveredLines.Add(coveredLine);
                            }
                        }
                    }
                }
            }

            return(coveredLines);
        }
Пример #30
0
 public CoverageNode(CoverageSession coverageSession)
 {
     _coverageSession = coverageSession;
     LazyLoading      = true;
 }