public void GetFullPathToDependentAssembliesShouldReturnV1FrameworkAssembly() { // Arrange. var v1AssemblyName = new AssemblyName("Microsoft.VisualStudio.QualityTools.UnitTestFramework"); var testableAssembly = new TestableAssembly(); testableAssembly.GetReferencedAssembliesSetter = () => { return(new AssemblyName[] { v1AssemblyName }); }; var mockAssemblyUtility = new Mock <IAssemblyUtility>(); mockAssemblyUtility.Setup(au => au.ReflectionOnlyLoadFrom(It.IsAny <string>())).Returns(testableAssembly); mockAssemblyUtility.Setup(au => au.ReflectionOnlyLoad(It.IsAny <string>())) .Returns(new TestableAssembly(v1AssemblyName.Name)); var worker = new AssemblyLoadWorker(mockAssemblyUtility.Object); // Act. var dependentAssemblies = worker.GetFullPathToDependentAssemblies("C:\\temp\\test3424.dll", out var warnings); // Assert. var utfassembly = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll"); CollectionAssert.Contains(dependentAssemblies, utfassembly); }
/// <summary> /// Returns the dependent assemblies of the parameter assembly. /// </summary> /// <param name="assemblyPath"> Path to assembly to get dependencies for. </param> /// <param name="configFile"> Config file to use while trying to resolve dependencies. </param> /// <param name="warnings"> The warnings. </param> /// <returns> The <see cref="string[]"/>. </returns> internal virtual string[] GetFullPathToDependentAssemblies(string assemblyPath, string configFile, out IList <string> warnings) { Debug.Assert(!string.IsNullOrEmpty(assemblyPath), "assemblyPath"); EqtTrace.InfoIf(EqtTrace.IsInfoEnabled, "AssemblyDependencyFinder.GetDependentAssemblies: start."); AppDomainSetup setupInfo = new AppDomainSetup(); setupInfo.ApplicationBase = Path.GetDirectoryName(Path.GetFullPath(assemblyPath)); Debug.Assert(string.IsNullOrEmpty(configFile) || File.Exists(configFile), "Config file is specified but does not exist: {0}", configFile); AppDomainUtilities.SetConfigurationFile(setupInfo, configFile); EqtTrace.InfoIf(EqtTrace.IsInfoEnabled, "AssemblyDependencyFinder.GetDependentAssemblies: Using config file: '{0}'.", setupInfo.ConfigurationFile); setupInfo.LoaderOptimization = LoaderOptimization.MultiDomainHost; AppDomain appDomain = null; try { appDomain = AppDomain.CreateDomain("Dependency finder domain", null, setupInfo); if (EqtTrace.IsInfoEnabled) { EqtTrace.Info("AssemblyDependencyFinder.GetDependentAssemblies: Created AppDomain."); } var assemblyResolverType = typeof(AssemblyResolver); EqtTrace.SetupRemoteEqtTraceListeners(appDomain); // This has to be LoadFrom, otherwise we will have to use AssemblyResolver to find self. using ( AssemblyResolver resolver = (AssemblyResolver)AppDomainUtilities.CreateInstance( appDomain, assemblyResolverType, new object[] { this.GetResolutionPaths() })) { // This has to be Load, otherwise Serialization of argument types will not work correctly. AssemblyLoadWorker worker = (AssemblyLoadWorker)AppDomainUtilities.CreateInstance(appDomain, typeof(AssemblyLoadWorker), null); EqtTrace.InfoIf(EqtTrace.IsInfoEnabled, "AssemblyDependencyFinder.GetDependentAssemblies: loaded the worker."); return(worker.GetFullPathToDependentAssemblies(assemblyPath, out warnings)); } } finally { if (appDomain != null) { EqtTrace.InfoIf(EqtTrace.IsInfoEnabled, "AssemblyDependencyFinder.GetDependentAssemblies: unloading AppDomain..."); AppDomain.Unload(appDomain); EqtTrace.InfoIf(EqtTrace.IsInfoEnabled, "AssemblyDependencyFinder.GetDependentAssemblies: unloading AppDomain succeeded."); } } }