Example #1
0
        private void RunAnalysisWithoutVerification(string path, SonarDiagnosticAnalyzer diagnosticAnalyzer,
                                                    IEnumerable <MetadataReference> additionalReferences = null)
        {
            var compilation = SolutionBuilder.Create()
                              .AddTestProject(AnalyzerLanguage.FromPath(path))
                              .AddReferences(additionalReferences)
                              .AddDocument(path)
                              .GetCompilation();

            DiagnosticVerifier.GetDiagnostics(compilation, diagnosticAnalyzer,
                                              CompilationErrorBehavior.FailTest,
                                              verifyNoExceptionIsThrown: false);
        }
        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);
            }
        }