internal ICollection GetTests()

in src/Adapter/MSTest.CoreAdapter/Discovery/AssemblyEnumeratorWrapper.cs [33:102]


        internal ICollection<UnitTestElement> GetTests(string assemblyFileName, IRunSettings runSettings, out ICollection<string> warnings)
        {
            warnings = new List<string>();

            if (string.IsNullOrEmpty(assemblyFileName))
            {
                return null;
            }

            var fullFilePath = PlatformServiceProvider.Instance.FileOperations.GetFullFilePath(assemblyFileName);

            try
            {
                if (!PlatformServiceProvider.Instance.FileOperations.DoesFileExist(fullFilePath))
                {
                    var message = string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_FileDoesNotExist, fullFilePath);
                    throw new FileNotFoundException(message);
                }

                if (!PlatformServiceProvider.Instance.TestSource.IsAssemblyReferenced(UnitTestFrameworkAssemblyName, fullFilePath))
                {
                    return null;
                }

                // Load the assembly in isolation if required.
                return this.GetTestsInIsolation(fullFilePath, runSettings, out warnings);
            }
            catch (FileNotFoundException ex)
            {
                var message = string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_AssemblyDiscoveryFailure, fullFilePath, ex.Message);
                PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning($"{nameof(AssemblyEnumeratorWrapper)}.{nameof(this.GetTests)}: {Resource.TestAssembly_AssemblyDiscoveryFailure}", fullFilePath, ex);
                warnings.Add(message);

                return null;
            }
            catch (ReflectionTypeLoadException ex)
            {
                var message = string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_AssemblyDiscoveryFailure, fullFilePath, ex.Message);
                PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning($"{nameof(AssemblyEnumeratorWrapper)}.{nameof(this.GetTests)}: {Resource.TestAssembly_AssemblyDiscoveryFailure}", fullFilePath, ex);
                PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning(Resource.ExceptionsThrown);
                warnings.Add(message);

                if (ex.LoaderExceptions != null)
                {
                    foreach (var loaderEx in ex.LoaderExceptions)
                    {
                        PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning("{0}", loaderEx);
                    }
                }

                return null;
            }
            catch (BadImageFormatException)
            {
                // Ignore BadImageFormatException when loading native dll in managed adapter.
                return null;
            }
            catch (Exception ex)
            {
                // Catch all exceptions, if discoverer fails to load the dll then let caller continue with other sources.
                // Discover test doesn't work if there is a managed C++ project in solution
                // Assembly.Load() fails to load the managed cpp executable, with FileLoadException. It can load the dll
                // successfully though. This is known CLR issue.
                PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning($"{nameof(AssemblyEnumeratorWrapper)}.{nameof(this.GetTests)}: {Resource.TestAssembly_AssemblyDiscoveryFailure}", fullFilePath, ex);
                var message = ex is FileNotFoundException fileNotFoundEx ? fileNotFoundEx.Message : string.Format(CultureInfo.CurrentCulture, Resource.TestAssembly_AssemblyDiscoveryFailure, fullFilePath, ex.Message);

                warnings.Add(message);
                return null;
            }
        }