public static void VerifyCSharpAnalyzer(string snippet, SonarDiagnosticAnalyzer diagnosticAnalyzer, IEnumerable <CSharpParseOptions> options = null, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default, IEnumerable <MetadataReference> additionalReferences = null) { var solution = SolutionBuilder .Create() .AddProject(AnalyzerLanguage.CSharp) .AddSnippet(snippet) .AddReferences(additionalReferences) .GetSolution(); // TODO: add [CallerLineNumber]int lineNumber = 0 // then add ability to shift result reports with this line number foreach (var compilation in solution.Compile(options?.ToArray())) { DiagnosticVerifier.Verify(compilation, diagnosticAnalyzer, checkMode); } }
public void DisablingRequestValidation_CS_WebConfig_SubFolders(string rootDirectory, params string[] subFolders) { List <Diagnostic> allDiagnostics; var compilation = SolutionBuilder.Create().AddProject(AnalyzerLanguage.CSharp).GetCompilation(); var languageVersion = compilation.LanguageVersionString(); var oldCulture = Thread.CurrentThread.CurrentCulture; var newCulture = (CultureInfo)oldCulture.Clone(); // decimal.TryParse() from the implementation might not recognize "1.2" under different culture newCulture.NumberFormat.NumberDecimalSeparator = ","; Thread.CurrentThread.CurrentCulture = newCulture; var rootFile = Path.Combine(rootDirectory, WebConfig); var filesToAnalyze = new List <string> { rootFile }; foreach (var subFolder in subFolders) { filesToAnalyze.Add(Path.Combine(rootDirectory, subFolder, WebConfig)); } try { allDiagnostics = DiagnosticVerifier.GetDiagnostics( compilation, new CS.DisablingRequestValidation(AnalyzerConfiguration.AlwaysEnabled), CompilationErrorBehavior.Default, sonarProjectConfigPath: TestHelper.CreateSonarProjectConfig(rootDirectory, TestHelper.CreateFilesToAnalyze(rootDirectory, filesToAnalyze.ToArray()))).ToList(); } finally { Thread.CurrentThread.CurrentCulture = oldCulture; // Restore, don't mess with other UTs using the same thread } allDiagnostics.Should().NotBeEmpty(); var rootWebConfig = Path.Combine(rootDirectory, WebConfig); VerifyResults(rootWebConfig, allDiagnostics, languageVersion); foreach (var subFolder in subFolders) { var subFolderWebConfig = Path.Combine(rootDirectory, subFolder, WebConfig); VerifyResults(subFolderWebConfig, allDiagnostics, languageVersion); } }
public void Compile() { var compilation = SolutionBuilder .Create() .AddProject(AnalyzerLanguage.CSharp, createExtraEmptyFile: false) .AddSnippet(TestInput) .GetCompilation(); this.tree = compilation.SyntaxTrees.First(); this.semanticModel = compilation.GetSemanticModel(this.tree); this.baseClassDeclaration = this.tree.GetRoot().DescendantNodes().OfType <ClassDeclarationSyntax>() .First(m => m.Identifier.ValueText == "Base"); this.derivedClassDeclaration1 = this.tree.GetRoot().DescendantNodes().OfType <ClassDeclarationSyntax>() .First(m => m.Identifier.ValueText == "Derived1"); this.derivedClassDeclaration2 = this.tree.GetRoot().DescendantNodes().OfType <ClassDeclarationSyntax>() .First(m => m.Identifier.ValueText == "Derived2"); this.interfaceDeclaration = this.tree.GetRoot().DescendantNodes().OfType <InterfaceDeclarationSyntax>() .First(m => m.Identifier.ValueText == "IInterface"); }
public static void VerifyNoIssueReported(string path, SonarDiagnosticAnalyzer diagnosticAnalyzer, IEnumerable <ParseOptions> options = null, params MetadataReference[] additionalReferences) { var projectBuilder = SolutionBuilder.Create() .AddProject(AnalyzerLanguage.FromPath(path)) .AddReferences(additionalReferences) .AddDocument(path); if (options == null) { var compilation = projectBuilder.GetCompilation(null); DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer); } else { foreach (var option in options) { var compilation = projectBuilder.GetCompilation(option); DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer); } } }
public void UsingCommandLineArguments_VB_Partial() { var compilation = SolutionBuilder.Create() .AddProject(AnalyzerLanguage.VisualBasic) .AddSnippet(@" Partial Class Program1 Private Shared Sub Main(ParamArray args As String()) ' Noncompliant System.Console.WriteLine(args) End Sub End Class ") .AddSnippet(@" Partial Class Program1 Private Shared Partial Sub Main(ParamArray args As String()) ' Compliant, we raise only on methods with implementation End Sub End Class ") .GetCompilation(); DiagnosticVerifier.Verify( compilation, new VB.UsingCommandLineArguments(AnalyzerConfiguration.AlwaysEnabled), CompilationErrorBehavior.Default); }
public void UsingCommandLineArguments_CS_Partial() { var compilation = SolutionBuilder.Create() .AddProject(AnalyzerLanguage.CSharp) .AddSnippet(@" partial class Program1 { static partial void Main(params string[] args) // Noncompliant { System.Console.WriteLine(args); } }") .AddSnippet(@" partial class Program1 { static partial void Main(params string[] args); // Compliant, we raise only on methods with implementation }") .GetCompilation(); DiagnosticVerifier.Verify( compilation, new CS.UsingCommandLineArguments(AnalyzerConfiguration.AlwaysEnabled), CompilationErrorBehavior.Default); }
public void DisablingRequestValidation_CS_WebConfig_Transformation(string configPath) => DiagnosticVerifier.VerifyExternalFile( SolutionBuilder.Create().AddProject(AnalyzerLanguage.CSharp).GetCompilation(), new CS.DisablingRequestValidation(AnalyzerConfiguration.AlwaysEnabled), File.ReadAllText(configPath), TestHelper.CreateSonarProjectConfig(Path.GetDirectoryName(configPath), TestHelper.CreateFilesToAnalyze(Path.GetDirectoryName(configPath), configPath)));
private static Compilation CreateCompilation() => SolutionBuilder.Create().AddProject(AnalyzerLanguage.CSharp).GetCompilation();