Skip to content

Commit 349e8e3

Browse files
committed
Set formatting options in analyzer config for testing
Workaround for dotnet/roslyn#66779
1 parent 8e7f30d commit 349e8e3

File tree

1 file changed

+87
-3
lines changed

1 file changed

+87
-3
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/StyleCopCodeFixVerifier`2.cs

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace StyleCop.Analyzers.Test.Verifiers
77
{
8+
using System;
89
using System.Collections.Generic;
910
using System.IO;
1011
using System.Linq;
@@ -95,6 +96,10 @@ internal class CSharpTest : CSharpCodeFixTest<TAnalyzer, TCodeFix, XUnitVerifier
9596
private const int DefaultTabSize = 4;
9697
private const bool DefaultUseTabs = false;
9798

99+
private int indentationSize = DefaultIndentationSize;
100+
private bool useTabs = DefaultUseTabs;
101+
private int tabSize = DefaultTabSize;
102+
98103
static CSharpTest()
99104
{
100105
// If we have outdated defaults from the host unit test application targeting an older .NET Framework,
@@ -200,7 +205,24 @@ public CSharpTest(LanguageVersion? languageVersion)
200205
/// <value>
201206
/// The value of the <see cref="FormattingOptions.IndentationSize"/> to apply to the test workspace.
202207
/// </value>
203-
public int IndentationSize { get; set; } = DefaultIndentationSize;
208+
public int IndentationSize
209+
{
210+
get
211+
{
212+
return this.indentationSize;
213+
}
214+
215+
set
216+
{
217+
if (this.indentationSize == value)
218+
{
219+
return;
220+
}
221+
222+
this.indentationSize = value;
223+
this.UpdateGlobalAnalyzerConfig();
224+
}
225+
}
204226

205227
/// <summary>
206228
/// Gets or sets a value indicating whether the <see cref="FormattingOptions.UseTabs"/> option is applied to the
@@ -209,15 +231,49 @@ public CSharpTest(LanguageVersion? languageVersion)
209231
/// <value>
210232
/// The value of the <see cref="FormattingOptions.UseTabs"/> to apply to the test workspace.
211233
/// </value>
212-
public bool UseTabs { get; set; } = DefaultUseTabs;
234+
public bool UseTabs
235+
{
236+
get
237+
{
238+
return this.useTabs;
239+
}
240+
241+
set
242+
{
243+
if (this.useTabs == value)
244+
{
245+
return;
246+
}
247+
248+
this.useTabs = value;
249+
this.UpdateGlobalAnalyzerConfig();
250+
}
251+
}
213252

214253
/// <summary>
215254
/// Gets or sets the value of the <see cref="FormattingOptions.TabSize"/> to apply to the test workspace.
216255
/// </summary>
217256
/// <value>
218257
/// The value of the <see cref="FormattingOptions.TabSize"/> to apply to the test workspace.
219258
/// </value>
220-
public int TabSize { get; set; } = DefaultTabSize;
259+
public int TabSize
260+
{
261+
get
262+
{
263+
return this.tabSize;
264+
}
265+
266+
set
267+
{
268+
if (this.tabSize == value)
269+
{
270+
return;
271+
}
272+
273+
this.tabSize = value;
274+
this.UpdateGlobalAnalyzerConfig();
275+
}
276+
}
221277

222278
/// <summary>
223279
/// Gets or sets the content of the settings file to use.
@@ -276,6 +332,34 @@ protected override IEnumerable<CodeFixProvider> GetCodeFixProviders()
276332
return new[] { codeFixProvider };
277333
}
278334

335+
private void UpdateGlobalAnalyzerConfig()
336+
{
337+
if (!LightupHelpers.SupportsCSharp11)
338+
{
339+
// Options support workspace options in this version
340+
// https://github.com/dotnet/roslyn/issues/66779
341+
return;
342+
}
343+
344+
if (this.TestState.AnalyzerConfigFiles.Count == 1
345+
&& this.TestState.AnalyzerConfigFiles[0].filename == "/.globalconfig")
346+
{
347+
this.TestState.AnalyzerConfigFiles.RemoveAt(0);
348+
}
349+
else if (this.TestState.AnalyzerConfigFiles.Count > 1
350+
|| (this.TestState.AnalyzerConfigFiles.Count > 0 && this.TestState.AnalyzerConfigFiles[0].filename != "/.globalconfig"))
351+
{
352+
throw new NotSupportedException("Additional configuration files are not currently supported by the test");
353+
}
354+
355+
this.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $@"is_global = true
356+
357+
indent_size = {this.IndentationSize}
358+
indent_style = {(this.UseTabs ? "tab" : "space")}
359+
tab_width = {this.TabSize}
360+
"));
361+
}
362+
279363
// NOTE: If needed, this method can be temporarily updated to default to a preview version
280364
private LanguageVersion? GetDefaultLanguageVersion()
281365
{

0 commit comments

Comments
 (0)