From 7bb008e642ea1dd495fd68007c390463d03e9828 Mon Sep 17 00:00:00 2001 From: made-by-karl Date: Fri, 25 Oct 2024 18:41:50 +0200 Subject: [PATCH] fill the ReferencedAssemblyNames property for all loaded assemblies Fixes issue #311 Signed-off-by: Karl Rhenius --- ArchUnitNET/Domain/Assembly.cs | 4 +--- ArchUnitNET/Loader/ArchBuilder.cs | 5 ++--- ArchUnitNET/Loader/ArchLoader.cs | 6 +++--- ArchUnitNETTests/Loader/ArchLoaderTests.cs | 16 ++++++++++++++-- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/ArchUnitNET/Domain/Assembly.cs b/ArchUnitNET/Domain/Assembly.cs index 6abcca9a..910da03b 100644 --- a/ArchUnitNET/Domain/Assembly.cs +++ b/ArchUnitNET/Domain/Assembly.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Linq; -using JetBrains.Annotations; namespace ArchUnitNET.Domain { @@ -16,7 +15,7 @@ public Assembly( string name, string fullName, bool isOnlyReferenced, - [CanBeNull] List referencedAssemblyNames + List referencedAssemblyNames ) { Name = name; @@ -29,7 +28,6 @@ [CanBeNull] List referencedAssemblyNames public string Name { get; } - [CanBeNull] public List ReferencedAssemblyNames { get; } public string FullName { get; } diff --git a/ArchUnitNET/Loader/ArchBuilder.cs b/ArchUnitNET/Loader/ArchBuilder.cs index eecf9549..c2353812 100644 --- a/ArchUnitNET/Loader/ArchBuilder.cs +++ b/ArchUnitNET/Loader/ArchBuilder.cs @@ -50,11 +50,10 @@ public ArchBuilder() public void AddAssembly( [NotNull] AssemblyDefinition moduleAssembly, - bool isOnlyReferenced, - [CanBeNull] IEnumerable 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)) { diff --git a/ArchUnitNET/Loader/ArchLoader.cs b/ArchUnitNET/Loader/ArchLoader.cs index e1046214..1d178cee 100644 --- a/ArchUnitNET/Loader/ArchLoader.cs +++ b/ArchUnitNET/Loader/ArchLoader.cs @@ -134,7 +134,7 @@ private void LoadModule( var processedAssemblies = new List { module.Assembly.Name }; var resolvedModules = new List(); _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) @@ -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); } } @@ -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); } diff --git a/ArchUnitNETTests/Loader/ArchLoaderTests.cs b/ArchUnitNETTests/Loader/ArchLoaderTests.cs index a18d12be..b9c70929 100644 --- a/ArchUnitNETTests/Loader/ArchLoaderTests.cs +++ b/ArchUnitNETTests/Loader/ArchLoaderTests.cs @@ -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) ); } @@ -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]