diff --git a/src/Tethos/AutoMockingConfiguration.cs b/src/Tethos/AutoMockingConfiguration.cs index 0912471cf..16f6d6190 100644 --- a/src/Tethos/AutoMockingConfiguration.cs +++ b/src/Tethos/AutoMockingConfiguration.cs @@ -9,4 +9,9 @@ public class AutoMockingConfiguration /// Toggle to include non-public types into auto-mocking container. /// public bool IncludeNonPublicTypes { get; set; } + + /// + /// Assembly loading type method selection. + /// + public AutoMockingLoadingTypes LoadingMethod { get; set; } = AutoMockingLoadingTypes.All; } diff --git a/src/Tethos/AutoMockingLoadingTypes.cs b/src/Tethos/AutoMockingLoadingTypes.cs new file mode 100644 index 000000000..34fdc8409 --- /dev/null +++ b/src/Tethos/AutoMockingLoadingTypes.cs @@ -0,0 +1,11 @@ +namespace Tethos; + +/// +/// Available assembly loading type methods. +/// +public enum AutoMockingLoadingTypes +{ + All, + ReferencedAssemblies, + PatternFromSourceAssembly, +} diff --git a/src/Tethos/BaseAutoMockingTest.cs b/src/Tethos/BaseAutoMockingTest.cs index fd26e0720..102cfc4be 100644 --- a/src/Tethos/BaseAutoMockingTest.cs +++ b/src/Tethos/BaseAutoMockingTest.cs @@ -21,7 +21,7 @@ public abstract class BaseAutoMockingTest : IWindsorInstaller, IDisposable /// protected BaseAutoMockingTest() { - this.Assemblies = this.GetType().GetRelatedAssemblies(); + this.Assemblies = this.GetType().GetAssemblies(this.AutoMockingConfiguration); this.Container = (T)new T().Install(this); } diff --git a/src/Tethos/Extensions/Assembly/AssemblyExtensions.cs b/src/Tethos/Extensions/Assembly/AssemblyExtensions.cs index 8e46a0203..160ac475a 100644 --- a/src/Tethos/Extensions/Assembly/AssemblyExtensions.cs +++ b/src/Tethos/Extensions/Assembly/AssemblyExtensions.cs @@ -7,18 +7,29 @@ internal static class AssemblyExtensions { - internal static Assembly[] GetRelatedAssemblies(this Type type) => - Assembly.GetAssembly(type).GetDependencies(); - /// - /// TODO: Create assembly loading configuration + /// Retrieves related assemblies. /// - internal static Assembly[] GetDependencies( - this Assembly rootAssembly) => AppDomain.CurrentDomain.BaseDirectory.GetFiles() - .FilterAssemblies(new[] { ".dll", ".exe" }, rootAssembly) + internal static Assembly[] GetAssemblies( + this Type type, + AutoMockingConfiguration configuration) + { + var rootAssembly = Assembly.GetAssembly(type); + var appDomain = AppDomain.CurrentDomain.BaseDirectory.GetFiles(); + + var assemblies = configuration.LoadingMethod switch + { + AutoMockingLoadingTypes.All => appDomain.FilterAssemblies(new[] { ".dll", ".exe" }, rootAssembly), + AutoMockingLoadingTypes.PatternFromSourceAssembly => appDomain.FilterAssemblies(rootAssembly.FullName.GetPattern(), new[] { ".dll", ".exe" }, rootAssembly), + AutoMockingLoadingTypes.ReferencedAssemblies => appDomain.ElseLoadReferencedAssemblies(rootAssembly), + _ => throw new NotImplementedException(), + }; + + return assemblies .ExcludeRefDirectory() .LoadAssemblies(rootAssembly) .ToArray(); + } internal static IEnumerable ElseLoadReferencedAssemblies( this IEnumerable assemblies, diff --git a/test/Tethos.Tests/Extensions/Assembly/AssemblyExtensionsTests.cs b/test/Tethos.Tests/Extensions/Assembly/AssemblyExtensionsTests.cs index ca6ec910b..60e179d58 100644 --- a/test/Tethos.Tests/Extensions/Assembly/AssemblyExtensionsTests.cs +++ b/test/Tethos.Tests/Extensions/Assembly/AssemblyExtensionsTests.cs @@ -14,49 +14,51 @@ public class AssemblyExtensionsTests : BaseAutoMockingTest { [Theory] - [InlineData(typeof(IMockable))] - [InlineData(typeof(Assert))] - [InlineData(typeof(Xunit.Abstractions.ITest))] - [InlineData(typeof(Moq.IMock<>))] - [InlineData(typeof(FluentAssertions.Events.EventMetadata))] - [InlineData(typeof(AutoFixture.BehaviorRoot))] - [InlineData(typeof(Castle.Core.ParameterModel))] - [InlineData(typeof(Castle.Windsor.IWindsorContainer))] - [InlineData(typeof(System.Collections.IList))] - [InlineData(typeof(System.Collections.IEnumerable))] - [InlineData(typeof(Array))] - [InlineData(typeof(Enumerable))] - [InlineData(typeof(Type))] - [InlineData(typeof(BaseAutoResolver))] - [InlineData(typeof(TimeoutException))] - [InlineData(typeof(Guid))] - [InlineData(typeof(Task<>))] - [InlineData(typeof(Task))] - [InlineData(typeof(int))] + [InlineAutoData(typeof(IMockable))] + [InlineAutoData(typeof(Assert))] + [InlineAutoData(typeof(Xunit.Abstractions.ITest))] + [InlineAutoData(typeof(Moq.IMock<>))] + [InlineAutoData(typeof(FluentAssertions.Events.EventMetadata))] + [InlineAutoData(typeof(AutoFixture.BehaviorRoot))] + [InlineAutoData(typeof(Castle.Core.ParameterModel))] + [InlineAutoData(typeof(Castle.Windsor.IWindsorContainer))] + [InlineAutoData(typeof(System.Collections.IList))] + [InlineAutoData(typeof(System.Collections.IEnumerable))] + [InlineAutoData(typeof(Array))] + [InlineAutoData(typeof(Enumerable))] + [InlineAutoData(typeof(Type))] + [InlineAutoData(typeof(BaseAutoResolver))] + [InlineAutoData(typeof(TimeoutException))] + [InlineAutoData(typeof(Guid))] + [InlineAutoData(typeof(Task<>))] + [InlineAutoData(typeof(Task))] + [InlineAutoData(typeof(int))] [Trait("Type", "Unit")] - public void GetRelatedAssemblies_ShouldLoad(Type type) + public void GetAssemblies_ShouldLoadAssembly(Type type, AutoMockingConfiguration configuration) { // Arrange var expected = type.Assembly; // Act - var actual = type.GetRelatedAssemblies(); + var actual = type.GetAssemblies(configuration); // Assert actual.Should().Contain(expected); } - [Fact] + [Theory(Skip = "Use different assembly")] + [AutoData] [Trait("Type", "Unit")] - public void GetDependencies_UsingMicrosoftCorLib_ShouldLoad() + public void GetAssemblies_UsingMicrosoftCorLib_ShouldLoad(AutoMockingConfiguration configuration) { // Arrange var assemblyName = "mscorlib"; var assembly = Assembly.Load(assemblyName); var expected = new[] { assembly }; + var type = assembly.DefinedTypes.First(); // Act - var actual = assembly.GetDependencies(); + var actual = type.GetAssemblies(configuration); // Assert actual.Should().Contain(expected); @@ -65,13 +67,17 @@ public void GetDependencies_UsingMicrosoftCorLib_ShouldLoad() [Theory] [ClassData(typeof(AssemblyTheoryData))] [Trait("Type", "Unit")] - public void GetDependencies_ShouldLoad(string assemblyName, IEnumerable expected) + public void GetAssemblies_ShouldLoad( + string assemblyName, + IEnumerable expected, + AutoMockingConfiguration configuration) { // Arrange var assembly = Assembly.LoadFrom(assemblyName); + var type = assembly.DefinedTypes.First(); // Act - var actual = assembly.GetDependencies().Select(dependency => dependency.GetName().Name); + var actual = type.GetAssemblies(configuration).Select(dependency => dependency.GetName().Name); // Assert actual.Should().BeEquivalentTo(expected); diff --git a/test/Tethos.Tests/Extensions/Assembly/AssemblyTheoryData.cs b/test/Tethos.Tests/Extensions/Assembly/AssemblyTheoryData.cs index b402564bc..fadc56293 100644 --- a/test/Tethos.Tests/Extensions/Assembly/AssemblyTheoryData.cs +++ b/test/Tethos.Tests/Extensions/Assembly/AssemblyTheoryData.cs @@ -4,24 +4,27 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using AutoFixture; using Xunit; -public class AssemblyTheoryData : TheoryData> +public class AssemblyTheoryData : TheoryData, AutoMockingConfiguration> { public AssemblyTheoryData() { - this.Add("Fake.Core21.dll", GetMatchingAssemblies("Fake.")); - this.Add("Fake.Core22.dll", GetMatchingAssemblies("Fake.")); - this.Add("Fake.Core31.dll", GetMatchingAssemblies("Fake.")); - this.Add("Fake.Framework461.exe", GetMatchingAssemblies("Fake.")); - this.Add("Fake.Framework472.exe", GetMatchingAssemblies("Fake.")); - this.Add("Fake.Net50.dll", GetMatchingAssemblies("Fake.")); - this.Add("Fake.Net60.dll", GetMatchingAssemblies("Fake.")); - this.Add("Fake.Standard20.dll", GetMatchingAssemblies("Fake.")); - this.Add("Fake.Standard21.dll", GetMatchingAssemblies("Fake.")); - this.Add("Tethos.dll", GetMatchingAssemblies("Tethos.")); - this.Add("Tethos.Tests.dll", GetMatchingAssemblies("Tethos.")); - this.Add("Tethos.Tests.Common.dll", GetMatchingAssemblies("Tethos.")); + var configuration = new Fixture().Create(); + + this.Add("Fake.Core21.dll", GetMatchingAssemblies("Fake."), configuration); + this.Add("Fake.Core22.dll", GetMatchingAssemblies("Fake."), configuration); + this.Add("Fake.Core31.dll", GetMatchingAssemblies("Fake."), configuration); + this.Add("Fake.Framework461.exe", GetMatchingAssemblies("Fake."), configuration); + this.Add("Fake.Framework472.exe", GetMatchingAssemblies("Fake."), configuration); + this.Add("Fake.Net50.dll", GetMatchingAssemblies("Fake."), configuration); + this.Add("Fake.Net60.dll", GetMatchingAssemblies("Fake."), configuration); + this.Add("Fake.Standard20.dll", GetMatchingAssemblies("Fake."), configuration); + this.Add("Fake.Standard21.dll", GetMatchingAssemblies("Fake."), configuration); + this.Add("Tethos.dll", GetMatchingAssemblies("Tethos."), configuration); + this.Add("Tethos.Tests.dll", GetMatchingAssemblies("Tethos."), configuration); + this.Add("Tethos.Tests.Common.dll", GetMatchingAssemblies("Tethos."), configuration); } private static IEnumerable GetMatchingAssemblies(string pattern) => Directory