Ejemplo n.º 1
0
        /// <summary>
        /// Construct an application domain for running a test package
        /// </summary>
        /// <param name="package">The TestPackage to be run</param>
        public AppDomain CreateDomain(TestPackage package)
        {
            FileInfo testFile = new FileInfo(package.FullName);

            AppDomainSetup setup = new AppDomainSetup();

            //For paralell tests, we need to use distinct application name
            setup.ApplicationName = "Tests" + "_" + Environment.TickCount;
            //setup.ApplicationName = package.Name;

            string appBase = package.BasePath;

            if (appBase == null || appBase == string.Empty)
            {
                appBase = testFile.DirectoryName;
            }
            setup.ApplicationBase = appBase;

            string configFile = package.ConfigurationFile;

            if (configFile == null || configFile == string.Empty)
            {
                configFile = NUnitProject.IsProjectFile(testFile.Name)
                                        ? Path.GetFileNameWithoutExtension(testFile.Name) + ".config"
                                        : testFile.Name + ".config";
            }
            // Note: Mono needs full path to config file...
            setup.ConfigurationFile = Path.Combine(appBase, configFile);

            string binPath = package.PrivateBinPath;

            if (package.AutoBinPath)
            {
                binPath = GetPrivateBinPath(appBase, package.Assemblies);
            }

            setup.PrivateBinPath = binPath;

            if (package.GetSetting("ShadowCopyFiles", true))
            {
                setup.ShadowCopyFiles       = "true";
                setup.ShadowCopyDirectories = appBase;
                setup.CachePath             = GetCachePath();
            }
            else
            {
                setup.ShadowCopyFiles = "false";
            }

            string domainName = "test-domain-" + package.Name;
            // Setup the Evidence
            Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);

            if (evidence.Count == 0)
            {
                Zone zone = new Zone(SecurityZone.MyComputer);
                evidence.AddHost(zone);
                Assembly assembly = Assembly.GetExecutingAssembly();
                Url      url      = new Url(assembly.CodeBase);
                evidence.AddHost(url);
                Hash hash = new Hash(assembly);
                evidence.AddHost(hash);
            }

            log.Info("Creating AppDomain " + domainName);

            AppDomain runnerDomain = AppDomain.CreateDomain(domainName, evidence, setup);

            // HACK: Only pass down our AddinRegistry one level so that tests of NUnit
            // itself start without any addins defined.
            if (!IsTestDomain(AppDomain.CurrentDomain))
            {
                runnerDomain.SetData("AddinRegistry", Services.AddinRegistry);
            }

            // Inject DomainInitializer into the remote domain - there are other
            // approaches, but this works for all CLR versions.
            DomainInitializer initializer = DomainInitializer.CreateInstance(runnerDomain);

            initializer.InitializeDomain(IsTestDomain(AppDomain.CurrentDomain)
                ? TraceLevel.Off : InternalTrace.Level);

            return(runnerDomain);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Construct an application domain for running a test package
        /// </summary>
        /// <param name="package">The TestPackage to be run</param>
        public AppDomain CreateDomain(TestPackage package)
        {
            AppDomainSetup setup = new AppDomainSetup();

            //For paralell tests, we need to use distinct application name
            setup.ApplicationName = "Tests" + "_" + Environment.TickCount;

            FileInfo testFile = package.FullName != null && package.FullName != string.Empty
                ? new FileInfo(package.FullName)
                : null;

            string appBase    = package.BasePath;
            string configFile = package.ConfigurationFile;
            string binPath    = package.PrivateBinPath;

            if (testFile != null)
            {
                if (appBase == null || appBase == string.Empty)
                {
                    appBase = testFile.DirectoryName;
                }

                if (configFile == null || configFile == string.Empty)
                {
                    configFile = Services.ProjectService.CanLoadProject(testFile.Name)
                        ? Path.GetFileNameWithoutExtension(testFile.Name) + ".config"
                        : testFile.Name + ".config";
                }
            }
            else if (appBase == null || appBase == string.Empty)
            {
                appBase = GetCommonAppBase(package.Assemblies);
            }

            char lastChar = appBase[appBase.Length - 1];

            if (lastChar != Path.DirectorySeparatorChar && lastChar != Path.AltDirectorySeparatorChar)
            {
                appBase += Path.DirectorySeparatorChar;
            }

            setup.ApplicationBase = appBase;
            // TODO: Check whether Mono still needs full path to config file...
            setup.ConfigurationFile = appBase != null && configFile != null
                ? Path.Combine(appBase, configFile)
                : configFile;

            if (package.AutoBinPath)
            {
                binPath = GetPrivateBinPath(appBase, package.Assemblies);
            }

            setup.PrivateBinPath = binPath;

            if (package.GetSetting("ShadowCopyFiles", true))
            {
                setup.ShadowCopyFiles       = "true";
                setup.ShadowCopyDirectories = appBase;
                setup.CachePath             = GetCachePath();
            }
            else
            {
                setup.ShadowCopyFiles = "false";
            }

            string domainName = "test-domain-" + package.Name;
            // Setup the Evidence
            Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);

            if (evidence.Count == 0)
            {
                Zone zone = new Zone(SecurityZone.MyComputer);
                evidence.AddHost(zone);
                Assembly assembly = Assembly.GetExecutingAssembly();
                Url      url      = new Url(assembly.CodeBase);
                evidence.AddHost(url);
                Hash hash = new Hash(assembly);
                evidence.AddHost(hash);
            }

            log.Info("Creating AppDomain " + domainName);

            AppDomain runnerDomain;

            // TODO: Try to eliminate this test. Currently, running on
            // Linux with the permission set specified causes an
            // unexplained crash when unloading the domain.
#if CLR_2_0 || CLR_4_0
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                PermissionSet permissionSet = new PermissionSet(PermissionState.Unrestricted);
                runnerDomain = AppDomain.CreateDomain(domainName, evidence, setup, permissionSet, null);
            }
            else
#endif
            runnerDomain = AppDomain.CreateDomain(domainName, evidence, setup);

            // Set PrincipalPolicy for the domain if called for in the settings
            if (Services.UserSettings.GetSetting("Options.TestLoader.SetPrincipalPolicy", false))
            {
                runnerDomain.SetPrincipalPolicy((PrincipalPolicy)Services.UserSettings.GetSetting(
                                                    "Options.TestLoader.PrincipalPolicy", PrincipalPolicy.UnauthenticatedPrincipal));
            }

            // HACK: Only pass down our AddinRegistry one level so that tests of NUnit
            // itself start without any addins defined.
            if (!IsTestDomain(AppDomain.CurrentDomain))
            {
                runnerDomain.SetData("AddinRegistry", Services.AddinRegistry);
            }

            // Inject DomainInitializer into the remote domain - there are other
            // approaches, but this works for all CLR versions.
            DomainInitializer initializer = DomainInitializer.CreateInstance(runnerDomain);

            // HACK: Under nunit-console, direct use of the enum fails
            int traceLevel = IsTestDomain(AppDomain.CurrentDomain)
                ? (int)InternalTraceLevel.Off : (int)InternalTrace.Level;

            initializer.InitializeDomain(traceLevel);

            return(runnerDomain);
        }