Exemplo n.º 1
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);
        }
Exemplo n.º 2
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;
            }
        }