/// <summary>
        /// Generates summary reports for LegacyTeamBuild and for Build Vnext
        /// </summary>
        public void GenerateReports(TeamBuildSettings settings, AnalysisConfig config, ProjectInfoAnalysisResult result, ILogger logger)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            this.settings = settings;
            this.config = config;
            this.result = result;
            this.logger = logger;

            this.GenerateReports();
        }
 private ProjectInfoReportBuilder(AnalysisConfig config, ProjectInfoAnalysisResult result, ILogger logger)
 {
     this.config = config;
     this.analysisResult = result;
     this.logger = logger;
     this.sb = new StringBuilder();
 }
        /* for test purposes */
        public static SummaryReportData CreateSummaryData(
            AnalysisConfig config,
            ProjectInfoAnalysisResult result)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            SummaryReportData summaryData = new SummaryReportData();

            summaryData.SkippedProjects = GetProjectsByStatus(result, ProjectInfoValidity.NoFilesToAnalyze).Count();
            summaryData.InvalidProjects = GetProjectsByStatus(result, ProjectInfoValidity.InvalidGuid).Count();
            summaryData.InvalidProjects += GetProjectsByStatus(result, ProjectInfoValidity.DuplicateGuid).Count();

            summaryData.ExcludedProjects = GetProjectsByStatus(result, ProjectInfoValidity.ExcludeFlagSet).Count();
            IEnumerable<ProjectInfo> validProjects = GetProjectsByStatus(result, ProjectInfoValidity.Valid);
            summaryData.ProductProjects = validProjects.Count(p => p.ProjectType == ProjectType.Product);
            summaryData.TestProjects = validProjects.Count(p => p.ProjectType == ProjectType.Test);

            summaryData.Succeeded = result.RanToCompletion;

            summaryData.DashboardUrl = GetSonarDashboadUrl(config);
            summaryData.ProjectDescription = string.Format(System.Globalization.CultureInfo.CurrentCulture,
                Resources.Report_SonarQubeProjectDescription, config.SonarProjectName, config.SonarProjectKey, config.SonarProjectVersion);
            return summaryData;
        }
        public void SummaryReport_AllTypesOfProjects()
        {
            // Arrange
            string hostUrl = "http://mySonarQube:9000";
            ProjectInfoAnalysisResult result = new ProjectInfoAnalysisResult() { RanToCompletion = true };
            AnalysisConfig config = new AnalysisConfig() { SonarProjectKey = "", SonarQubeHostUrl = hostUrl };

            AddProjectInfoToResult(result, ProjectInfoValidity.DuplicateGuid, count: 3);
            AddProjectInfoToResult(result, ProjectInfoValidity.ExcludeFlagSet, type: ProjectType.Product, count: 4);
            AddProjectInfoToResult(result, ProjectInfoValidity.ExcludeFlagSet, type: ProjectType.Test , count: 1);
            AddProjectInfoToResult(result, ProjectInfoValidity.InvalidGuid, type: ProjectType.Product, count: 7);
            AddProjectInfoToResult(result, ProjectInfoValidity.InvalidGuid, type: ProjectType.Test , count: 8);
            AddProjectInfoToResult(result, ProjectInfoValidity.NoFilesToAnalyze, count: 11);
            AddProjectInfoToResult(result, ProjectInfoValidity.Valid, type: ProjectType.Product, count: 13);
            AddProjectInfoToResult(result, ProjectInfoValidity.Valid, type: ProjectType.Test, count: 17);

            // Act
            var summaryReportData = SummaryReportBuilder.CreateSummaryData(config, result);

            // Assert
            VerifySummaryReportData(summaryReportData, result, hostUrl, config);
            VerifySummaryProjectCounts(
                summaryReportData,
                expectedExcludedProjects: 5, // ExcludeFlagSet
                expectedInvalidProjects: 18, // InvalidGuid + DuplicateGuid
                expectedSkippedProjects: 11, // No files to analyze
                expectedProductProjects: 13,
                expectedTestProjects: 17);
        }
        public static void WriteSummaryReport(AnalysisConfig config, ProjectInfoAnalysisResult result, ILogger logger)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            ProjectInfoReportBuilder builder = new ProjectInfoReportBuilder(config, result, logger);
            builder.Generate();
        }
        public static void WriteSummaryReport(AnalysisConfig config, ProjectInfoAnalysisResult result, ILogger logger)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            ProjectInfoReportBuilder builder = new ProjectInfoReportBuilder(config, result, logger);

            builder.Generate();
        }
        /// <summary>
        /// Locates the ProjectInfo.xml files and uses the information in them to generate
        /// a sonar-runner properties file
        /// </summary>
        /// <returns>Information about each of the project info files that was processed, together with
        /// the full path to generated file.
        /// Note: the path to the generated file will be null if the file could not be generated.</returns>
        public static ProjectInfoAnalysisResult GenerateFile(AnalysisConfig config, ILogger logger)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            string fileName = Path.Combine(config.SonarOutputDir, ProjectPropertiesFileName);

            logger.LogMessage(Resources.DIAG_GeneratingProjectProperties, fileName);

            IEnumerable <ProjectInfo> projects = ProjectLoader.LoadFrom(config.SonarOutputDir);

            if (projects == null || !projects.Any())
            {
                logger.LogError(Resources.ERR_NoProjectInfoFilesFound);
                return(new ProjectInfoAnalysisResult());
            }

            PropertiesWriter writer = new PropertiesWriter(config);

            ProjectInfoAnalysisResult result = ProcessProjectInfoFiles(projects, writer, logger);

            IEnumerable <ProjectInfo> validProjects = result.GetProjectsByStatus(ProjectInfoValidity.Valid);

            if (validProjects.Any())
            {
                string contents = writer.Flush();

                result.FullPropertiesFilePath = fileName;
                File.WriteAllText(result.FullPropertiesFilePath, contents, Encoding.ASCII);
            }
            else
            {
                logger.LogError(Resources.ERR_NoValidProjectInfoFiles);
            }
            return(result);
        }
        public void SummaryReport_NoProjects()
        {
            // Arrange
            string hostUrl = "http://mySonarQube:9000";
            ProjectInfoAnalysisResult result = new ProjectInfoAnalysisResult() { RanToCompletion = false };
            AnalysisConfig config = new AnalysisConfig() { SonarProjectKey = "Foo" , SonarQubeHostUrl = hostUrl };

            // Act
            var summaryReportData = SummaryReportBuilder.CreateSummaryData(config, result);

            // Assert
            VerifySummaryReportData(summaryReportData, result, hostUrl, config);
            VerifySummaryProjectCounts(
                summaryReportData,
                expectedExcludedProjects: 0,
                expectedInvalidProjects: 0,
                expectedSkippedProjects: 0,
                expectedProductProjects: 0,
                expectedTestProjects: 0);
        }
