/
ProjectWithAnalyzersTests.cs
354 lines (285 loc) · 17.2 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.IO;
using System.Linq;
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);
}
}
[Fact]
public async Task WhenProjectIsLoadedThenItContainsAnalyzerConfigurationFromEditorConfig()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAnalyzersAndEditorConfig"))
using (var host = CreateMSBuildTestHost(testProject.Directory, configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true, editorConfigEnabled: true)))
{
var diagnostics = await host.RequestCodeCheckAsync(Path.Combine(testProject.Directory, "Program.cs"));
Assert.NotEmpty(diagnostics.QuickFixes);
var quickFix = diagnostics.QuickFixes.OfType<DiagnosticLocation>().Single(x => x.Id == "IDE0005");
Assert.Equal("Error", quickFix.LogLevel);
}
}
[Fact]
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.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");
}
}
[Fact]
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");
}
}
[Fact]
public async Task WhenProjectIsLoadedThenItContainsAnalyzerConfigurationFromParentFolder()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithParentEditorConfig"))
using (var host = CreateMSBuildTestHost(testProject.Directory, configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true, editorConfigEnabled: true)))
{
var project = host.Workspace.CurrentSolution.Projects.Single();
var projectFolderPath = Path.GetDirectoryName(project.FilePath);
var projectParentFolderPath = Path.GetDirectoryName(projectFolderPath);
var analyzerConfigDocument = project.AnalyzerConfigDocuments.Single();
var editorConfigFolderPath = Path.GetDirectoryName(analyzerConfigDocument.FilePath);
Assert.Equal(projectParentFolderPath, editorConfigFolderPath);
}
}
[Fact]
public async Task WhenProjectIsLoadedThenItContainsAnalyzerConfigurationFromParentEditorConfig()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithParentEditorConfig"))
using (var host = CreateMSBuildTestHost(testProject.Directory, configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true, editorConfigEnabled: true)))
{
var project = host.Workspace.CurrentSolution.Projects.Single();
var projectFolderPath = Path.GetDirectoryName(project.FilePath);
var diagnostics = await host.RequestCodeCheckAsync(Path.Combine(projectFolderPath, "Program.cs"));
Assert.NotEmpty(diagnostics.QuickFixes);
var quickFix = diagnostics.QuickFixes.OfType<DiagnosticLocation>().Single(x => x.Id == "IDE0005");
Assert.Equal("Error", quickFix.LogLevel);
}
}
[Fact]
public async Task WhenProjectParentEditorConfigIsChangedThenAnalyzerConfigurationUpdates()
{
var emitter = new ProjectLoadTestEventEmitter();
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithParentEditorConfig"))
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.Single();
File.WriteAllText(analyzerConfigDocument.FilePath, @"
root = true
[*.cs]
# IDE0005: Unnecessary using
dotnet_diagnostic.IDE0005.severity = none
");
await NotifyFileChanged(host, analyzerConfigDocument.FilePath);
emitter.WaitForProjectUpdate();
var project = host.Workspace.CurrentSolution.Projects.Single();
var projectFolderPath = Path.GetDirectoryName(project.FilePath);
var diagnostics = await host.RequestCodeCheckAsync(Path.Combine(projectFolderPath, "Program.cs"));
Assert.NotEmpty(diagnostics.QuickFixes);
Assert.DoesNotContain(diagnostics.QuickFixes.OfType<DiagnosticLocation>(), x => x.Id == "IDE0005");
}
}
[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.
}
}
[Fact]
public async Task WhenProjectIsLoadedThenItRespectsDiagnosticSuppressors()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("TwoProjectsWithAnalyzerSuppressor"))
using (var host = CreateMSBuildTestHost(testProject.Directory, configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true)))
{
var project = host.Workspace.CurrentSolution.Projects.First(p => p.Name == "App");
// by default Stylecop reported diagnostics should be:
// - The file header is missing or not located at the top of the file. [App] SA1633
// - Elements should be documented [App] SA1600
// - Element 'Program' should declare an access modifier [App] SA1400
// - Element 'Main' should declare an access modifier [App] SA1400
// However, SA1200 should be suppressed
var diagnostics = await host.RequestCodeCheckAsync(Path.Combine(testProject.Directory, "App", "Program.cs"));
Assert.NotEmpty(diagnostics.QuickFixes);
Assert.Contains(diagnostics.QuickFixes.OfType<DiagnosticLocation>(), x => x.Id == "SA1633" && x.LogLevel == "Warning");
Assert.Contains(diagnostics.QuickFixes.OfType<DiagnosticLocation>(), x => x.Id == "SA1600" && x.LogLevel == "Warning");
Assert.Contains(diagnostics.QuickFixes.OfType<DiagnosticLocation>(), x => x.Id == "SA1400" && x.LogLevel == "Warning");
Assert.DoesNotContain(diagnostics.QuickFixes.OfType<DiagnosticLocation>(), x => x.Id == "SA1200");
}
}
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);
}
}
}