Skip to content

Commit

Permalink
Review 3
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-paidis-sonarsource committed Feb 28, 2024
1 parent 213405d commit 687a6f8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

using static Roslyn.Utilities.SonarAnalyzer.Shared.LoggingFrameworkMethods;

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Rules.MessageTemplates;

internal static class MessageTemplateExtractor
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

using System.Text.RegularExpressions;
using SonarAnalyzer.Rules.MessageTemplates;

namespace SonarAnalyzer.Rules.CSharp;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ public void MessageTemplatesShouldBeCorrect_CS() =>
.Verify();

[DataTestMethod]
[DataRow("LogCritical")]
[DataRow("LogDebug")]
[DataRow("LogError")]
[DataRow("LogInformation")]
[DataRow("LogTrace")]
[DataRow("LogWarning")]
public void MessageTemplatesShouldBeCorrect_MicrosoftExtensionsLogging_CS(string methodName) =>
[DataRow("LogCritical", "")]
[DataRow("LogDebug", "")]
[DataRow("LogError", "")]
[DataRow("LogInformation", "")]
[DataRow("LogTrace", "")]
[DataRow("LogWarning", "")]
[DataRow("Log", "LogLevel.Information,")]
public void MessageTemplatesShouldBeCorrect_MicrosoftExtensionsLogging_CS(string methodName, string logLevel) =>
Builder
.AddReferences(NuGetMetadataReference.MicrosoftExtensionsLoggingAbstractions())
.AddSnippet($$$"""
Expand All @@ -50,26 +51,27 @@ public void MessageTemplatesShouldBeCorrect_MicrosoftExtensionsLogging_CS(string
public class Program
{
public void Method(ILogger logger, string user, int count)
public void Method(ILogger logger, string user)
{
Console.WriteLine("Login failed for {User", user); // Compliant
logger.{{{methodName}}}("Login failed for {User}", user); // Compliant
Console.WriteLine("Login failed for {User", user); // Compliant
logger.{{{methodName}}}({{{logLevel}}} "Login failed for {User}", user); // Compliant
logger.{{{methodName}}}("{", user); // Noncompliant
logger.Log(LogLevel.Information, "{", user); // Noncompliant
LoggerExtensions.{{{methodName}}}(logger, "{", user); // Noncompliant
logger.{{{methodName}}}({{{logLevel}}} "{", user); // Noncompliant
LoggerExtensions.{{{methodName}}}(logger, {{{logLevel}}} "{", user); // Noncompliant
}
}
""").Verify();
""")
.Verify();

[DataTestMethod]
[DataRow("Debug")]
[DataRow("Error")]
[DataRow("Information")]
[DataRow("Fatal")]
[DataRow("Warning")]
[DataRow("Verbose")]
public void MessageTemplatesShouldBeCorrect_Serilog_CS(string methodName) =>
[DataRow("Debug", "")]
[DataRow("Error", "")]
[DataRow("Information", "")]
[DataRow("Fatal", "")]
[DataRow("Warning", "")]
[DataRow("Verbose", "")]
[DataRow("Write", "LogEventLevel.Verbose,")]
public void MessageTemplatesShouldBeCorrect_Serilog_CS(string methodName, string logEventLevel) =>
Builder
.AddReferences(NuGetMetadataReference.Serilog(Constants.NuGetLatestVersion))
.AddSnippet($$$"""
Expand All @@ -79,17 +81,17 @@ public void MessageTemplatesShouldBeCorrect_Serilog_CS(string methodName) =>
public class Program
{
public void Method(ILogger logger, string user, int count)
public void Method(ILogger logger, string user)
{
Console.WriteLine("Login failed for {User", user); // Compliant
logger.{{{methodName}}}("Login failed for {User}", user); // Compliant
Console.WriteLine("Login failed for {User", user); // Compliant
logger.{{{methodName}}}({{{logEventLevel}}} "Login failed for {User}", user); // Compliant
logger.{{{methodName}}}("{", user); // Noncompliant
Log.{{{methodName}}}("{", user); // Noncompliant
Log.Write(LogEventLevel.Verbose, "{", user); // Noncompliant
logger.{{{methodName}}}({{{logEventLevel}}} "{", user); // Noncompliant
Log.{{{methodName}}}({{{logEventLevel}}} "{", user); // Noncompliant
}
}
""").Verify();
""")
.Verify();

[DataTestMethod]
[DataRow("Debug")]
Expand All @@ -109,17 +111,40 @@ public void MessageTemplatesShouldBeCorrect_NLog_CS(string methodName) =>
public class Program
{
public void Method(ILogger iLogger, Logger logger, MyLogger myLogger, string user, int count)
public void Method(ILogger iLogger, Logger logger, MyLogger myLogger, string user)
{
Console.WriteLine("Login failed for {User", user); // Compliant
logger.{{{methodName}}}("Login failed for {User}", user); // Compliant
iLogger.{{{methodName}}}("{", user); // Noncompliant
logger.{{{methodName}}}("{", user); // Noncompliant
myLogger.{{{methodName}}}("{", user); // Noncompliant
ILoggerExtensions.ConditionalDebug(iLogger, "{", user); // Noncompliant
}
}
public class MyLogger : Logger { }
""").Verify();
""")
.Verify();

[DataTestMethod]
[DataRow("ConditionalDebug")]
[DataRow("ConditionalTrace")]
public void MessageTemplatesShouldBeCorrect_NLog_ConditionalExtensions_CS(string methodName) =>
Builder
.AddReferences(NuGetMetadataReference.NLog(Constants.NuGetLatestVersion))
.AddSnippet($$$"""
using System;
using NLog;
public class Program
{
public void Method(ILogger iLogger, string user)
{
Console.WriteLine("Login failed for {User", user); // Compliant
ILoggerExtensions.{{{methodName}}}(iLogger, "{", user); // Noncompliant
}
}
public class MyLogger : Logger { }
""")
.Verify();
}

0 comments on commit 687a6f8

Please sign in to comment.