コード例 #1
0
        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.
                }
        }
コード例 #2
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");
                }
        }
コード例 #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");
                }
        }
コード例 #4
0
        public async Task The_correct_project_capablities_is_emitted()
        {
            // Arrange
            var emitter = new ProjectLoadTestEventEmitter();
            var expectedCapabilities = new HashSet <string>()
            {
                "GenerateDocumentationFile",
                "CSharp",
                "Managed",
                "RelativePathDerivedDefaultNamespace",
                "AssemblyReferences",
                "COMReferences",
                "ProjectReferences",
                "SharedProjectReferences",
                "OutputGroups",
                "AllTargetOutputGroups",
                "VisualStudioWellKnownOutputGroups",
                "SingleFileGenerators",
                "DeclaredSourceItems",
                "UserSourceItems",
                "CrossPlatformExecutable",
                "Pack",
            };

            using var testProject = await TestAssets.Instance.GetTestProjectAsync("NetCore31Project");

            using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory));
            Assert.Single(emitter.ReceivedMessages);

            Assert.ProperSuperset(expectedCapabilities, emitter.ReceivedMessages[0].ProjectCapabilities.ToHashSet());
        }
コード例 #5
0
        public async Task The_correct_sdk_version_is_emitted_2()
        {
            // Arrange
            var emitter = new ProjectLoadTestEventEmitter();

            using var testProject = await TestAssets.Instance.GetTestProjectAsync("Net50Project");

            using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory));
            Assert.Equal(2, emitter.ReceivedMessages.Length);
            Assert.Equal(GetHashedFileExtension("5.0.100-rc.1.20452.10"), emitter.ReceivedMessages[0].SdkVersion);
        }
コード例 #6
0
        public async Task The_correct_project_capablities_is_emitted()
        {
            // Arrange
            var emitter = new ProjectLoadTestEventEmitter();

            using var testProject = await TestAssets.Instance.GetTestProjectAsync("NetCore31Project");

            using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory));
            Assert.Single(emitter.ReceivedMessages);
            Assert.Equal("GenerateDocumentationFile CSharp Managed ReferencesFolder LanguageService RelativePathDerivedDefaultNamespace AssemblyReferences COMReferences ProjectReferences SharedProjectReferences OutputGroups AllTargetOutputGroups VisualStudioWellKnownOutputGroups SingleFileGenerators DeclaredSourceItems UserSourceItems BuildWindowsDesktopTarget CrossPlatformExecutable Pack", string.Join(" ", emitter.ReceivedMessages[0].ProjectCapabilities));
        }
コード例 #7
0
        public async Task The_output_kind_is_emitted()
        {
            // Arrange
            var emitter = new ProjectLoadTestEventEmitter();

            using var testProject = await TestAssets.Instance.GetTestProjectAsync("NetCore31Project");

            using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory));
            Assert.Single(emitter.ReceivedMessages);
            Assert.Equal((int)OutputKind.ConsoleApplication, emitter.ReceivedMessages[0].OutputKind);
        }
コード例 #8
0
        public async Task The_correct_sdk_version_is_emitted_NET6()
        {
            // Arrange
            var emitter = new ProjectLoadTestEventEmitter();

            using var testProject = await TestAssets.Instance.GetTestProjectAsync("Net60Project");

            using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory));
            Assert.Single(emitter.ReceivedMessages);
            Assert.Equal(GetHashedFileExtension("6.0.100-preview.1.21103.13"), emitter.ReceivedMessages[0].SdkVersion);
        }
コード例 #9
0
        public async Task The_target_framework_is_emitted()
        {
            // Arrange
            var expectedTFM = "netcoreapp2.1";
            var emitter     = new ProjectLoadTestEventEmitter();

            using var testProject = await TestAssets.Instance.GetTestProjectAsync("HelloWorld");

            using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory));
            Assert.Single(emitter.ReceivedMessages);
            Assert.Equal(emitter.ReceivedMessages[0].TargetFrameworks.First(), expectedTFM);
        }
コード例 #10
0
        public async Task The_hashed_references_of_the_source_files_are_emitted()
        {
            // Arrange
            var emitter = new ProjectLoadTestEventEmitter();

            using var testProject = await TestAssets.Instance.GetTestProjectAsync("HelloWorld");

            using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory));
            Assert.Single(emitter.ReceivedMessages);
            Assert.Single(emitter.ReceivedMessages[0].FileExtensions);
            Assert.Equal(emitter.ReceivedMessages[0].FileExtensions.First(), GetHashedFileExtension(".cs"));
        }
コード例 #11
0
        public async Task Given_a_restored_project_the_references_are_emitted()
        {
            var emitter = new ProjectLoadTestEventEmitter();

            using var testProject = await TestAssets.Instance.GetTestProjectAsync("HelloWorld");

            var dotnetCliService = new DotNetCliService(LoggerFactory, emitter);
            await dotnetCliService.RestoreAsync(testProject.Directory);

            using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory));
            Assert.Single(emitter.ReceivedMessages);
            Assert.NotEmpty(emitter.ReceivedMessages[0].References.Where(reference => reference == GetHashedReference("system.core")));
        }
コード例 #12
0
        public async Task If_there_is_a_solution_file_the_project_guid_present_in_it_is_emitted()
        {
            // Arrange
            var emitter = new ProjectLoadTestEventEmitter();

            using var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectAndSolution");

            using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory));
            var expectedGuid = "A4C2694D-AEB4-4CB1-8951-5290424EF883".ToLower();

            Assert.Single(emitter.ReceivedMessages);
            Assert.Equal(emitter.ReceivedMessages[0].ProjectId, expectedGuid);
        }
コード例 #13
0
        public async Task If_there_are_multiple_target_frameworks_they_are_emitted()
        {
            var emitter = new ProjectLoadTestEventEmitter();

            using var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithMultiTFMLib/Lib");

            using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory));
            Assert.Single(emitter.ReceivedMessages);
            var tfm = emitter.ReceivedMessages[0].TargetFrameworks.ToArray();

            Assert.Equal(2, tfm.Count());
            Assert.Equal("netstandard1.3", tfm[0]);
            Assert.Equal("netstandard2.0", tfm[1]);
        }
コード例 #14
0
        public async Task If_there_is_no_solution_file_the_hash_of_project_file_content_and_name_is_emitted()
        {
            // Arrange
            var emitter = new ProjectLoadTestEventEmitter();

            using var testProject = await TestAssets.Instance.GetTestProjectAsync("HelloWorld");

            using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory));
            var projectFileContent = File.ReadAllText(Directory.GetFiles(testProject.Directory, "*.csproj").Single());
            var expectedGuid       = GetHashedReference($"Filename: HelloWorld.csproj\n{projectFileContent}");

            Assert.Single(emitter.ReceivedMessages);
            Assert.Equal(emitter.ReceivedMessages[0].ProjectId, expectedGuid);
        }
コード例 #15
0
        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);
                }
        }