Example #1
0
 public bool TryCreateInjectionControllerForAssembly(ExceptionAggregator aggregator, IReflectionAssemblyInfo assembly, out IXunitInjectionController controller)
 {
     controller = new DefaultConstructorInjectionController();
     return(true);
 }
Example #2
0
 public bool TryCreateInjectionControllerForType(ExceptionAggregator aggregator, IReflectionTypeInfo type, out IXunitInjectionController controller)
 {
     controller = new DefaultConstructorInjectionController();
     return(true);
 }
Example #3
0
 public bool TryCreateInjectionControllerForType(ExceptionAggregator aggregator, IReflectionTypeInfo type, out IXunitInjectionController controller) => TryCreateInjectionControllerCore(aggregator, out controller);
Example #4
0
        private bool TryCreateInjectionControllerCore(ExceptionAggregator aggregator, out IXunitInjectionController controller)
        {
            var asm = Assembly.Load(_assemblyName);

            if (asm == null)
            {
                aggregator.Add(new InvalidOperationException($"Cannot load assembly: {_assemblyName}"));
                controller = null;
                return(false);
            }

            var typ = asm.GetType(_typeName);

            if (typ == null)
            {
                aggregator.Add(new InvalidOperationException($"Cannot find type: {_typeName}"));
                controller = null;
                return(false);
            }

            var method = typ.GetMethod(_methodName, BindingFlags.Static | BindingFlags.Public, Type.DefaultBinder, Type.EmptyTypes, new ParameterModifier[0]);

            if (method == null)
            {
                aggregator.Add(new InvalidOperationException($"Cannot find public static parameter-less method: {_methodName}"));
                controller = null;
                return(false);
            }

            if (method.ReturnType != typeof(IServiceProvider))
            {
                aggregator.Add(new InvalidOperationException($"{_methodName} does not return an {nameof(IServiceProvider)}"));
                controller = null;
                return(false);
            }

            // Invoke the method
            var result = method.Invoke(null, new object[0]);

            if (result == null)
            {
                aggregator.Add(new InvalidOperationException($"{_methodName} returned null"));
                controller = null;
                return(false);
            }

            controller = new ServiceProviderXunitInjectionController((IServiceProvider)result);
            return(true);
        }
 public bool TryCreateInjectionControllerForType(ExceptionAggregator aggregator, IReflectionTypeInfo type, out IXunitInjectionController controller)
 {
     controller = null;
     return(false);
 }
Example #6
0
 public bool TryCreateInjectionControllerForAssembly(ExceptionAggregator aggregator, IReflectionAssemblyInfo assembly, out IXunitInjectionController controller) => TryCreateInjectionControllerCore(aggregator, out controller);
        public bool TryCreateInjectionControllerForAssembly(ExceptionAggregator aggregator, IReflectionAssemblyInfo assembly, out IXunitInjectionController controller)
        {
            if (TryFindStartupType(assembly, out var type))
            {
                // TODO: Use generic host?

                // Check for a ConfigureServices method
                var configureServicesMethod = type.GetMethod("ConfigureServices", BindingFlags.Public | BindingFlags.Instance, Type.DefaultBinder, new[] { typeof(IServiceCollection) }, new ParameterModifier[0]);
                if (configureServicesMethod == null)
                {
                    aggregator.Add(new InvalidOperationException($"Could not find appropriate ConfigureServices method on TestStartup type {type.FullName}"));
                    controller = null;
                    return(false);
                }

                // Create the service collection
                var services = new ServiceCollection();

                try
                {
                    // Activate the type
                    var startup = Activator.CreateInstance(type);

                    // Configure the services
                    configureServicesMethod.Invoke(startup, new object[] { services });
                }
                catch (TargetInvocationException tex)
                {
                    aggregator.Add(tex.InnerException);
                    controller = null;
                    return(false);
                }
                catch (Exception ex)
                {
                    aggregator.Add(ex);
                    controller = null;
                    return(false);
                }

                // Create a ServiceProvider-based controller from the service provider
                controller = new ServiceProviderXunitInjectionController(services.BuildServiceProvider());
                return(true);
            }

            aggregator.Add(new InvalidOperationException("Could not find TestStartup class in target assembly"));
            controller = null;
            return(false);
        }
Example #8
0
 public bool TryCreateInjectionControllerForType(ExceptionAggregator aggregator, IReflectionTypeInfo type, out IXunitInjectionController controller)
 {
     controller = DefaultXunitInjectionController.Instance;
     return(true);
 }
Example #9
0
 public bool TryCreateInjectionControllerForAssembly(ExceptionAggregator aggregator, IReflectionAssemblyInfo assembly, out IXunitInjectionController controller)
 {
     controller = DefaultXunitInjectionController.Instance;
     return(true);
 }
 private bool TryCreateController(IEnumerable <Attribute> attributes, IReflectionAssemblyInfo assembly, IReflectionTypeInfo type, out IXunitInjectionController controller)
 {
     foreach (var attribute in attributes)
     {
         if (attribute is IXunitInjectionControllerAttribute controllerAttribute)
         {
             if (assembly != null)
             {
                 return(controllerAttribute.TryCreateInjectionControllerForAssembly(Aggregator, assembly, out controller));
             }
             else
             {
                 return(controllerAttribute.TryCreateInjectionControllerForType(Aggregator, type, out controller));
             }
         }
     }
     controller = null;
     return(false);
 }
Example #11
0
        private bool TryCreateInjectionControllerCore(ExceptionAggregator aggregator, out IXunitInjectionController controller)
        {
            var asm = Assembly.Load(_assemblyName);

            if (asm == null)
            {
                aggregator.Add(new InvalidOperationException($"Cannot load assembly: {_assemblyName}"));
                controller = null;
                return(false);
            }

            var typ = asm.GetType(_typeName);

            if (typ == null)
            {
                aggregator.Add(new InvalidOperationException($"Cannot find type: {_typeName}"));
                controller = null;
                return(false);
            }

            controller = (IXunitInjectionController)Activator.CreateInstance(typ);
            return(true);
        }