private bool TryFindStartupType(IReflectionAssemblyInfo assembly, out Type type)
        {
            if (!string.IsNullOrEmpty(_typeName))
            {
                var asm = string.IsNullOrEmpty(_assemblyName) ? assembly.Assembly : Assembly.Load(_assemblyName);
                if (asm == null)
                {
                    throw new InvalidOperationException($"Could not load Assembly {_assemblyName}");
                }

                type = asm.GetType(_typeName);
                return(type != null);
            }

            // Find the TestStartup class
            foreach (var typ in assembly.GetTypes(includePrivateTypes: false))
            {
                if (typ.ToRuntimeType().Name.Equals("TestStartup"))
                {
                    // Found a match, use it.
                    type = typ.ToRuntimeType();
                    return(true);
                }
            }

            type = null;
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the unique ID for the test case.
        /// </summary>
        protected virtual string GetUniqueID()
        {
            using (var stream = new MemoryStream())
            {
                var assemblyName = TestMethod.TestClass.TestCollection.TestAssembly.Assembly.Name;

                //Get just the assembly name (without version info) when obtained by reflection
                IReflectionAssemblyInfo assembly = TestMethod.TestClass.TestCollection.TestAssembly.Assembly as IReflectionAssemblyInfo;
                if (assembly != null)
                {
                    assemblyName = assembly.Assembly.GetName().Name;
                }

                Write(stream, assemblyName);
                Write(stream, TestMethod.TestClass.Class.Name);
                Write(stream, TestMethod.Method.Name);

                if (TestMethodArguments != null)
                {
                    Write(stream, SerializationHelper.Serialize(TestMethodArguments));
                }

                var genericTypes = MethodGenericTypes;
                if (genericTypes != null)
                {
                    for (var idx = 0; idx < genericTypes.Length; idx++)
                    {
                        Write(stream, TypeUtility.ConvertToSimpleTypeName(genericTypes[idx]));
                    }
                }

                stream.Position = 0;

                var hash = new byte[20];
                var data = stream.ToArray();

                var hasher = new Sha1Digest();
                hasher.BlockUpdate(data, 0, data.Length);
                hasher.DoFinal(hash, 0);

                return(BytesToHexString(hash));
            }
        }
 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);
 }
Ejemplo n.º 4
0
 public bool TryCreateInjectionControllerForAssembly(ExceptionAggregator aggregator, IReflectionAssemblyInfo assembly, out IXunitInjectionController controller)
 {
     controller = new DefaultConstructorInjectionController();
     return(true);
 }
Ejemplo n.º 5
0
 public SpecFlowProjectReflectionAssemblyInfo(IReflectionAssemblyInfo originalAssemblyInfo)
     : base(originalAssemblyInfo)
 {
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 8
0
 public bool TryCreateInjectionControllerForAssembly(ExceptionAggregator aggregator, IReflectionAssemblyInfo assembly, out IXunitInjectionController controller)
 {
     controller = DefaultXunitInjectionController.Instance;
     return(true);
 }