Remove Unity (No longer maintained)
This commit is contained in:
parent
908d79f2f6
commit
173f456683
|
@ -4,7 +4,7 @@
|
||||||
<DefineConstants Condition="'$(RuntimeIdentifier)' == 'linux-musl-x64' or '$(RuntimeIdentifier)' == 'linux-musl-arm64'">ISMUSL</DefineConstants>
|
<DefineConstants Condition="'$(RuntimeIdentifier)' == 'linux-musl-x64' or '$(RuntimeIdentifier)' == 'linux-musl-arm64'">ISMUSL</DefineConstants>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DryIoc.dll" Version="4.8.6" />
|
<PackageReference Include="DryIoc.dll" Version="5.3.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
|
|
|
@ -9,8 +9,8 @@
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.4.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.4.0" />
|
||||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
|
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
|
||||||
<PackageReference Include="DryIoc.dll" Version="4.8.6" />
|
<PackageReference Include="DryIoc.dll" Version="5.3.0" />
|
||||||
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="5.1.0" />
|
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="6.1.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\NzbDrone.Common\Sonarr.Common.csproj" />
|
<ProjectReference Include="..\NzbDrone.Common\Sonarr.Common.csproj" />
|
||||||
|
|
|
@ -3,18 +3,11 @@ using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using DryIoc;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Moq.Language.Flow;
|
|
||||||
using NzbDrone.Common.Composition;
|
using NzbDrone.Common.Composition;
|
||||||
using NzbDrone.Common.EnvironmentInfo;
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
using NzbDrone.Test.Common.AutoMoq.Unity;
|
|
||||||
using Unity;
|
|
||||||
using Unity.Resolution;
|
|
||||||
|
|
||||||
[assembly: InternalsVisibleTo("AutoMoq.Tests")]
|
|
||||||
|
|
||||||
namespace NzbDrone.Test.Common.AutoMoq
|
namespace NzbDrone.Test.Common.AutoMoq
|
||||||
{
|
{
|
||||||
|
@ -22,32 +15,18 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||||
public class AutoMoqer
|
public class AutoMoqer
|
||||||
{
|
{
|
||||||
public readonly MockBehavior DefaultBehavior = MockBehavior.Default;
|
public readonly MockBehavior DefaultBehavior = MockBehavior.Default;
|
||||||
public Type ResolveType;
|
private IContainer _container;
|
||||||
private IUnityContainer _container;
|
|
||||||
private IDictionary<Type, object> _registeredMocks;
|
private IDictionary<Type, object> _registeredMocks;
|
||||||
|
|
||||||
public AutoMoqer()
|
public AutoMoqer()
|
||||||
{
|
{
|
||||||
SetupAutoMoqer(new UnityContainer());
|
SetupAutoMoqer(CreateTestContainer(new Container()));
|
||||||
}
|
|
||||||
|
|
||||||
public AutoMoqer(MockBehavior defaultBehavior)
|
|
||||||
{
|
|
||||||
DefaultBehavior = defaultBehavior;
|
|
||||||
SetupAutoMoqer(new UnityContainer());
|
|
||||||
}
|
|
||||||
|
|
||||||
public AutoMoqer(IUnityContainer container)
|
|
||||||
{
|
|
||||||
SetupAutoMoqer(container);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual T Resolve<T>()
|
public virtual T Resolve<T>()
|
||||||
{
|
{
|
||||||
ResolveType = typeof(T);
|
|
||||||
var result = _container.Resolve<T>();
|
var result = _container.Resolve<T>();
|
||||||
SetConstant(result);
|
SetConstant(result);
|
||||||
ResolveType = null;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +39,6 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||||
public virtual Mock<T> GetMock<T>(MockBehavior behavior)
|
public virtual Mock<T> GetMock<T>(MockBehavior behavior)
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
ResolveType = null;
|
|
||||||
var type = GetTheMockType<T>();
|
var type = GetTheMockType<T>();
|
||||||
if (GetMockHasNotBeenCalledForThisType(type))
|
if (GetMockHasNotBeenCalledForThisType(type))
|
||||||
{
|
{
|
||||||
|
@ -79,89 +57,88 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||||
|
|
||||||
public virtual void SetMock(Type type, Mock mock)
|
public virtual void SetMock(Type type, Mock mock)
|
||||||
{
|
{
|
||||||
if (_registeredMocks.ContainsKey(type) == false)
|
if (GetMockHasNotBeenCalledForThisType(type))
|
||||||
{
|
{
|
||||||
_registeredMocks.Add(type, mock);
|
_registeredMocks.Add(type, mock);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mock != null)
|
if (mock != null)
|
||||||
{
|
{
|
||||||
_container.RegisterInstance(type, mock.Object);
|
_container.RegisterInstance(type, mock.Object, ifAlreadyRegistered: IfAlreadyRegistered.Replace);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void SetConstant<T>(T instance)
|
public virtual void SetConstant<T>(T instance)
|
||||||
{
|
{
|
||||||
_container.RegisterInstance(instance);
|
_container.RegisterInstance(instance, ifAlreadyRegistered: IfAlreadyRegistered.Replace);
|
||||||
SetMock(instance.GetType(), null);
|
SetMock(instance.GetType(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ISetup<T> Setup<T>(Expression<Action<T>> expression)
|
private IContainer CreateTestContainer(IContainer container)
|
||||||
where T : class
|
|
||||||
{
|
{
|
||||||
return GetMock<T>().Setup(expression);
|
var c = container.CreateChild(IfAlreadyRegistered.Replace,
|
||||||
|
container.Rules
|
||||||
|
.WithDynamicRegistration((serviceType, serviceKey) =>
|
||||||
|
{
|
||||||
|
// ignore services with non-default key
|
||||||
|
if (serviceKey != null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ISetup<T, TResult> Setup<T, TResult>(Expression<Func<T, TResult>> expression)
|
if (serviceType == typeof(object))
|
||||||
where T : class
|
|
||||||
{
|
{
|
||||||
return GetMock<T>().Setup(expression);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Verify<T>(Expression<Action<T>> expression)
|
if (serviceType.IsGenericType && serviceType.IsOpenGeneric())
|
||||||
where T : class
|
|
||||||
{
|
{
|
||||||
GetMock<T>().Verify(expression);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Verify<T>(Expression<Action<T>> expression, string failMessage)
|
if (serviceType == typeof(System.Text.Json.Serialization.JsonConverter))
|
||||||
where T : class
|
|
||||||
{
|
{
|
||||||
GetMock<T>().Verify(expression, failMessage);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Verify<T>(Expression<Action<T>> expression, Times times)
|
// get the Mock object for the abstract class or interface
|
||||||
where T : class
|
if (serviceType.IsInterface || serviceType.IsAbstract)
|
||||||
{
|
{
|
||||||
GetMock<T>().Verify(expression, times);
|
var mockType = typeof(Mock<>).MakeGenericType(serviceType);
|
||||||
|
var mockFactory = DelegateFactory.Of(r =>
|
||||||
|
{
|
||||||
|
var mock = (Mock)r.Resolve(mockType);
|
||||||
|
SetMock(serviceType, mock);
|
||||||
|
return mock.Object;
|
||||||
|
}, Reuse.Singleton);
|
||||||
|
|
||||||
|
return new[] { new DynamicRegistration(mockFactory, IfAlreadyRegistered.Keep) };
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Verify<T>(Expression<Action<T>> expression, Times times, string failMessage)
|
// concrete types
|
||||||
where T : class
|
var concreteTypeFactory = serviceType.ToFactory(Reuse.Singleton, FactoryMethod.ConstructorWithResolvableArgumentsIncludingNonPublic);
|
||||||
{
|
|
||||||
GetMock<T>().Verify(expression, times, failMessage);
|
return new[] { new DynamicRegistration(concreteTypeFactory) };
|
||||||
|
},
|
||||||
|
DynamicRegistrationFlags.Service | DynamicRegistrationFlags.AsFallback));
|
||||||
|
|
||||||
|
c.Register(typeof(Mock<>), Reuse.Singleton, FactoryMethod.DefaultConstructor());
|
||||||
|
|
||||||
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void VerifyAllMocks()
|
private void SetupAutoMoqer(IContainer container)
|
||||||
{
|
|
||||||
foreach (var registeredMock in _registeredMocks)
|
|
||||||
{
|
|
||||||
if (registeredMock.Value is Mock mock)
|
|
||||||
{
|
|
||||||
mock.VerifyAll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetupAutoMoqer(IUnityContainer container)
|
|
||||||
{
|
{
|
||||||
_container = container;
|
_container = container;
|
||||||
container.RegisterInstance(this);
|
container.RegisterInstance(this);
|
||||||
|
|
||||||
_registeredMocks = new Dictionary<Type, object>();
|
_registeredMocks = new Dictionary<Type, object>();
|
||||||
|
|
||||||
RegisterPlatformLibrary(container);
|
LoadPlatformLibrary();
|
||||||
AddTheAutoMockingContainerExtensionToTheContainer(container);
|
|
||||||
|
|
||||||
AssemblyLoader.RegisterNativeResolver(new[] { "System.Data.SQLite", "Sonarr.Core" });
|
AssemblyLoader.RegisterNativeResolver(new[] { "System.Data.SQLite", "Sonarr.Core" });
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void AddTheAutoMockingContainerExtensionToTheContainer(IUnityContainer container)
|
|
||||||
{
|
|
||||||
container.AddNewExtension<AutoMockingContainerExtension>();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Mock<T> TheRegisteredMockForThisType<T>(Type type)
|
private Mock<T> TheRegisteredMockForThisType<T>(Type type)
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
|
@ -178,7 +155,7 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||||
|
|
||||||
private bool GetMockHasNotBeenCalledForThisType(Type type)
|
private bool GetMockHasNotBeenCalledForThisType(Type type)
|
||||||
{
|
{
|
||||||
return _registeredMocks.ContainsKey(type) == false;
|
return !_registeredMocks.ContainsKey(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Type GetTheMockType<T>()
|
private static Type GetTheMockType<T>()
|
||||||
|
@ -187,7 +164,7 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||||
return typeof(T);
|
return typeof(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RegisterPlatformLibrary(IUnityContainer container)
|
private void LoadPlatformLibrary()
|
||||||
{
|
{
|
||||||
var assemblyName = "Sonarr.Windows";
|
var assemblyName = "Sonarr.Windows";
|
||||||
|
|
||||||
|
|
|
@ -1,79 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using Moq;
|
|
||||||
using Unity;
|
|
||||||
using Unity.Builder;
|
|
||||||
using Unity.Strategies;
|
|
||||||
|
|
||||||
namespace NzbDrone.Test.Common.AutoMoq.Unity
|
|
||||||
{
|
|
||||||
public class AutoMockingBuilderStrategy : BuilderStrategy
|
|
||||||
{
|
|
||||||
private readonly IUnityContainer _container;
|
|
||||||
private readonly MockRepository _mockFactory;
|
|
||||||
private readonly IEnumerable<Type> _registeredTypes;
|
|
||||||
|
|
||||||
public AutoMockingBuilderStrategy(IEnumerable<Type> registeredTypes, IUnityContainer container)
|
|
||||||
{
|
|
||||||
var autoMoqer = container.Resolve<AutoMoqer>();
|
|
||||||
_mockFactory = new MockRepository(autoMoqer.DefaultBehavior);
|
|
||||||
_registeredTypes = registeredTypes;
|
|
||||||
_container = container;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void PreBuildUp(ref BuilderContext context)
|
|
||||||
{
|
|
||||||
var autoMoqer = _container.Resolve<AutoMoqer>();
|
|
||||||
|
|
||||||
var type = GetTheTypeFromTheBuilderContext(context);
|
|
||||||
if (AMockObjectShouldBeCreatedForThisType(type))
|
|
||||||
{
|
|
||||||
var mock = CreateAMockObject(type);
|
|
||||||
context.Existing = mock.Object;
|
|
||||||
autoMoqer.SetMock(type, mock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool AMockObjectShouldBeCreatedForThisType(Type type)
|
|
||||||
{
|
|
||||||
var mocker = _container.Resolve<AutoMoqer>();
|
|
||||||
return TypeIsNotRegistered(type) && (mocker.ResolveType == null || mocker.ResolveType != type);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Type GetTheTypeFromTheBuilderContext(BuilderContext context)
|
|
||||||
{
|
|
||||||
return context.Type;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool TypeIsNotRegistered(Type type)
|
|
||||||
{
|
|
||||||
return _registeredTypes.Any(x => x.Equals(type)) == false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Mock CreateAMockObject(Type type)
|
|
||||||
{
|
|
||||||
var createMethod = GenerateAnInterfaceMockCreationMethod(type);
|
|
||||||
|
|
||||||
return InvokeTheMockCreationMethod(createMethod);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Mock InvokeTheMockCreationMethod(MethodInfo createMethod)
|
|
||||||
{
|
|
||||||
return (Mock)createMethod.Invoke(_mockFactory, new object[] { new List<object>().ToArray() });
|
|
||||||
}
|
|
||||||
|
|
||||||
private MethodInfo GenerateAnInterfaceMockCreationMethod(Type type)
|
|
||||||
{
|
|
||||||
var createMethodWithNoParameters = _mockFactory.GetType().GetMethod("Create", EmptyArgumentList());
|
|
||||||
|
|
||||||
return createMethodWithNoParameters.MakeGenericMethod(new[] { type });
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Type[] EmptyArgumentList()
|
|
||||||
{
|
|
||||||
return new[] { typeof(object[]) };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,35 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Unity.Builder;
|
|
||||||
using Unity.Extension;
|
|
||||||
|
|
||||||
namespace NzbDrone.Test.Common.AutoMoq.Unity
|
|
||||||
{
|
|
||||||
public class AutoMockingContainerExtension : UnityContainerExtension
|
|
||||||
{
|
|
||||||
private readonly IList<Type> _registeredTypes = new List<Type>();
|
|
||||||
|
|
||||||
protected override void Initialize()
|
|
||||||
{
|
|
||||||
SetEventsOnContainerToTrackAllRegisteredTypes();
|
|
||||||
SetBuildingStrategyForBuildingUnregisteredTypes();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetEventsOnContainerToTrackAllRegisteredTypes()
|
|
||||||
{
|
|
||||||
Context.Registering += (sender, e) => RegisterType(e.TypeFrom);
|
|
||||||
Context.RegisteringInstance += (sender, e) => RegisterType(e.RegisteredType);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void RegisterType(Type typeToRegister)
|
|
||||||
{
|
|
||||||
_registeredTypes.Add(typeToRegister);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetBuildingStrategyForBuildingUnregisteredTypes()
|
|
||||||
{
|
|
||||||
var strategy = new AutoMockingBuilderStrategy(_registeredTypes, Container);
|
|
||||||
Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -9,7 +9,6 @@
|
||||||
<PackageReference Include="NLog" Version="4.7.14" />
|
<PackageReference Include="NLog" Version="4.7.14" />
|
||||||
<PackageReference Include="NUnit" Version="3.13.2" />
|
<PackageReference Include="NUnit" Version="3.13.2" />
|
||||||
<PackageReference Include="RestSharp" Version="106.15.0" />
|
<PackageReference Include="RestSharp" Version="106.15.0" />
|
||||||
<PackageReference Include="Unity" Version="5.11.10" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\NzbDrone.Common\Sonarr.Common.csproj" />
|
<ProjectReference Include="..\NzbDrone.Common\Sonarr.Common.csproj" />
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
<TargetFrameworks>net6.0</TargetFrameworks>
|
<TargetFrameworks>net6.0</TargetFrameworks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DryIoc.dll" Version="4.8.6" />
|
<PackageReference Include="DryIoc.dll" Version="5.3.0" />
|
||||||
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="5.1.0" />
|
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="6.1.0" />
|
||||||
<PackageReference Include="NLog" Version="4.7.14" />
|
<PackageReference Include="NLog" Version="4.7.14" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
Loading…
Reference in New Issue