Skip to content

Commit

Permalink
Fix MSTEST0005 to report only inside test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink committed Apr 2, 2024
1 parent c741b0e commit 2aeefc1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Analyzers/MSTest.Analyzers/TestContextShouldBeValidAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,25 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(context =>
{
if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestContext, out var testContextSymbol))
if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestContext, out var testContextSymbol)
&& context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute, out var testClassAttributeSymbol))
{
bool canDiscoverInternals = context.Compilation.CanDiscoverInternals();
context.RegisterSymbolAction(context => AnalyzeSymbol(context, testContextSymbol, canDiscoverInternals), SymbolKind.Field, SymbolKind.Property);
context.RegisterSymbolAction(
context => AnalyzeSymbol(context, testContextSymbol, testClassAttributeSymbol, canDiscoverInternals),
SymbolKind.Field, SymbolKind.Property);
}
});
}

private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testContextSymbol, bool canDiscoverInternals)
private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testContextSymbol, INamedTypeSymbol testClassAttributeSymbol,
bool canDiscoverInternals)
{
if (!context.Symbol.ContainingType.GetAttributes().Any(attr => attr.AttributeClass.Inherits(testClassAttributeSymbol)))
{
return;
}

if (context.Symbol is IFieldSymbol fieldSymbol)
{
AnalyzeFieldSymbol(context, fieldSymbol, testContextSymbol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,34 @@ await VerifyCS.VerifyAnalyzerAsync(
VerifyCS.Diagnostic(TestContextShouldBeValidAnalyzer.NotReadonlyRule)
.WithLocation(0));
}

[Arguments("TestContext", "private")]
[Arguments("TestContext", "public")]
[Arguments("TestContext", "internal")]
[Arguments("TestContext", "protected")]
[Arguments("testcontext", "private")]
[Arguments("testcontext", "public")]
[Arguments("testcontext", "internal")]
[Arguments("testcontext", "protected")]
[Arguments("TESTCONTEXT", "private")]
[Arguments("TESTCONTEXT", "public")]
[Arguments("TESTCONTEXT", "internal")]
[Arguments("TESTCONTEXT", "protected")]
[Arguments("TeStCoNtExT", "private")]
[Arguments("TeStCoNtExT", "public")]
[Arguments("TeStCoNtExT", "internal")]
[Arguments("TeStCoNtExT", "protected")]
public async Task WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string fieldName, string accessibility)
{
var code = $$"""
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class MyTestClass
{
{{accessibility}} TestContext {{fieldName}};
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
}
}
16 changes: 16 additions & 0 deletions test/UnitTests/MSTest.Analyzers.UnitTests/testsbaseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyze
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextCaseInsensitiveIsField_Diagnostic(string, string) (fieldName: "TestContext", accessibility: "public")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextCaseInsensitiveIsField_Diagnostic(string, string) (fieldName: "TeStCoNtExT", accessibility: "public")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextCaseInsensitiveIsField_Diagnostic(string, string) (fieldName: "TESTCONTEXT", accessibility: "public")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "testcontext", accessibility: "internal")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TestContext", accessibility: "internal")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TeStCoNtExT", accessibility: "internal")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TESTCONTEXT", accessibility: "internal")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "testcontext", accessibility: "private")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TestContext", accessibility: "private")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TeStCoNtExT", accessibility: "private")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TESTCONTEXT", accessibility: "private")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "testcontext", accessibility: "protected")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TestContext", accessibility: "protected")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TeStCoNtExT", accessibility: "protected")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TESTCONTEXT", accessibility: "protected")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "testcontext", accessibility: "public")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TestContext", accessibility: "public")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TeStCoNtExT", accessibility: "public")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextIsFieldNotOnTestClass_NoDiagnostic(string, string) (fieldName: "TESTCONTEXT", accessibility: "public")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextPropertyIsPrivateOrInternal_Diagnostic(string, string) (propertyName: "testcontext", accessibility: "internal")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextPropertyIsPrivateOrInternal_Diagnostic(string, string) (propertyName: "TestContext", accessibility: "internal")
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.TestContextShouldBeValidAnalyzerTests.WhenTestContextPropertyIsPrivateOrInternal_Diagnostic(string, string) (propertyName: "TeStCoNtExT", accessibility: "internal")
Expand Down

0 comments on commit 2aeefc1

Please sign in to comment.