Skip to content

Commit 3896c05

Browse files
Handle UnavailableType when creating an Attribute
1 parent 929d0cb commit 3896c05

File tree

7 files changed

+43
-3
lines changed

7 files changed

+43
-3
lines changed

ArchUnit.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.TUnit", "ArchUn
4444
EndProject
4545
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.TUnitTests", "ArchUnitNET.TUnitTests\ArchUnitNET.TUnitTests.csproj", "{C54143CE-3256-4313-B991-0706705EEA1D}"
4646
EndProject
47+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FilteredDirectoryAssemblyAttributeAssembly", "FilteredDirectoryAssemblyAttributeAssembly\FilteredDirectoryAssemblyAttributeAssembly.csproj", "{AA695589-8CCC-4474-9A7F-01A6376C375A}"
48+
EndProject
4749
Global
4850
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4951
Debug|Any CPU = Debug|Any CPU
@@ -130,6 +132,10 @@ Global
130132
{C54143CE-3256-4313-B991-0706705EEA1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
131133
{C54143CE-3256-4313-B991-0706705EEA1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
132134
{C54143CE-3256-4313-B991-0706705EEA1D}.Release|Any CPU.Build.0 = Release|Any CPU
135+
{AA695589-8CCC-4474-9A7F-01A6376C375A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
136+
{AA695589-8CCC-4474-9A7F-01A6376C375A}.Debug|Any CPU.Build.0 = Debug|Any CPU
137+
{AA695589-8CCC-4474-9A7F-01A6376C375A}.Release|Any CPU.ActiveCfg = Release|Any CPU
138+
{AA695589-8CCC-4474-9A7F-01A6376C375A}.Release|Any CPU.Build.0 = Release|Any CPU
133139
EndGlobalSection
134140
GlobalSection(SolutionProperties) = preSolution
135141
HideSolutionNode = FALSE
@@ -141,5 +147,6 @@ Global
141147
{0243F2D4-AC89-4561-A936-D647B6BB821F} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
142148
{5A24529B-1794-4080-ADCC-77440BA0A0B3} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
143149
{E6CB8C69-25F5-4C94-8EA3-D56E444EB46B} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
150+
{AA695589-8CCC-4474-9A7F-01A6376C375A} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
144151
EndGlobalSection
145152
EndGlobal

ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ TypeFactory typeFactory
2020
var attributeType = typeFactory.GetOrCreateStubTypeInstanceFromTypeReference(
2121
attributeTypeReference
2222
);
23-
if (!(attributeType.Type is Attribute attribute))
23+
var attribute = attributeType.Type as Attribute;
24+
if (attributeType.Type is UnavailableType unavailableType)
25+
{
26+
attribute = new Attribute(unavailableType, null, null);
27+
}
28+
if (attribute == null)
2429
{
2530
throw new ArgumentException(
2631
$"Attribute type {attributeType.Type.FullName} is not an attribute."

ArchUnitNETTests/Loader/ArchLoaderTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
extern alias OtherLoaderTestAssemblyAlias;
33

44
using System;
5+
using System.IO;
56
using System.Linq;
67
using ArchUnitNET.Domain;
78
using ArchUnitNET.Domain.Extensions;
@@ -132,15 +133,18 @@ public void UnavailableTypeTest()
132133
// When loading an assembly from a file, there are situations where the assemblies dependencies are not
133134
// available in the current AppDomain. This test checks that the loader does not throw an exception in this
134135
// case.
135-
var assemblyPath = AppDomain.CurrentDomain.BaseDirectory[
136+
var currentAssemblyPath = AppDomain.CurrentDomain.BaseDirectory[
136137
..AppDomain.CurrentDomain.BaseDirectory.IndexOf(
137138
@"ArchUnitNETTests",
138139
StringComparison.InvariantCulture
139140
)
140141
];
142+
var assemblySearchPath = Path.Combine(
143+
[currentAssemblyPath, "TestAssemblies", "FilteredDirectoryLoaderTestAssembly"]
144+
);
141145
var architecture = new ArchLoader()
142146
.LoadFilteredDirectory(
143-
assemblyPath,
147+
assemblySearchPath,
144148
"FilteredDirectoryLoaderTestAssembly.dll",
145149
System.IO.SearchOption.AllDirectories
146150
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace FilteredDirectoryAssemblyAttributeAssembly;
2+
3+
[AttributeUsage(AttributeTargets.Assembly)]
4+
public class AssemblyAttribute : Attribute
5+
{
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

TestAssemblies/FilteredDirectoryLoaderTestAssembly/Class1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Serilog;
22

3+
[assembly:FilteredDirectoryAssemblyAttributeAssembly.AssemblyAttribute]
4+
35
namespace FilteredDirectoryLoaderTestAssembly;
46

57
public class Class1

TestAssemblies/FilteredDirectoryLoaderTestAssembly/FilteredDirectoryLoaderTestAssembly.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@
1010
<PackageReference Include="Serilog" Version="4.2.0" />
1111
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
1212
</ItemGroup>
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\FilteredDirectoryAssemblyAttributeAssembly\FilteredDirectoryAssemblyAttributeAssembly.csproj">
15+
<!-- Do not copy the referenced assembly to the output directory -->
16+
<!-- in order to test handling of unavailable types as attribute assemblies -->
17+
<Private>False</Private>
18+
</ProjectReference>
19+
</ItemGroup>
1320
</Project>

0 commit comments

Comments
 (0)