Adds additional search directories for assembly resolution
Inheritance: IDisposable
        public void AssemblyResolver_NoImpactOnDefaultResolution()
        {
            // Arrange
            TestLogger logger = new TestLogger();
            string testFolder = TestUtils.CreateTestDirectory(this.TestContext);
            CompileSimpleAssembly("SimpleAssembly.dll", testFolder, logger);

            object simpleObject = null;

            // Act
            using (AssemblyResolver resolver = new AssemblyResolver(logger, testFolder))
            {
                // Look in every assembly under the supplied directory
                foreach (string assemblyPath in Directory.GetFiles(testFolder, "*.dll", SearchOption.AllDirectories))
                {
                    Assembly assembly = Assembly.LoadFile(assemblyPath);
                    
                    foreach (Type type in assembly.GetExportedTypes())
                    {
                        if (!type.IsAbstract)
                        {
                            simpleObject = Activator.CreateInstance(type);
                        }
                    }
                }

                // Assert
                Assert.IsNotNull(simpleObject);
                Assert.AreEqual<string>("SimpleProgram", simpleObject.GetType().ToString());
                AssertResolverCaller(resolver);

            }
        }
        /// <summary>
        /// Loads all of the given assemblies and instantiates Roslyn diagnostic objects - i.e. existing types deriving from
        /// <see cref="DiagnosticAnalyzer"/>. Non-assembly files will be ignored.
        /// </summary>
        /// <returns>Enumerable with instances of DiagnosticAnalyzer from discovered assemblies</returns>
        public IEnumerable<DiagnosticAnalyzer> InstantiateDiagnostics(string language, params string[] files)
        {
            // If there were any additional assembly search directories specified in the constructor, use them
            AssemblyResolver additionalAssemblyResolver = null;
            if (additionalSearchFolders.Any())
            {
                additionalAssemblyResolver = new AssemblyResolver(this.logger, additionalSearchFolders.ToArray());
            }

            List<DiagnosticAnalyzer> analyzers = new List<DiagnosticAnalyzer>();

            try
            {
                foreach (string assemblyPath in files.Where(f => Utilities.IsAssemblyLibraryFileName(f)))
                {
                    analyzers.AddRange(InstantiateDiagnosticsFromAssembly(assemblyPath, language));
                }

            }
            finally
            {
                // Dispose of the AssemblyResolver instance, if applicable
                if (additionalAssemblyResolver != null)
                {
                    additionalAssemblyResolver.Dispose();
                }
            }
            return analyzers;
        }
        public void AssemblyResolver_NonExistentAssembly_ResolutionFails()
        {
            // Arrange
            TestLogger logger = new TestLogger();
            string testFolder = TestUtils.CreateTestDirectory(this.TestContext);

            // Act
            using (AssemblyResolver resolver = new AssemblyResolver(logger, testFolder))
            {
                AssertAssemblyLoadFails("nonexistent library");

                // Assert
                AssertResolverCaller(resolver);
            }
        }
 private static void AssertResolverCaller(AssemblyResolver resolver)
 {
     Assert.IsTrue(resolver.ResolverCalled, "Expected the assembly resolver to have been called");
 }
        private Assembly AssertAssemblyLoadSucceedsOnlyWithResolver(string asmRef, string searchPath)
        {
            // Check the assembly load fails without the assembly resolver
            AssertAssemblyLoadFails(asmRef);

            // Act
            Assembly resolveResult;

            // Create a test logger that will only record output from the resolver
            // so we can check it has been called
            using (AssemblyResolver resolver = new AssemblyResolver(new TestLogger(), searchPath))
            {
                resolveResult = Assembly.Load(asmRef);

                // Assert
                AssertResolverCaller(resolver);
            }

            // Assert
            Assert.IsNotNull(resolveResult, "Failed to the load the assembly");

            return resolveResult;
        }
        public void AssemblyResolver_VersionAssemblyRequested()
        {
            // Setup
            string testFolder = TestUtils.CreateTestDirectory(this.TestContext);
            Assembly testAssembly = CompileSimpleAssembly("VersionAsm1.dll", testFolder, new TestLogger(), "2.1.0.4");

            // 1. Search for a version that can be found -> succeeds
            Assembly resolvedAssembly = AssertAssemblyLoadSucceedsOnlyWithResolver("VersionAsm1, Version = 2.1.0.4, Culture = neutral, PublicKeyToken = null", testFolder);
            AssertExpectedAssemblyLoaded(testAssembly, resolvedAssembly);

            // 2. Search for a version that can't be found -> fails
            using (AssemblyResolver resolver = new AssemblyResolver(new TestLogger(), testFolder))
            {
                AssertAssemblyLoadFails("VersionAsm1, Version = 1.0.0.4, Culture = neutral, PublicKeyToken = null");
                AssertResolverCaller(resolver);
            }
        }
        /// <summary>
        /// Load the assembly at the given path into memory, with the given additional assembly search directories.
        /// </summary>
        private Assembly LoadAnalyzerAssembly(string assemblyPath)
        {
            // If there were any additional assembly search directories specified in the constructor, use them
            AssemblyResolver additionalAssemblyResolver = null;
            if (additionalSearchFolders.Any())
            {
                additionalAssemblyResolver = new AssemblyResolver(this.logger, additionalSearchFolders.ToArray());
            }

            Assembly analyzerAssembly = null;
            try
            {
                analyzerAssembly = Assembly.LoadFrom(assemblyPath);
            }
            finally
            {
                // Dispose of the AssemblyResolver instance, if applicable
                if (additionalAssemblyResolver != null)
                {
                    additionalAssemblyResolver.Dispose();
                }
            }

            this.logger.LogInfo(UIResources.Scanner_AssemblyLoadSuccess, analyzerAssembly.FullName);
            return analyzerAssembly;
        }
        public void TestAssemblyResolution()
        {
            // Arrange
            TestLogger logger = new TestLogger();
            string testFolder = TestUtils.CreateTestDirectory(this.TestContext);
            String simpleAssemblyPath = Path.Combine(testFolder, "SimpleAssembly.dll");
            Assembly simpleAssembly = CompileSimpleAssembly(simpleAssemblyPath, logger);
            
            // Act
            Assembly resolveResult;
            using (AssemblyResolver assemblyResolver = new AssemblyResolver(logger, testFolder))
            {
                ResolveEventArgs resolveEventArgs = new ResolveEventArgs(simpleAssembly.FullName, this.GetType().Assembly);
                resolveResult = assemblyResolver.CurrentDomain_AssemblyResolve(this, resolveEventArgs);
            }

            // Assert
            Assert.IsNotNull(resolveResult);
            Assert.AreEqual<string>(simpleAssembly.ToString(), resolveResult.ToString());
            Assert.AreEqual<string>(simpleAssemblyPath, resolveResult.Location);
        }
        public void TestAssemblyResolutionFail()
        {
            // Arrange
            TestLogger logger = new TestLogger();
            string testFolder = TestUtils.CreateTestDirectory(this.TestContext);

            // Act
            Assembly resolveResult;
            using (AssemblyResolver assemblyResolver = new AssemblyResolver(logger, testFolder))
            {
                ResolveEventArgs resolveEventArgs = new ResolveEventArgs("nonexistent library", this.GetType().Assembly);
                resolveResult = assemblyResolver.CurrentDomain_AssemblyResolve(this, resolveEventArgs);
            }

            // Assert
            Assert.IsNull(resolveResult);
        }