Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 9 additions & 6 deletions src/NetArchTest.Rules/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Runtime.CompilerServices;
using NetArchTest.Rules.Extensions;
using Mono.Cecil;
using NetArchTest.Rules.Dependencies;

/// <summary>
/// Creates a list of types that can have predicates and conditions applied to it.
Expand All @@ -18,9 +19,11 @@ public sealed class Types
private readonly List<TypeDefinition> _types;

/// <summary> The list of namespaces to exclude from the current domain. </summary>
private static List<string> _exclusionList = new List<string>
private static readonly List<string> _exclusionList = new List<string>
{ "System", "Microsoft", "Mono.Cecil", "netstandard", "NetArchTest.Rules", "<Module>", "xunit" };

private static readonly NamespaceTree _exclusionTree = new NamespaceTree(_exclusionList);

/// <summary>
/// Prevents any external class initializing a new instance of the <see cref="Types"/> class.
/// </summary>
Expand All @@ -39,7 +42,7 @@ public static Types InCurrentDomain()
var currentDomain = new List<Assembly>();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (!_exclusionList.Any(e => assembly.FullName.StartsWith(e)))
if (!_exclusionTree.GetAllMatchingNames(assembly.FullName).Any())
{
currentDomain.Add(assembly);
}
Expand Down Expand Up @@ -222,8 +225,8 @@ public static Types FromPath(string path, IEnumerable<string> 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));
}
Expand Down Expand Up @@ -251,8 +254,8 @@ private static IEnumerable<TypeDefinition> GetAllTypes(IEnumerable<TypeDefinitio
while (check.Count > 0)
{
var type = check.Dequeue();

if (!_exclusionList.Any(e => type.FullName.StartsWith(e, StringComparison.InvariantCultureIgnoreCase)))
if (!_exclusionTree.GetAllMatchingNames(type.FullName).Any())
{
output.Add(type);

Expand Down
15 changes: 11 additions & 4 deletions test/NetArchTest.Rules.UnitTests/TypesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")]
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SystemAsNamespacePrefix
{
class ExampleClass
{
}
}