未验证 提交 33a47a6b 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #41803 from sharwell/analyzer-config

Apply options via .editorconfig for analyzer testing
......@@ -45,8 +45,18 @@ public Test()
solution = solution.WithProjectCompilationOptions(projectId, compilationOptions);
#if !CODE_STYLE // TODO: Add support for Options based tests in CodeStyle layer
var (analyzerConfigSource, remainingOptions) = CodeFixVerifierHelper.ConvertOptionsToAnalyzerConfig(DefaultFileExt, Options);
if (analyzerConfigSource is object)
{
foreach (var id in solution.ProjectIds)
{
var documentId = DocumentId.CreateNewId(id, ".editorconfig");
solution = solution.AddAnalyzerConfigDocument(documentId, ".editorconfig", analyzerConfigSource, filePath: "/.editorconfig");
}
}
var options = solution.Options;
foreach (var (key, value) in Options)
foreach (var (key, value) in remainingOptions)
{
options = options.WithChangedOption(key, value);
}
......
......@@ -5,8 +5,13 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions
......@@ -75,5 +80,62 @@ private static void VerifyMessageHelpLinkUri(DiagnosticAnalyzer analyzer)
Assert.NotEqual("", descriptor.HelpLinkUri ?? "");
}
}
#if !CODE_STYLE
public static (SourceText? analyzerConfig, IEnumerable<KeyValuePair<OptionKey, object?>> options) ConvertOptionsToAnalyzerConfig(string defaultFileExtension, OptionsCollection options)
{
if (options.Count == 0)
{
return (null, options);
}
var optionSet = new OptionSetWrapper(options.ToDictionary<KeyValuePair<OptionKey, object?>, OptionKey, object?>(option => option.Key, option => option.Value));
var remainingOptions = new List<KeyValuePair<OptionKey, object?>>();
var analyzerConfig = new StringBuilder();
analyzerConfig.AppendLine("root = true");
analyzerConfig.AppendLine();
analyzerConfig.AppendLine($"[*.{defaultFileExtension}]");
foreach (var (key, value) in options)
{
var editorConfigStorageLocation = key.Option.StorageLocations.OfType<IEditorConfigStorageLocation2>().FirstOrDefault();
if (editorConfigStorageLocation is null)
{
remainingOptions.Add(KeyValuePairUtil.Create<OptionKey, object?>(key, value));
continue;
}
analyzerConfig.AppendLine(editorConfigStorageLocation.GetEditorConfigString(value, optionSet));
}
return (SourceText.From(analyzerConfig.ToString(), Encoding.UTF8), remainingOptions);
}
private sealed class OptionSetWrapper : OptionSet
{
private readonly Dictionary<OptionKey, object?> _options;
public OptionSetWrapper(Dictionary<OptionKey, object?> options)
{
_options = options;
}
public override object? GetOption(OptionKey optionKey)
{
if (!_options.TryGetValue(optionKey, out var value))
{
value = optionKey.Option.DefaultValue;
}
return value;
}
public override OptionSet WithChangedOption(OptionKey optionAndLanguage, object? value)
=> throw new NotSupportedException();
internal override IEnumerable<OptionKey> GetChangedOptions(OptionSet optionSet)
=> SpecializedCollections.EmptyEnumerable<OptionKey>();
}
#endif
}
}
......@@ -9,7 +9,7 @@
namespace Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions
{
public sealed class OptionsCollection : IEnumerable<KeyValuePair<OptionKey, object>>
public sealed class OptionsCollection : IReadOnlyCollection<KeyValuePair<OptionKey, object>>
{
private readonly Dictionary<OptionKey, object> _options = new Dictionary<OptionKey, object>();
private readonly string _languageName;
......@@ -19,6 +19,8 @@ public OptionsCollection(string languageName)
_languageName = languageName;
}
public int Count => _options.Count;
public void Add<T>(Option<T> option, T value)
=> _options.Add(new OptionKey(option), value);
......
......@@ -32,8 +32,18 @@ public Test()
solution = solution.WithProjectParseOptions(projectId, parseOptions.WithLanguageVersion(LanguageVersion));
#if !CODE_STYLE // TODO: Add support for Options based tests in CodeStyle layer
var (analyzerConfigSource, remainingOptions) = CodeFixVerifierHelper.ConvertOptionsToAnalyzerConfig(DefaultFileExt, Options);
if (analyzerConfigSource is object)
{
foreach (var id in solution.ProjectIds)
{
var documentId = DocumentId.CreateNewId(id, ".editorconfig");
solution = solution.AddAnalyzerConfigDocument(documentId, ".editorconfig", analyzerConfigSource, filePath: "/.editorconfig");
}
}
var options = solution.Options;
foreach (var (key, value) in Options)
foreach (var (key, value) in remainingOptions)
{
options = options.WithChangedOption(key, value);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册