Esempio n. 9
0
        public ProjectInfoAnalysisResult Execute(AnalysisConfig config, IEnumerable <string> userCmdLineArguments, ILogger logger)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (userCmdLineArguments == null)
            {
                throw new ArgumentNullException("cmdLineArguments");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            ProjectInfoAnalysisResult result = PropertiesFileGenerator.GenerateFile(config, logger);

            Debug.Assert(result != null, "Not expecting the file generator to return null");

            ProjectInfoReportBuilder.WriteSummaryReport(config, result, logger);

            result.RanToCompletion = false;

            if (result.FullPropertiesFilePath == null)
            {
                // We expect a detailed error message to have been logged explaining
                // why the properties file generation could not be performed
                logger.LogInfo(Resources.MSG_PropertiesGenerationFailed);
            }
            else
            {
                string exeFileName = FindRunnerExe(config, logger);
                if (exeFileName != null)
                {
                    result.RanToCompletion = ExecuteJavaRunner(config, userCmdLineArguments, logger, exeFileName, result.FullPropertiesFilePath);
                }
            }

            return(result);
        }
Esempio n. 10
0
        public static int Main(string[] args)
        {
            ConsoleLogger logger = new ConsoleLogger();

            if (args == null || args.Length != 1)
            {
                logger.LogError(Resources.ERR_InvalidCommandLineArgs);
                return(1);
            }

            AnalysisConfig config = TryGetAnalysisConfig(args[0], logger);

            if (config == null)
            {
                return(1);
            }

            ISonarRunner runnerShim          = new SonarRunnerWrapper();
            ProjectInfoAnalysisResult result = runnerShim.Execute(config, logger);

            return(result.RanToCompletion ? 0 : 1);
        }
        public void SummaryReport_WithBranch()
        {
            // Arrange
            string hostUrl = "http://mySonarQube:9000";
            ProjectInfoAnalysisResult result = new ProjectInfoAnalysisResult { RanToCompletion = false };
            AnalysisConfig config = new AnalysisConfig() { SonarProjectKey = "Foo", SonarQubeHostUrl = hostUrl };
            config.LocalSettings = new AnalysisProperties();
            config.LocalSettings.Add(new Property() { Id = SonarProperties.Branch, Value = "master" });
            AddProjectInfoToResult(result, ProjectInfoValidity.Valid, type: ProjectType.Product, count: 4);

            // Act
            var summaryReportData = SummaryReportBuilder.CreateSummaryData(config, result);

            // Assert
            VerifySummaryReportData(summaryReportData, result, hostUrl, config);
            VerifySummaryProjectCounts(
                summaryReportData,
                expectedExcludedProjects: 0,
                expectedInvalidProjects: 0,
                expectedSkippedProjects: 0,
                expectedProductProjects: 4,
                expectedTestProjects: 0);
        }
        private static void VerifySummaryReportData(
            SummaryReportBuilder.SummaryReportData summaryReportData,
            ProjectInfoAnalysisResult analysisResult,
            string expectedHostUrl,
            AnalysisConfig config)
        {
            string expectedUrl;
            string branch;

            config.GetAnalysisSettings(false).TryGetValue("sonar.branch", out branch);

            if (String.IsNullOrEmpty(branch))
            {
                expectedUrl = String.Format(
                    SummaryReportBuilder.DashboardUrlFormat, 
                    expectedHostUrl, 
                    config.SonarProjectKey);
            }
            else
            {
                expectedUrl = String.Format(
                    SummaryReportBuilder.DashboardUrlFormatWithBranch, 
                    expectedHostUrl, 
                    config.SonarProjectKey, 
                    branch);
            }

            Assert.AreEqual(expectedUrl, summaryReportData.DashboardUrl, "Invalid dashboard url");
            Assert.AreEqual(analysisResult.RanToCompletion, summaryReportData.Succeeded, "Invalid outcome");

        }
        public void GenerateReports(TeamBuildSettings settings, AnalysisConfig config, ProjectInfoAnalysisResult result, ILogger logger)
        {
            Assert.IsFalse(methodCalled, "Generate reports has already been called");

            this.methodCalled = true;
        }
        private static void InternalExecute(AnalysisConfig config, IEnumerable<string> userCmdLineArguments, ILogger logger, ProjectInfoAnalysisResult result)
        {
            ProjectInfoReportBuilder.WriteSummaryReport(config, result, logger);

            if (result.FullPropertiesFilePath == null)
            {
                // We expect a detailed error message to have been logged explaining
                // why the properties file generation could not be performed
                logger.LogInfo(Resources.MSG_PropertiesGenerationFailed);
            }
            else
            {
                string exeFileName = FindRunnerExe(config, logger);
                if (exeFileName != null)
                {
                    result.RanToCompletion = ExecuteJavaRunner(config, userCmdLineArguments, logger, exeFileName, result.FullPropertiesFilePath);
                }
            }
        }
        private static ProjectInfoAnalysisResult ProcessProjectInfoFiles(IEnumerable<ProjectInfo> projects, PropertiesWriter writer, ILogger logger)
        {
            ProjectInfoAnalysisResult result = new ProjectInfoAnalysisResult();

            foreach (ProjectInfo projectInfo in projects)
            {
                ProjectInfoValidity status = ClassifyProject(projectInfo, projects, logger);

                if (status == ProjectInfoValidity.Valid)
                {
                    IEnumerable<string> files = GetFilesToAnalyze(projectInfo, logger);
                    if (files == null || !files.Any())
                    {
                        status = ProjectInfoValidity.NoFilesToAnalyze;
                    }
                    else
                    {
                        string fxCopReport = TryGetFxCopReport(projectInfo, logger);
                        string vsCoverageReport = TryGetCodeCoverageReport(projectInfo, logger);
                        writer.WriteSettingsForProject(projectInfo, files, fxCopReport, vsCoverageReport);
                    }
                }

                result.Projects.Add(projectInfo, status);
            }
            return result;
        }
 private static IEnumerable<ProjectInfo> GetProjectsByStatus(ProjectInfoAnalysisResult result, ProjectInfoValidity status)
 {
     return result.Projects.Where(p => p.Value == status).Select(p => p.Key);
 }
        public void SummaryReport_ReportIsGenerated()
        {
            // Arrange
            string hostUrl = "http://mySonarQube:9000";
            ProjectInfoAnalysisResult result = new ProjectInfoAnalysisResult();
            AnalysisConfig config = new AnalysisConfig() { SonarProjectKey = "Foo" , SonarQubeHostUrl = hostUrl};

            TeamBuildSettings settings = TeamBuildSettings.CreateNonTeamBuildSettings(this.TestContext.DeploymentDirectory);
            config.SonarOutputDir = TestContext.TestDeploymentDir; // this will be cleaned up by VS when there are too many results
            string expectedReportPath = Path.Combine(TestContext.TestDeploymentDir, SummaryReportBuilder.SummaryMdFilename);

            // Act
            SummaryReportBuilder builder = new SummaryReportBuilder();
            builder.GenerateReports(settings, config, result, new TestLogger());

            // Assert
            Assert.IsTrue(File.Exists(expectedReportPath) && (new FileInfo(expectedReportPath)).Length > 0, "The report file cannot be found or is empty");
        }
 private static void AddProjectInfoToResult(ProjectInfoAnalysisResult result, ProjectInfoValidity validity, ProjectType type = ProjectType.Product, uint count = 1)
 {
     for (int i = 0; i < count; i++)
     {
         ProjectInfo pi = new ProjectInfo() { ProjectType = type };
         result.Projects[pi] = validity;
     }
 }
        private static void InternalExecute(AnalysisConfig config, IEnumerable <string> userCmdLineArguments, ILogger logger, ProjectInfoAnalysisResult result)
        {
            ProjectInfoReportBuilder.WriteSummaryReport(config, result, logger);

            if (result.FullPropertiesFilePath == null)
            {
                // We expect a detailed error message to have been logged explaining
                // why the properties file generation could not be performed
                logger.LogInfo(Resources.MSG_PropertiesGenerationFailed);
            }
            else
            {
                string exeFileName = FindRunnerExe(config, logger);
                if (exeFileName != null)
                {
                    result.RanToCompletion = ExecuteJavaRunner(config, userCmdLineArguments, logger, exeFileName, result.FullPropertiesFilePath);
                }
            }
        }
        private static void VerifySummaryReportData(
            SummaryReportBuilder.SummaryReportData summaryReportData,
            ProjectInfoAnalysisResult analysisResult,
            string expectedHostUrl,
            AnalysisConfig config)
        {
            Assert.AreEqual(
                            String.Format(SummaryReportBuilder.DashboardUrlFormat, expectedHostUrl, config.SonarProjectKey),
                            summaryReportData.DashboardUrl,
                            "Invalid dashboard url");

            Assert.AreEqual(analysisResult.RanToCompletion, summaryReportData.Succeeded, "Invalid outcome");
        }