5
5
6
6
namespace StyleCop . Analyzers . Test . Verifiers
7
7
{
8
+ using System ;
8
9
using System . Collections . Generic ;
9
10
using System . IO ;
10
11
using System . Linq ;
@@ -95,6 +96,10 @@ internal class CSharpTest : CSharpCodeFixTest<TAnalyzer, TCodeFix, XUnitVerifier
95
96
private const int DefaultTabSize = 4 ;
96
97
private const bool DefaultUseTabs = false ;
97
98
99
+ private int indentationSize = DefaultIndentationSize ;
100
+ private bool useTabs = DefaultUseTabs ;
101
+ private int tabSize = DefaultTabSize ;
102
+
98
103
static CSharpTest ( )
99
104
{
100
105
// If we have outdated defaults from the host unit test application targeting an older .NET Framework,
@@ -200,7 +205,24 @@ public CSharpTest(LanguageVersion? languageVersion)
200
205
/// <value>
201
206
/// The value of the <see cref="FormattingOptions.IndentationSize"/> to apply to the test workspace.
202
207
/// </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
+ }
204
226
205
227
/// <summary>
206
228
/// 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)
209
231
/// <value>
210
232
/// The value of the <see cref="FormattingOptions.UseTabs"/> to apply to the test workspace.
211
233
/// </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
+ }
213
252
214
253
/// <summary>
215
254
/// Gets or sets the value of the <see cref="FormattingOptions.TabSize"/> to apply to the test workspace.
216
255
/// </summary>
217
256
/// <value>
218
257
/// The value of the <see cref="FormattingOptions.TabSize"/> to apply to the test workspace.
219
258
/// </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
+ }
221
277
222
278
/// <summary>
223
279
/// Gets or sets the content of the settings file to use.
@@ -276,6 +332,34 @@ protected override IEnumerable<CodeFixProvider> GetCodeFixProviders()
276
332
return new [ ] { codeFixProvider } ;
277
333
}
278
334
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
+
279
363
// NOTE: If needed, this method can be temporarily updated to default to a preview version
280
364
private LanguageVersion ? GetDefaultLanguageVersion ( )
281
365
{
0 commit comments