/// <summary>
        /// Returns an implementation of <see cref="IFrontController"/> which can be used
        /// for both discovery and execution.
        /// </summary>
        /// <param name="projectAssembly">The test project assembly.</param>
        /// <param name="sourceInformationProvider">The source information provider. If <c>null</c>, uses the default (<see cref="T:Xunit.VisualStudioSourceInformationProvider"/>).</param>
        /// <param name="diagnosticMessageSink">The message sink which receives <see cref="_DiagnosticMessage"/> messages.</param>
        public static IFrontController ForDiscoveryAndExecution(
            XunitProjectAssembly projectAssembly,
            _ISourceInformationProvider?sourceInformationProvider = null,
            _IMessageSink?diagnosticMessageSink = null)
        {
            Guard.ArgumentNotNull(projectAssembly);

            var innerController  = default(IFrontController);
            var assemblyFileName = projectAssembly.AssemblyFileName;
            var assemblyFolder   = Path.GetDirectoryName(assemblyFileName);

            if (diagnosticMessageSink == null)
            {
                diagnosticMessageSink = _NullMessageSink.Instance;
            }

#if NETFRAMEWORK
            if (sourceInformationProvider == null)
            {
                if (assemblyFileName == null)
                {
                    sourceInformationProvider = _NullSourceInformationProvider.Instance;
                }
                else
                {
                    sourceInformationProvider = new VisualStudioSourceInformationProvider(assemblyFileName, diagnosticMessageSink);
                }
            }

            if (assemblyFolder != null)
            {
                if (Directory.EnumerateFiles(assemblyFolder, "xunit.execution.*.dll").Any())
                {
                    innerController = Xunit2.ForDiscoveryAndExecution(projectAssembly, sourceInformationProvider, diagnosticMessageSink);
                }
                else
                {
                    var xunitPath = Path.Combine(assemblyFolder, "xunit.dll");
                    if (File.Exists(xunitPath))
                    {
                        innerController = Xunit1.ForDiscoveryAndExecution(projectAssembly, sourceInformationProvider, diagnosticMessageSink);
                    }
                }
            }
#else
            if (sourceInformationProvider == null)
            {
                sourceInformationProvider = _NullSourceInformationProvider.Instance;
            }

            innerController = Xunit2.ForDiscoveryAndExecution(projectAssembly, sourceInformationProvider, diagnosticMessageSink);
#endif

            if (innerController == null)
            {
                throw new InvalidOperationException($"Unknown test framework: could not find xunit.dll (v1) or xunit.execution.*.dll (v2) in {assemblyFolder ?? "<unknown assembly folder>"}");
            }

            return(new XunitFrontController(innerController));
        }
        // Factory methods

        /// <summary>
        /// Returns an implementation of <see cref="IFrontControllerDiscoverer"/> which can be
        /// used to discovery tests, including source-based discovery (note that xUnit.net v1
        /// does not support source-based discovery).
        /// </summary>
        /// <param name="assemblyInfo">The assembly to use for discovery</param>
        /// <param name="projectAssembly">The test project assembly.</param>
        /// <param name="referenceList">The full path names of all referenced assemblies. This is used to
        /// search for references to specific xUnit.net reference assemblies to determine which version
        /// of xUnit.net the tests were written against.</param>
        /// <param name="sourceInformationProvider">The optional source information provider.</param>
        /// <param name="diagnosticMessageSink">The message sink which receives <see cref="_DiagnosticMessage"/> messages.</param>
        /// <returns></returns>
        public static IFrontControllerDiscoverer ForDiscovery(
            _IAssemblyInfo assemblyInfo,
            XunitProjectAssembly projectAssembly,
            IReadOnlyCollection <string> referenceList,
            _ISourceInformationProvider?sourceInformationProvider = null,
            _IMessageSink?diagnosticMessageSink = null)
        {
            Guard.ArgumentNotNull(assemblyInfo);
            Guard.ArgumentNotNull(projectAssembly);
            Guard.ArgumentNotNull(referenceList);

            var innerDiscoverer  = default(IFrontControllerDiscoverer);
            var assemblyFileName = projectAssembly.AssemblyFileName;

            if (diagnosticMessageSink == null)
            {
                diagnosticMessageSink = _NullMessageSink.Instance;
            }

            if (sourceInformationProvider == null)
            {
                sourceInformationProvider = _NullSourceInformationProvider.Instance;
#if NETFRAMEWORK
                if (assemblyFileName != null)
                {
                    sourceInformationProvider = new VisualStudioSourceInformationProvider(assemblyFileName, diagnosticMessageSink);
                }
#endif
            }

            var v2PathPattern        = new Regex(@"^xunit\.execution\..*\.dll$");
            var v2ExecutionReference = referenceList.FirstOrDefault(reference => v2PathPattern.IsMatch(Path.GetFileNameWithoutExtension(reference)));
            if (v2ExecutionReference != null)
            {
                innerDiscoverer = Xunit2.ForDiscovery(assemblyInfo, projectAssembly, v2ExecutionReference, sourceInformationProvider, diagnosticMessageSink);
            }

#if NETFRAMEWORK
            if (referenceList.Any(reference => Path.GetFileNameWithoutExtension(reference) == "xunit.dll"))
            {
                innerDiscoverer = Xunit1.ForDiscoveryAndExecution(projectAssembly, sourceInformationProvider, diagnosticMessageSink);
            }
#endif

            if (innerDiscoverer == null)
            {
                throw new InvalidOperationException($"Unknown test framework: could not find xunit.dll (v1) or xunit.execution.*.dll (v2) in assembly reference list");
            }

            return(new XunitFrontController(innerDiscoverer));
        }
Exemple #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XunitFrontController"/> class.
        /// </summary>
        /// <param name="assemblyFileName">The test assembly.</param>
        /// <param name="configFileName">The test assembly configuration file.</param>
        /// <param name="shadowCopy">If set to <c>true</c>, runs tests in a shadow copied app domain, which allows
        /// tests to be discovered and run without locking assembly files on disk.</param>
        public XunitFrontController(string assemblyFileName, string configFileName = null, bool shadowCopy = true)
        {
            Guard.FileExists("assemblyFileName", assemblyFileName);
            var xunit1Path = Path.Combine(Path.GetDirectoryName(assemblyFileName), "xunit.dll");
            var xunit2Path = Path.Combine(Path.GetDirectoryName(assemblyFileName), "xunit2.dll");
            var sourceInformationProvider = new VisualStudioSourceInformationProvider();

            if (File.Exists(xunit2Path))
                innerController = new Xunit2(sourceInformationProvider, assemblyFileName, configFileName, shadowCopy);
            else if (File.Exists(xunit1Path))
                innerController = new Xunit1(sourceInformationProvider, assemblyFileName, configFileName, shadowCopy);
            else
                throw new ArgumentException("Unknown test framework: Could not find xunit.dll or xunit2.dll.", assemblyFileName);
        }