From 35530a1fa5971557ab9c41f2a44659e59f8682cc Mon Sep 17 00:00:00 2001 From: Nattaaek Wattanuyan <26399807+nattaaek@users.noreply.github.com> Date: Wed, 1 Feb 2023 22:42:33 +0700 Subject: [PATCH 1/7] allow AG0019 for unit test --- ...019PreventUseOfInternalsVisibleToAttributeFixProvider.cs | 6 ++++++ src/Agoda.Analyzers.Vsix/Agoda.Analyzers.Vsix.csproj | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Agoda.Analyzers.CodeFixes/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttributeFixProvider.cs b/src/Agoda.Analyzers.CodeFixes/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttributeFixProvider.cs index fb3da47..8947b00 100644 --- a/src/Agoda.Analyzers.CodeFixes/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttributeFixProvider.cs +++ b/src/Agoda.Analyzers.CodeFixes/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttributeFixProvider.cs @@ -35,6 +35,12 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { + var projectName = document.Project.Name; + if (projectName.Contains("test")) + { + return document; + } + var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var token = root.FindToken(diagnostic.Location.SourceSpan.Start); diff --git a/src/Agoda.Analyzers.Vsix/Agoda.Analyzers.Vsix.csproj b/src/Agoda.Analyzers.Vsix/Agoda.Analyzers.Vsix.csproj index 72baaa3..368cf1a 100644 --- a/src/Agoda.Analyzers.Vsix/Agoda.Analyzers.Vsix.csproj +++ b/src/Agoda.Analyzers.Vsix/Agoda.Analyzers.Vsix.csproj @@ -16,7 +16,7 @@ Properties Agoda.Analyzers.Vsix Agoda.Analyzers.Vsix - v4.6.2 + v4.8 false false false @@ -67,7 +67,7 @@ False - TargetFramework=net462 + TargetFramework=net462 {4F934D25-9BFF-4153-8965-F12F52BA41DF} Agoda.Analyzers BuiltProjectOutputGroup%3bBuiltProjectOutputGroupDependencies%3bGetCopyToOutputDirectoryItems%3bSatelliteDllsProjectOutputGroup%3b From 659b90a2062205a392e9f82331bb6b33fd2dc47f Mon Sep 17 00:00:00 2001 From: nwattanuyan Date: Fri, 3 Feb 2023 10:53:10 +0700 Subject: [PATCH 2/7] adding unit test --- ...fInternalsVisibleToAttributeFixProvider.cs | 6 --- .../AgodaCustom/AG0019UnitTests.cs | 39 ++++++++++++++++++- .../AgodaCustom/AG0019FixProviderUnitTests.cs | 2 +- .../Helpers/DiagnosticResult.cs | 4 +- .../Helpers/DiagnosticVerifier.cs | 12 +++--- ...PreventUseOfInternalsVisibleToAttribute.cs | 12 ++++-- 6 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/Agoda.Analyzers.CodeFixes/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttributeFixProvider.cs b/src/Agoda.Analyzers.CodeFixes/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttributeFixProvider.cs index 8947b00..fb3da47 100644 --- a/src/Agoda.Analyzers.CodeFixes/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttributeFixProvider.cs +++ b/src/Agoda.Analyzers.CodeFixes/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttributeFixProvider.cs @@ -35,12 +35,6 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { - var projectName = document.Project.Name; - if (projectName.Contains("test")) - { - return document; - } - var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var token = root.FindToken(diagnostic.Location.SourceSpan.Start); diff --git a/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs b/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs index 467e338..5a36517 100644 --- a/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs +++ b/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs @@ -41,15 +41,50 @@ static void Main(string[] args) } "; + var expected = new DiagnosticLocation[] + { + }; + await VerifyDiagnosticsAsync(code, expected); + } + + [Test] + public async Task AG0019_RemoveInternalsVisibleToAttributeShouldNotReport() + { + var code = @" + using System; + using System.Diagnostics; + using System.Reflection; + using System.Runtime.CompilerServices; + + [assembly: AssemblyTitle(""MyApplication"")] + [assembly: InternalsVisibleTo(""Agoda.Website.Core"")] + [assembly: AssemblyDescription(""Description""), InternalsVisibleTo(""Agoda.Website.Core"")] + [assembly: InternalsVisibleTo(""Agoda.Website.Core""), AssemblyDefaultAlias(""alias"")] + [assembly: AssemblyCopyright(""CopyRight""), InternalsVisibleTo(""Agoda.Website.Core""), InternalsVisibleTo(""Agoda.Website.Core""), AssemblyFileVersion(""0.0.0.0"")] + + namespace RoslynTest + { + [Serializable] + public class Program + { + [Conditional(""DEBUG""), Conditional(""Core1"")] + static void Main(string[] args) + { + Console.WriteLine(""Hello World!""); + } + } + } + "; + var expected = new[] { new DiagnosticLocation(8, 28), new DiagnosticLocation(9, 64), new DiagnosticLocation(10, 28), new DiagnosticLocation(11, 60), - new DiagnosticLocation(11, 115) + new DiagnosticLocation(11, 102) }; - await VerifyDiagnosticsAsync(code, expected); + await VerifyDiagnosticsAsync(code, expected, "Core1.cs"); } } } diff --git a/src/Agoda.Analyzers.Test/CodeFixes/AgodaCustom/AG0019FixProviderUnitTests.cs b/src/Agoda.Analyzers.Test/CodeFixes/AgodaCustom/AG0019FixProviderUnitTests.cs index b84c431..de6df30 100644 --- a/src/Agoda.Analyzers.Test/CodeFixes/AgodaCustom/AG0019FixProviderUnitTests.cs +++ b/src/Agoda.Analyzers.Test/CodeFixes/AgodaCustom/AG0019FixProviderUnitTests.cs @@ -74,7 +74,7 @@ static void Main(string[] args) } } "; - await VerifyCodeFixAsync(code, result, allowNewCompilerDiagnostics:true); + await VerifyCodeFixAsync(code, result, allowNewCompilerDiagnostics: true, oldFileName: "Core.cs"); } } } diff --git a/src/Agoda.Analyzers.Test/Helpers/DiagnosticResult.cs b/src/Agoda.Analyzers.Test/Helpers/DiagnosticResult.cs index b8f4f35..894d15b 100644 --- a/src/Agoda.Analyzers.Test/Helpers/DiagnosticResult.cs +++ b/src/Agoda.Analyzers.Test/Helpers/DiagnosticResult.cs @@ -85,9 +85,9 @@ public DiagnosticResult WithMessageFormat(LocalizableString messageFormat) return result; } - public DiagnosticResult WithLocation(int line, int column) + public DiagnosticResult WithLocation(int line, int column, string path = null) { - return WithLocation(DefaultPath, line, column); + return WithLocation(path ?? DefaultPath, line, column); } public DiagnosticResult WithLocation(string path, int line, int column) diff --git a/src/Agoda.Analyzers.Test/Helpers/DiagnosticVerifier.cs b/src/Agoda.Analyzers.Test/Helpers/DiagnosticVerifier.cs index 2465292..5a4b968 100644 --- a/src/Agoda.Analyzers.Test/Helpers/DiagnosticVerifier.cs +++ b/src/Agoda.Analyzers.Test/Helpers/DiagnosticVerifier.cs @@ -90,9 +90,9 @@ protected async Task VerifyDiagnosticsAsync(string code, DiagnosticLocation expe await VerifyDiagnosticsAsync(new CodeDescriptor(code), new [] { expectedLocations}); } - protected async Task VerifyDiagnosticsAsync(string code, DiagnosticLocation[] expectedLocations) + protected async Task VerifyDiagnosticsAsync(string code, DiagnosticLocation[] expectedLocations, string filename = null) { - await VerifyDiagnosticsAsync(new CodeDescriptor(code), expectedLocations); + await VerifyDiagnosticsAsync(new CodeDescriptor(code), expectedLocations, filename); } protected async Task VerifyDiagnosticsAsync(CodeDescriptor descriptor, DiagnosticLocation expectedLocations) @@ -100,12 +100,12 @@ protected async Task VerifyDiagnosticsAsync(CodeDescriptor descriptor, Diagnosti await VerifyDiagnosticsAsync(descriptor, new [] { expectedLocations}); } - protected async Task VerifyDiagnosticsAsync(CodeDescriptor descriptor, DiagnosticLocation[] expectedLocations) + protected async Task VerifyDiagnosticsAsync(CodeDescriptor descriptor, DiagnosticLocation[] expectedLocations, string filename = null) { var baseResult = CSharpDiagnostic(DiagnosticId); - var expected = expectedLocations.Select(l => baseResult.WithLocation(l.Line, l.Col)).ToArray(); - - var doc = CreateProject(new[] {descriptor.Code}) + var expected = expectedLocations.Select(l => baseResult.WithLocation(l.Line, l.Col, filename)).ToArray(); + + var doc = CreateProject(new[] { descriptor.Code }, filenames: new[] { filename }) .AddMetadataReferences(descriptor.References.Select(assembly => MetadataReference.CreateFromFile(assembly.Location))) .Documents .First(); diff --git a/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs b/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs index 0ae8e91..78b882a 100644 --- a/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs +++ b/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs @@ -4,6 +4,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using System.Collections.Immutable; +using System.Linq; namespace Agoda.Analyzers.AgodaCustom { @@ -34,14 +35,17 @@ public AG0019PreventUseOfInternalsVisibleToAttribute() public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(_diagnosticDescriptor); - public override void Initialize(AnalysisContext context) => context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.AttributeList); + public override void Initialize(AnalysisContext context) => + context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.AttributeList); private void AnalyzeNode(SyntaxNodeAnalysisContext context) { var attributeNode = (AttributeListSyntax)context.Node; - - if (attributeNode.Target?.Identifier.Text != "assembly") return; - + var targetSyntaxTree = context.Compilation?.SyntaxTrees.FirstOrDefault(tree => tree == attributeNode.SyntaxTree); + if (attributeNode.Target?.Identifier.Text != "assembly" || targetSyntaxTree == null || targetSyntaxTree.FilePath.ToLower().Contains("test")) + { + return; + } foreach (var attribute in attributeNode.Attributes) { if (attribute.Name is IdentifierNameSyntax name && name.Identifier.Text == "InternalsVisibleTo") From d8d0d77e62b0f2a66ca1d465ac96ef3e40365f2d Mon Sep 17 00:00:00 2001 From: nwattanuyan Date: Fri, 3 Feb 2023 11:10:14 +0700 Subject: [PATCH 3/7] Update AG0019PreventUseOfInternalsVisibleToAttribute.cs --- .../AG0019PreventUseOfInternalsVisibleToAttribute.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs b/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs index 78b882a..d89fa6a 100644 --- a/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs +++ b/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs @@ -42,7 +42,7 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context) { var attributeNode = (AttributeListSyntax)context.Node; var targetSyntaxTree = context.Compilation?.SyntaxTrees.FirstOrDefault(tree => tree == attributeNode.SyntaxTree); - if (attributeNode.Target?.Identifier.Text != "assembly" || targetSyntaxTree == null || targetSyntaxTree.FilePath.ToLower().Contains("test")) + if (attributeNode.Target?.Identifier.Text != "assembly" || targetSyntaxTree == null || IsTestSourceFile(targetSyntaxTree.FilePath)) { return; } @@ -54,5 +54,11 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context) } } } + + private bool IsTestSourceFile(string aFilePath) + { + return aFilePath.ToLower().EndsWith("test") || aFilePath.ToLower().EndsWith("tests"); + + } } } From 18f834ac225f79bd579699da7ec45517c2d775a0 Mon Sep 17 00:00:00 2001 From: nwattanuyan Date: Fri, 3 Feb 2023 11:10:48 +0700 Subject: [PATCH 4/7] Update AG0019UnitTests.cs --- src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs b/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs index 5a36517..7e3f146 100644 --- a/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs +++ b/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs @@ -44,7 +44,7 @@ static void Main(string[] args) var expected = new DiagnosticLocation[] { }; - await VerifyDiagnosticsAsync(code, expected); + await VerifyDiagnosticsAsync(code, expected, "Core1Test.cs"); } [Test] From 16b8fb2418014e63b06ee5d99776ce97d28f2cb1 Mon Sep 17 00:00:00 2001 From: nwattanuyan Date: Fri, 3 Feb 2023 11:43:14 +0700 Subject: [PATCH 5/7] Update AG0019PreventUseOfInternalsVisibleToAttribute.cs --- .../AG0019PreventUseOfInternalsVisibleToAttribute.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs b/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs index d89fa6a..715462a 100644 --- a/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs +++ b/src/Agoda.Analyzers/AgodaCustom/AG0019PreventUseOfInternalsVisibleToAttribute.cs @@ -57,7 +57,7 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context) private bool IsTestSourceFile(string aFilePath) { - return aFilePath.ToLower().EndsWith("test") || aFilePath.ToLower().EndsWith("tests"); + return aFilePath.ToLower().EndsWith("test.cs") || aFilePath.ToLower().EndsWith("tests.cs"); } } From 8964257faed6b4ee6e3192875b30e042840f93bd Mon Sep 17 00:00:00 2001 From: Nattaaek Wattanuyan <26399807+nattaaek@users.noreply.github.com> Date: Sun, 26 Feb 2023 17:14:28 +0700 Subject: [PATCH 6/7] Update AG0019UnitTests.cs --- src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs b/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs index 7e3f146..09d1808 100644 --- a/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs +++ b/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs @@ -57,9 +57,9 @@ public async Task AG0019_RemoveInternalsVisibleToAttributeShouldNotReport() using System.Runtime.CompilerServices; [assembly: AssemblyTitle(""MyApplication"")] - [assembly: InternalsVisibleTo(""Agoda.Website.Core"")] + [assembly: InternalsVisibleTo(""Agoda.Website.UnitTestFramework"")] [assembly: AssemblyDescription(""Description""), InternalsVisibleTo(""Agoda.Website.Core"")] - [assembly: InternalsVisibleTo(""Agoda.Website.Core""), AssemblyDefaultAlias(""alias"")] + [assembly: InternalsVisibleTo(""Agoda.Website.UnitTestFramework""), AssemblyDefaultAlias(""alias"")] [assembly: AssemblyCopyright(""CopyRight""), InternalsVisibleTo(""Agoda.Website.Core""), InternalsVisibleTo(""Agoda.Website.Core""), AssemblyFileVersion(""0.0.0.0"")] namespace RoslynTest From 5fc20dfcc19bfd07f6da5e8432b5ec283db5a4b7 Mon Sep 17 00:00:00 2001 From: Nattaaek Wattanuyan <26399807+nattaaek@users.noreply.github.com> Date: Sun, 26 Feb 2023 17:15:09 +0700 Subject: [PATCH 7/7] Update AG0019UnitTests.cs --- src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs b/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs index 09d1808..4c95d93 100644 --- a/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs +++ b/src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs @@ -58,9 +58,9 @@ public async Task AG0019_RemoveInternalsVisibleToAttributeShouldNotReport() [assembly: AssemblyTitle(""MyApplication"")] [assembly: InternalsVisibleTo(""Agoda.Website.UnitTestFramework"")] - [assembly: AssemblyDescription(""Description""), InternalsVisibleTo(""Agoda.Website.Core"")] + [assembly: AssemblyDescription(""Description""), InternalsVisibleTo(""Agoda.Website.UnitTestFramework"")] [assembly: InternalsVisibleTo(""Agoda.Website.UnitTestFramework""), AssemblyDefaultAlias(""alias"")] - [assembly: AssemblyCopyright(""CopyRight""), InternalsVisibleTo(""Agoda.Website.Core""), InternalsVisibleTo(""Agoda.Website.Core""), AssemblyFileVersion(""0.0.0.0"")] + [assembly: AssemblyCopyright(""CopyRight""), InternalsVisibleTo(""Agoda.Website.UnitTestFramework""), InternalsVisibleTo(""Agoda.Website.Core""), AssemblyFileVersion(""0.0.0.0"")] namespace RoslynTest {