Skip to content

Commit

Permalink
fill the ReferencedAssemblyNames property for all loaded assemblies
Browse files Browse the repository at this point in the history
Fixes issue #311

Signed-off-by: Karl Rhenius <[email protected]>
  • Loading branch information
made-by-karl committed Oct 25, 2024
1 parent 7fd68bc commit fbdec12
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
4 changes: 1 addition & 3 deletions ArchUnitNET/Domain/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;

namespace ArchUnitNET.Domain
{
Expand All @@ -16,7 +15,7 @@ public Assembly(
string name,
string fullName,
bool isOnlyReferenced,
[CanBeNull] List<string> referencedAssemblyNames
List<string> referencedAssemblyNames
)
{
Name = name;
Expand All @@ -29,7 +28,6 @@ [CanBeNull] List<string> referencedAssemblyNames

public string Name { get; }

[CanBeNull]
public List<string> ReferencedAssemblyNames { get; }
public string FullName { get; }

Expand Down
5 changes: 2 additions & 3 deletions ArchUnitNET/Loader/ArchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ public ArchBuilder()

public void AddAssembly(
[NotNull] AssemblyDefinition moduleAssembly,
bool isOnlyReferenced,
[CanBeNull] IEnumerable<AssemblyNameReference> moduleReferences
bool isOnlyReferenced
)
{
var references = moduleReferences?.Select(reference => reference.Name).ToList();
var references = moduleAssembly.MainModule.AssemblyReferences.Select(reference => reference.Name).ToList();

if (!_assemblyRegistry.ContainsAssembly(moduleAssembly.Name.FullName))
{
Expand Down
6 changes: 3 additions & 3 deletions ArchUnitNET/Loader/ArchLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void LoadModule(
var processedAssemblies = new List<AssemblyNameReference> { module.Assembly.Name };
var resolvedModules = new List<ModuleDefinition>();
_assemblyResolver.AddLib(module.Assembly);
_archBuilder.AddAssembly(module.Assembly, false, module.AssemblyReferences);
_archBuilder.AddAssembly(module.Assembly, false);
foreach (var assemblyReference in module.AssemblyReferences)
{
if (includeDependencies && recursive)
Expand All @@ -157,7 +157,7 @@ private void LoadModule(
var assemblyDefinition =
_assemblyResolver.Resolve(assemblyReference)
?? throw new AssemblyResolutionException(assemblyReference);
_archBuilder.AddAssembly(assemblyDefinition, false, null);
_archBuilder.AddAssembly(assemblyDefinition, false);
resolvedModules.AddRange(assemblyDefinition.Modules);
}
}
Expand Down Expand Up @@ -203,7 +203,7 @@ FilterFunc filterFunc
var filterResult = filterFunc?.Invoke(assemblyDefinition);
if (filterResult?.LoadThisAssembly != false)
{
_archBuilder.AddAssembly(assemblyDefinition, false, null);
_archBuilder.AddAssembly(assemblyDefinition, false);
resolvedModules.AddRange(assemblyDefinition.Modules);
}

Expand Down
16 changes: 14 additions & 2 deletions ArchUnitNETTests/Loader/ArchLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ public void SameFullNameInMultipleAssemblies()
[Fact]
public void LoadAssembliesIncludingRecursiveDependencies()
{
var archUnitNetTestArchitectureWithRecursiveDependencies = new ArchLoader()
var architecture = new ArchLoader()
.LoadAssembliesIncludingDependencies(new[] { typeof(BaseClass).Assembly }, true)
.Build();

Assert.True(
archUnitNetTestArchitectureWithRecursiveDependencies.Assemblies.Count() > 100
architecture.Assemblies.Count() > 100
);

// Check for well-known assemblies, that ReferencedAssemblyNames is not empty
Assert.All(
architecture.Assemblies.Where(x => x.Name.StartsWith("ArchUnit")),
x => Assert.NotEmpty(x.ReferencedAssemblyNames)
);
}

Expand All @@ -63,6 +69,12 @@ public void LoadAssembliesRecursivelyWithCustomFilter()
.Build();

Assert.Equal(3, architecture.Assemblies.Count());

// Check that ReferencedAssemblyNames is not empty
Assert.All(
architecture.Assemblies,
x => Assert.NotEmpty(x.ReferencedAssemblyNames)
);
}

[Fact]
Expand Down

0 comments on commit fbdec12

Please sign in to comment.