예제 #1
0
        internal static string GetTargetFrameworkVersionString(string testSourcePath)
        {
            AppDomainSetup appDomainSetup = new AppDomainSetup();

            appDomainSetup.LoaderOptimization = LoaderOptimization.MultiDomainHost;

            AppDomainUtilities.SetConfigurationFile(appDomainSetup, new DeploymentUtility().GetConfigFile(testSourcePath));

            if (File.Exists(testSourcePath))
            {
                AppDomain appDomain = null;

                try
                {
                    appDomain = AppDomain.CreateDomain("Framework Version String Domain", null, appDomainSetup);

                    // Wire the eqttrace logs in this domain to the current domain.
                    EqtTrace.SetupRemoteEqtTraceListeners(appDomain);

                    // Add an assembly resolver to resolve ObjectModel or any Test Platform dependencies.
                    // Not moving to IMetaDataImport APIs because the time taken for this operation is <20 ms and
                    // IMetaDataImport needs COM registration which is not a guarantee in Dev15.
                    var assemblyResolverType = typeof(AssemblyResolver);

                    var resolutionPaths = new List <string> {
                        Path.GetDirectoryName(typeof(TestCase).Assembly.Location)
                    };
                    resolutionPaths.Add(Path.GetDirectoryName(testSourcePath));

                    AppDomainUtilities.CreateInstance(
                        appDomain,
                        assemblyResolverType,
                        new object[] { resolutionPaths });

                    var assemblyLoadWorker =
                        (AssemblyLoadWorker)AppDomainUtilities.CreateInstance(
                            appDomain,
                            typeof(AssemblyLoadWorker),
                            null);

                    return(assemblyLoadWorker.GetTargetFrameworkVersionStringFromPath(testSourcePath));
                }
                catch (Exception exception)
                {
                    if (EqtTrace.IsErrorEnabled)
                    {
                        EqtTrace.Error(exception);
                    }
                }
                finally
                {
                    if (appDomain != null)
                    {
                        AppDomain.Unload(appDomain);
                    }
                }
            }

            return(string.Empty);
        }
예제 #2
0
        /// <summary>
        /// Setup the isolation host.
        /// </summary>
        public void SetupHost()
        {
            List <string> resolutionPaths = this.GetResolutionPaths(this.sourceFileName, VSInstallationUtilities.IsCurrentProcessRunningInPortableMode());

            if (EqtTrace.IsInfoEnabled)
            {
                EqtTrace.Info("DesktopTestSourceHost.SetupHost(): Creating assembly resolver with resolution paths {0}.", string.Join(",", resolutionPaths.ToArray()));
            }

            // Case when DisableAppDomain setting is present in runsettings and no child-appdomain needs to be created
            if (this.isAppDomainCreationDisabled)
            {
                this.parentDomainAssemblyResolver = new AssemblyResolver(resolutionPaths);
                this.AddSearchDirectoriesSpecifiedInRunSettingsToAssemblyResolver(this.parentDomainAssemblyResolver, Path.GetDirectoryName(this.sourceFileName));
            }

            // Create child-appdomain and set assembly resolver on it
            else
            {
                // Setup app-domain
                var appDomainSetup = new AppDomainSetup();
                this.targetFrameworkVersion = this.GetTargetFrameworkVersionString(this.sourceFileName);
                AppDomainUtilities.SetAppDomainFrameworkVersionBasedOnTestSource(appDomainSetup, this.targetFrameworkVersion);

                // Temporarily set appbase to the location from where adapter should be picked up from. We will later reset this to test source location
                // once adapter gets loaded in the child app domain.
                appDomainSetup.ApplicationBase = Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location);

                var configFile = this.GetConfigFileForTestSource(this.sourceFileName);
                AppDomainUtilities.SetConfigurationFile(appDomainSetup, configFile);

                EqtTrace.Info("DesktopTestSourceHost.SetupHost(): Creating app-domain for source {0} with application base path {1}.", this.sourceFileName, appDomainSetup.ApplicationBase);

                string domainName = string.Format("TestSourceHost: Enumering source ({0})", this.sourceFileName);
                this.domain = this.appDomain.CreateDomain(domainName, null, appDomainSetup);

                // Load objectModel before creating assembly resolver otherwise in 3.5 process, we run into a recurive assembly resolution
                // which is trigged by AppContainerUtilities.AttachEventToResolveWinmd method.
                EqtTrace.SetupRemoteEqtTraceListeners(this.domain);

                // Add an assembly resolver in the child app-domain...
                Type assemblyResolverType = typeof(AssemblyResolver);

                EqtTrace.Info("DesktopTestSourceHost.SetupHost(): assemblyenumerator location: {0} , fullname: {1} ", assemblyResolverType.Assembly.Location, assemblyResolverType.FullName);

                var resolver = AppDomainUtilities.CreateInstance(
                    this.domain,
                    assemblyResolverType,
                    new object[] { resolutionPaths });

                EqtTrace.Info(
                    "DesktopTestSourceHost.SetupHost(): resolver type: {0} , resolve type assembly: {1} ",
                    resolver.GetType().FullName,
                    resolver.GetType().Assembly.Location);

                this.childDomainAssemblyResolver = (AssemblyResolver)resolver;

                this.AddSearchDirectoriesSpecifiedInRunSettingsToAssemblyResolver(this.childDomainAssemblyResolver, Path.GetDirectoryName(this.sourceFileName));
            }
        }
