From 66634f478ae70630cd67828f80cc065cf77796c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Wed, 27 Oct 2021 18:41:33 +0200 Subject: [PATCH 1/3] Add failing test case for issues with severity --- .../InspectCodeReport_IssuesWithSeverity.xml | 20 ++++++++ src/NVika.Tests/Data/TestClass.txt | 11 ++++ src/NVika.Tests/NVika.Tests.csproj | 2 + .../Parsers/InspectCodeParserTest.cs | 51 +++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 src/NVika.Tests/Data/InspectCodeReport_IssuesWithSeverity.xml create mode 100644 src/NVika.Tests/Data/TestClass.txt diff --git a/src/NVika.Tests/Data/InspectCodeReport_IssuesWithSeverity.xml b/src/NVika.Tests/Data/InspectCodeReport_IssuesWithSeverity.xml new file mode 100644 index 00000000..6255ddbc --- /dev/null +++ b/src/NVika.Tests/Data/InspectCodeReport_IssuesWithSeverity.xml @@ -0,0 +1,20 @@ + + + + + InspectCodeTest.sln + + Solution + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/NVika.Tests/Data/TestClass.txt b/src/NVika.Tests/Data/TestClass.txt new file mode 100644 index 00000000..20e36f1a --- /dev/null +++ b/src/NVika.Tests/Data/TestClass.txt @@ -0,0 +1,11 @@ +namespace InspectCodeTest +{ + public class TestClass + { + public static double MetresToInches(double metres) + { + var conversionFactor = 0.0254; + return metres * conversionFactor; + } + } +} \ No newline at end of file diff --git a/src/NVika.Tests/NVika.Tests.csproj b/src/NVika.Tests/NVika.Tests.csproj index aa6b6590..7d9e01f3 100644 --- a/src/NVika.Tests/NVika.Tests.csproj +++ b/src/NVika.Tests/NVika.Tests.csproj @@ -28,6 +28,8 @@ PreserveNewest + + diff --git a/src/NVika.Tests/Parsers/InspectCodeParserTest.cs b/src/NVika.Tests/Parsers/InspectCodeParserTest.cs index 99c3c4cc..41e5291b 100644 --- a/src/NVika.Tests/Parsers/InspectCodeParserTest.cs +++ b/src/NVika.Tests/Parsers/InspectCodeParserTest.cs @@ -1,3 +1,4 @@ +using System; using NVika.Parsers; using System.Collections.Generic; using System.IO; @@ -226,5 +227,55 @@ public void Parse_SlnInSubFolder() Assert.Equal(IssueSeverity.Warning, issue.Severity); Assert.Equal("InspectCode", issue.Source); } + + [Fact] + public void Parse_IssueSeverityTakesPrecedenceOverIssueTypeSeverity() + { + // arrange + var report = XDocument.Parse(TestUtilities.GetEmbeddedResourceContent("InspectCodeReport_IssuesWithSeverity.xml")); + report.Root.Element("Information").Element("Solution").Value = Path.Combine("src", "Vika.sln"); + + var fileSystem = new MockFileSystem(new Dictionary + { + { "InspectCodeReport_IssuesWithSeverity.xml", new MockFileData(report.ToString()) }, + { Path.Combine("src", @"InspectCodeTest\TestClass.cs"), new MockFileData(TestUtilities.GetEmbeddedResourceContent("TestClass.txt")) } + }); + var parser = new InspectCodeParser(); + parser.FileSystem = fileSystem; + + // act + var result = parser.Parse("InspectCodeReport_IssuesWithSeverity.xml").ToArray(); + + // assert + Assert.Equal(2, result.Length); + + var issueWithSeverity = result[0]; + Assert.Equal("Syntax Style", issueWithSeverity.Category); + Assert.Equal("Use preferred 'var' style: For built-in types", issueWithSeverity.Description); + Assert.Equal(Path.Combine("src", @"InspectCodeTest\TestClass.cs"), issueWithSeverity.FilePath); + Assert.Equal(new Uri("https://www.jetbrains.com/resharperplatform/help?Keyword=SuggestVarOrType_BuiltInTypes"), issueWithSeverity.HelpUri); + Assert.Equal(7u, issueWithSeverity.Line); + Assert.Equal("Use explicit type (built-in types)", issueWithSeverity.Message); + Assert.Equal("SuggestVarOrType_BuiltInTypes", issueWithSeverity.Name); + Assert.Equal(12u, issueWithSeverity.Offset.Start); + Assert.Equal(15u, issueWithSeverity.Offset.End); + Assert.Equal("InspectCodeTest", issueWithSeverity.Project); + Assert.Equal(IssueSeverity.Warning, issueWithSeverity.Severity); + Assert.Equal("InspectCode", issueWithSeverity.Source); + + var issueWithoutSeverity = result[1]; + Assert.Equal("Common Practices and Code Improvements", issueWithoutSeverity.Category); + Assert.Equal("Convert local variable or field to constant: Private accessibility", issueWithoutSeverity.Description); + Assert.Equal(Path.Combine("src", @"InspectCodeTest\TestClass.cs"), issueWithoutSeverity.FilePath); + Assert.Null(issueWithoutSeverity.HelpUri); + Assert.Equal(7u, issueWithoutSeverity.Line); + Assert.Equal("Convert to constant", issueWithoutSeverity.Message); + Assert.Equal("ConvertToConstant.Local", issueWithoutSeverity.Name); + Assert.Equal(16u, issueWithoutSeverity.Offset.Start); + Assert.Equal(32u, issueWithoutSeverity.Offset.End); + Assert.Equal("InspectCodeTest", issueWithoutSeverity.Project); + Assert.Equal(IssueSeverity.Warning, issueWithoutSeverity.Severity); + Assert.Equal("InspectCode", issueWithoutSeverity.Source); + } } } From 6ca8a110d106e071efa55cf6eda002690aa134f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Wed, 27 Oct 2021 18:49:12 +0200 Subject: [PATCH 2/3] Read & use individual issue severity if present --- src/NVika/Parsers/InspectCodeParser.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/NVika/Parsers/InspectCodeParser.cs b/src/NVika/Parsers/InspectCodeParser.cs index 58151c74..384d4adc 100644 --- a/src/NVika/Parsers/InspectCodeParser.cs +++ b/src/NVika/Parsers/InspectCodeParser.cs @@ -44,7 +44,7 @@ protected override IEnumerable Parse(XDocument report) Line = lineNumber, Message = issue.Attribute("Message").Value, Name = issue.Attribute("TypeId").Value, - Severity = GetSeverity(issueType.Attribute("Severity")), + Severity = GetSeverity(issue.Attribute("Severity"), issueType.Attribute("Severity")), Offset = GetOffset(offsetAttribute, sourceFilePath, lineNumber), Source = Name, }; @@ -84,9 +84,14 @@ private Offset GetOffset(XAttribute offsetAttribute, string sourceFilePath, uint }; } - private static IssueSeverity GetSeverity(XAttribute severityAttribute) + private static IssueSeverity GetSeverity(XAttribute issueSeverity, XAttribute issueTypeSeverity) { - switch (severityAttribute.Value) + // The severity type is always present on the issue type element, + // but it is also overridden by an individual issue's severity, if one is present. + // See: https://www.jetbrains.com/help/resharper/InspectCode.html#understanding-output + var finalSeverity = issueSeverity?.Value ?? issueTypeSeverity.Value; + + switch (finalSeverity) { case "HINT": return IssueSeverity.Hint; From 817e2c4eaf0faedab8e48b2d995993e22a427166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Sat, 30 Oct 2021 13:00:29 +0200 Subject: [PATCH 3/3] Use a more fitting name for class from test case --- src/NVika.Tests/Data/{TestClass.txt => Conversions.txt} | 6 +++--- .../Data/InspectCodeReport_IssuesWithSeverity.xml | 4 ++-- src/NVika.Tests/NVika.Tests.csproj | 2 +- src/NVika.Tests/Parsers/InspectCodeParserTest.cs | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) rename src/NVika.Tests/Data/{TestClass.txt => Conversions.txt} (73%) diff --git a/src/NVika.Tests/Data/TestClass.txt b/src/NVika.Tests/Data/Conversions.txt similarity index 73% rename from src/NVika.Tests/Data/TestClass.txt rename to src/NVika.Tests/Data/Conversions.txt index 20e36f1a..5bdf8374 100644 --- a/src/NVika.Tests/Data/TestClass.txt +++ b/src/NVika.Tests/Data/Conversions.txt @@ -1,6 +1,6 @@ -namespace InspectCodeTest +namespace InspectCodeTest { - public class TestClass + public static class Conversions { public static double MetresToInches(double metres) { @@ -8,4 +8,4 @@ return metres * conversionFactor; } } -} \ No newline at end of file +} diff --git a/src/NVika.Tests/Data/InspectCodeReport_IssuesWithSeverity.xml b/src/NVika.Tests/Data/InspectCodeReport_IssuesWithSeverity.xml index 6255ddbc..73d5f4b2 100644 --- a/src/NVika.Tests/Data/InspectCodeReport_IssuesWithSeverity.xml +++ b/src/NVika.Tests/Data/InspectCodeReport_IssuesWithSeverity.xml @@ -13,8 +13,8 @@ - - + + \ No newline at end of file diff --git a/src/NVika.Tests/NVika.Tests.csproj b/src/NVika.Tests/NVika.Tests.csproj index 7d9e01f3..b250975f 100644 --- a/src/NVika.Tests/NVika.Tests.csproj +++ b/src/NVika.Tests/NVika.Tests.csproj @@ -28,7 +28,7 @@ PreserveNewest - + diff --git a/src/NVika.Tests/Parsers/InspectCodeParserTest.cs b/src/NVika.Tests/Parsers/InspectCodeParserTest.cs index 41e5291b..c2252477 100644 --- a/src/NVika.Tests/Parsers/InspectCodeParserTest.cs +++ b/src/NVika.Tests/Parsers/InspectCodeParserTest.cs @@ -238,7 +238,7 @@ public void Parse_IssueSeverityTakesPrecedenceOverIssueTypeSeverity() var fileSystem = new MockFileSystem(new Dictionary { { "InspectCodeReport_IssuesWithSeverity.xml", new MockFileData(report.ToString()) }, - { Path.Combine("src", @"InspectCodeTest\TestClass.cs"), new MockFileData(TestUtilities.GetEmbeddedResourceContent("TestClass.txt")) } + { Path.Combine("src", @"InspectCodeTest\Conversions.cs"), new MockFileData(TestUtilities.GetEmbeddedResourceContent("Conversions.txt")) } }); var parser = new InspectCodeParser(); parser.FileSystem = fileSystem; @@ -252,7 +252,7 @@ public void Parse_IssueSeverityTakesPrecedenceOverIssueTypeSeverity() var issueWithSeverity = result[0]; Assert.Equal("Syntax Style", issueWithSeverity.Category); Assert.Equal("Use preferred 'var' style: For built-in types", issueWithSeverity.Description); - Assert.Equal(Path.Combine("src", @"InspectCodeTest\TestClass.cs"), issueWithSeverity.FilePath); + Assert.Equal(Path.Combine("src", @"InspectCodeTest\Conversions.cs"), issueWithSeverity.FilePath); Assert.Equal(new Uri("https://www.jetbrains.com/resharperplatform/help?Keyword=SuggestVarOrType_BuiltInTypes"), issueWithSeverity.HelpUri); Assert.Equal(7u, issueWithSeverity.Line); Assert.Equal("Use explicit type (built-in types)", issueWithSeverity.Message); @@ -266,7 +266,7 @@ public void Parse_IssueSeverityTakesPrecedenceOverIssueTypeSeverity() var issueWithoutSeverity = result[1]; Assert.Equal("Common Practices and Code Improvements", issueWithoutSeverity.Category); Assert.Equal("Convert local variable or field to constant: Private accessibility", issueWithoutSeverity.Description); - Assert.Equal(Path.Combine("src", @"InspectCodeTest\TestClass.cs"), issueWithoutSeverity.FilePath); + Assert.Equal(Path.Combine("src", @"InspectCodeTest\Conversions.cs"), issueWithoutSeverity.FilePath); Assert.Null(issueWithoutSeverity.HelpUri); Assert.Equal(7u, issueWithoutSeverity.Line); Assert.Equal("Convert to constant", issueWithoutSeverity.Message);