Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Do not merge] Support compilation preprocessor symbols for language and runtime in UT scenarios #8873

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions analyzers/tests/SonarAnalyzer.Test/Rules/CommentedOutCodeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,38 @@ public class CommentedOutCodeTest
{
private readonly VerifierBuilder builder = new VerifierBuilder<CommentedOutCode>();

[TestMethod]
public void CommentedOutCode_Nonconcurrent() =>
builder.AddPaths("CommentedOutCode_Nonconcurrent.cs").WithConcurrentAnalysis(false).Verify();

[TestMethod]
public void CommentedOutCode() =>
builder.AddPaths("CommentedOutCode.cs").Verify();

[TestMethod]
public void CommentedOutCode_NoDocumentation() =>
builder.AddPaths("CommentedOutCode.cs")
.WithConcurrentAnalysis(false)
.WithOptions(ImmutableArray.Create<ParseOptions>(new CSharpParseOptions(documentationMode: DocumentationMode.None)))
.Verify();
public void CommentedOutCode_CSharp7() =>
builder.AddPaths("CommentedOutCode.cs").WithOptions(ParseOptionsHelper.FromCSharp7).Verify();

[TestMethod]
public void CommentedOutCode_CSharp8() =>
builder.AddPaths("CommentedOutCode.cs").WithOptions(ParseOptionsHelper.FromCSharp8).Verify();

[TestMethod]
public void CommentedOutCode_CSharp9() =>
builder.AddPaths("CommentedOutCode.cs").WithOptions(ParseOptionsHelper.FromCSharp9).Verify();

[TestMethod]
public void CommentedOutCode_CSharp10() =>
builder.AddPaths("CommentedOutCode.cs").WithOptions(ParseOptionsHelper.FromCSharp10).Verify();

[TestMethod]
public void CommentedOutCode_CodeFix_SingleLine() =>
builder.AddPaths("CommentedOutCode.SingleLine.ToFix.cs")
.WithCodeFix<CommentedOutCodeCodeFix>()
.WithCodeFixedPaths("CommentedOutCode.SingleLine.Fixed.cs")
.VerifyCodeFix();
public void CommentedOutCode_CSharp11() =>
builder.AddPaths("CommentedOutCode.cs").WithOptions(ParseOptionsHelper.FromCSharp11).Verify();

[TestMethod]
public void CommentedOutCode_CodeFix_MultiLine() =>
builder.AddPaths("CommentedOutCode.MultiLine.ToFix.cs")
.WithCodeFix<CommentedOutCodeCodeFix>()
.WithCodeFixedPaths("CommentedOutCode.MultiLine.Fixed.cs")
.VerifyCodeFix();
public void CommentedOutCode_CSharp12() =>
builder.AddPaths("CommentedOutCode.cs").WithOptions(ParseOptionsHelper.FromCSharp12).Verify();

[TestMethod]
public void CommentedOutCode_Latest() =>
builder
.AddPaths("CommentedOutCode.cs")
.WithOptions(ParseOptionsHelper.CSharpLatest)
.Verify();
}
50 changes: 50 additions & 0 deletions analyzers/tests/SonarAnalyzer.Test/TestCases/CommentedOutCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,53 @@ void CommentedOutCommentsSeparatedByBlock()
// Noncompliant: var y = 42;
}
}

#if NET
/* var x = "x"; */
#endif
#if NETFRAMEWORK
/* var x = "x"; */
#endif

#if CSHARP7_OR_GREATER
/* var x = "x"; */
#endif
#if CSHARP8_OR_GREATER
/* var x = "x"; */
#endif
#if CSHARP9_OR_GREATER
/* var x = "x"; */
#endif
#if CSHARP10_OR_GREATER
/* var x = "x"; */
#endif
#if CSHARP11_OR_GREATER
/* var x = "x"; */
#endif
#if CSHARP12_OR_GREATER
/* var x = "x"; */
#endif

#if CSHARP7
/* var x = "x"; */
#endif
#if CSHARP8
/* var x = "x"; */
#endif
#if CSHARP9
/* var x = "x"; */
#endif
#if CSHARP10
/* var x = "x"; */
#endif
#if CSHARP11
/* var x = "x"; */
#endif
#if CSHARP12
/* var x = "x"; */
#endif

#if NET && CSHARP11
/* var x = "x"; */
#endif

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Linq;
using System.Numerics;
using Microsoft.CodeAnalysis.CSharp;
using static Microsoft.CodeAnalysis.CSharp.LanguageVersion;
using static Microsoft.CodeAnalysis.VisualBasic.LanguageVersion;
using CS = Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -113,13 +116,49 @@ public static ImmutableArray<ParseOptions> Latest(AnalyzerLanguage language) =>
};

private static ImmutableArray<ParseOptions> FilterByEnvironment(this IEnumerable<ParseOptions> options) =>
TestContextHelper.IsAzureDevOpsContext && !TestContextHelper.IsPullRequestBuild
true
? options.ToImmutableArray()
: ImmutableArray.Create(options.First()); // Use only the oldest version for local test run and debug

private static IEnumerable<ParseOptions> CreateOptions(params CS.LanguageVersion[] options) =>
options.Select(x => new CS.CSharpParseOptions(x));
options.Select(x => new CS.CSharpParseOptions(x, preprocessorSymbols: BuildCSharpPreprocessorSymbols(x)));

private static IEnumerable<ParseOptions> CreateOptions(params VB.LanguageVersion[] options) =>
options.Select(x => new VB.VisualBasicParseOptions(x));
options.Select(x => new VB.VisualBasicParseOptions(x, preprocessorSymbols: BuildVisualBasicPreprocessorSymbols(x)));

private static string[] BuildCSharpPreprocessorSymbols(CS.LanguageVersion version) =>
[
.. BuildNetRuntimePreprocessorSymbols(),
.. BuildLangVersionOrLaterPreprocessorSymbols(version),
version.ToString().ToUpperInvariant(),
];

private static IEnumerable<KeyValuePair<string, object>> BuildVisualBasicPreprocessorSymbols(VB.LanguageVersion version)
{
return [
.. BuildNetRuntimePreprocessorSymbols().Select(ToValuedSymbol),
.. BuildLangVersionOrLaterPreprocessorSymbols(version).Select(ToValuedSymbol),
ToValuedSymbol(version.ToString().ToUpperInvariant()),
];

KeyValuePair<string, object> ToValuedSymbol(string name) => new KeyValuePair<string, object>(name, 1);
}

private static IEnumerable<string> BuildLangVersionOrLaterPreprocessorSymbols<T>(T version) where T : struct, Enum =>
Enum.GetValues(typeof(T))
.Cast<T>()
.Where(x => ((IComparable)x).CompareTo(version) <= 0) // Includes Default
.Select(x => $"{x.ToString().ToUpperInvariant()}_OR_GREATER");

private static List<string> BuildNetRuntimePreprocessorSymbols()
{
var result = new List<string>();
#if NET
result.Add("NET");
#endif
#if NETFRAMEWORK
result.Add("NETFRAMEWORK");
#endif
return result;
}
}
Loading