-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathProjectWithAnalyzersTests.cs
185 lines (151 loc) · 8.35 KB
/
ProjectWithAnalyzersTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;
using OmniSharp.Eventing;
using OmniSharp.FileWatching;
using OmniSharp.Models.Diagnostics;
using OmniSharp.Models.FilesChanged;
using OmniSharp.Services;
using TestUtility;
using Xunit;
using Xunit.Abstractions;
using static OmniSharp.MSBuild.Tests.ProjectLoadListenerTests;
namespace OmniSharp.MSBuild.Tests
{
public class ProjectWithAnalyzersTests : AbstractMSBuildTestFixture
{
public ProjectWithAnalyzersTests(ITestOutputHelper output)
: base(output)
{
}
[Fact]
public async Task WhenProjectIsRestoredThenReanalyzeProject()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAnalyzers"))
using (var host = CreateMSBuildTestHost(testProject.Directory, configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true)))
{
await host.RestoreProject(testProject);
await Task.Delay(2000);
var diagnostics = await host.RequestCodeCheckAsync(Path.Combine(testProject.Directory, "Program.cs"));
Assert.NotEmpty(diagnostics.QuickFixes);
Assert.Contains(diagnostics.QuickFixes.OfType<DiagnosticLocation>(), x => x.Id == "IDE0060"); // Unused args.
}
}
[Fact]
public async Task WhenProjectHasAnalyzersItDoesntLockAnalyzerDlls()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAnalyzers"))
{
// TODO: Restore when host is running doesn't reload new analyzer references yet, move this
// after host start after that is fixed.
await RestoreProject(testProject);
using (var host = CreateMSBuildTestHost(testProject.Directory, configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true)))
{
var analyzerReferences = host.Workspace.CurrentSolution.Projects.Single().AnalyzerReferences.ToList();
Assert.NotEmpty(analyzerReferences);
// This should not throw when analyzers are shadow copied.
Directory.Delete(Path.Combine(testProject.Directory, "./nugets"), true);
}
}
}
[Fact]
public async Task WhenProjectIsLoadedThenItContainsCustomRulesetsFromCsproj()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAnalyzers"))
using (var host = CreateMSBuildTestHost(testProject.Directory, configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true)))
{
var project = host.Workspace.CurrentSolution.Projects.Single();
Assert.Contains(project.CompilationOptions.SpecificDiagnosticOptions, x => x.Key == "CA1021" && x.Value == ReportDiagnostic.Warn);
}
}
[Theory]
[InlineData("ProjectWithDisabledAnalyzers")]
[InlineData("ProjectWithDisabledAnalyzers2")]
public async Task WhenProjectWithRunAnalyzersDisabledIsLoadedThenAnalyzersAreIgnored(string projectName)
{
using var testProject = await TestAssets.Instance.GetTestProjectAsync(projectName);
await RestoreProject(testProject);
using var host = CreateMSBuildTestHost(testProject.Directory, configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true));
var analyzerReferences = host.Workspace.CurrentSolution.Projects.Single().AnalyzerReferences.ToList();
Assert.Empty(analyzerReferences);
}
[Fact]
public async Task WhenProjectRulesetFileIsChangedThenUpdateRulesAccordingly()
{
var emitter = new ProjectLoadTestEventEmitter();
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAnalyzers"))
using (var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory)))
{
var csprojFile = ModifyXmlFileInPlace(Path.Combine(testProject.Directory, "ProjectWithAnalyzers.csproj"),
csprojFileXml => csprojFileXml.Descendants("CodeAnalysisRuleSet").Single().Value = "witherrorlevel.ruleset");
await NotifyFileChanged(host, csprojFile);
emitter.WaitForProjectUpdate();
var project = host.Workspace.CurrentSolution.Projects.Single();
Assert.Contains(project.CompilationOptions.SpecificDiagnosticOptions, x => x.Key == "CA1021" && x.Value == ReportDiagnostic.Error);
}
}
[Fact]
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);
}
}
// Unstable with MSBuild 16.3 on *nix
[ConditionalFact(typeof(WindowsOnly))]
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.
}
}
private string ModifyXmlFileInPlace(string file, Action<XDocument> docUpdateAction)
{
var xmlFile = XDocument.Load(file);
docUpdateAction(xmlFile);
xmlFile.Save(file);
return file;
}
private static async Task NotifyFileChanged(OmniSharpTestHost host, string file)
{
await host.GetFilesChangedService().Handle(new[] {
new FilesChangedRequest() {
FileName = file,
ChangeType = FileChangeType.Change
}
});
}
private static async Task RestoreProject(ITestProject testProject)
{
await new DotNetCliService(new LoggerFactory(), NullEventEmitter.Instance).RestoreAsync(testProject.Directory);
}
}
}