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

Allow "WithConcurrentAnalysis" in more places #9550

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public void AsyncVoidMethod_MsTestV2_CSharp11(string testFwkVersion) =>
// The first version of the framework is not compatible with Net 7 so we need to test only v2 with C#11 features
.WithOptions(ParseOptionsHelper.FromCSharp11)
.AddReferences(NuGetMetadataReference.MSTestTestFramework(testFwkVersion))
.WithConcurrentAnalysis(false)
.Verify();

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class ClassAndMethodNameTest
public void ClassAndMethodName_CS() =>
builderCS.AddPaths("ClassAndMethodName.cs", "ClassAndMethodName.Partial.cs")
.AddReferences(MetadataReferenceFacade.NetStandard21)
.WithConcurrentAnalysis(false)
.WithOptions(ParseOptionsHelper.FromCSharp8)
.Verify();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public void CognitiveComplexity_CS_CSharp9() =>
public void CognitiveComplexity_CS_CSharp10() =>
builderCS.AddPaths("CognitiveComplexity.CSharp10.cs")
.WithOptions(ParseOptionsHelper.FromCSharp10)
.WithConcurrentAnalysis(false)
.Verify();

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ public void EmptyNamespace() =>
public void EmptyNamespace_CSharp10() =>
builder.AddPaths("EmptyNamespace.CSharp10.Empty.cs", "EmptyNamespace.CSharp10.NotEmpty.cs")
.WithOptions(ParseOptionsHelper.FromCSharp10)
.WithConcurrentAnalysis(false)
.Verify();

[TestMethod]
public void EmptyNamespace_CSharp10_CodeFix() =>
builder.AddPaths("EmptyNamespace.CSharp10.Empty.cs")
.WithCodeFix<EmptyNamespaceCodeFix>()
.WithOptions(ParseOptionsHelper.FromCSharp10)
.WithAutogenerateConcurrentFiles(false)
.WithCodeFixedPaths("EmptyNamespace.CSharp10.Fixed.cs")
.VerifyCodeFix();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public void ExtensionMethodShouldBeInSeparateNamespace_CSharp9() =>
public void ExtensionMethodShouldBeInSeparateNamespace_CSharp10() =>
builder
.AddPaths("ExtensionMethodShouldBeInSeparateNamespace.CSharp10.cs")
.WithConcurrentAnalysis(false)
.WithOptions(ParseOptionsHelper.FromCSharp10)
.Verify();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public void RequestsWithExcessiveLength_Csharp10() =>
public void RequestsWithExcessiveLength_Csharp11() =>
builderCS
.AddPaths(@"RequestsWithExcessiveLength.CSharp11.cs")
.WithConcurrentAnalysis(false)
.WithOptions(ParseOptionsHelper.FromCSharp11).Verify();

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public void StringLiteralShouldNotBeDuplicated_CSharp11() =>
public void StringLiteralShouldNotBeDuplicated_Attributes_CS() =>
new VerifierBuilder().AddAnalyzer(() => new CS.StringLiteralShouldNotBeDuplicated { Threshold = 2 })
.AddPaths("StringLiteralShouldNotBeDuplicated_Attributes.cs")
.WithConcurrentAnalysis(false)
.Verify();

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ private static VerifierBuilder CreateVerifier(Func<DiagnosticAnalyzer> createCon
.AddAnalyzer(createConfiguredAnalyzer)
.WithOnlyDiagnostics(onlyDiagnostics)
.AddReferences(MetadataReferenceFacade.SystemThreading)
.WithBasePath(@"SymbolicExecution\Roslyn")
.WithConcurrentAnalysis(false);
.WithBasePath(@"SymbolicExecution\Roslyn");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public void Verify_AutogenerateConcurrentFiles()
// Concurrent analysis by-default automatically generates concurrent files - File.Concurrent.cs
builder.Invoking(x => x.Verify()).Should().Throw<DiagnosticVerifierException>().WithMessage("""
There are differences for CSharp7 File.Concurrent.cs:
Line 1: Missing expected issue
Line 2: Missing expected issue

There are differences for CSharp7 File.cs:
Line 1: Missing expected issue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
using System.Text.RegularExpressions;
using Google.Protobuf;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;
using SonarAnalyzer.Rules;
using SonarAnalyzer.TestFramework.Build;

Expand Down Expand Up @@ -229,7 +231,7 @@ private string InsertConcurrentNamespace(string content)
{
return language.LanguageName switch
{
LanguageNames.CSharp => $"namespace AppendedNamespaceForConcurrencyTest {{ {content} {Environment.NewLine}}}", // Last line can be a comment
LanguageNames.CSharp => EncloseInNamespace(content),
LanguageNames.VisualBasic => content.Insert(ImportsIndexVB(), "Namespace AppendedNamespaceForConcurrencyTest : ") + Environment.NewLine + " : End Namespace",
_ => throw new UnexpectedLanguageException(language)
};
Expand All @@ -238,6 +240,37 @@ int ImportsIndexVB() =>
ImportsRegexVB.Match(content) is { Success: true } match ? match.Index + match.Length + 1 : 0;
}

private static string EncloseInNamespace(string content)
{
var tree = CSharpSyntaxTree.ParseText(content);
if (tree.TryGetRoot(out var root) && root is CompilationUnitSyntax { Members: { } members } compilationUnit)
{
if (members.OfType<FileScopedNamespaceDeclarationSyntax>().FirstOrDefault() is { } fileScoped)
{
root = root.ReplaceNode(fileScoped, fileScoped.WithName(SyntaxFactory.ParseName($"ConcurrencyTest.{fileScoped.Name}")));
}
else
{
var newNamespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(" AppendedNamespaceForConcurrencyTest"))
.WithMembers(compilationUnit.Members)
.WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken).WithLeadingTrivia(SyntaxFactory.Whitespace("\n")));
if (newNamespace.Members.Any() && newNamespace.Members[0] is NamespaceDeclarationSyntax)
{
// Move the leading trivia of the first member to newNamespace
newNamespace = newNamespace.WithLeadingTrivia(newNamespace.Members[0].GetLeadingTrivia());
newNamespace = newNamespace.WithMembers(newNamespace.Members.Replace(newNamespace.Members[0], newNamespace.Members[0].WithoutLeadingTrivia()));
}
root = compilationUnit.WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>(new[] { newNamespace }));
}

return root.ToFullString();
}
else
{
return $"namespace AppendedNamespaceForConcurrencyTest {{ {content} {Environment.NewLine}}}";
}
}

private string TestCaseDirectory() =>
Path.GetFullPath(builder.BasePath == null ? TestCases : Path.Combine(TestCases, builder.BasePath));

Expand Down