Skip to content

Commit

Permalink
Change VerifyNoIssuesReported
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim-Pohlmann committed May 8, 2024
1 parent 28d9df3 commit a3ecee4
Show file tree
Hide file tree
Showing 42 changed files with 346 additions and 329 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public class AvoidValueTupleTest

[TestMethod]
public void AvoidValueTuple_TestCode() =>
builder.AddTestReference().VerifyNoIssueReported();
builder.AddTestReference().VerifyNoIssues();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ public class FileScopeNamespaceTest

[TestMethod]
public void FileScopeNamespace_Compliant() =>
builder.AddPaths("FileScopeNamespace.Compliant.cs").VerifyNoIssueReported();
builder.AddPaths("FileScopeNamespace.Compliant.cs").VerifyNoIssues();
}
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ private static void VerifyEmpty(string fileName, string snippet, DiagnosticAnaly
LanguageNames.VisualBasic => builder.WithLanguageVersion(Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.Latest),
_ => throw new UnexpectedLanguageException(language)
};
builder.VerifyNoIssueReported();
builder.VerifyNoIssues();
}

private sealed class DummySourceText : SourceText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public async Task SonarCompilationStartAnalysisContext_RegisterSymbolStartAction
.AddAnalyzer(() => new DummyAnalyzerWithLocation(ruleId, DiagnosticDescriptorFactory.MainSourceScopeTag))
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.AddSnippet(Snippet(extension), $"SomeFile.{extension}")
.VerifyNoIssueReported();
.VerifyNoIssues();

[DataTestMethod]
[DataRow("razor")]
Expand All @@ -440,7 +440,7 @@ public async Task SonarCompilationStartAnalysisContext_RegisterSymbolStartAction
.AddAnalyzer(() => new DummyAnalyzerWithLocation("DummyId", DiagnosticDescriptorFactory.TestSourceScopeTag))
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Test))
.AddSnippet(Snippet(extension), $"SomeFile.{extension}")
.VerifyNoIssueReported();
.VerifyNoIssues();

[DataTestMethod]
[DataRow("razor")]
Expand Down Expand Up @@ -472,7 +472,7 @@ private static int RaiseHere()
new VerifierBuilder()
.AddAnalyzer(() => new DummyAnalyzerCS())
.AddSnippet(@"<p>Some Html</p>", $"SomeFile.{extension}")
.VerifyNoIssueReported();
.VerifyNoIssues();

private static string Snippet(string extension)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
using Moq;
using SonarAnalyzer.AnalysisContext;
using SonarAnalyzer.Rules.CSharp;
using SonarAnalyzer.Test.Helpers;
using SonarAnalyzer.Test.Rules;
using RoslynAnalysisContext = Microsoft.CodeAnalysis.Diagnostics.AnalysisContext;

namespace SonarAnalyzer.Test.AnalysisContext;

