Example #1
0
        private SonarConverter GetSonarConverter(ISonarReportGenerator sonarReportGenerator, Options options)
        {
            var sonarConverter = new SonarConverter(options);
            var mockSonarReportGeneratorFactory = new Mock <SonarReportGeneratorFactory>();

            mockSonarReportGeneratorFactory.Setup(
                f => f.GetGenerator(It.IsAny <SonarOutputFormat>())).Returns(sonarReportGenerator);
            sonarConverter.SonarReportGeneratorFactory = mockSonarReportGeneratorFactory.Object;

            return(sonarConverter);
        }
Example #2
0
        public void ConvertToAbsolutePaths_ShouldConvertAllRelativePaths(string solutionFile, string solutionDir)
        {
            var report = new Report
            {
                Information = new ReportInformation
                {
                    Solution = solutionFile
                },
                Issues = new []
                {
                    new ReportProject
                    {
                        Issue = new []
                        {
                            new ReportProjectIssue
                            {
                                File = "path1"
                            },
                            new ReportProjectIssue
                            {
                                File = "path2"
                            }
                        }
                    },
                    new ReportProject
                    {
                        Issue = new []
                        {
                            new ReportProjectIssue
                            {
                                File = "path3"
                            }
                        }
                    }
                }
            };

            SonarConverter.ConvertToAbsolutePaths(report);

            Assert.Equal(Path.Combine(solutionDir, "path1"), report.Issues[0].Issue[0].File);
            Assert.Equal(Path.Combine(solutionDir, "path2"), report.Issues[0].Issue[1].File);
            Assert.Equal(Path.Combine(solutionDir, "path3"), report.Issues[1].Issue[0].File);
        }
Example #3
0
        public void SonarReportGenerator_WithValidInput_ShouldProduceCorrectReport(
            SonarOutputFormat outputFormat,
            string expectedResultFile)
        {
            Report reSharperReport;

            using (var reader = new StreamReader("Resources/ReSharperTestReport.xml"))
            {
                reSharperReport = (Report)ReSharperXmlSerializer.Deserialize(reader);
            }

            SonarConverter.ConvertToAbsolutePaths(reSharperReport);

            var reportGeneratorFactory = new SonarReportGeneratorFactory();
            var reportGenerator        = reportGeneratorFactory.GetGenerator(outputFormat);

            // JArray.FromObject is not used, because serializer settings cannot be provided
            var actualSonarReports = JArray.Parse(
                JsonConvert.SerializeObject(reportGenerator.Generate(reSharperReport), JsonSerializerSettings));

            FixReportPaths(actualSonarReports, outputFormat);

            var convertSourceRootFunc = outputFormat == SonarOutputFormat.Roslyn
                ? (Func <string, string>)FileUtils.FilePathToFileUrl : x => x.Replace("\\", "/");

            var sourceRoot = Directory.GetCurrentDirectory();
            var expectedSonarReportsAsString =
                File.ReadAllText(expectedResultFile)
                .Replace("{sourceRoot}", convertSourceRootFunc(sourceRoot));

            var expectedSonarReports = JArray.Parse(expectedSonarReportsAsString);

            try
            {
                Assert.True(JToken.DeepEquals(expectedSonarReports, actualSonarReports));
            }
            catch
            {
                _output.WriteLine($"Expected sonar reports:\n{expectedSonarReports}, actual: {actualSonarReports}.");
                throw;
            }
        }
Example #4
0
        public void RemoveExcludedRules_ShouldFilterRulesCorrectly()
        {
            var reportXml = @"
                <Report>  
                    <Issues>   
                        <Project Name=""Test1"">    
                            <Issue TypeId=""UnusedParameter.Global"" 
                                   Message=""Parameter 'countrygeoid' is used neither in this nor in overriding methods"" />     
                            <Issue TypeId=""UnusedParameter.Global"" 
                                   Message=""Parameter 'geocodeResolution' is never used""/>      
                            <Issue TypeId=""CheckNamespace"" />       
                            <Issue TypeId=""UnusedVariable"" />        
                        </Project>        
                        <Project Name=""Test2"">         
                           <Issue TypeId=""RedundantUsingDirective""
                                  Message=""Using directive is not required by the code and can be safely removed"" />          
                           <Issue TypeId=""UnusedType.Global"" />           
                        </Project>           
                    </Issues>
                </Report>";

            var reSharperReport = ParseReSharperReport(reportXml);

            // test exclude filters by type only
            SonarConverter.RemoveExcludedRules(
                reSharperReport, "RedundantUsingDirective|UnusedVariable|UnusedParameter.Global");

            AssertEqualIssueTypes(reSharperReport, new[] { "CheckNamespace" }, new[] { "UnusedType.Global" });

            // test message filtering
            reSharperReport = ParseReSharperReport(reportXml);
            SonarConverter.RemoveExcludedRules(
                reSharperReport,
                "RedundantUsingDirective|UnusedParameter.Global##Parameter 'geocodeResolution'.*|UnusedVariable");

            AssertEqualIssueTypes(
                reSharperReport, new[] { "UnusedParameter.Global", "CheckNamespace" }, new[] { "UnusedType.Global" });
            Assert.Equal(
                "Parameter 'countrygeoid' is used neither in this nor in overriding methods",
                reSharperReport.Issues[0].Issue[0].Message);

            // test message filtering with partial match
            reSharperReport = ParseReSharperReport(reportXml);
            SonarConverter.RemoveExcludedRules(
                reSharperReport,
                "RedundantUsingDirective##directive|UnusedParameter.Global|UnusedVariable");

            AssertEqualIssueTypes(
                reSharperReport,
                new[] { "CheckNamespace" },
                new[] { "UnusedType.Global" });

            // test message filtering with full match
            reSharperReport = ParseReSharperReport(reportXml);
            SonarConverter.RemoveExcludedRules(
                reSharperReport,
                "RedundantUsingDirective##^directive$|UnusedParameter.Global|UnusedVariable");

            AssertEqualIssueTypes(
                reSharperReport,
                new[] { "CheckNamespace" },
                new[] { "RedundantUsingDirective", "UnusedType.Global" });

            // test message filtering overridden by type filtering
            reSharperReport = ParseReSharperReport(reportXml);
            SonarConverter.RemoveExcludedRules(
                reSharperReport,
                "RedundantUsingDirective##^directive$|RedundantUsingDirective|UnusedParameter.Global|UnusedVariable");

            AssertEqualIssueTypes(
                reSharperReport,
                new[] { "CheckNamespace" },
                new[] { "UnusedType.Global" });
        }