Skip to content

Fix code quality things, bump to net standard 2.0, bump packages, fix related errors #246

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

Merged
merged 7 commits into from
Jan 5, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static AnalyzerTests()
[TestMethod]
public async Task RunOnSelf()
{
await ProcessProject(new FileInfo(Path.Combine("..", "..", "..", "..", "IntelliTect.Analyzer","IntelliTect.Analyzer.csproj")))
await ProcessProject(new FileInfo(Path.Combine("..", "..", "..", "..", "IntelliTect.Analyzer", "IntelliTect.Analyzer.csproj")))
.ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -19,7 +19,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4-beta1.22559.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ static void Main()
await VerifyCSharpFix(test, fixTest);
}

private DiagnosticResult GetExpectedDiagnosticResult(int line, int col)
private static DiagnosticResult GetExpectedDiagnosticResult(int line, int col)
{
return new DiagnosticResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Simplification;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Simplification;

namespace TestHelper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Microsoft.CodeAnalysis;
using System;
using Microsoft.CodeAnalysis;

namespace TestHelper
{
/// <summary>
/// Location where the diagnostic appears, as determined by path, line number, and column number.
/// </summary>
public struct DiagnosticResultLocation
public readonly struct DiagnosticResultLocation
{
public DiagnosticResultLocation(string path, int line, int column)
{
Expand Down Expand Up @@ -36,17 +36,14 @@ public DiagnosticResultLocation(string path, int line, int column)
public struct DiagnosticResult
{
private DiagnosticResultLocation[] _Locations;

#pragma warning disable CA1819 // Properties should not return arrays

public DiagnosticResultLocation[] Locations
{
get
{
if (_Locations == null)
{
_Locations = Array.Empty<DiagnosticResultLocation>();
}
_Locations ??= Array.Empty<DiagnosticResultLocation>();
return _Locations;
}

Expand All @@ -55,7 +52,7 @@ public DiagnosticResultLocation[] Locations
_Locations = value;
}
}

#pragma warning restore CA1819 // Properties should not return arrays

public DiagnosticSeverity Severity { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;

namespace TestHelper
{
Expand Down Expand Up @@ -52,7 +52,7 @@ protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyz
{
throw new ArgumentNullException(nameof(documents));
}

var projects = new HashSet<Project>();
foreach (Document document in documents)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<NoWarn>CA2007,CA1815,CA1303,CA1707,CA1305</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4-beta1.22559.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -16,8 +16,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class TypeName

VerifyCSharpDiagnostic(test, expected);
}

[TestMethod]
public void FieldWithNamingViolation_FieldTwoLeadingUnderScores_Warning()
{
Expand Down Expand Up @@ -224,7 +224,7 @@ class TypeName
}";
await VerifyCSharpFix(test, fixTest);
}

[TestMethod]
public async Task FieldTwoLeadingUnderScores_CodeFix_FixNamingViolation_FieldIsNamedCorrectly()
{
Expand Down Expand Up @@ -368,7 +368,7 @@ void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object

VerifyCSharpDiagnostic(test);
}

[TestMethod]
[Description("Issue 14")]
public void FieldWithNamingViolation_ClassHasGeneratedCodeAttribute_Ignored()
Expand All @@ -392,7 +392,7 @@ class TypeName

VerifyCSharpDiagnostic(test);
}

[TestMethod]
[Description("Issue 14")]
public void FieldWithNamingViolation_FieldHasGeneratedCodeAttribute_Ignored()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void Foo()
var expected = new DiagnosticResult
{
Id = Analyzers.UnusedLocalVariable.DiagnosticId,
Message = "Local variable 'foo' should be used",
Message = "Local variable 'foo' should be used",
Severity = DiagnosticSeverity.Info,
Locations = new[]
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace TestHelper
{
Expand Down Expand Up @@ -109,8 +109,7 @@ private static async Task VerifyFix(string language, DiagnosticAnalyzer analyzer
document = document.WithSyntaxRoot(Formatter.Format(document.GetSyntaxRootAsync().Result, Formatter.Annotation, document.Project.Solution.Workspace));
newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document));

Assert.IsTrue(false,
$"Fix introduced new compiler diagnostics:{string.Join(Environment.NewLine, newCompilerDiagnostics.Select(d => d.ToString()))}" +
Assert.Fail($"Fix introduced new compiler diagnostics:{string.Join(Environment.NewLine, newCompilerDiagnostics.Select(d => d.ToString()))}" +
$"{Environment.NewLine}{Environment.NewLine}New document:{Environment.NewLine}{document.GetSyntaxRootAsync().Result.ToFullString()}{Environment.NewLine}");
}

Expand All @@ -124,7 +123,7 @@ private static async Task VerifyFix(string language, DiagnosticAnalyzer analyzer
//after applying all of the code fixes, compare the resulting string to the inputted one
string actual = GetStringFromDocument(document);
Assert.AreEqual<string>(
newSource?.Replace("\r","",StringComparison.InvariantCulture),
newSource?.Replace("\r", "", StringComparison.InvariantCulture),
actual?.Replace("\r", "", StringComparison.InvariantCulture));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestHelper
{
Expand Down Expand Up @@ -106,14 +105,7 @@ private static void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResult
{
int expectedCount = expectedResults.Length;
int actualCount = actualResults.Count();

if (expectedCount != actualCount)
{
string diagnosticsOutput = actualResults.Any() ? FormatDiagnostics(analyzer, actualResults.ToArray()) : " NONE.";

Assert.IsTrue(false,
string.Format("Mismatch between number of diagnostics returned, expected \"{0}\" actual \"{1}\"\r\n\r\nDiagnostics:\r\n{2}\r\n", expectedCount, actualCount, diagnosticsOutput));
}
Assert.AreEqual(expectedCount, actualCount, $"Mismatch between number of diagnostics returned, expected \" {expectedCount} \" actual \" {actualCount} \"{Environment.NewLine}{Environment.NewLine}Diagnostics:{Environment.NewLine}{(actualResults.Any() ? FormatDiagnostics(analyzer, actualResults.ToArray()) : "NONE.")}{Environment.NewLine}");

for (int i = 0; i < expectedResults.Length; i++)
{
Expand All @@ -122,52 +114,35 @@ private static void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResult

if (expected.Line == -1 && expected.Column == -1)
{
if (actual.Location != Location.None)
{
Assert.IsTrue(false,
string.Format("Expected:\nA project diagnostic with No location\nActual:\n{0}",
FormatDiagnostics(analyzer, actual)));
}
Assert.AreEqual(actual.Location,
Location.None,
string.Format($"Expected:{Environment.NewLine}A project diagnostic with No location{Environment.NewLine}Actual:{Environment.NewLine}{FormatDiagnostics(analyzer, actual)}"));
}
else
{
VerifyDiagnosticLocation(analyzer, actual, actual.Location, expected.Locations.First());
Location[] additionalLocations = actual.AdditionalLocations.ToArray();

if (additionalLocations.Length != expected.Locations.Length - 1)
{
Assert.IsTrue(false,
string.Format("Expected {0} additional locations but got {1} for Diagnostic:\r\n {2}\r\n",
expected.Locations.Length - 1, additionalLocations.Length,
FormatDiagnostics(analyzer, actual)));
}
Assert.AreEqual(additionalLocations.Length,
expected.Locations.Length - 1,
$"Expected {expected.Locations.Length - 1} additional locations but got {additionalLocations.Length} for Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, actual)}{Environment.NewLine}");

for (int j = 0; j < additionalLocations.Length; ++j)
{
VerifyDiagnosticLocation(analyzer, actual, additionalLocations[j], expected.Locations[j + 1]);
}
}
Assert.AreEqual(actual.Id,
expected.Id,
$"Expected diagnostic id to be \"{expected.Id}\" was \"{actual.Id}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, actual)}{Environment.NewLine}");

if (actual.Id != expected.Id)
{
Assert.IsTrue(false,
string.Format("Expected diagnostic id to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Id, actual.Id, FormatDiagnostics(analyzer, actual)));
}

if (actual.Severity != expected.Severity)
{
Assert.IsTrue(false,
string.Format("Expected diagnostic severity to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Severity, actual.Severity, FormatDiagnostics(analyzer, actual)));
}
Assert.AreEqual(actual.Severity,
expected.Severity,
$"Expected diagnostic severity to be \"{expected.Severity}\" was \"{actual.Severity}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, actual)}{Environment.NewLine}");

if (actual.GetMessage() != expected.Message)
{
Assert.IsTrue(false,
string.Format("Expected diagnostic message to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Message, actual.GetMessage(), FormatDiagnostics(analyzer, actual)));
}
Assert.AreEqual(actual.GetMessage(),
expected.Message,
$"Expected diagnostic message to be \"{expected.Message}\" was \"{actual.GetMessage()}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, actual)}{Environment.NewLine}");
}
}

Expand All @@ -182,34 +157,27 @@ private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer, Diagno
{
FileLinePositionSpan actualSpan = actual.GetLineSpan();

Assert.IsTrue(actualSpan.Path == expected.Path || (actualSpan.Path != null
&& actualSpan.Path.Contains("Test0.", StringComparison.Ordinal)
Assert.IsTrue(actualSpan.Path == expected.Path || (actualSpan.Path != null
&& actualSpan.Path.Contains("Test0.", StringComparison.Ordinal)
&& expected.Path.Contains("Test.", StringComparison.Ordinal)),
string.Format("Expected diagnostic to be in file \"{0}\" was actually in file \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Path, actualSpan.Path, FormatDiagnostics(analyzer, diagnostic)));
$"Expected diagnostic to be in file \"{expected.Path}\" was actually in file \"{actualSpan.Path}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, diagnostic)}{Environment.NewLine}");

Microsoft.CodeAnalysis.Text.LinePosition actualLinePosition = actualSpan.StartLinePosition;

// Only check line position if there is an actual line in the real diagnostic
if (actualLinePosition.Line > 0)
{
if (actualLinePosition.Line + 1 != expected.Line)
{
Assert.IsTrue(false,
string.Format("Expected diagnostic to be on line \"{0}\" was actually on line \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Line, actualLinePosition.Line + 1, FormatDiagnostics(analyzer, diagnostic)));
}
Assert.AreEqual(actualLinePosition.Line + 1,
expected.Line,
$"Expected diagnostic to be on line \"{expected.Line}\" was actually on line \"{actualLinePosition.Line + 1}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, diagnostic)}{Environment.NewLine}");
}

// Only check column position if there is an actual column position in the real diagnostic
if (actualLinePosition.Character > 0)
{
if (actualLinePosition.Character + 1 != expected.Column)
{
Assert.IsTrue(false,
string.Format("Expected diagnostic to start at column \"{0}\" was actually at column \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Column, actualLinePosition.Character + 1, FormatDiagnostics(analyzer, diagnostic)));
}
Assert.AreEqual(actualLinePosition.Character + 1,
expected.Column,
$"Expected diagnostic to start at column \"{expected.Column}\" was actually at column \"{actualLinePosition.Character + 1}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, diagnostic)}{Environment.NewLine}");
}
}
#endregion
Expand Down Expand Up @@ -243,7 +211,7 @@ private static string FormatDiagnostics(DiagnosticAnalyzer analyzer, params Diag
else
{
Assert.IsTrue(location.IsInSource,
$"Test base does not currently handle diagnostics in metadata locations. Diagnostic in metadata: {diagnostics[i]}\r\n");
$"Test base does not currently handle diagnostics in metadata locations. Diagnostic in metadata: {diagnostics[i]}{Environment.NewLine}");

string resultMethodName = diagnostics[i].Location.SourceTree.FilePath.EndsWith(".cs", StringComparison.Ordinal) ? "GetCSharpResultAt" : "GetBasicResultAt";
Microsoft.CodeAnalysis.Text.LinePosition linePosition = diagnostics[i].Location.GetLineSpan().StartLinePosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private static string GetEnumAttributeValue<TAttribute>(Enum value, Func<TAttrib
{
FieldInfo fi = value.GetType().GetRuntimeField(value.ToString());

var attributes = (TAttribute[]) fi?.GetCustomAttributes(typeof(TAttribute), false);
var attributes = (TAttribute[])fi?.GetCustomAttributes(typeof(TAttribute), false);

return attributes?.Length > 0 ? valueAccessor(attributes[0]) : null;
}
Expand Down
Loading