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

Loader: Match Types By Assembly Qualified Name #301

Merged
merged 2 commits into from
Sep 4, 2024
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
14 changes: 14 additions & 0 deletions ArchUnit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AttributeAssembly", "TestAs
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisibilityAssembly", "TestAssemblies\VisibilityAssembly\VisibilityAssembly.csproj", "{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoaderTestAssembly", "TestAssemblies\LoaderTestAssembly\LoaderTestAssembly.csproj", "{0243F2D4-AC89-4561-A936-D647B6BB821F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OtherLoaderTestAssembly", "TestAssemblies\OtherLoaderTestAssembly\OtherLoaderTestAssembly.csproj", "{5A24529B-1794-4080-ADCC-77440BA0A0B3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -82,6 +86,14 @@ Global
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}.Release|Any CPU.Build.0 = Release|Any CPU
{0243F2D4-AC89-4561-A936-D647B6BB821F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0243F2D4-AC89-4561-A936-D647B6BB821F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0243F2D4-AC89-4561-A936-D647B6BB821F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0243F2D4-AC89-4561-A936-D647B6BB821F}.Release|Any CPU.Build.0 = Release|Any CPU
{5A24529B-1794-4080-ADCC-77440BA0A0B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A24529B-1794-4080-ADCC-77440BA0A0B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A24529B-1794-4080-ADCC-77440BA0A0B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A24529B-1794-4080-ADCC-77440BA0A0B3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -90,5 +102,7 @@ Global
{10A70A38-A18D-4FA8-AF25-2B25B3D60BE6} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
{FB457140-47B4-4B20-8505-BA9BFC73C705} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
{0243F2D4-AC89-4561-A936-D647B6BB821F} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
{5A24529B-1794-4080-ADCC-77440BA0A0B3} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
EndGlobalSection
EndGlobal
11 changes: 9 additions & 2 deletions ArchUnitNET/Loader/ArchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,17 @@ public void LoadTypesForModule(ModuleDefinition module, string namespaceFilter)
.ForEach(typeDefinition =>
{
var type = _typeFactory.GetOrCreateTypeFromTypeReference(typeDefinition);
if (!_architectureTypes.ContainsKey(type.FullName) && !type.IsCompilerGenerated)
var assemblyQualifiedName = System.Reflection.Assembly.CreateQualifiedName(
module.Assembly.Name.Name,
typeDefinition.FullName
);
if (
!_architectureTypes.ContainsKey(assemblyQualifiedName)
&& !type.IsCompilerGenerated
)
{
currentTypes.Add(type);
_architectureTypes.Add(type.FullName, type);
_architectureTypes.Add(assemblyQualifiedName, type);
}
});

Expand Down
2 changes: 0 additions & 2 deletions ArchUnitNET/Loader/TypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
//
// SPDX-License-Identifier: Apache-2.0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using ArchUnitNET.Domain;
using ArchUnitNET.Loader.LoadTasks;
Expand Down
6 changes: 5 additions & 1 deletion ArchUnitNET/Loader/TypeRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ public ITypeInstance<IType> GetOrCreateTypeFromTypeReference(
[NotNull] Func<string, ITypeInstance<IType>> createFunc
)
{
var assemblyQualifiedName = System.Reflection.Assembly.CreateQualifiedName(
typeReference.Module.Assembly.FullName,
typeReference.BuildFullName()
);
return RegistryUtils.GetFromDictOrCreateAndAdd(
typeReference.BuildFullName(),
assemblyQualifiedName,
_allTypes,
createFunc
);
Expand Down
3 changes: 3 additions & 0 deletions ArchUnitNETTests/ArchUnitNETTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<ProjectReference Include="..\TestAssembly\TestAssembly.csproj" />
<ProjectReference Include="..\TestAssemblies\AttributeAssembly\AttributeAssembly.csproj" />
<ProjectReference Include="..\TestAssemblies\DependencyAssembly\DependencyAssembly.csproj" />
<ProjectReference Include="..\TestAssemblies\LoaderTestAssembly\LoaderTestAssembly.csproj" />
<ProjectReference
Include="..\TestAssemblies\OtherLoaderTestAssembly\OtherLoaderTestAssembly.csproj" />
<ProjectReference Include="..\TestAssemblies\VisibilityAssembly\VisibilityAssembly.csproj" />
</ItemGroup>

Expand Down
13 changes: 13 additions & 0 deletions ArchUnitNETTests/Loader/ArchLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ public void LoadAssemblies()
Assert.NotEmpty(ArchUnitNETTestAssemblyArchitectureWithDependencies.Assemblies);
}

[Fact]
public void SameFullNameInMultipleAssemblies()
{
var types = LoaderTestArchitecture.Types.Where(type =>
type.Namespace.FullName == "DuplicateClassAcrossAssemblies"
);
Assert.Equal(2, types.Count());
Assert.Single(types.Where(type => type.Assembly.Name.StartsWith("LoaderTestAssembly")));
Assert.Single(
types.Where(type => type.Assembly.Name.StartsWith("OtherLoaderTestAssembly"))
);
}

[Fact]
public void LoadAssembliesIncludingRecursiveDependencies()
{
Expand Down
7 changes: 7 additions & 0 deletions ArchUnitNETTests/StaticTestArchitectures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public static class StaticTestArchitectures
.LoadAssemblies(typeof(TypeDependencyNamespace.BaseClass).Assembly)
.Build();

public static readonly Architecture LoaderTestArchitecture = new ArchLoader()
.LoadAssemblies(
typeof(LoaderTestAssembly.LoaderTestAssembly).Assembly,
typeof(OtherLoaderTestAssembly.OtherLoaderTestAssembly).Assembly
)
.Build();

public static readonly Architecture VisibilityArchitecture = new ArchLoader()
.LoadAssemblies(typeof(VisibilityNamespace.PublicClass).Assembly)
.Build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace DuplicateClassAcrossAssemblies;

internal class DuplicateClass { }
3 changes: 3 additions & 0 deletions TestAssemblies/LoaderTestAssembly/LoaderTestAssembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace LoaderTestAssembly;

public class LoaderTestAssembly { }
9 changes: 9 additions & 0 deletions TestAssemblies/LoaderTestAssembly/LoaderTestAssembly.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace DuplicateClassAcrossAssemblies;

internal class DuplicateClass { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace OtherLoaderTestAssembly;

public class OtherLoaderTestAssembly { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Loading