Expand All @@ -33,26 +31,6 @@ public partial class SonarAnalysisContextTest
{
public TestContext TestContext { get; set; }

private sealed class TestSetup
{
public string Path { get; }
public DiagnosticAnalyzer Analyzer { get; }
public VerifierBuilder Builder { get; }

public TestSetup(string testCase, SonarDiagnosticAnalyzer analyzer) : this(testCase, analyzer, Enumerable.Empty<MetadataReference>()) { }

public TestSetup(string testCase, SonarDiagnosticAnalyzer analyzer, IEnumerable<MetadataReference> additionalReferences)
{
Path = testCase;
Analyzer = analyzer;
additionalReferences = additionalReferences
.Concat(MetadataReferenceFacade.SystemComponentModelPrimitives)
.Concat(MetadataReferenceFacade.NetStandard)
.Concat(MetadataReferenceFacade.SystemData);
Builder = new VerifierBuilder().AddAnalyzer(() => analyzer).AddPaths(Path).AddReferences(additionalReferences);
}
}

// Various classes that invoke all the `ReportIssue` methods in AnalysisContextExtensions
// We mention in comments the type of Context that is used to invoke (directly or indirectly) the `ReportIssue` method
private readonly List<TestSetup> testCases = new(
Expand Down Expand Up @@ -105,7 +83,7 @@ public void WhenShouldAnalysisBeDisabledReturnsTrue_NoIssueReported()
// ToDo: We should find a way to ack the fact the action was not run
testCase.Builder
.WithOptions(ParseOptionsHelper.FromCSharp8)
.VerifyNoIssueReported();
.VerifyNoIssuesIgnoreErrors();
}
}
finally
Expand All @@ -132,7 +110,7 @@ public void WhenProjectType_IsTest_RunRulesWithTestScope_SonarLint()
var sonarProjectConfig = AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Test, false);
foreach (var testCase in testCases)
{
var hasTestScope = testCase.Analyzer.SupportedDiagnostics.Any(d => d.CustomTags.Contains(DiagnosticDescriptorFactory.TestSourceScopeTag));
var hasTestScope = testCase.Analyzer.SupportedDiagnostics.Any(x => x.CustomTags.Contains(DiagnosticDescriptorFactory.TestSourceScopeTag));
if (hasTestScope)
{
testCase.Builder
Expand All @@ -146,7 +124,7 @@ public void WhenProjectType_IsTest_RunRulesWithTestScope_SonarLint()
testCase.Builder
.WithOptions(ParseOptionsHelper.FromCSharp8)
.WithAdditionalFilePath(sonarProjectConfig)
.VerifyNoIssueReported();
.VerifyNoIssuesIgnoreErrors();
}
}
}
Expand All @@ -157,14 +135,14 @@ public void WhenProjectType_IsTest_RunRulesWithTestScope_Scanner()
var sonarProjectConfig = AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Test);
foreach (var testCase in testCases)
{
var hasProductScope = testCase.Analyzer.SupportedDiagnostics.Any(d => d.CustomTags.Contains(DiagnosticDescriptorFactory.MainSourceScopeTag));
var hasProductScope = testCase.Analyzer.SupportedDiagnostics.Any(x => x.CustomTags.Contains(DiagnosticDescriptorFactory.MainSourceScopeTag));
if (hasProductScope)
{
// MAIN-only and MAIN & TEST rules
testCase.Builder
.WithOptions(ParseOptionsHelper.FromCSharp8)
.WithAdditionalFilePath(sonarProjectConfig)
.VerifyNoIssueReported();
.VerifyNoIssuesIgnoreErrors();
}
else
{
Expand Down Expand Up @@ -196,7 +174,7 @@ public void WhenProjectType_IsTest_RunRulesWithMainScope()
testCase.Builder
.WithOptions(ParseOptionsHelper.FromCSharp8)
.WithAdditionalFilePath(sonarProjectConfig)
.VerifyNoIssueReported();
.VerifyNoIssues();
}
}
}
Expand All @@ -212,7 +190,7 @@ public void WhenAnalysisDisabledBaseOnSyntaxTree_ReportIssuesForEnabledRules()
var testCase2 = testCases[2];
SonarAnalysisContext.ShouldExecuteRegisteredAction = (diags, tree) => tree.FilePath.EndsWith(new FileInfo(testCase.Path).Name, StringComparison.OrdinalIgnoreCase);
testCase.Builder.WithConcurrentAnalysis(false).Verify();
testCase2.Builder.VerifyNoIssueReported();
testCase2.Builder.VerifyNoIssues();
}
finally
{
Expand Down Expand Up @@ -248,7 +226,7 @@ public void WhenReportDiagnosticActionNotNull_AllowToControlWhetherOrNotToReport
{
testCase.Builder
.WithOptions(ParseOptionsHelper.FromCSharp8)
.VerifyNoIssueReported();
.VerifyNoIssues();
}
else
{
Expand Down Expand Up @@ -304,4 +282,24 @@ public void ReportDiagnosticIfNonGenerated_UnchangedFiles_CompilationAnalysisCon

wasReported.Should().Be(expected);
}

private sealed class TestSetup
{
public string Path { get; }
public DiagnosticAnalyzer Analyzer { get; }
public VerifierBuilder Builder { get; }

public TestSetup(string testCase, SonarDiagnosticAnalyzer analyzer) : this(testCase, analyzer, Enumerable.Empty<MetadataReference>()) { }

public TestSetup(string testCase, SonarDiagnosticAnalyzer analyzer, IEnumerable<MetadataReference> additionalReferences)
{
Path = testCase;
Analyzer = analyzer;
additionalReferences = additionalReferences
.Concat(MetadataReferenceFacade.SystemComponentModelPrimitives)
.Concat(MetadataReferenceFacade.NetStandard)
.Concat(MetadataReferenceFacade.SystemData);
Builder = new VerifierBuilder().AddAnalyzer(() => analyzer).AddPaths(Path).AddReferences(additionalReferences);
}
}
}
2 changes: 1 addition & 1 deletion analyzers/tests/SonarAnalyzer.Test/Common/RuleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ private void UnchangedFiles_Verify(VerifierBuilder builder, string unchangedFile
builder = builder.WithConcurrentAnalysis(false).WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfigWithUnchangedFiles(TestContext, unchangedFileName));
if (expectEmptyResults)
{
builder.VerifyNoIssueReported();
builder.VerifyNoIssuesIgnoreErrors();
}
else
{
Expand Down
159 changes: 79 additions & 80 deletions analyzers/tests/SonarAnalyzer.Test/Common/SecurityHotspotTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,106 +23,105 @@
using SonarAnalyzer.Test.Rules;
using SonarAnalyzer.Test.TestFramework;

namespace SonarAnalyzer.Test.Common
namespace SonarAnalyzer.Test.Common;

[TestClass]
public class SecurityHotspotTest
{
[TestClass]
public class SecurityHotspotTest
{
[TestMethod]
public void SecurityHotspotRules_DoNotRaiseIssues_CS() =>
VerifyNoIssueReported(AnalyzerLanguage.CSharp, ParseOptionsHelper.FromCSharp9);
[TestMethod]
public void SecurityHotspotRules_DoNotRaiseIssues_CS() =>
VerifyNoIssues(AnalyzerLanguage.CSharp, ParseOptionsHelper.FromCSharp9);

[TestMethod]
public void SecurityHotspotRules_DoNotRaiseIssues_VB() =>
VerifyNoIssueReported(AnalyzerLanguage.VisualBasic, ParseOptionsHelper.FromVisualBasic12);
[TestMethod]
public void SecurityHotspotRules_DoNotRaiseIssues_VB() =>
VerifyNoIssues(AnalyzerLanguage.VisualBasic, ParseOptionsHelper.FromVisualBasic12);

private static void VerifyNoIssueReported(AnalyzerLanguage language, ImmutableArray<ParseOptions> parseOptions)
private static void VerifyNoIssues(AnalyzerLanguage language, ImmutableArray<ParseOptions> parseOptions)
{
foreach (var analyzer in GetHotspotAnalyzers(language))
{
foreach (var analyzer in GetHotspotAnalyzers(language))
{
var analyzerName = analyzer.GetType().Name;
var analyzerName = analyzer.GetType().Name;

#if NETFRAMEWORK

if (analyzerName is nameof(DisablingCsrfProtection) || analyzerName is nameof(PermissiveCors))
{
continue;
}
if (analyzerName is nameof(DisablingCsrfProtection) || analyzerName is nameof(PermissiveCors))
{
continue;
}
#endif

new VerifierBuilder()
.AddPaths(@$"Hotspots\{GetTestCaseFileName(analyzerName)}{language.FileExtension}")
.AddAnalyzer(() => analyzer)
.WithOptions(parseOptions)
.AddReferences(GetAdditionalReferences(analyzerName))
.WithConcurrentAnalysis(analyzerName is not nameof(ClearTextProtocolsAreSensitive))
.VerifyNoIssueReported();
}
new VerifierBuilder()
.AddPaths(@$"Hotspots\{GetTestCaseFileName(analyzerName)}{language.FileExtension}")
.AddAnalyzer(() => analyzer)
.WithOptions(parseOptions)
.AddReferences(GetAdditionalReferences(analyzerName))
.WithConcurrentAnalysis(analyzerName is not nameof(ClearTextProtocolsAreSensitive))
.VerifyNoIssuesIgnoreErrors();
}
}

private static IEnumerable<SonarDiagnosticAnalyzer> GetHotspotAnalyzers(AnalyzerLanguage language) =>
RuleFinder.GetAnalyzerTypes(language)
.Where(type => typeof(SonarDiagnosticAnalyzer).IsAssignableFrom(type)) // Avoid IRuleFactory and SE rules
.Select(type => (SonarDiagnosticAnalyzer)Activator.CreateInstance(type))
.Where(IsSecurityHotspot);
private static IEnumerable<SonarDiagnosticAnalyzer> GetHotspotAnalyzers(AnalyzerLanguage language) =>
RuleFinder.GetAnalyzerTypes(language)
.Where(type => typeof(SonarDiagnosticAnalyzer).IsAssignableFrom(type)) // Avoid IRuleFactory and SE rules
.Select(type => (SonarDiagnosticAnalyzer)Activator.CreateInstance(type))
.Where(IsSecurityHotspot);

private static bool IsSecurityHotspot(DiagnosticAnalyzer analyzer) =>
analyzer.SupportedDiagnostics.Any(IsSecurityHotspot);
private static bool IsSecurityHotspot(DiagnosticAnalyzer analyzer) =>
analyzer.SupportedDiagnostics.Any(IsSecurityHotspot);

private static bool IsSecurityHotspot(DiagnosticDescriptor diagnostic)
{
var type = RuleTypeMappingCS.Rules.GetValueOrDefault(diagnostic.Id) ?? RuleTypeMappingVB.Rules.GetValueOrDefault(diagnostic.Id);
return type == "SECURITY_HOTSPOT";
}
private static bool IsSecurityHotspot(DiagnosticDescriptor diagnostic)
{
var type = RuleTypeMappingCS.Rules.GetValueOrDefault(diagnostic.Id) ?? RuleTypeMappingVB.Rules.GetValueOrDefault(diagnostic.Id);
return type == "SECURITY_HOTSPOT";
}

private static string GetTestCaseFileName(string analyzerName) =>
analyzerName switch
{
"ConfiguringLoggers" => "ConfiguringLoggers_Log4Net",
"CookieShouldBeHttpOnly" => "CookieShouldBeHttpOnly_Nancy",
"CookieShouldBeSecure" => "CookieShouldBeSecure_Nancy",
"DoNotHardcodeCredentials" => "DoNotHardcodeCredentials.DefaultValues",
"DeliveringDebugFeaturesInProduction" => "DeliveringDebugFeaturesInProduction.NetCore2",
private static string GetTestCaseFileName(string analyzerName) =>
analyzerName switch
{
"ConfiguringLoggers" => "ConfiguringLoggers_Log4Net",
"CookieShouldBeHttpOnly" => "CookieShouldBeHttpOnly_Nancy",
"CookieShouldBeSecure" => "CookieShouldBeSecure_Nancy",
"DoNotHardcodeCredentials" => "DoNotHardcodeCredentials.DefaultValues",
"DeliveringDebugFeaturesInProduction" => "DeliveringDebugFeaturesInProduction.NetCore2",
#if NETFRAMEWORK
"ExecutingSqlQueries" => "ExecutingSqlQueries.Net46",
"UsingCookies" => "UsingCookies_Net46",
"LooseFilePermissions" => "LooseFilePermissions.Windows",
"ExecutingSqlQueries" => "ExecutingSqlQueries.Net46",
"UsingCookies" => "UsingCookies_Net46",
"LooseFilePermissions" => "LooseFilePermissions.Windows",
#else
"ExecutingSqlQueries" => "ExecutingSqlQueries.EntityFrameworkCoreLatest",
"UsingCookies" => "UsingCookies_NetCore",
"LooseFilePermissions" => "LooseFilePermissions.Unix",
"PermissiveCors" => "PermissiveCors.Net",
"ExecutingSqlQueries" => "ExecutingSqlQueries.EntityFrameworkCoreLatest",
"UsingCookies" => "UsingCookies_NetCore",
"LooseFilePermissions" => "LooseFilePermissions.Unix",
"PermissiveCors" => "PermissiveCors.Net",
#endif
_ => analyzerName
};
_ => analyzerName
};

private static IEnumerable<MetadataReference> GetAdditionalReferences(string analyzerName) =>
analyzerName switch
{
nameof(ClearTextProtocolsAreSensitive) => ClearTextProtocolsAreSensitiveTest.AdditionalReferences,
nameof(CookieShouldBeHttpOnly) => CookieShouldBeHttpOnlyTest.AdditionalReferences,
nameof(CookieShouldBeSecure) => CookieShouldBeSecureTest.AdditionalReferences,
nameof(ConfiguringLoggers) => ConfiguringLoggersTest.Log4NetReferences,
nameof(DeliveringDebugFeaturesInProduction) => DeliveringDebugFeaturesInProductionTest.AdditionalReferencesForAspNetCore2,
nameof(DisablingRequestValidation) => NuGetMetadataReference.MicrosoftAspNetMvc(Constants.NuGetLatestVersion),
nameof(DoNotHardcodeCredentials) => DoNotHardcodeCredentialsTest.AdditionalReferences,
nameof(DoNotUseRandom) => MetadataReferenceFacade.SystemSecurityCryptography,
nameof(ExpandingArchives) => ExpandingArchivesTest.AdditionalReferences,
nameof(RequestsWithExcessiveLength) => RequestsWithExcessiveLengthTest.GetAdditionalReferences(),
nameof(SpecifyTimeoutOnRegex) => MetadataReferenceFacade.RegularExpressions
.Concat(NuGetMetadataReference.SystemComponentModelAnnotations()),
private static IEnumerable<MetadataReference> GetAdditionalReferences(string analyzerName) =>
analyzerName switch
{
nameof(ClearTextProtocolsAreSensitive) => ClearTextProtocolsAreSensitiveTest.AdditionalReferences,
nameof(CookieShouldBeHttpOnly) => CookieShouldBeHttpOnlyTest.AdditionalReferences,
nameof(CookieShouldBeSecure) => CookieShouldBeSecureTest.AdditionalReferences,
nameof(ConfiguringLoggers) => ConfiguringLoggersTest.Log4NetReferences,
nameof(DeliveringDebugFeaturesInProduction) => DeliveringDebugFeaturesInProductionTest.AdditionalReferencesForAspNetCore2,
nameof(DisablingRequestValidation) => NuGetMetadataReference.MicrosoftAspNetMvc(Constants.NuGetLatestVersion),
nameof(DoNotHardcodeCredentials) => DoNotHardcodeCredentialsTest.AdditionalReferences,
nameof(DoNotUseRandom) => MetadataReferenceFacade.SystemSecurityCryptography,
nameof(ExpandingArchives) => ExpandingArchivesTest.AdditionalReferences,
nameof(RequestsWithExcessiveLength) => RequestsWithExcessiveLengthTest.GetAdditionalReferences(),
nameof(SpecifyTimeoutOnRegex) => MetadataReferenceFacade.RegularExpressions
.Concat(NuGetMetadataReference.SystemComponentModelAnnotations()),

#if NET
nameof(DisablingCsrfProtection) => DisablingCsrfProtectionTest.AdditionalReferences(),
nameof(ExecutingSqlQueries) => ExecutingSqlQueriesTest.GetReferencesEntityFrameworkNetCore("7.0.14"),
nameof(LooseFilePermissions) => NuGetMetadataReference.MonoPosixNetStandard(),
nameof(PermissiveCors) => PermissiveCorsTest.AdditionalReferences,
nameof(DisablingCsrfProtection) => DisablingCsrfProtectionTest.AdditionalReferences(),
nameof(ExecutingSqlQueries) => ExecutingSqlQueriesTest.GetReferencesEntityFrameworkNetCore("7.0.14"),
nameof(LooseFilePermissions) => NuGetMetadataReference.MonoPosixNetStandard(),
nameof(PermissiveCors) => PermissiveCorsTest.AdditionalReferences,
#else
nameof(ExecutingSqlQueries) => ExecutingSqlQueriesTest.GetReferencesNet46(Constants.NuGetLatestVersion),
nameof(ExecutingSqlQueries) => ExecutingSqlQueriesTest.GetReferencesNet46(Constants.NuGetLatestVersion),
#endif
_ => MetadataReferenceFacade.SystemNetHttp
.Concat(MetadataReferenceFacade.SystemDiagnosticsProcess)
.Concat(MetadataReferenceFacade.SystemSecurityCryptography)
};
}
_ => MetadataReferenceFacade.SystemNetHttp
.Concat(MetadataReferenceFacade.SystemDiagnosticsProcess)
.Concat(MetadataReferenceFacade.SystemSecurityCryptography)
};
}
Loading

0 comments on commit a3ecee4

Please sign in to comment.