public CoverageReporterManager(CoverageSettings coverageSettings)
 {
     m_CoverageSettings = coverageSettings;
 }
 protected CoverageResultWriterBase(CoverageSettings coverageSettings)
 {
     m_CoverageSettings = coverageSettings;
 }
        static CoverageReporterStarter()
        {
#if NO_COV_EDITORPREF
            if (!CommandLineManager.instance.runFromCommandLine)
            {
                bool localCoverageEnabled = CoveragePreferences.instance.GetBool("EnableCodeCoverage", false);
                if (localCoverageEnabled != Coverage.enabled)
                {
                    Coverage.enabled = localCoverageEnabled;
                }

                PackageManager.Events.registeringPackages += OnRegisteringPackages;
            }
#endif
            if (!Coverage.enabled)
            {
                return;
            }

#if CONDITIONAL_IGNORE_SUPPORTED
            ConditionalIgnoreAttribute.AddConditionalIgnoreMapping("IgnoreForCoverage", true);
#endif
            CoverageReporterListener listener = ScriptableObject.CreateInstance <CoverageReporterListener>();

#if TEST_FRAMEWORK_1_2_OR_NEWER
            TestRunnerApi.RegisterTestCallback(listener);
#else
            TestRunnerApi api = ScriptableObject.CreateInstance <TestRunnerApi>();
            api.RegisterCallbacks(listener);
#endif

            CoverageSettings coverageSettings = new CoverageSettings()
            {
                resultsPathFromCommandLine = CommandLineManager.instance.coverageResultsPath,
                historyPathFromCommandLine = CommandLineManager.instance.coverageHistoryPath
            };

            CoverageReporterManager = new CoverageReporterManager(coverageSettings);

            listener.CoverageReporterManager = CoverageReporterManager;

            AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload;
            AssemblyReloadEvents.afterAssemblyReload  += OnAfterAssemblyReload;

            // Generate a report if running from the command line,
            // generateHTMLReport or generateBadgeReport is passed to -coverageOptions
            // and -runTests has not been passed to the command line,
            // regardless if AutoGenerate is selected in the UI
            if (CommandLineManager.instance.runFromCommandLine &&
                CoverageReporterManager.ShouldAutoGenerateReport(false) &&
                !CommandLineManager.instance.runTests &&
                !CoverageRunData.instance.reportWasGenerated)
            {
                // Start the timer for analytics for Report only
                CoverageAnalytics.instance.StartTimer();
                CoverageAnalytics.instance.CurrentCoverageEvent.actionID = ActionID.ReportOnly;

                coverageSettings.rootFolderPath    = CoverageUtils.GetRootFolderPath(coverageSettings);
                coverageSettings.historyFolderPath = CoverageUtils.GetHistoryFolderPath(coverageSettings);

                CoverageReporterManager.ReportGenerator.Generate(coverageSettings);
            }
        }
        public void Generate(CoverageSettings coverageSettings)
        {
            CoverageRunData.instance.ReportGenerationStart();

            if (coverageSettings == null)
            {
                EditorUtility.ClearProgressBar();
                ResultsLogger.Log(ResultID.Warning_FailedReportNullCoverageSettings);
                CoverageRunData.instance.ReportGenerationEnd(false);
                return;
            }

            string includeAssemblies = string.Empty;

            if (CommandLineManager.instance.batchmode && !CommandLineManager.instance.useProjectSettings)
            {
                includeAssemblies = CommandLineManager.instance.assemblyFiltering.includedAssemblies;
            }
            else
            {
                includeAssemblies = CommandLineManager.instance.assemblyFiltersSpecified ?
                                    CommandLineManager.instance.assemblyFiltering.includedAssemblies :
                                    CoveragePreferences.instance.GetString("IncludeAssemblies", AssemblyFiltering.GetUserOnlyAssembliesString());
            }

            // If override for include assemblies is set in coverageSettings, use overrideIncludeAssemblies instead
            if (!String.IsNullOrEmpty(coverageSettings.overrideIncludeAssemblies))
            {
                includeAssemblies = coverageSettings.overrideIncludeAssemblies;
            }

            string[] includeAssembliesArray = includeAssemblies.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < includeAssembliesArray.Length; i++)
            {
                includeAssembliesArray[i] = "+" + includeAssembliesArray[i];
            }
            string assemblies = string.Join(",", includeAssembliesArray);

            string[] assemblyFilters = assemblies.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            if (assemblyFilters.Length == 0)
            {
                EditorUtility.ClearProgressBar();
                ResultsLogger.Log(ResultID.Error_FailedReportNoAssemblies);
                CoverageRunData.instance.ReportGenerationEnd(false);
                return;
            }

            string rootFolderPath = coverageSettings.rootFolderPath;

            if (!CoverageUtils.DoesFolderExistAndNotEmpty(rootFolderPath) || CoverageUtils.GetNumberOfFilesInFolder(rootFolderPath, "*.xml", SearchOption.AllDirectories) == 0)
            {
                EditorUtility.ClearProgressBar();
                ResultsLogger.Log(ResultID.Error_FailedReportNoTests);
                CoverageRunData.instance.ReportGenerationEnd(false);
                return;
            }

            // Only include xml files with the correct filename format
            string sourceXmlPath           = CoverageUtils.JoinPaths(rootFolderPath, "**");
            string testResultsXmlPath      = CoverageUtils.JoinPaths(sourceXmlPath, "TestCoverageResults_????.xml");
            string recordingResultsXmlPath = CoverageUtils.JoinPaths(sourceXmlPath, "RecordingCoverageResults_????.xml");

            string[] reportFilePatterns = new string[] { testResultsXmlPath, recordingResultsXmlPath };

            bool includeHistoryInReport = CommandLineManager.instance.batchmode && !CommandLineManager.instance.useProjectSettings ?
                                          CommandLineManager.instance.generateHTMLReportHistory :
                                          CommandLineManager.instance.generateHTMLReportHistory || CoveragePreferences.instance.GetBool("IncludeHistoryInReport", true);

            string historyDirectory = includeHistoryInReport ? coverageSettings.historyFolderPath : null;

            string targetDirectory = CoverageUtils.JoinPaths(rootFolderPath, CoverageSettings.ReportFolderName);

            string[] sourceDirectories = CommandLineManager.instance.sourcePathsSpecified ? CommandLineManager.instance.sourcePaths.Split(',') : new string[] { };

            bool generateHTMLReport = CommandLineManager.instance.batchmode && !CommandLineManager.instance.useProjectSettings ?
                                      CommandLineManager.instance.generateHTMLReport :
                                      CommandLineManager.instance.generateHTMLReport || CoveragePreferences.instance.GetBool("GenerateHTMLReport", true);

            if (coverageSettings.overrideGenerateReport)
            {
                generateHTMLReport = true;
            }

            bool generateBadge = CommandLineManager.instance.batchmode && !CommandLineManager.instance.useProjectSettings ?
                                 CommandLineManager.instance.generateBadgeReport :
                                 CommandLineManager.instance.generateBadgeReport || CoveragePreferences.instance.GetBool("GenerateBadge", true);

            string reportTypesString = "xmlSummary,";

            if (generateHTMLReport)
            {
                reportTypesString += "Html,";
            }
            if (generateBadge)
            {
                reportTypesString += "Badges,";
            }

            string[] reportTypes = reportTypesString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] plugins     = new string[] { };

            bool includeAdditionalMetrics = CommandLineManager.instance.batchmode && !CommandLineManager.instance.useProjectSettings ?
                                            CommandLineManager.instance.generateAdditionalMetrics :
                                            CommandLineManager.instance.generateAdditionalMetrics || CoveragePreferences.instance.GetBool("GenerateAdditionalMetrics", false);

            string[] classFilters   = new string[] { };
            string[] fileFilters    = new string[] { };
            string   verbosityLevel = null;
            string   tag            = null;

            ReportConfiguration config = new ReportConfiguration(
                reportFilePatterns,
                targetDirectory,
                sourceDirectories,
                historyDirectory,
                reportTypes,
                plugins,
                assemblyFilters,
                classFilters,
                fileFilters,
                verbosityLevel,
                tag);

            DebugFactory loggerFactory = new DebugFactory();

            LoggerFactory.Configure(loggerFactory);

            try
            {
                if (!CommandLineManager.instance.batchmode)
                {
                    EditorUtility.DisplayProgressBar(ReportGeneratorStyles.ProgressTitle.text, ReportGeneratorStyles.ProgressInfo.text, 0.4f);
                }

                if (Directory.Exists(targetDirectory))
                {
                    Directory.Delete(targetDirectory, true);
                }

                Generator generator = new Generator();
                if (generator.GenerateReport(config, new Settings()
                {
                    DisableRiskHotspots = !includeAdditionalMetrics
                }, new RiskHotspotsAnalysisThresholds()))
                {
                    ResultsLogger.Log(ResultID.Log_ReportSaved, targetDirectory);

                    CoverageRunData.instance.ReportGenerationEnd(true);

                    // Send Analytics event (Report Only / Data & Report)
                    CoverageAnalytics.instance.SendCoverageEvent(true);

                    if (!CommandLineManager.instance.batchmode &&
                        coverageSettings.revealReportInFinder)
                    {
                        string indexHtm = CoverageUtils.JoinPaths(targetDirectory, "index.htm");
                        if (File.Exists(indexHtm))
                        {
                            EditorUtility.RevealInFinder(indexHtm);
                        }
                        else
                        {
                            EditorUtility.RevealInFinder(targetDirectory);
                        }
                    }
                }
                else
                {
                    ResultsLogger.Log(ResultID.Error_FailedReport);

                    CoverageRunData.instance.ReportGenerationEnd(false);
                }
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }