public async Task GetStyleCopSettings_WithSettingsJson_SettingsAreReadCorrectly() { // arrange var settingsJson = @" { ""rules"": [ { ""type"": ""allow"", ""from"": ""*"", ""to"": ""*"" }, { ""type"": ""deny"", ""from"": ""fo.bar.*"", ""to"": ""camp.hack.*"" } ] } "; var context = await CreateAnalysisContextAsync(settingsJson).ConfigureAwait(false); // act var settings = context.GetDependencyCopSettings(CancellationToken.None); // assert var expected = new DependencyCopSettings { Rules = new[] { new DependencyRuleEntry { Type = DependencyRuleType.Allow, From = "*", To = "*" }, new DependencyRuleEntry { Type = DependencyRuleType.Deny, From = "fo.bar.*", To = "camp.hack.*" }, } }; AssertSettings(settings, expected); }
public static void RegisterSymbolAction(this AnalysisContext context, Action <SymbolAnalysisContext, DependencyCopSettings> action, params SymbolKind[] symbolKinds) { context.RegisterSymbolAction( c => { DependencyCopSettings settings = context.GetDependencyCopSettings(c.Options, c.CancellationToken); action(c, settings); }, symbolKinds); }
private static void AssertDefaultSettings(DependencyCopSettings settings) { var expected = new DependencyCopSettings { Rules = new[] { new DependencyRuleEntry { Type = DependencyRuleType.Allow, From = "*", To = "*" } } }; AssertSettings(settings, expected); }
// private void AnalyzeNode(SyntaxNodeAnalysisContext context) // { // var localDeclaration = (LocalDeclarationStatementSyntax)context.Node; // Console.WriteLine(localDeclaration); // } // private static void AnalyzeSymbol(SymbolAnalysisContext context) // { // // TODO: Replace the following code with your own analysis, generating Diagnostic objects for any issues you find // var namedTypeSymbol = (INamedTypeSymbol)context.Symbol; // // Find just those named type symbols with names containing lowercase letters. // if (namedTypeSymbol.Name.ToCharArray().Any(char.IsLower)) // { // // For all such symbols, produce a diagnostic. // var diagnostic = Diagnostic.Create(Rule, namedTypeSymbol.Locations[0], namedTypeSymbol.Name); // context.ReportDiagnostic(diagnostic); // } // } private static void AnalyzeFieldSymbol(SymbolAnalysisContext context, DependencyCopSettings settings) { var fieldSymbol = (IFieldSymbol)context.Symbol; var validator = new DependencyValidator(settings); var fromNamespace = fieldSymbol.ContainingNamespace.GetNamespace(); var toNamespace = fieldSymbol.Type.ContainingNamespace.GetNamespace(); if (!validator.IsAllowedDependency(fromNamespace, toNamespace)) { var diagnostic = Diagnostic.Create(Rule, fieldSymbol.Locations[0], fromNamespace, toNamespace); context.ReportDiagnostic(diagnostic); } }
private static void AssertSettings(DependencyCopSettings actual, DependencyCopSettings expected) { Assert.NotNull(actual); Assert.NotNull(actual.Rules); Assert.Equal(actual.Rules.Length, expected.Rules.Length); for (int i = 0; i < actual.Rules.Length; i++) { Assert.Equal(actual.Rules[0].Type, expected.Rules[0].Type); Assert.Equal(actual.Rules[0].From, expected.Rules[0].From); Assert.Equal(actual.Rules[0].To, expected.Rules[0].To); } }
public void IsAllowedDependency_WithMatchingAllowedRule_ReturnsTrue() { // arrange var settings = new DependencyCopSettings { Rules = new[] { new DependencyRuleEntry { Type = DependencyRuleType.Allow, From = "*", To = "Foo.Pilot" } } }; var validator = new DependencyValidator(settings); // act bool actual = validator.IsAllowedDependency(new Namespace("Foo.Bar"), new Namespace("Foo.Pilot")); // assert Assert.True(actual); }
public DependencyValidator(DependencyCopSettings settings) { this.ruleSet = settings.Rules .GroupBy(r => r.Type) .ToDictionary(r => r.Key, r => r.Select(i => new DependencyRule(i.From, i.To))); }
public void GetDependencyCopSettings_WithoutSpecificSettings_ReturnsSettingsDefaults() { DependencyCopSettings settings = SettingsHelper.GetDependencyCopSettings(default(SymbolAnalysisContext), CancellationToken.None); AssertDefaultSettings(settings); }