Skip to content

Commit d063dd6

Browse files
Fix attribute loading for cases where the base class is not available
Signed-off-by: Alexander Linne <[email protected]>
1 parent cfbfe15 commit d063dd6

File tree

8 files changed

+20
-10
lines changed

8 files changed

+20
-10
lines changed

ArchUnit.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.TUnitTests", "A
4646
EndProject
4747
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterfaceAssembly", "TestAssemblies\InterfaceAssembly\InterfaceAssembly.csproj", "{076E223C-32D3-4672-8B00-925CDBC1383E}"
4848
EndProject
49-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FilteredDirectoryAssemblyAttributeAssembly", "TestAssemblies\FilteredDirectoryAssemblyAttributeAssembly\FilteredDirectoryAssemblyAttributeAssembly.csproj", "{AA695589-8CCC-4474-9A7F-01A6376C375A}"
49+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FilteredDirectoryUnavailableTypesAssembly", "TestAssemblies\FilteredDirectoryUnavailableTypesAssembly\FilteredDirectoryUnavailableTypesAssembly.csproj", "{AA695589-8CCC-4474-9A7F-01A6376C375A}"
5050
EndProject
5151
Global
5252
GlobalSection(SolutionConfigurationPlatforms) = preSolution

ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ TypeFactory typeFactory
2121
attributeTypeReference
2222
);
2323
var attribute = attributeType.Type as Attribute;
24-
if (attributeType.Type is UnavailableType unavailableType)
24+
if (attribute == null && (attributeType.Type is UnavailableType || attributeType.Type is Class))
2525
{
26-
attribute = new Attribute(unavailableType, null, null);
26+
attribute = new Attribute(attributeType.Type, null, null);
2727
}
2828
if (attribute == null)
2929
{

ArchUnitNETTests/Loader/ArchLoaderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void UnavailableTypeTest()
149149
System.IO.SearchOption.AllDirectories
150150
)
151151
.Build();
152-
Assert.Single(architecture.Types);
152+
Assert.Equal(3, architecture.Types.Count());
153153
var loggerType = architecture.ReferencedTypes.WhereFullNameIs("Serilog.ILogger");
154154
Assert.NotNull(loggerType);
155155
Assert.True(loggerType is UnavailableType);

TestAssemblies/FilteredDirectoryAssemblyAttributeAssembly/AssemblyAttribute.cs

Lines changed: 0 additions & 4 deletions
This file was deleted.

TestAssemblies/FilteredDirectoryLoaderTestAssembly/Class1.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
using FilteredDirectoryUnavailableTypesAssembly;
12
using Serilog;
23

3-
[assembly: FilteredDirectoryAssemblyAttributeAssembly.AssemblyAttribute]
4+
[assembly: Assembly]
45

56
namespace FilteredDirectoryLoaderTestAssembly;
67

@@ -13,3 +14,8 @@ public Class1()
1314
this.logger = new LoggerConfiguration().CreateLogger();
1415
}
1516
}
17+
18+
public class DerivedAttribute : BaseAttribute { }
19+
20+
[Derived]
21+
public class ClassWithDerivedAttribute { }

TestAssemblies/FilteredDirectoryLoaderTestAssembly/FilteredDirectoryLoaderTestAssembly.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
1212
</ItemGroup>
1313
<ItemGroup>
14-
<ProjectReference Include="..\FilteredDirectoryAssemblyAttributeAssembly\FilteredDirectoryAssemblyAttributeAssembly.csproj">
14+
<ProjectReference Include="..\FilteredDirectoryUnavailableTypesAssembly\FilteredDirectoryUnavailableTypesAssembly.csproj">
1515
<!-- Do not copy the referenced assembly to the output directory -->
1616
<!-- in order to test handling of unavailable types as attribute assemblies -->
1717
<Private>False</Private>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<ImplicitUsings>enable</ImplicitUsings>
55
<Nullable>enable</Nullable>
66
<IsPackable>false</IsPackable>
7+
<RootNamespace>FilteredDirectoryUnavailableTypesAssembly</RootNamespace>
78
</PropertyGroup>
89
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace FilteredDirectoryUnavailableTypesAssembly;
2+
3+
[AttributeUsage(AttributeTargets.Assembly)]
4+
public class AssemblyAttribute : Attribute { }
5+
6+
[AttributeUsage(AttributeTargets.All)]
7+
public class BaseAttribute : Attribute { }

0 commit comments

Comments
 (0)