Ejemplo n.º 1
0
        public async Task WhenProjectEditorConfigIsChangedThenAnalyzerConfigurationUpdates()
        {
            var emitter = new ProjectLoadTestEventEmitter();

            using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAnalyzersAndEditorConfig"))
                using (var host = CreateMSBuildTestHost(
                           testProject.Directory,
                           emitter.AsExportDescriptionProvider(LoggerFactory),
                           TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true, editorConfigEnabled: true)))
                {
                    var initialProject         = host.Workspace.CurrentSolution.Projects.Single();
                    var analyzerConfigDocument = initialProject.AnalyzerConfigDocuments.Where(document => document.Name.Equals(".editorconfig")).Single();

                    File.WriteAllText(analyzerConfigDocument.FilePath, @"
root = true

[*.cs]
# IDE0005: Unnecessary using
dotnet_diagnostic.IDE0005.severity = none
");

                    await NotifyFileChanged(host, analyzerConfigDocument.FilePath);

                    emitter.WaitForProjectUpdate();

                    var diagnostics = await host.RequestCodeCheckAsync(Path.Combine(testProject.Directory, "Program.cs"));

                    Assert.NotEmpty(diagnostics.QuickFixes);
                    Assert.DoesNotContain(diagnostics.QuickFixes.OfType <DiagnosticLocation>(), x => x.Id == "IDE0005");
                }
        }
        public async Task WhenNewAnalyzerReferenceIsAdded_ThenAutomaticallyUseItWithoutRestart()
        {
            var emitter = new ProjectLoadTestEventEmitter();

            using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAnalyzers"))
                using (var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory), configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true)))
                {
                    var csprojFile = ModifyXmlFileInPlace(Path.Combine(testProject.Directory, "ProjectWithAnalyzers.csproj"),
                                                          csprojFileXml =>
                    {
                        var referencesGroup = csprojFileXml.Descendants("ItemGroup").FirstOrDefault();
                        referencesGroup.Add(new XElement("PackageReference", new XAttribute("Include", "Roslynator.Analyzers"), new XAttribute("Version", "2.1.0")));
                    });

                    await NotifyFileChanged(host, csprojFile);

                    emitter.WaitForProjectUpdate();
                    await host.RestoreProject(testProject);

                    // Todo: This can be removed and replaced with wait for event (project analyzed eg.) once they are available.
                    await Task.Delay(2000);

                    var diagnostics = await host.RequestCodeCheckAsync(Path.Combine(testProject.Directory, "Program.cs"));

                    Assert.Contains(diagnostics.QuickFixes.OfType <DiagnosticLocation>(), x => x.Id == "RCS1102"); // Analysis result from roslynator.
                }
        }
Ejemplo n.º 3
0
        public async Task WhenProjectChangesAnalyzerConfigurationIsPreserved()
        {
            var emitter = new ProjectLoadTestEventEmitter();

            using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAnalyzersAndEditorConfig"))
                using (var host = CreateMSBuildTestHost(
                           testProject.Directory,
                           emitter.AsExportDescriptionProvider(LoggerFactory),
                           TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true, editorConfigEnabled: true)))
                {
                    var initialProject      = host.Workspace.CurrentSolution.Projects.Single();
                    var firstDiagnosticsSet = await host.RequestCodeCheckAsync(Path.Combine(testProject.Directory, "Program.cs"));

                    Assert.NotEmpty(firstDiagnosticsSet.QuickFixes);
                    Assert.Contains(firstDiagnosticsSet.QuickFixes.OfType <DiagnosticLocation>(), x => x.Id == "IDE0005" && x.LogLevel == "Error");

                    // report reloading of a project
                    await NotifyFileChanged(host, initialProject.FilePath);

                    emitter.WaitForProjectUpdate();

                    var secondDiagnosticsSet = await host.RequestCodeCheckAsync(Path.Combine(testProject.Directory, "Program.cs"));

                    Assert.NotEmpty(secondDiagnosticsSet.QuickFixes);
                    Assert.Contains(secondDiagnosticsSet.QuickFixes.OfType <DiagnosticLocation>(), x => x.Id == "IDE0005" && x.LogLevel == "Error");
                }
        }
        public async Task WhenProjectRulesetFileRuleIsUpdatedThenUpdateRulesAccordingly()
        {
            var emitter = new ProjectLoadTestEventEmitter();

            using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAnalyzers"))
                using (var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory)))
                {
                    var ruleFile = ModifyXmlFileInPlace(Path.Combine(testProject.Directory, "default.ruleset"),
                                                        ruleXml => ruleXml.Descendants("Rule").Single().Attribute("Action").Value = "Error");

                    await NotifyFileChanged(host, ruleFile);

                    emitter.WaitForProjectUpdate();

                    var project = host.Workspace.CurrentSolution.Projects.Single();
                    Assert.Contains(project.CompilationOptions.SpecificDiagnosticOptions, x => x.Key == "CA1021" && x.Value == ReportDiagnostic.Error);
                }
        }