예제 #3
0
        /// <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.");
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Setup the isolation host.
        /// </summary>
        public void SetupHost()
        {
            List <string> resolutionPaths = this.GetResolutionPaths(this.sourceFileName, VSInstallationUtilities.IsCurrentProcessRunningInPortableMode());

            // Check if user specified any runsettings
            MSTestAdapterSettings adapterSettings = MSTestSettingsProvider.Settings;

            if (resolutionPaths != null && resolutionPaths.Count > 0)
            {
                if (EqtTrace.IsInfoEnabled)
                {
                    EqtTrace.Info("TestSourceHost: Creating assembly resolver with resolution paths {0}.", string.Join(",", resolutionPaths.ToArray()));
                }

                // Adding adapter folder to resolution paths
                if (!resolutionPaths.Contains(Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location)))
                {
                    resolutionPaths.Add(Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location));
                }

                // Adding extensions folder to resolution paths
                if (!resolutionPaths.Contains(Path.GetDirectoryName(typeof(AssemblyHelper).Assembly.Location)))
                {
                    resolutionPaths.Add(Path.GetDirectoryName(typeof(AssemblyHelper).Assembly.Location));
                }
            }

            // Honour DisableAppDomain setting if it is present in runsettings
            if (this.runSettings != null && MSTestAdapterSettings.IsAppDomainCreationDisabled(this.runSettings.SettingsXml))
            {
                if (adapterSettings != null)
                {
                    try
                    {
                        this.assemblyResolver = new AssemblyResolver(resolutionPaths);
                        this.assemblyResolver.AddSearchDirectoriesFromRunSetting(adapterSettings.GetDirectoryListWithRecursiveProperty(null));
                    }
                    catch (Exception exception)
                    {
                        if (EqtTrace.IsErrorEnabled)
                        {
                            EqtTrace.Error(exception);
                        }
                    }
                }
            }

            var appDomainSetup = new AppDomainSetup();

            // The below logic of preferential setting the appdomains appbase is needed because:
            // 1. We set this to the location of the test source if it is built for Full CLR  -> Ideally this needs to be done in all situations.
            // 2. We set this to the location where the current adapter is being picked up from for UWP and .Net Core scenarios -> This needs to be
            //    different especially for UWP because we use the desktop adapter(from %temp%\VisualStudioTestExplorerExtensions) itself for test discovery
            //    in IDE scenarios. If the app base is set to the test source location, discovery will not work because we drop the
            //    UWP platform service assembly at the test source location and since CLR starts looking for assemblies from the app base location,
            //    there would be a mismatch of platform service assemblies during discovery.
            var frameworkVersionString = this.GetTargetFrameworkVersionString(this.sourceFileName);

            if (frameworkVersionString.Contains(PlatformServices.Constants.DotNetFrameWorkStringPrefix))
            {
                appDomainSetup.ApplicationBase = Path.GetDirectoryName(this.sourceFileName)
                                                 ?? Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location);
            }
            else
            {
                appDomainSetup.ApplicationBase = Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location);
            }

            if (EqtTrace.IsInfoEnabled)
            {
                EqtTrace.Info("TestSourceHost: Creating app-domain for source {0} with application base path {1}.", this.sourceFileName, appDomainSetup.ApplicationBase);
            }

            AppDomainUtilities.SetAppDomainFrameworkVersionBasedOnTestSource(appDomainSetup, frameworkVersionString);

            var configFile = this.GetConfigFileForTestSource(this.sourceFileName);

            AppDomainUtilities.SetConfigurationFile(appDomainSetup, configFile);

            this.domain = this.appDomain.CreateDomain("TestSourceHost: Enumering assembly", null, appDomainSetup);

            // Load objectModel before creating assembly resolver otherwise in 3.5 process, we run into a recurive assembly resolution
            // which is trigged by AppContainerUtilities.AttachEventToResolveWinmd method.
            EqtTrace.SetupRemoteEqtTraceListeners(this.domain);

            // Add an assembly resolver...
            Type assemblyResolverType = typeof(AssemblyResolver);

            if (EqtTrace.IsInfoEnabled)
            {
                EqtTrace.Info("TestSourceHost: assemblyenumerator location: {0} , fullname: {1} ", assemblyResolverType.Assembly.Location, assemblyResolverType.FullName);
            }

            var resolver = AppDomainUtilities.CreateInstance(
                this.domain,
                assemblyResolverType,
                new object[] { resolutionPaths });

            if (EqtTrace.IsInfoEnabled)
            {
                EqtTrace.Info(
                    "TestSourceHost: resolver type: {0} , resolve type assembly: {1} ",
                    resolver.GetType().FullName,
                    resolver.GetType().Assembly.Location);
            }

            this.assemblyResolver = (AssemblyResolver)resolver;

            if (adapterSettings != null)
            {
                try
                {
                    var additionalSearchDirectories =
                        adapterSettings.GetDirectoryListWithRecursiveProperty(appDomainSetup.ApplicationBase);
                    if (additionalSearchDirectories?.Count > 0)
                    {
                        this.assemblyResolver.AddSearchDirectoriesFromRunSetting(
                            adapterSettings.GetDirectoryListWithRecursiveProperty(appDomainSetup.ApplicationBase));
                    }
                }
                catch (Exception exception)
                {
                    if (EqtTrace.IsErrorEnabled)
                    {
                        EqtTrace.Error(exception);
                    }
                }
            }
        }