Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read issue severity from issue element rather than issue type element if present #307

Merged
merged 3 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/NVika.Tests/Data/Conversions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace InspectCodeTest
{
public static class Conversions
{
public static double MetresToInches(double metres)
{
var conversionFactor = 0.0254;
return metres * conversionFactor;
}
}
}
20 changes: 20 additions & 0 deletions src/NVika.Tests/Data/InspectCodeReport_IssuesWithSeverity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated by JetBrains Inspect Code 2021.2.2 -->
<Report ToolsVersion="212.0.20211008.81907">
<Information>
<Solution>InspectCodeTest.sln</Solution>
<InspectionScope>
<Element>Solution</Element>
</InspectionScope>
</Information>
<IssueTypes>
<IssueType Id="ConvertToConstant.Local" Category="Common Practices and Code Improvements" CategoryId="BestPractice" SubCategory="Convert local variable or field to constant" Description="Convert local variable or field to constant: Private accessibility" Severity="WARNING" />
<IssueType Id="SuggestVarOrType_BuiltInTypes" Category="Syntax Style" CategoryId="CodeStyleIssues" SubCategory="Use preferred 'var' style" Description="Use preferred 'var' style: For built-in types" Severity="SUGGESTION" WikiUrl="https://www.jetbrains.com/resharperplatform/help?Keyword=SuggestVarOrType_BuiltInTypes" />
</IssueTypes>
<Issues>
<Project Name="InspectCodeTest">
<Issue TypeId="SuggestVarOrType_BuiltInTypes" Severity="WARNING" File="InspectCodeTest\Conversions.cs" Offset="151-154" Line="7" Message="Use explicit type (built-in types)" />
<Issue TypeId="ConvertToConstant.Local" File="InspectCodeTest\Conversions.cs" Offset="155-171" Line="7" Message="Convert to constant" />
</Project>
</Issues>
</Report>
2 changes: 2 additions & 0 deletions src/NVika.Tests/NVika.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<Content Include="Data\WrongReport.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<EmbeddedResource Include="Data\Conversions.txt" />
<EmbeddedResource Include="Data\InspectCodeReport_IssuesWithSeverity.xml" />
</ItemGroup>

<ItemGroup>
Expand Down
51 changes: 51 additions & 0 deletions src/NVika.Tests/Parsers/InspectCodeParserTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using NVika.Parsers;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -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<string, MockFileData>
{
{ "InspectCodeReport_IssuesWithSeverity.xml", new MockFileData(report.ToString()) },
{ Path.Combine("src", @"InspectCodeTest\Conversions.cs"), new MockFileData(TestUtilities.GetEmbeddedResourceContent("Conversions.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\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);
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\Conversions.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);
}
}
}
11 changes: 8 additions & 3 deletions src/NVika/Parsers/InspectCodeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected override IEnumerable<Issue> 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,
};
Expand Down Expand Up @@ -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;

Expand Down