Skip to content

Commit

Permalink
Allow multiple input files for SymbolIsBannedAnalyzer
Browse files Browse the repository at this point in the history
Accept BannedSymbols.txt and BannedSymbols.*.txt
  • Loading branch information
ltrzesniewski committed Jul 13, 2021
1 parent f64768f commit bbce803
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*.user
.vs/
.vscode/
.idea/

# Build results
artifacts/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ namespace Microsoft.CodeAnalysis.BannedApiAnalyzers
{
internal static class SymbolIsBannedAnalyzer
{
public const string BannedSymbolsFileName = "BannedSymbols.txt";

public static readonly DiagnosticDescriptor SymbolIsBannedRule = new(
id: DiagnosticIds.SymbolIsBannedRuleId,
title: new LocalizableResourceString(nameof(BannedApiAnalyzerResources.SymbolIsBannedTitle), BannedApiAnalyzerResources.ResourceManager, typeof(BannedApiAnalyzerResources)),
Expand Down Expand Up @@ -181,7 +179,8 @@ private void OnCompilationStart(CompilationStartAnalysisContext compilationConte
{
var query =
from additionalFile in compilationContext.Options.AdditionalFiles
where StringComparer.Ordinal.Equals(Path.GetFileName(additionalFile.Path), SymbolIsBannedAnalyzer.BannedSymbolsFileName)
let fileName = Path.GetFileName(additionalFile.Path)
where fileName != null && fileName.StartsWith("BannedSymbols.", StringComparison.Ordinal) && fileName.EndsWith(".txt", StringComparison.Ordinal)
let sourceText = additionalFile.GetText(compilationContext.CancellationToken)
where sourceText != null
from line in sourceText.Lines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Microsoft.CodeAnalysis.BannedApiAnalyzers.UnitTests

public class SymbolIsBannedAnalyzerTests
{
private const string BannedSymbolsFileName = "BannedSymbols.txt";

private static DiagnosticResult GetCSharpResultAt(int markupKey, DiagnosticDescriptor descriptor, string bannedMemberName, string message)
=> VerifyCS.Diagnostic(descriptor)
.WithLocation(markupKey)
Expand Down Expand Up @@ -47,7 +49,7 @@ private static async Task VerifyBasicAnalyzerAsync(string source, string bannedA
TestState =
{
Sources = { source },
AdditionalFiles = { (SymbolIsBannedAnalyzer.BannedSymbolsFileName, bannedApiText) },
AdditionalFiles = { (BannedSymbolsFileName, bannedApiText) },
},
};

Expand All @@ -62,7 +64,7 @@ private static async Task VerifyCSharpAnalyzerAsync(string source, string banned
TestState =
{
Sources = { source },
AdditionalFiles = { (SymbolIsBannedAnalyzer.BannedSymbolsFileName, bannedApiText) },
AdditionalFiles = { (BannedSymbolsFileName, bannedApiText) },
},
};

Expand Down Expand Up @@ -208,6 +210,46 @@ void M()
await VerifyCSharpAnalyzerAsync(source, bannedText, GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "Banned", ""));
}

[Fact]
public async Task CSharp_BannedApi_MultipleFiles()
{
var source = @"
namespace N
{
class BannedA { }
class BannedB { }
class NotBanned { }
class C
{
void M()
{
var a = {|#0:new BannedA()|};
var b = {|#1:new BannedB()|};
var c = new NotBanned();
}
}
}";

var test = new VerifyCS.Test
{
TestState =
{
Sources = { source },
AdditionalFiles =
{
("BannedSymbols.txt", @"T:N.BannedA") ,
("BannedSymbols.Other.txt", @"T:N.BannedB"),
("OtherFile.txt", @"T:N.NotBanned")
},
},
};

test.ExpectedDiagnostics.Add(GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedA", ""));
test.ExpectedDiagnostics.Add(GetCSharpResultAt(1, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedB", ""));

await test.RunAsync();
}

[Fact]
public async Task CSharp_BannedType_Constructor()
{
Expand Down Expand Up @@ -1046,6 +1088,43 @@ void M1()
await VerifyCSharpAnalyzerAsync(source, bannedText);
}

[Fact]
public async Task VisualBasic_BannedApi_MultipleFiles()
{
var source = @"
Namespace N
Class BannedA : End Class
Class BannedB : End Class
Class NotBanned : End Class
Class C
Sub M()
Dim a As {|#0:New BannedA()|}
Dim b As {|#1:New BannedB()|}
Dim c As New NotBanned()
End Sub
End Class
End Namespace";

var test = new VerifyVB.Test
{
TestState =
{
Sources = { source },
AdditionalFiles =
{
("BannedSymbols.txt", @"T:N.BannedA") ,
("BannedSymbols.Other.txt", @"T:N.BannedB"),
("OtherFile.txt", @"T:N.NotBanned")
},
},
};

test.ExpectedDiagnostics.Add(GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedA", ""));
test.ExpectedDiagnostics.Add(GetBasicResultAt(1, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedB", ""));

await test.RunAsync();
}

[Fact]
public async Task VisualBasic_BannedType_Constructor()
{
Expand Down

0 comments on commit bbce803

Please sign in to comment.