Skip to content

Commit

Permalink
Merge pull request #3580 from bjornhellander/feature/new-roslyn-test11
Browse files Browse the repository at this point in the history
Update Microsoft.CodeAnalysis.CSharp.Workspaces to version 4.4.0 for the c# 11 test project
  • Loading branch information
sharwell committed Feb 9, 2023
2 parents 2f6d02f + 349e8e3 commit 3c55643
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.2.0-4.final" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" PrivateAssets="all" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace StyleCop.Analyzers.Test.Verifiers
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -95,6 +96,10 @@ internal class CSharpTest : CSharpCodeFixTest<TAnalyzer, TCodeFix, XUnitVerifier
private const int DefaultTabSize = 4;
private const bool DefaultUseTabs = false;

private int indentationSize = DefaultIndentationSize;
private bool useTabs = DefaultUseTabs;
private int tabSize = DefaultTabSize;

static CSharpTest()
{
// If we have outdated defaults from the host unit test application targeting an older .NET Framework,
Expand Down Expand Up @@ -200,7 +205,24 @@ public CSharpTest(LanguageVersion? languageVersion)
/// <value>
/// The value of the <see cref="FormattingOptions.IndentationSize"/> to apply to the test workspace.
/// </value>
public int IndentationSize { get; set; } = DefaultIndentationSize;
public int IndentationSize
{
get
{
return this.indentationSize;
}

set
{
if (this.indentationSize == value)
{
return;
}

this.indentationSize = value;
this.UpdateGlobalAnalyzerConfig();
}
}

/// <summary>
/// Gets or sets a value indicating whether the <see cref="FormattingOptions.UseTabs"/> option is applied to the
Expand All @@ -209,15 +231,49 @@ public CSharpTest(LanguageVersion? languageVersion)
/// <value>
/// The value of the <see cref="FormattingOptions.UseTabs"/> to apply to the test workspace.
/// </value>
public bool UseTabs { get; set; } = DefaultUseTabs;
public bool UseTabs
{
get
{
return this.useTabs;
}

set
{
if (this.useTabs == value)
{
return;
}

this.useTabs = value;
this.UpdateGlobalAnalyzerConfig();
}
}

/// <summary>
/// Gets or sets the value of the <see cref="FormattingOptions.TabSize"/> to apply to the test workspace.
/// </summary>
/// <value>
/// The value of the <see cref="FormattingOptions.TabSize"/> to apply to the test workspace.
/// </value>
public int TabSize { get; set; } = DefaultTabSize;
public int TabSize
{
get
{
return this.tabSize;
}

set
{
if (this.tabSize == value)
{
return;
}

this.tabSize = value;
this.UpdateGlobalAnalyzerConfig();
}
}

/// <summary>
/// Gets or sets the content of the settings file to use.
Expand Down Expand Up @@ -276,16 +332,37 @@ protected override IEnumerable<CodeFixProvider> GetCodeFixProviders()
return new[] { codeFixProvider };
}

// TODO: Remove when c# 11 is a supported language version
private LanguageVersion? GetDefaultLanguageVersion()
private void UpdateGlobalAnalyzerConfig()
{
// Temporary fix since c# 11 is not yet the default language version
// in the c# 11 test project.
if (LightupHelpers.SupportsCSharp11)
if (!LightupHelpers.SupportsCSharp11)
{
return LanguageVersionEx.Preview;
// Options support workspace options in this version
// https://github.com/dotnet/roslyn/issues/66779
return;
}

if (this.TestState.AnalyzerConfigFiles.Count == 1
&& this.TestState.AnalyzerConfigFiles[0].filename == "/.globalconfig")
{
this.TestState.AnalyzerConfigFiles.RemoveAt(0);
}
else if (this.TestState.AnalyzerConfigFiles.Count > 1
|| (this.TestState.AnalyzerConfigFiles.Count > 0 && this.TestState.AnalyzerConfigFiles[0].filename != "/.globalconfig"))
{
throw new NotSupportedException("Additional configuration files are not currently supported by the test");
}

this.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $@"is_global = true
indent_size = {this.IndentationSize}
indent_style = {(this.UseTabs ? "tab" : "space")}
tab_width = {this.TabSize}
"));
}

// NOTE: If needed, this method can be temporarily updated to default to a preview version
private LanguageVersion? GetDefaultLanguageVersion()
{
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,9 @@ protected override ParseOptions CreateParseOptions()
return parseOptions;
}

// TODO: Remove when c# 11 is a supported language version
// NOTE: If needed, this method can be temporarily updated to default to a preview version
private LanguageVersion? GetDefaultLanguageVersion()
{
// Temporary fix since c# 11 is not yet the default language version
// in the c# 11 test project.
if (LightupHelpers.SupportsCSharp11)
{
return LanguageVersionEx.Preview;
}

return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal static class LanguageVersionEx
public const LanguageVersion CSharp8 = (LanguageVersion)800;
public const LanguageVersion CSharp9 = (LanguageVersion)900;
public const LanguageVersion CSharp10 = (LanguageVersion)1000;
public const LanguageVersion CSharp11 = (LanguageVersion)1100;
public const LanguageVersion LatestMajor = (LanguageVersion)int.MaxValue - 2;
public const LanguageVersion Preview = (LanguageVersion)int.MaxValue - 1;
public const LanguageVersion Latest = (LanguageVersion)int.MaxValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ private static readonly ConcurrentDictionary<Type, ConcurrentDictionary<Operatio
public static bool SupportsCSharp10 { get; }
= Enum.GetNames(typeof(LanguageVersion)).Contains(nameof(LanguageVersionEx.CSharp10));

// NOTE: Since c# is only in preview yet, we need to check something else than available language versions. Picked a new syntax kind temporarily.
// TODO: Update when c# 11 is available as a language version.
public static bool SupportsCSharp11 { get; }
= Enum.GetNames(typeof(SyntaxKind)).Contains(nameof(SyntaxKindEx.SlicePattern));
= Enum.GetNames(typeof(LanguageVersion)).Contains(nameof(LanguageVersionEx.CSharp11));

public static bool SupportsIOperation => SupportsCSharp73;

Expand Down

0 comments on commit 3c55643

Please sign in to comment.