From 6a228336064962fac598629e99c9eeaa71424a2c Mon Sep 17 00:00:00 2001 From: NeVeSpl Date: Sun, 15 Mar 2020 20:33:22 +0100 Subject: [PATCH] fix false positive exclusion of system types --- src/NetArchTest.Rules/Types.cs | 15 +++++++++------ test/NetArchTest.Rules.UnitTests/TypesTests.cs | 15 +++++++++++---- .../SystemAsNamespacePrefix/ExampleClass.cs | 10 ++++++++++ 3 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 test/NetArchTest.TestStructure/FalsePositives/SystemAsNamespacePrefix/ExampleClass.cs diff --git a/src/NetArchTest.Rules/Types.cs b/src/NetArchTest.Rules/Types.cs index 3b9a03b..aa5de0b 100644 --- a/src/NetArchTest.Rules/Types.cs +++ b/src/NetArchTest.Rules/Types.cs @@ -8,6 +8,7 @@ using System.Runtime.CompilerServices; using NetArchTest.Rules.Extensions; using Mono.Cecil; + using NetArchTest.Rules.Dependencies; /// /// Creates a list of types that can have predicates and conditions applied to it. @@ -18,9 +19,11 @@ public sealed class Types private readonly List _types; /// The list of namespaces to exclude from the current domain. - private static List _exclusionList = new List + private static readonly List _exclusionList = new List { "System", "Microsoft", "Mono.Cecil", "netstandard", "NetArchTest.Rules", "", "xunit" }; + private static readonly NamespaceTree _exclusionTree = new NamespaceTree(_exclusionList); + /// /// Prevents any external class initializing a new instance of the class. /// @@ -39,7 +42,7 @@ public static Types InCurrentDomain() var currentDomain = new List(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { - if (!_exclusionList.Any(e => assembly.FullName.StartsWith(e))) + if (!_exclusionTree.GetAllMatchingNames(assembly.FullName).Any()) { currentDomain.Add(assembly); } @@ -222,8 +225,8 @@ public static Types FromPath(string path, IEnumerable searchDirectories foreach (var file in files) { var assembly = ReadAssemblyDefinition(file, readerParams); - - if (assembly != null && !_exclusionList.Any(e => assembly.FullName.StartsWith(e))) + + if (assembly != null && !_exclusionTree.GetAllMatchingNames(assembly.FullName).Any()) { types.AddRange(assembly.Modules.SelectMany(t => t.Types)); } @@ -251,8 +254,8 @@ private static IEnumerable GetAllTypes(IEnumerable 0) { var type = check.Dequeue(); - - if (!_exclusionList.Any(e => type.FullName.StartsWith(e, StringComparison.InvariantCultureIgnoreCase))) + + if (!_exclusionTree.GetAllMatchingNames(type.FullName).Any()) { output.Add(type); diff --git a/test/NetArchTest.Rules.UnitTests/TypesTests.cs b/test/NetArchTest.Rules.UnitTests/TypesTests.cs index 3f9c271..bf818d3 100644 --- a/test/NetArchTest.Rules.UnitTests/TypesTests.cs +++ b/test/NetArchTest.Rules.UnitTests/TypesTests.cs @@ -17,9 +17,16 @@ public void InCurrentDomain_SystemTypesExcluded() { var result = Types.InCurrentDomain().GetTypeDefinitions(); - Assert.DoesNotContain(result, t => t.FullName.StartsWith("System")); - Assert.DoesNotContain(result, t => t.FullName.StartsWith("Microsoft")); - Assert.DoesNotContain(result, t => t.FullName.StartsWith("netstandard")); + Assert.DoesNotContain(result, t => t.FullName.StartsWith("System.") || t.FullName.Equals("System")); + Assert.DoesNotContain(result, t => t.FullName.StartsWith("Microsoft.") || t.FullName.Equals("Microsoft")); + Assert.DoesNotContain(result, t => t.FullName.StartsWith("netstandard.") | t.FullName.Equals("netstandard")); + } + [Fact(DisplayName = "Types that reside in namespace that has \"System\" prefix but is not system namespace should be included in the current domain. ")] + public void InCurrentDomain_SystemTypesExcluded2() + { + var result = Types.InCurrentDomain().GetTypeDefinitions(); + + Assert.Contains(result, t => t.FullName == typeof(SystemAsNamespacePrefix.ExampleClass).FullName); } [Fact(DisplayName = "NetArchTest types should be excluded from the current domain.")] @@ -65,7 +72,7 @@ public void FromFile_TypesReturned() var expected = Types.InCurrentDomain().That().ResideInNamespace("NetArchTest.TestStructure").GetTypeDefinitions().Count(); // Act - var result = Types.FromFile("NetArchTest.TestStructure.dll").GetTypes(); + var result = Types.FromFile("NetArchTest.TestStructure.dll").That().ResideInNamespace("NetArchTest.TestStructure").GetTypes(); // Assert Assert.Equal(expected, result.Count()); diff --git a/test/NetArchTest.TestStructure/FalsePositives/SystemAsNamespacePrefix/ExampleClass.cs b/test/NetArchTest.TestStructure/FalsePositives/SystemAsNamespacePrefix/ExampleClass.cs new file mode 100644 index 0000000..4b38ba9 --- /dev/null +++ b/test/NetArchTest.TestStructure/FalsePositives/SystemAsNamespacePrefix/ExampleClass.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SystemAsNamespacePrefix +{ + class ExampleClass + { + } +}