-
Notifications
You must be signed in to change notification settings - Fork 222
/
Verifier.cs
388 lines (358 loc) · 17 KB
/
Verifier.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2024 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Google.Protobuf;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.MSBuild;
using SonarAnalyzer.Rules;
using SonarAnalyzer.TestFramework.Build;
namespace SonarAnalyzer.TestFramework.Verification;
internal class Verifier
{
private const string TestCases = "TestCases";
private static readonly Regex ImportsRegexVB = new(@"^\s*Imports\s+.+$", RegexOptions.Multiline | RegexOptions.RightToLeft, RegexConstants.DefaultTimeout);
private readonly VerifierBuilder builder;
private readonly DiagnosticAnalyzer[] analyzers;
private readonly SonarCodeFix codeFix;
private readonly AnalyzerLanguage language;
private readonly string[] onlyDiagnosticIds;
public Verifier(VerifierBuilder builder)
{
this.builder = builder ?? throw new ArgumentNullException(nameof(builder));
onlyDiagnosticIds = builder.OnlyDiagnostics.Select(x => x.Id).ToArray();
analyzers = builder.Analyzers.Select(x => x()).ToArray();
if (!analyzers.Any())
{
throw new ArgumentException($"{nameof(builder.Analyzers)} cannot be empty. Use {nameof(VerifierBuilder)}<TAnalyzer> instead or add at least one analyzer using {nameof(builder)}.{nameof(builder.AddAnalyzer)}().");
}
if (Array.Exists(analyzers, x => x == null))
{
throw new ArgumentException("Analyzer instance cannot be null.");
}
var allLanguages = analyzers.SelectMany(x => x.GetType().GetCustomAttributes<DiagnosticAnalyzerAttribute>()).SelectMany(x => x.Languages).Distinct().ToArray();
if (allLanguages.Length > 1)
{
throw new ArgumentException($"All {nameof(builder.Analyzers)} must declare the same language in their DiagnosticAnalyzerAttribute.");
}
language = AnalyzerLanguage.FromName(allLanguages.Single());
if (!builder.Paths.Any() && !builder.Snippets.Any())
{
throw new ArgumentException($"{nameof(builder.Paths)} cannot be empty. Add at least one file using {nameof(builder)}.{nameof(builder.AddPaths)}() or {nameof(builder.AddSnippet)}().");
}
foreach (var path in builder.Paths)
{
ValidateExtension(path);
}
if (builder.ProtobufPath is not null)
{
ValidateSingleAnalyzer(nameof(builder.ProtobufPath));
if (analyzers.Single() is not UtilityAnalyzerBase)
{
throw new ArgumentException($"{analyzers.Single().GetType().Name} does not inherit from {nameof(UtilityAnalyzerBase)}.");
}
}
if (builder.CodeFix is not null)
{
codeFix = builder.CodeFix();
ValidateCodeFix();
}
if (builder.IsRazor)
{
if (builder.ParseOptions.IsEmpty)
{
throw new ArgumentException($"{nameof(builder.IsRazor)} was set. {nameof(ParseOptions)} must be specified.");
}
else if (language != AnalyzerLanguage.CSharp)
{
throw new ArgumentException($"{nameof(builder.IsRazor)} was set for {language} analyzer. Only C# is supported.");
}
}
}
public void Verify() // This should never have any arguments
{
if (codeFix != null)
{
throw new InvalidOperationException($"Cannot use {nameof(Verify)} with {nameof(builder.CodeFix)} set.");
}
foreach (var compilation in Compile(builder.ConcurrentAnalysis))
{
DiagnosticVerifier.Verify(compilation.Compilation, analyzers, builder.ErrorBehavior, builder.AdditionalFilePath, onlyDiagnosticIds, compilation.AdditionalSourceFiles);
}
}
public void VerifyNoIssueReported() // This should never have any arguments
{
foreach (var compilation in Compile(builder.ConcurrentAnalysis))
{
foreach (var analyzer in analyzers)
{
DiagnosticVerifier.VerifyNoIssueReported(compilation.Compilation, analyzer, builder.ErrorBehavior, builder.AdditionalFilePath, onlyDiagnosticIds);
}
}
}
public void VerifyCodeFix() // This should never have any arguments
{
_ = codeFix ?? throw new InvalidOperationException($"{nameof(builder.CodeFix)} was not set.");
var document = CreateProject(false).FindDocument(Path.Combine(builder.BasePath ?? string.Empty, Path.GetFileName(builder.Paths.Single())));
var codeFixVerifier = new CodeFixVerifier(analyzers.Single(), codeFix, document, builder.CodeFixTitle);
var fixAllProvider = codeFix.GetFixAllProvider();
foreach (var parseOptions in builder.ParseOptions.OrDefault(language.LanguageName))
{
codeFixVerifier.VerifyWhileDocumentChanges(parseOptions, TestCasePath(builder.CodeFixedPath));
if (fixAllProvider is not null)
{
codeFixVerifier.VerifyFixAllProvider(fixAllProvider, parseOptions, TestCasePath(builder.CodeFixedPathBatch ?? builder.CodeFixedPath));
}
}
}
public void VerifyUtilityAnalyzerProducesEmptyProtobuf() // This should never have any arguments
{
foreach (var compilation in Compile(false))
{
DiagnosticVerifier.Verify(compilation.Compilation, analyzers.Single(), builder.AdditionalFilePath);
new FileInfo(builder.ProtobufPath).Length.Should().Be(0, "protobuf file should be empty");
}
}
public void VerifyUtilityAnalyzer<TMessage>(Action<IReadOnlyList<TMessage>> verifyProtobuf)
where TMessage : IMessage<TMessage>, new()
{
foreach (var compilation in Compile(false))
{
DiagnosticVerifier.Verify(compilation.Compilation, analyzers.Single(), builder.AdditionalFilePath);
verifyProtobuf(ReadProtobuf().ToList());
}
IEnumerable<TMessage> ReadProtobuf()
{
using var input = File.OpenRead(builder.ProtobufPath);
var parser = new MessageParser<TMessage>(() => new TMessage());
while (input.Position < input.Length)
{
yield return parser.ParseDelimitedFrom(input);
}
}
}
public IEnumerable<CompilationData> Compile(bool concurrentAnalysis)
{
using var scope = new EnvironmentVariableScope { EnableConcurrentAnalysis = concurrentAnalysis };
return builder.IsRazor
? CompileRazor()
: CreateProject(concurrentAnalysis).Solution.Compile(builder.ParseOptions.ToArray()).Select(x => new CompilationData(x, null));
}
private IEnumerable<CompilationData> CompileRazor()
{
if (!MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterDefaults();
}
using var workspace = MSBuildWorkspace.Create();
workspace.WorkspaceFailed += (_, failure) => Console.WriteLine(failure.Diagnostic);
// Copy razor project directory and test case files to a temporary build location
var tempPath = Path.Combine(Path.GetTempPath(), $"ut-razor-{Guid.NewGuid()}");
Directory.CreateDirectory(tempPath);
try
{
foreach (var langVersion in builder.ParseOptions.Cast<CSharpParseOptions>().Select(LangVersion))
{
var projectRoot = Path.Combine(tempPath, langVersion);
Directory.CreateDirectory(projectRoot);
var csProjPath = PrepareRazorProject(projectRoot, langVersion);
var razorFiles = PrepareRazorFiles(projectRoot);
// To avoid reference loading issues, ensure that the project references are restored before compilation.
if (RestorePackages(csProjPath, projectRoot))
{
yield return new(workspace.OpenProjectAsync(csProjPath).Result.GetCompilationAsync().Result, razorFiles.ToArray());
}
else
{
throw new InvalidOperationException($"Failed to restore project {csProjPath}.");
}
}
}
finally
{
Directory.Delete(tempPath, true);
}
static string LangVersion(CSharpParseOptions options) =>
options.LanguageVersion switch
{
// 5 and 6 should not be needed here
// 7 also does not support with Nullable context
// 8 and 9 do not support global using directives
LanguageVersion.CSharp10 => "10.0",
LanguageVersion.CSharp11 => "11.0",
LanguageVersion.CSharp12 => "12.0",
_ => throw new NotSupportedException($"Unexpected language version {options.LanguageVersion}. Update this switch to add the new version.")
};
}
private string PrepareRazorProject(string projectRoot, string langVersion)
{
// To improve: Paths are currently relative to entry assembly => needs to be duplicated in different projects for now.
foreach (var file in Directory.GetFiles(@"TestCases\Razor\EmptyProject"))
{
File.Copy(file, Path.Combine(projectRoot, Path.GetFileName(file)));
}
var csProjPath = Path.Combine(projectRoot, "EmptyProject.csproj");
var xml = XElement.Load(csProjPath);
xml.Descendants("LangVersion").Single().Value = langVersion;
var references = xml.Descendants("ItemGroup").Single();
foreach (var reference in builder.References)
{
references.Add(new XElement("Reference", new XAttribute("Include", reference.Display)));
}
xml.Save(csProjPath);
return csProjPath;
}
private List<string> PrepareRazorFiles(string projectRoot)
{
var razorFiles = new List<string>();
var snippetCount = 0;
// To improve: Paths are currently relative to entry assembly => needs to be duplicated in different projects for now.
foreach (var file in builder.Paths.Select(TestCasePath))
{
var filePath = Path.Combine(projectRoot, Path.GetFileName(file));
File.Copy(file, filePath);
if (IsRazorOrCshtml(filePath))
{
razorFiles.Add(filePath);
}
}
foreach (var snippet in builder.Snippets)
{
var filePath = Path.Combine(projectRoot, snippet.FileName ?? $"snippet.{snippetCount++}{language.FileExtension}");
File.WriteAllText(filePath, snippet.Content);
if (IsRazorOrCshtml(filePath))
{
razorFiles.Add(filePath);
}
}
return razorFiles;
}
private ProjectBuilder CreateProject(bool concurrentAnalysis)
{
var paths = builder.Paths.Select(TestCasePath).ToList();
var contentFilePaths = paths.Where(IsRazorOrCshtml).ToArray();
var sourceFilePaths = paths.Except(contentFilePaths).ToArray();
var editorConfigGenerator = new EditorConfigGenerator(TestCaseDirectory());
return SolutionBuilder.Create()
.AddProject(language, builder.OutputKind)
.AddDocuments(sourceFilePaths)
.AddAdditionalDocuments(contentFilePaths)
.AddDocuments(concurrentAnalysis && builder.AutogenerateConcurrentFiles ? CreateConcurrencyTest(sourceFilePaths) : [])
.AddAdditionalDocuments(concurrentAnalysis && builder.AutogenerateConcurrentFiles ? CreateConcurrencyTest(contentFilePaths) : [])
.AddAnalyzerReferences(contentFilePaths.Length > 0 ? SourceGeneratorProvider.SourceGenerators : [])
.AddSnippets(builder.Snippets.ToArray())
.AddReferences(builder.References)
.AddAnalyzerConfigDocument(Path.Combine(TestCaseDirectory(), ".editorconfig"), editorConfigGenerator.Generate(contentFilePaths));
}
private IEnumerable<string> CreateConcurrencyTest(IEnumerable<string> paths)
{
foreach (var path in paths)
{
var newPath = Path.ChangeExtension(path, ".Concurrent" + Path.GetExtension(path));
var content = File.ReadAllText(path, Encoding.UTF8);
File.WriteAllText(newPath, InsertConcurrentNamespace(content));
yield return newPath;
}
}
private string InsertConcurrentNamespace(string content)
{
return language.LanguageName switch
{
LanguageNames.CSharp => $"namespace AppendedNamespaceForConcurrencyTest {{ {content} {Environment.NewLine}}}", // Last line can be a comment
LanguageNames.VisualBasic => content.Insert(ImportsIndexVB(), "Namespace AppendedNamespaceForConcurrencyTest : ") + Environment.NewLine + " : End Namespace",
_ => throw new UnexpectedLanguageException(language)
};
int ImportsIndexVB() =>
ImportsRegexVB.Match(content) is { Success: true } match ? match.Index + match.Length + 1 : 0;
}
private string TestCaseDirectory() =>
Path.GetFullPath(builder.BasePath == null ? TestCases : Path.Combine(TestCases, builder.BasePath));
private string TestCasePath(string fileName) =>
Path.Combine(TestCaseDirectory(), fileName);
private void ValidateSingleAnalyzer(string propertyName)
{
if (builder.Analyzers.Length != 1)
{
throw new ArgumentException($"When {propertyName} is set, {nameof(builder.Analyzers)} must contain only 1 analyzer, but {analyzers.Length} were found.");
}
}
private void ValidateExtension(string path)
{
if (!Path.GetExtension(path).Equals(language.FileExtension, StringComparison.OrdinalIgnoreCase) && !IsRazorOrCshtml(path))
{
throw new ArgumentException($"Path '{path}' doesn't match {language.LanguageName} file extension '{language.FileExtension}'.");
}
}
private void ValidateCodeFix()
{
_ = builder.CodeFixedPath ?? throw new ArgumentException($"{nameof(builder.CodeFixedPath)} was not set.");
ValidateSingleAnalyzer(nameof(builder.CodeFix));
if (builder.Paths.Length != 1)
{
throw new ArgumentException($"{nameof(builder.Paths)} must contain only 1 file, but {builder.Paths.Length} were found.");
}
if (builder.Snippets.Any())
{
throw new ArgumentException($"{nameof(builder.Snippets)} must be empty when {nameof(builder.CodeFix)} is set.");
}
ValidateExtension(builder.CodeFixedPath);
if (builder.CodeFixedPathBatch is not null)
{
ValidateExtension(builder.CodeFixedPathBatch);
}
if (codeFix.GetType().GetCustomAttribute<ExportCodeFixProviderAttribute>() is { } codeFixAttribute)
{
if (codeFixAttribute.Languages.Single() != language.LanguageName)
{
throw new ArgumentException($"{analyzers.Single().GetType().Name} language {language.LanguageName} does not match {codeFix.GetType().Name} language.");
}
}
else
{
throw new ArgumentException($"{codeFix.GetType().Name} does not have {nameof(ExportCodeFixProviderAttribute)}.");
}
if (!analyzers.Single().SupportedDiagnostics.Select(x => x.Id).Intersect(codeFix.FixableDiagnosticIds).Any())
{
throw new ArgumentException($"{analyzers.Single().GetType().Name} does not support diagnostics fixable by the {codeFix.GetType().Name}.");
}
}
private static bool IsRazorOrCshtml(string path) =>
Path.GetExtension(path) is { } extension
&& (extension.Equals(".razor", StringComparison.OrdinalIgnoreCase) || extension.Equals(".cshtml", StringComparison.OrdinalIgnoreCase));
private static bool RestorePackages(string path, string workingDirectory)
{
using var process = new Process();
process.StartInfo = new ProcessStartInfo
{
FileName = Environment.GetEnvironmentVariable("DOTNET_PATH") ?? "dotnet",
Arguments = $"restore \"{path}\"",
WorkingDirectory = workingDirectory
};
process.Start();
process.WaitForExit();
return process.ExitCode == 0;
}
public sealed record CompilationData(Compilation Compilation, string[] AdditionalSourceFiles);
}