/
AbstractFormatterTests.cs
323 lines (272 loc) · 16.5 KB
/
AbstractFormatterTests.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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Tools.Formatters;
using Microsoft.CodeAnalysis.Tools.Tests.Utilities;
using Microsoft.CodeAnalysis.Tools.Utilities;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.Composition;
using Xunit;
namespace Microsoft.CodeAnalysis.Tools.Tests.Formatters
{
public abstract class AbstractFormatterTest
{
private static MetadataReference MicrosoftVisualBasicReference => MetadataReference.CreateFromFile(typeof(Microsoft.VisualBasic.Strings).Assembly.Location);
private static Lazy<IExportProviderFactory> ExportProviderFactory { get; }
static AbstractFormatterTest()
{
ExportProviderFactory = new Lazy<IExportProviderFactory>(
() =>
{
var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance, isNonPublicSupported: true);
var parts = Task.Run(() => discovery.CreatePartsAsync(MefHostServices.DefaultAssemblies)).GetAwaiter().GetResult();
var catalog = ComposableCatalog.Create(Resolver.DefaultInstance).AddParts(parts);
var configuration = CompositionConfiguration.Create(catalog);
var runtimeComposition = RuntimeComposition.CreateRuntimeComposition(configuration);
return runtimeComposition.CreateExportProviderFactory();
},
LazyThreadSafetyMode.ExecutionAndPublication);
}
protected virtual ReferenceAssemblies ReferenceAssemblies => ReferenceAssemblies.Default;
protected virtual string DefaultFilePathPrefix => "Test";
protected virtual string DefaultTestProjectName => "TestProject";
// This folder path needs to appear rooted when adding the AnalyzerConfigDocument.
// We achieve this by prepending a directory separator.
protected virtual string DefaultFolderPath => Path.DirectorySeparatorChar + DefaultTestProjectName;
protected virtual string DefaultTestProjectPath => Path.Combine(DefaultFolderPath, $"{DefaultTestProjectName}.{DefaultFileExt}proj");
protected virtual string DefaultEditorConfigPath => Path.Combine(DefaultFolderPath, ".editorconfig");
protected virtual string DefaultFilePath => Path.Combine(DefaultFolderPath, $"{DefaultFilePathPrefix}0.{DefaultFileExt}");
protected abstract string DefaultFileExt { get; }
private protected abstract ICodeFormatter Formatter { get; }
protected AbstractFormatterTest()
{
TestState = new SolutionState(DefaultFilePathPrefix, DefaultFileExt);
}
/// <summary>
/// Gets the language name used for the test.
/// </summary>
public abstract string Language { get; }
private static TestLogger Logger => new TestLogger();
public SolutionState TestState { get; }
private protected string ToEditorConfig(IReadOnlyDictionary<string, string> editorConfig) => $@"root = true
[*.{DefaultFileExt}]
{ string.Join(Environment.NewLine, editorConfig.Select(kvp => $"{kvp.Key} = {kvp.Value}")) }
";
private protected Task<SourceText> AssertCodeUnchangedAsync(
string code,
IReadOnlyDictionary<string, string> editorConfig,
Encoding encoding = null,
FixCategory fixCategory = FixCategory.Whitespace,
DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
DiagnosticSeverity analyzerSeverity = DiagnosticSeverity.Error)
{
return AssertCodeChangedAsync(code, code, ToEditorConfig(editorConfig), encoding, fixCategory, codeStyleSeverity, analyzerSeverity);
}
private protected Task<SourceText> AssertCodeChangedAsync(
string testCode,
string expectedCode,
IReadOnlyDictionary<string, string> editorConfig,
Encoding encoding = null,
FixCategory fixCategory = FixCategory.Whitespace,
DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
DiagnosticSeverity analyzerSeverity = DiagnosticSeverity.Error)
{
return AssertCodeChangedAsync(testCode, expectedCode, ToEditorConfig(editorConfig), encoding, fixCategory, codeStyleSeverity, analyzerSeverity);
}
private protected async Task<SourceText> AssertCodeChangedAsync(
string testCode,
string expectedCode,
string editorConfig,
Encoding encoding = null,
FixCategory fixCategory = FixCategory.Whitespace,
DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
DiagnosticSeverity analyzerSeverity = DiagnosticSeverity.Error)
{
var text = SourceText.From(testCode, encoding ?? Encoding.UTF8);
TestState.Sources.Add(text);
var solution = GetSolution(TestState.Sources.ToArray(), TestState.AdditionalFiles.ToArray(), TestState.AdditionalReferences.ToArray(), editorConfig);
var project = solution.Projects.Single();
var document = project.Documents.Single();
var fileMatcher = SourceFileMatcher.CreateMatcher(new[] { document.FilePath }, exclude: Array.Empty<string>());
var formatOptions = new FormatOptions(
workspaceFilePath: project.FilePath,
workspaceType: WorkspaceType.Solution,
logLevel: LogLevel.Trace,
fixCategory,
codeStyleSeverity,
analyzerSeverity,
saveFormattedFiles: true,
changesAreErrors: false,
fileMatcher,
reportPath: string.Empty,
includeGeneratedFiles: false);
var pathsToFormat = GetOnlyFileToFormat(solution);
var formattedSolution = await Formatter.FormatAsync(solution, pathsToFormat, formatOptions, Logger, new List<FormattedFile>(), default);
var formattedDocument = GetOnlyDocument(formattedSolution);
var formattedText = await formattedDocument.GetTextAsync();
Assert.Equal(expectedCode, formattedText.ToString());
return formattedText;
}
/// <summary>
/// Gets the only <see cref="DocumentId"/>.
/// </summary>
/// <param name="solution">A Solution containing a single Project containing a single Document.</param>
/// <returns>The only document id.</returns>
internal ImmutableArray<DocumentId> GetOnlyFileToFormat(Solution solution) => ImmutableArray.Create(GetOnlyDocument(solution).Id);
/// <summary>
/// Gets the only <see cref="Document"/> contained within the only <see cref="Project"/> within the <see cref="Solution"/>.
/// </summary>
/// <param name="solution">A Solution containing a single Project containing a single Document.</param>
/// <returns>The document contained within.</returns>
public Document GetOnlyDocument(Solution solution) => solution.Projects.Single().Documents.Single();
/// <summary>
/// Gets the collection of inputs to provide to the XML documentation resolver.
/// </summary>
/// <remarks>
/// <para>Files in this collection may be referenced via <c><include></c> elements in documentation
/// comments.</para>
/// </remarks>
public Dictionary<string, string> XmlReferences { get; } = new Dictionary<string, string>();
/// <summary>
/// Given an array of strings as sources and a language, turn them into a <see cref="Project"/> and return the
/// solution.
/// </summary>
/// <param name="sources">Classes in the form of strings.</param>
/// <param name="additionalFiles">Additional documents to include in the project.</param>
/// <param name="additionalMetadataReferences">Additional metadata references to include in the project.</param>
/// <param name="editorConfig">The .editorconfig to apply to this solution.</param>
/// <returns>A solution containing a project with the specified sources and additional files.</returns>
private protected Solution GetSolution((string filename, SourceText content)[] sources, (string filename, SourceText content)[] additionalFiles, MetadataReference[] additionalMetadataReferences, IReadOnlyDictionary<string, string> editorConfig)
{
return GetSolution(sources, additionalFiles, additionalMetadataReferences, ToEditorConfig(editorConfig));
}
/// <summary>
/// Given an array of strings as sources and a language, turn them into a <see cref="Project"/> and return the
/// solution.
/// </summary>
/// <param name="sources">Classes in the form of strings.</param>
/// <param name="additionalFiles">Additional documents to include in the project.</param>
/// <param name="additionalMetadataReferences">Additional metadata references to include in the project.</param>
/// <param name="editorConfig">The .editorconfig to apply to this solution.</param>
/// <returns>A solution containing a project with the specified sources and additional files.</returns>
private protected Solution GetSolution((string filename, SourceText content)[] sources, (string filename, SourceText content)[] additionalFiles, MetadataReference[] additionalMetadataReferences, string editorConfig)
{
var project = CreateProject(sources, additionalFiles, additionalMetadataReferences, Language, SourceText.From(editorConfig, Encoding.UTF8));
return project.Solution;
}
/// <summary>
/// Create a project using the input strings as sources.
/// </summary>
/// <remarks>
/// <para>This method first creates a <see cref="Project"/> by calling <see cref="CreateProjectImpl"/>, and then
/// applies compilation options to the project by calling <see cref="ApplyCompilationOptions"/>.</para>
/// </remarks>
/// <param name="sources">Classes in the form of strings.</param>
/// <param name="additionalFiles">Additional documents to include in the project.</param>
/// <param name="additionalMetadataReferences">Additional metadata references to include in the project.</param>
/// <param name="language">The language the source classes are in. Values may be taken from the
/// <see cref="LanguageNames"/> class.</param>
/// <param name="editorConfigText">The .editorconfig to apply to this project.</param>
/// <returns>A <see cref="Project"/> created out of the <see cref="Document"/>s created from the source
/// strings.</returns>
protected Project CreateProject((string filename, SourceText content)[] sources, (string filename, SourceText content)[] additionalFiles, MetadataReference[] additionalMetadataReferences, string language, SourceText editorConfigText)
{
language ??= Language;
return CreateProjectImpl(sources, additionalFiles, additionalMetadataReferences, language, editorConfigText);
}
/// <summary>
/// Create a project using the input strings as sources.
/// </summary>
/// <param name="sources">Classes in the form of strings.</param>
/// <param name="additionalFiles">Additional documents to include in the project.</param>
/// <param name="additionalMetadataReferences">Additional metadata references to include in the project.</param>
/// <param name="language">The language the source classes are in. Values may be taken from the
/// <see cref="LanguageNames"/> class.</param>
/// <param name="editorConfigText">The .editorconfig to apply to this project.</param>
/// <returns>A <see cref="Project"/> created out of the <see cref="Document"/>s created from the source
/// strings.</returns>
protected virtual Project CreateProjectImpl((string filename, SourceText content)[] sources, (string filename, SourceText content)[] additionalFiles, MetadataReference[] additionalMetadataReferences, string language, SourceText editorConfigText)
{
var projectId = ProjectId.CreateNewId(debugName: DefaultTestProjectName);
var solution = CreateSolution(projectId, language, editorConfigText);
solution = solution.AddMetadataReferences(projectId, additionalMetadataReferences);
for (var i = 0; i < sources.Length; i++)
{
(var newFileName, var source) = sources[i];
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddDocument(documentId, newFileName, source, filePath: Path.Combine(DefaultFolderPath, newFileName));
}
for (var i = 0; i < additionalFiles.Length; i++)
{
(var newFileName, var source) = additionalFiles[i];
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddAdditionalDocument(documentId, newFileName, source);
}
return solution.GetProject(projectId);
}
/// <summary>
/// Creates a solution that will be used as parent for the sources that need to be checked.
/// </summary>
/// <param name="projectId">The project identifier to use.</param>
/// <param name="language">The language for which the solution is being created.</param>
/// <param name="editorConfigText">The .editorconfig to apply to this solution.</param>
/// <returns>The created solution.</returns>
protected virtual Solution CreateSolution(ProjectId projectId, string language, SourceText editorConfigText)
{
var xmlReferenceResolver = new TestXmlReferenceResolver();
foreach (var xmlReference in XmlReferences)
{
xmlReferenceResolver.XmlReferences.Add(xmlReference.Key, xmlReference.Value);
}
var compilationOptions = CreateCompilationOptions()
.WithXmlReferenceResolver(xmlReferenceResolver)
.WithAssemblyIdentityComparer(ReferenceAssemblies.AssemblyIdentityComparer);
var parseOptions = CreateParseOptions();
var referenceAssemblies = ReferenceAssemblies.ResolveAsync(language, CancellationToken.None).GetAwaiter().GetResult();
var editorConfigDocument = DocumentInfo.Create(
DocumentId.CreateNewId(projectId, DefaultEditorConfigPath),
name: DefaultEditorConfigPath,
loader: TextLoader.From(TextAndVersion.Create(editorConfigText, VersionStamp.Create())),
filePath: DefaultEditorConfigPath);
var projectInfo = ProjectInfo.Create(
projectId,
VersionStamp.Create(),
name: DefaultTestProjectName,
assemblyName: DefaultTestProjectName,
language,
filePath: DefaultTestProjectPath,
outputFilePath: Path.ChangeExtension(DefaultTestProjectPath, "dll"),
compilationOptions: compilationOptions,
parseOptions: parseOptions,
metadataReferences: referenceAssemblies,
isSubmission: false)
.WithDefaultNamespace(DefaultTestProjectName)
.WithAnalyzerConfigDocuments(ImmutableArray.Create(editorConfigDocument));
var solution = CreateWorkspace()
.CurrentSolution
.AddProject(projectInfo);
if (language == LanguageNames.VisualBasic)
{
solution = solution.AddMetadataReference(projectId, MicrosoftVisualBasicReference);
}
return solution;
}
public virtual AdhocWorkspace CreateWorkspace()
{
var exportProvider = ExportProviderFactory.Value.CreateExportProvider();
var host = MefHostServices.Create(exportProvider.AsCompositionContext());
return new AdhocWorkspace(host);
}
protected abstract CompilationOptions CreateCompilationOptions();
protected abstract ParseOptions CreateParseOptions();
}
}