Skip to content

Commit

Permalink
Merge branch 'main' into fix/multiple-instances-of-same-type
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderlinne authored Jan 24, 2025
2 parents 4272e7f + 9a8486f commit 5d19d1f
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"csharpier": {
"version": "0.29.2",
"version": "0.30.6",
"commands": [
"dotnet-csharpier"
],
Expand All @@ -17,7 +17,7 @@
"rollForward": false
},
"dotnet-reportgenerator-globaltool": {
"version": "5.3.10",
"version": "5.4.3",
"commands": [
"reportgenerator"
],
Expand Down
28 changes: 14 additions & 14 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup .NET
uses: actions/setup-dotnet@6bd8b7f7774af54e05809fcc5431931b3eb1ddee # v4.0.1
uses: actions/setup-dotnet@87b7050bc53ea08284295505d98d2aa94301e852 # v4.2.0
with:
dotnet-version: 8.0.303
dotnet-version: 8.0.405
- name: Install tools
run: dotnet tool restore
- name: Check formatting
Expand All @@ -31,11 +31,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup .NET
uses: actions/setup-dotnet@6bd8b7f7774af54e05809fcc5431931b3eb1ddee # v4.0.1
uses: actions/setup-dotnet@87b7050bc53ea08284295505d98d2aa94301e852 # v4.2.0
with:
dotnet-version: 8.0.303
dotnet-version: 8.0.405
- name: Run tests
run: dotnet test -c Debug --collect:"XPlat Code Coverage" --settings coverlet.runsettings ArchUnitNETTests/
- name: Upload coverage reports to Codecov
Expand All @@ -50,11 +50,11 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup .NET
uses: actions/setup-dotnet@6bd8b7f7774af54e05809fcc5431931b3eb1ddee # v4.0.1
uses: actions/setup-dotnet@87b7050bc53ea08284295505d98d2aa94301e852 # v4.2.0
with:
dotnet-version: 8.0.303
dotnet-version: 8.0.405
- name: Run tests
run: dotnet test -c Debug
publish-docs:
Expand All @@ -68,7 +68,7 @@ jobs:
contents: write
steps:
- name: Checkout
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup doxygen
run: sudo apt-get install doxygen graphviz
- name: Create temporary directory
Expand All @@ -88,7 +88,7 @@ jobs:
fi
fi
- name: Checkout gh-pages branch
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
ref: gh-pages
path: ${{ env.DOCS_TEMP_DIR }}
Expand Down Expand Up @@ -136,11 +136,11 @@ jobs:
- run-tests
steps:
- name: Checkout
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup .NET
uses: actions/setup-dotnet@6bd8b7f7774af54e05809fcc5431931b3eb1ddee # v4.0.1
uses: actions/setup-dotnet@87b7050bc53ea08284295505d98d2aa94301e852 # v4.2.0
with:
dotnet-version: 8.0.303
dotnet-version: 8.0.405
- name: Build
run: dotnet build -c Release
- name: Pack
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ jobs:
language: ["csharp"]
steps:
- name: Checkout repository
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Initialize CodeQL
uses: github/codeql-action/init@6db8d6351fd0be61f9ed8ebd12ccd35dcec51fea # v3.26.11
uses: github/codeql-action/init@ee117c905ab18f32fa0f66c2fe40ecc8013f3e04 # v3.28.4
with:
languages: ${{ matrix.language }}
- name: Autobuild
uses: github/codeql-action/autobuild@6db8d6351fd0be61f9ed8ebd12ccd35dcec51fea # v3.26.11
uses: github/codeql-action/autobuild@ee117c905ab18f32fa0f66c2fe40ecc8013f3e04 # v3.28.4
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@6db8d6351fd0be61f9ed8ebd12ccd35dcec51fea # v3.26.11
uses: github/codeql-action/analyze@ee117c905ab18f32fa0f66c2fe40ecc8013f3e04 # v3.28.4
with:
category: "/language:${{matrix.language}}"
7 changes: 7 additions & 0 deletions ArchUnit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoaderTestAssembly", "TestA
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OtherLoaderTestAssembly", "TestAssemblies\OtherLoaderTestAssembly\OtherLoaderTestAssembly.csproj", "{5A24529B-1794-4080-ADCC-77440BA0A0B3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FilteredDirectoryLoaderTestAssembly", "TestAssemblies\FilteredDirectoryLoaderTestAssembly\FilteredDirectoryLoaderTestAssembly.csproj", "{E6CB8C69-25F5-4C94-8EA3-D56E444EB46B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -94,6 +96,10 @@ Global
{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
{E6CB8C69-25F5-4C94-8EA3-D56E444EB46B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6CB8C69-25F5-4C94-8EA3-D56E444EB46B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6CB8C69-25F5-4C94-8EA3-D56E444EB46B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6CB8C69-25F5-4C94-8EA3-D56E444EB46B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -104,5 +110,6 @@ Global
{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}
{E6CB8C69-25F5-4C94-8EA3-D56E444EB46B} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion ArchUnitNET.NUnitTests/ArchUnitNET.NUnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions ArchUnitNET.xUnit/ArchUnitNET.xUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

<ItemGroup>
<PackageReference Include="xunit.assert" Version="2.4.1"/>
<PackageReference Include="System.Net.Http" Version="4.3.4"/>
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1"/>
</ItemGroup>

<ItemGroup>
Expand Down
75 changes: 75 additions & 0 deletions ArchUnitNET/Domain/UnavailableType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2019 Florian Gather <[email protected]>
// Copyright 2019 Fritz Brandhuber <[email protected]>
// Copyright 2020 Pavel Fischer <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0

using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Domain.Dependencies;

namespace ArchUnitNET.Domain
{
public class UnavailableType : IType

Check failure on line 13 in ArchUnitNET/Domain/UnavailableType.cs

View workflow job for this annotation

GitHub Actions / Check code coverage

'UnavailableType' does not implement interface member 'IHasAssemblyQualifiedName.AssemblyQualifiedName'

Check failure on line 13 in ArchUnitNET/Domain/UnavailableType.cs

View workflow job for this annotation

GitHub Actions / Run tests (ubuntu-latest)

'UnavailableType' does not implement interface member 'IHasAssemblyQualifiedName.AssemblyQualifiedName'

Check failure on line 13 in ArchUnitNET/Domain/UnavailableType.cs

View workflow job for this annotation

GitHub Actions / Run tests (ubuntu-latest)

'UnavailableType' does not implement interface member 'IHasAssemblyQualifiedName.AssemblyQualifiedName'
{
public UnavailableType(IType type)
{
Type = type;
}

private IType Type { get; }
public string Name => Type.Name;
public string FullName => Type.FullName;

public Visibility Visibility => Type.Visibility;
public bool IsNested => Type.IsNested;
public bool IsGeneric => Type.IsGeneric;
public bool IsGenericParameter => Type.IsGenericParameter;
public bool IsStub => true;
public bool IsCompilerGenerated => Type.IsCompilerGenerated;

public Namespace Namespace => Type.Namespace;
public Assembly Assembly => Type.Assembly;

public IEnumerable<Attribute> Attributes =>
AttributeInstances.Select(instance => instance.Type);
public List<AttributeInstance> AttributeInstances => Type.AttributeInstances;

public List<ITypeDependency> Dependencies => Type.Dependencies;
public List<ITypeDependency> BackwardsDependencies => Type.BackwardsDependencies;
public IEnumerable<IType> ImplementedInterfaces => Type.ImplementedInterfaces;

public MemberList Members => Type.Members;
public List<GenericParameter> GenericParameters => Type.GenericParameters;

public override string ToString()
{
return FullName;
}

private bool Equals(Struct other)
{
return Equals(Type, other.Type);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

return obj.GetType() == GetType() && Equals((Struct)obj);
}

public override int GetHashCode()
{
return Type != null ? Type.GetHashCode() : 0;
}
}
}
24 changes: 23 additions & 1 deletion ArchUnitNET/Loader/TypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,29 @@ TypeReference typeReference
}
if (typeDefinition == null)
{
throw new ArchLoaderException($"Could not resolve type {typeReference.FullName}");
// When assemblies are loaded by path, there are cases where a dependent type cannot be resolved because
// the assembly dependency is not loaded in the current application domain. In this case, we create a
// stub type.
return new TypeInstance<IType>(
new UnavailableType(
new Type(
typeReference.BuildFullName(),
typeReference.Name,
_assemblyRegistry.GetOrCreateAssembly(
typeReference.Module.Assembly.Name.FullName,
typeReference.Module.Assembly.FullName,
true,
null
),
_namespaceRegistry.GetOrCreateNamespace(typeReference.Namespace),
NotAccessible,
typeReference.IsNested,
typeReference.HasGenericParameters,
true,
typeReference.IsCompilerGenerated()
)
)
);
}
return typeDefinition;
}
Expand Down
5 changes: 3 additions & 2 deletions ArchUnitNETTests/ArchUnitNETTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ProjectReference Include="..\TestAssembly\TestAssembly.csproj" />
<ProjectReference Include="..\TestAssemblies\AttributeAssembly\AttributeAssembly.csproj" />
<ProjectReference Include="..\TestAssemblies\DependencyAssembly\DependencyAssembly.csproj" />
<ProjectReference Include="..\TestAssemblies\FilteredDirectoryLoaderTestAssembly\FilteredDirectoryLoaderTestAssembly.csproj" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\TestAssemblies\LoaderTestAssembly\LoaderTestAssembly.csproj" Aliases="global,LoaderTestAssemblyAlias" />
<ProjectReference
Include="..\TestAssemblies\OtherLoaderTestAssembly\OtherLoaderTestAssembly.csproj" Aliases="global,OtherLoaderTestAssemblyAlias" />
Expand All @@ -21,12 +22,12 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PackageReference Include="coverlet.collector" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Verify.xunit" Version="26.6.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

Expand Down
28 changes: 28 additions & 0 deletions ArchUnitNETTests/Loader/ArchLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
extern alias LoaderTestAssemblyAlias;
extern alias OtherLoaderTestAssemblyAlias;

using System;
using System.Linq;
using ArchUnitNET.Domain;
using ArchUnitNET.Domain.Extensions;
using ArchUnitNET.Loader;
using ArchUnitNET.xUnit;
using ArchUnitNETTests.Domain.Dependencies.Members;
Expand Down Expand Up @@ -124,5 +127,30 @@ public void TypesAreAssignedToCorrectAssemblies()
.ResideInAssembly(GetType().Assembly)
.Check(architecture);
}

[Fact]
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[
..AppDomain.CurrentDomain.BaseDirectory.IndexOf(
@"ArchUnitNETTests",
StringComparison.InvariantCulture
)
];
var architecture = new ArchLoader()
.LoadFilteredDirectory(
assemblyPath,
"FilteredDirectoryLoaderTestAssembly.dll",
System.IO.SearchOption.AllDirectories
)
.Build();
Assert.Single(architecture.Types);
var loggerType = architecture.ReferencedTypes.WhereFullNameIs("Serilog.ILogger");
Assert.NotNull(loggerType);
Assert.True(loggerType is UnavailableType);
}
}
}
2 changes: 1 addition & 1 deletion ExampleTest/ExampleTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions TestAssemblies/AttributeAssembly/AttributeAssembly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IsPackable>false</IsPackable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IsPackable>false</IsPackable>
</PropertyGroup>

</Project>
13 changes: 13 additions & 0 deletions TestAssemblies/FilteredDirectoryLoaderTestAssembly/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Serilog;

namespace FilteredDirectoryLoaderTestAssembly;

public class Class1
{
public ILogger logger;

public Class1()
{
this.logger = new LoggerConfiguration().CreateLogger();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit 5d19d1f

Please sign in to comment.