From 1186eb655040899fb2c7624c0b251bbbcee96a34 Mon Sep 17 00:00:00 2001 From: Alexander Linne Date: Wed, 24 Sep 2025 12:19:36 +0200 Subject: [PATCH] Handle UnavailableType when creating an Attribute Signed-off-by: Alexander Linne --- ArchUnit.sln | 7 +++++++ ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs | 7 ++++++- ArchUnitNETTests/Loader/ArchLoaderTests.cs | 8 ++++++-- .../AssemblyAttribute.cs | 4 ++++ .../FilteredDirectoryAssemblyAttributeAssembly.csproj | 7 +++++++ .../FilteredDirectoryLoaderTestAssembly/Class1.cs | 2 ++ .../FilteredDirectoryLoaderTestAssembly.csproj | 7 +++++++ 7 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 FilteredDirectoryAssemblyAttributeAssembly/AssemblyAttribute.cs create mode 100644 FilteredDirectoryAssemblyAttributeAssembly/FilteredDirectoryAssemblyAttributeAssembly.csproj diff --git a/ArchUnit.sln b/ArchUnit.sln index 87328636a..e2ea2bbfd 100644 --- a/ArchUnit.sln +++ b/ArchUnit.sln @@ -46,6 +46,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.TUnitTests", "A EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterfaceAssembly", "TestAssemblies\InterfaceAssembly\InterfaceAssembly.csproj", "{076E223C-32D3-4672-8B00-925CDBC1383E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FilteredDirectoryAssemblyAttributeAssembly", "FilteredDirectoryAssemblyAttributeAssembly\FilteredDirectoryAssemblyAttributeAssembly.csproj", "{AA695589-8CCC-4474-9A7F-01A6376C375A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -136,6 +138,10 @@ Global {076E223C-32D3-4672-8B00-925CDBC1383E}.Debug|Any CPU.Build.0 = Debug|Any CPU {076E223C-32D3-4672-8B00-925CDBC1383E}.Release|Any CPU.ActiveCfg = Release|Any CPU {076E223C-32D3-4672-8B00-925CDBC1383E}.Release|Any CPU.Build.0 = Release|Any CPU + {AA695589-8CCC-4474-9A7F-01A6376C375A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AA695589-8CCC-4474-9A7F-01A6376C375A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AA695589-8CCC-4474-9A7F-01A6376C375A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AA695589-8CCC-4474-9A7F-01A6376C375A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -148,5 +154,6 @@ Global {5A24529B-1794-4080-ADCC-77440BA0A0B3} = {B1191F18-91CB-4387-B775-A5EB64D3AC30} {E6CB8C69-25F5-4C94-8EA3-D56E444EB46B} = {B1191F18-91CB-4387-B775-A5EB64D3AC30} {076E223C-32D3-4672-8B00-925CDBC1383E} = {B1191F18-91CB-4387-B775-A5EB64D3AC30} + {AA695589-8CCC-4474-9A7F-01A6376C375A} = {B1191F18-91CB-4387-B775-A5EB64D3AC30} EndGlobalSection EndGlobal diff --git a/ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs b/ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs index c6533a40e..389b0f7f4 100644 --- a/ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs +++ b/ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs @@ -20,7 +20,12 @@ TypeFactory typeFactory var attributeType = typeFactory.GetOrCreateStubTypeInstanceFromTypeReference( attributeTypeReference ); - if (!(attributeType.Type is Attribute attribute)) + var attribute = attributeType.Type as Attribute; + if (attributeType.Type is UnavailableType unavailableType) + { + attribute = new Attribute(unavailableType, null, null); + } + if (attribute == null) { throw new ArgumentException( $"Attribute type {attributeType.Type.FullName} is not an attribute." diff --git a/ArchUnitNETTests/Loader/ArchLoaderTests.cs b/ArchUnitNETTests/Loader/ArchLoaderTests.cs index 356e2d07a..44f1ddbbc 100644 --- a/ArchUnitNETTests/Loader/ArchLoaderTests.cs +++ b/ArchUnitNETTests/Loader/ArchLoaderTests.cs @@ -2,6 +2,7 @@ extern alias OtherLoaderTestAssemblyAlias; using System; +using System.IO; using System.Linq; using ArchUnitNET.Domain; using ArchUnitNET.Domain.Extensions; @@ -132,15 +133,18 @@ public void UnavailableTypeTest() // When loading an assembly from a file, there are situations where the assemblies dependencies are not // available in the current AppDomain. This test checks that the loader does not throw an exception in this // case. - var assemblyPath = AppDomain.CurrentDomain.BaseDirectory[ + var currentAssemblyPath = AppDomain.CurrentDomain.BaseDirectory[ ..AppDomain.CurrentDomain.BaseDirectory.IndexOf( @"ArchUnitNETTests", StringComparison.InvariantCulture ) ]; + var assemblySearchPath = Path.Combine( + [currentAssemblyPath, "TestAssemblies", "FilteredDirectoryLoaderTestAssembly"] + ); var architecture = new ArchLoader() .LoadFilteredDirectory( - assemblyPath, + assemblySearchPath, "FilteredDirectoryLoaderTestAssembly.dll", System.IO.SearchOption.AllDirectories ) diff --git a/FilteredDirectoryAssemblyAttributeAssembly/AssemblyAttribute.cs b/FilteredDirectoryAssemblyAttributeAssembly/AssemblyAttribute.cs new file mode 100644 index 000000000..2af89891d --- /dev/null +++ b/FilteredDirectoryAssemblyAttributeAssembly/AssemblyAttribute.cs @@ -0,0 +1,4 @@ +namespace FilteredDirectoryAssemblyAttributeAssembly; + +[AttributeUsage(AttributeTargets.Assembly)] +public class AssemblyAttribute : Attribute { } diff --git a/FilteredDirectoryAssemblyAttributeAssembly/FilteredDirectoryAssemblyAttributeAssembly.csproj b/FilteredDirectoryAssemblyAttributeAssembly/FilteredDirectoryAssemblyAttributeAssembly.csproj new file mode 100644 index 000000000..bf9d3be59 --- /dev/null +++ b/FilteredDirectoryAssemblyAttributeAssembly/FilteredDirectoryAssemblyAttributeAssembly.csproj @@ -0,0 +1,7 @@ + + + net9.0 + enable + enable + + diff --git a/TestAssemblies/FilteredDirectoryLoaderTestAssembly/Class1.cs b/TestAssemblies/FilteredDirectoryLoaderTestAssembly/Class1.cs index ea4b8c902..86785f5ee 100644 --- a/TestAssemblies/FilteredDirectoryLoaderTestAssembly/Class1.cs +++ b/TestAssemblies/FilteredDirectoryLoaderTestAssembly/Class1.cs @@ -1,5 +1,7 @@ using Serilog; +[assembly: FilteredDirectoryAssemblyAttributeAssembly.AssemblyAttribute] + namespace FilteredDirectoryLoaderTestAssembly; public class Class1 diff --git a/TestAssemblies/FilteredDirectoryLoaderTestAssembly/FilteredDirectoryLoaderTestAssembly.csproj b/TestAssemblies/FilteredDirectoryLoaderTestAssembly/FilteredDirectoryLoaderTestAssembly.csproj index 7ea3844e6..b7c5856da 100644 --- a/TestAssemblies/FilteredDirectoryLoaderTestAssembly/FilteredDirectoryLoaderTestAssembly.csproj +++ b/TestAssemblies/FilteredDirectoryLoaderTestAssembly/FilteredDirectoryLoaderTestAssembly.csproj @@ -10,4 +10,11 @@ + + + + + False + +