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 AG0019 for unit test #171

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
39 changes: 37 additions & 2 deletions src/Agoda.Analyzers.Test/AgodaCustom/AG0019UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static void Main(string[] args)
}
}
";
await VerifyCodeFixAsync(code, result, allowNewCompilerDiagnostics:true);
await VerifyCodeFixAsync(code, result, allowNewCompilerDiagnostics: true, oldFileName: "Core.cs");
}
}
}
4 changes: 2 additions & 2 deletions src/Agoda.Analyzers.Test/Helpers/DiagnosticResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions src/Agoda.Analyzers.Test/Helpers/DiagnosticVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ 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)
{
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();
Expand Down
4 changes: 2 additions & 2 deletions src/Agoda.Analyzers.Vsix/Agoda.Analyzers.Vsix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Agoda.Analyzers.Vsix</RootNamespace>
<AssemblyName>Agoda.Analyzers.Vsix</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<GeneratePkgDefFile>false</GeneratePkgDefFile>
<IncludeAssemblyInVSIXContainer>false</IncludeAssemblyInVSIXContainer>
<IncludeDebugSymbolsInVSIXContainer>false</IncludeDebugSymbolsInVSIXContainer>
Expand Down Expand Up @@ -67,7 +67,7 @@
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\Agoda.Analyzers\Agoda.Analyzers.csproj">
<AdditionalProperties>TargetFramework=net462</AdditionalProperties>
<AdditionalProperties>TargetFramework=net462</AdditionalProperties>
<Project>{4F934D25-9BFF-4153-8965-F12F52BA41DF}</Project>
<Name>Agoda.Analyzers</Name>
<IncludeOutputGroupsInVSIX>BuiltProjectOutputGroup%3bBuiltProjectOutputGroupDependencies%3bGetCopyToOutputDirectoryItems%3bSatelliteDllsProjectOutputGroup%3b</IncludeOutputGroupsInVSIX>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
using System.Linq;

namespace Agoda.Analyzers.AgodaCustom
{
Expand Down Expand Up @@ -34,14 +35,17 @@ public AG0019PreventUseOfInternalsVisibleToAttribute()

public override ImmutableArray<DiagnosticDescriptor> 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"))
nattaaek marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}
foreach (var attribute in attributeNode.Attributes)
{
if (attribute.Name is IdentifierNameSyntax name && name.Identifier.Text == "InternalsVisibleTo")
Expand Down