public void CorrectRunnerIsUsed(string files, string domainUsage, Type expectedType)
 {
     var package = new TestPackage(files.Split(new char[] { ' ' }));
     if (domainUsage != null)
         package.Settings["DomainUsage"] = domainUsage;
     Assert.That(_factory.MakeTestRunner(package), Is.TypeOf(expectedType));
 }
Example #2
0
        /// <summary>
        /// Expands a TestPackage based on a known project format, populating it
        /// with the project contents and any settings the project provides. 
        /// Note that the package file path must be checked to ensure that it is
        /// a known project format before calling this method.
        /// </summary>
        /// <param name="package">The TestPackage to be expanded</param>
        public void ExpandProjectPackage(TestPackage package)
        {
            Guard.ArgumentNotNull(package, "package");
            Guard.ArgumentValid(package.SubPackages.Count == 0, "Package is already expanded", "package");

            string path = package.FullName;
            if (!File.Exists(path))
                return;

            IProject project = LoadFrom(path);
            Guard.ArgumentValid(project != null, "Unable to load project " + path, "package");

            string configName = package.GetSetting(EnginePackageSettings.ActiveConfig, (string)null); // Need RunnerSetting
            TestPackage tempPackage = project.GetTestPackage(configName);

            // The original package held overrides, so don't change them, but
            // do apply any settings specified within the project itself.
            foreach (string key in tempPackage.Settings.Keys)
                if (!package.Settings.ContainsKey(key)) // Don't override settings from command line
                    package.Settings[key] = tempPackage.Settings[key];

            foreach (var subPackage in tempPackage.SubPackages)
                package.AddSubPackage(subPackage);

            // If no config is specified (by user or by the proejct loader) check
            // to see if one exists in same directory as the package. If so, we
            // use it. If not, each assembly will use it's own config, if present.
            if (!package.Settings.ContainsKey(EnginePackageSettings.ConfigurationFile))
            {
                var packageConfig = Path.ChangeExtension(path, ".config");
                if (File.Exists(packageConfig))
                    package.Settings[EnginePackageSettings.ConfigurationFile] = packageConfig;
            }
        }
 public void GetById_ReturnsProject_WithMatchedId()
 {
     var p = new TestPackage();
     var expectedId = p.ProjectSetMock.Object.First().ProjectId;
     var project = p.Tested.Get(expectedId);
     Assert.AreEqual(project.ProjectId, expectedId);
 }
        public void NamespaceSetUpFixtureReplacesNamespaceNodeInTree()
        {
            string nameSpace = "NUnit.TestData.SetupFixture.Namespace1";
            TestSuiteBuilder builder = new TestSuiteBuilder();
            TestPackage package = new TestPackage( testAssembly );
            package.TestName = nameSpace;
            Test suite= builder.Build( package );

            Assert.IsNotNull(suite);

            Assert.AreEqual(testAssembly, suite.TestName.Name);
            Assert.AreEqual(1, suite.Tests.Count);

            string[] nameSpaceBits = nameSpace.Split('.');
            for (int i = 0; i < nameSpaceBits.Length; i++)
            {
                suite = suite.Tests[0] as TestSuite;
                Assert.AreEqual(nameSpaceBits[i], suite.TestName.Name);
                Assert.AreEqual(1, suite.Tests.Count);
            }

            Assert.IsInstanceOf(typeof(SetUpFixture), suite);

            suite = suite.Tests[0] as TestSuite;
            Assert.AreEqual("SomeTestFixture", suite.TestName.Name);
            Assert.AreEqual(1, suite.Tests.Count);
        }
Example #5
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 = CreateAppDomainSetup(package);

            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);

            // Set PrincipalPolicy for the domain if called for in the settings
            if (_settingsService != null && _settingsService.GetSetting("Options.TestLoader.SetPrincipalPolicy", false))
            {
                runnerDomain.SetPrincipalPolicy(_settingsService.GetSetting(
                    "Options.TestLoader.PrincipalPolicy", 
                    PrincipalPolicy.UnauthenticatedPrincipal));
            }

            return runnerDomain;
        }
        /// <summary>
        /// Returns a test runner based on the settings in a TestPackage.
        /// Any setting that is "consumed" by the factory is removed, so
        /// that downstream runners using the factory will not repeatedly
        /// create the same type of runner.
        /// </summary>
        /// <param name="package">The TestPackage to be loaded and run</param>
        /// <returns>An ITestEngineRunner</returns>
        public virtual ITestEngineRunner MakeTestRunner(TestPackage package)
        {
            DomainUsage domainUsage = (DomainUsage)System.Enum.Parse(
                typeof(DomainUsage),
                package.GetSetting(EnginePackageSettings.DomainUsage, "Default"));

            switch (domainUsage)
            {
                default:
                case DomainUsage.Default:
                    if (package.SubPackages.Count > 1)
                        return new MultipleTestDomainRunner(this.ServiceContext, package);
                    else
                        return new TestDomainRunner(this.ServiceContext, package);

                case DomainUsage.Multiple:
                    return new MultipleTestDomainRunner(ServiceContext, package);

                case DomainUsage.None:
                    return new LocalTestRunner(ServiceContext, package);

                case DomainUsage.Single:
                    return new TestDomainRunner(ServiceContext, package);
            }
        }
        /// <summary>
        /// Load a TestPackage for possible execution
        /// </summary>
        /// <param name="package">The TestPackage to be loaded</param>
        /// <returns>A TestEngineResult.</returns>
        protected override TestEngineResult LoadPackage()
        {
            List<TestPackage> packages = new List<TestPackage>();

            foreach (string testFile in TestPackage.TestFiles)
            {
                TestPackage subPackage = new TestPackage(testFile);
                if (Services.ProjectService.IsProjectFile(testFile))
                    Services.ProjectService.ExpandProjectPackage(subPackage);
                foreach (string key in TestPackage.Settings.Keys)
                    subPackage.Settings[key] = TestPackage.Settings[key];
                packages.Add(subPackage);
            }

            List<TestEngineResult> results = new List<TestEngineResult>();

            foreach (TestPackage subPackage in packages)
            {
                var runner = CreateRunner(subPackage);
                _runners.Add(runner);
                results.Add(runner.Load());
            }

            return ResultHelper.Merge(results);
        }
Example #8
0
 public AbstractTestRunner(IServiceLocator services, TestPackage package)
 {
     Services = services;
     TestPackage = package;
     TestRunnerFactory = Services.GetService<ITestRunnerFactory>();
     ProjectService = Services.GetService<IProjectService>();
 }
 public void ChangePassword_Validates_And_Saves()
 {
     var p = new TestPackage();
     p.Tested.ChangePassword(1, "Password", "password123");
     p.PasswordSetMock.Verify(x => x.Attach(It.IsAny<Password>()), Times.Once);
     p.ContextProviderMock.Verify(x => x.SetEntryModified(It.IsAny<Password>()), Times.Once);
     p.ContextProviderMock.Verify(x => x.SaveChanges(), Times.Once);
 }
Example #10
0
        public void calling_Reserve_with_2_queues_should_call_BLPop_with_2_queues()
        {
            var package = new TestPackage(new[] { "queue1", "queue2" });

            var job = package.UnderTest.Reserve();

            package.RedisMock.Verify(x => x.BLPop(new[] { "queue:queue1", "queue:queue2" }, It.IsAny<int>()));
        }
Example #11
0
        public void calling_Reserve_should_get_a_job()
        {
            var package = new TestPackage(new[] {""});

            var job = package.UnderTest.Reserve();

            Assert.That(job, Is.Not.Null);
        }
        public void Setup()
        {
            TestSuiteBuilder builder = new TestSuiteBuilder();
            TestPackage package = new TestPackage(AssemblyHelper.GetAssemblyPath(typeof(ActionAttributeExceptionFixture)));
            package.TestName = typeof(ActionAttributeExceptionFixture).Namespace;

            _Suite = builder.Build(package);
        }
Example #13
0
        /// <summary>
        /// Creates a new AppDomain for running the test package
        /// </summary>
        /// <param name="package">The test package to be run</param>
        /// <returns></returns>
        public static AppDomain CreateDomain(TestPackage package)
        {
            AppDomainSetup setup = CreateAppDomainSetup(package);

            string domainName = "test-domain-" + package.Name;

            return AppDomain.CreateDomain(domainName, null, setup);
        }
		[SetUp] public void LoadFixture()
		{
            string testsDll = AssemblyHelper.GetAssemblyPath(typeof(DerivedTestFixture));
			TestSuiteBuilder builder = new TestSuiteBuilder();
			TestPackage package = new TestPackage( testsDll );
			package.TestName = typeof(DerivedTestFixture).FullName;
			suite= builder.Build( package );
		}
		[SetUp] public void LoadFixture()
		{
			string testsDll = "test-assembly.dll";
			TestSuiteBuilder builder = new TestSuiteBuilder();
			TestPackage package = new TestPackage( testsDll );
			package.TestName = "NUnit.TestData.TestFixtureExtension.DerivedTestFixture";
			suite= builder.Build( package );
		}
        private TestResult runTests(string nameSpace, TestFilter filter)
        {
            TestSuiteBuilder builder = new TestSuiteBuilder();
			TestPackage package = new TestPackage( testAssembly );
			package.TestName = nameSpace;
            Test suite = builder.Build( package );
            return suite.Run(new NullListener(),filter);
        }
 // Create a MultipleTestProcessRunner with a fake package consisting of
 // some number of assemblies and with an optional MaxAgents setting.
 // Zero means that MaxAgents is not specified.
 MultipleTestProcessRunner CreateRunner(int assemblyCount, int maxAgents)
 {
     // Currently, we can get away with null entries here
     var package = new TestPackage(new string[assemblyCount]);
     if (maxAgents > 0)
         package.Settings[EnginePackageSettings.MaxAgents] = maxAgents;
     return new MultipleTestProcessRunner(new ServiceContext(), package);
 }
Example #18
0
 public void LoadTestFixtureFromAssembly()
 {
     TestSuiteBuilder builder = new TestSuiteBuilder();
     TestPackage package = new TestPackage( thisDll );
     package.TestName = this.GetType().FullName;
     Test suite= builder.Build( package );
     Assert.IsNotNull(suite, "Should not be Null");
 }
 public void Create_AddsProject_And_SavesContext()
 {
     var p = new TestPackage();
     var user = p.ContextProviderMock.Object.Users.First();
     var newProject = p.Tested.Create(new Project {Title = "new project", UserId = user.UserId});
     Assert.Greater(newProject.ProjectId, 0);
     p.ProjectSetMock.Verify(x => x.Add(It.IsAny<Project>()), Times.Once());
     p.ContextProviderMock.Verify(x => x.SaveChanges(), Times.Once);
 }
        public void Delete_Removes_And_SavesContext()
        {
            var p = new TestPackage();
            var project = p.ContextProviderMock.Object.Projects.First();
            p.Tested.Delete(project.ProjectId);

            p.ProjectSetMock.Verify(x => x.Remove(It.IsAny<Project>()), Times.Once());
            p.ContextProviderMock.Verify(x => x.SaveChanges(), Times.Once);
        }
Example #21
0
        public AbstractTestRunner(ServiceContext services, TestPackage package)
        {
            this.Services = services;
            this.TestPackage = package;
            TestRunnerFactory = Services.GetService<ITestRunnerFactory>();
#if NUNIT_ENGINE
            ProjectService = Services.GetService<IProjectService>();
#endif
        }
        /// <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 = CreateAppDomainSetup(package);

            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: Find an approach that works across all platforms
          
            //// TODO: Try to eliminate this test. Currently, running on
            //// Linux with the permission set specified causes an
            //// unexplained crash when unloading the domain.
            //if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            //{
            //    PermissionSet permissionSet = new PermissionSet( PermissionState.Unrestricted );	
            //    runnerDomain = AppDomain.CreateDomain(domainName, evidence, setup, permissionSet, null);
            //}
            //else
                runnerDomain = AppDomain.CreateDomain(domainName, evidence, setup);
            
            // Set PrincipalPolicy for the domain if called for in the settings
                if (ServiceContext.UserSettings.GetSetting("Options.TestLoader.SetPrincipalPolicy", false))
                    runnerDomain.SetPrincipalPolicy((PrincipalPolicy)ServiceContext.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;
        }
Example #23
0
        /// <summary>
        /// Returns a test runner based on the settings in a TestPackage.
        /// Any setting that is "consumed" by the factory is removed, so
        /// that downstream runners using the factory will not repeatedly
        /// create the same type of runner.
        /// </summary>
        /// <param name="package">The TestPackage to be loaded and run</param>
        /// <returns>A TestRunner</returns>
        public override ITestEngineRunner MakeTestRunner(TestPackage package)
        {

            int assemblyCount = 0;
            int projectCount = 0;

            foreach (var subPackage in package.SubPackages)
            {
                var testFile = subPackage.FullName;

                if (PathUtils.IsAssemblyFileType(testFile))
                    assemblyCount++;
                else if (_projectService.CanLoadFrom(testFile))
                    projectCount++;
            }

            // If we have multiple projects or a project plus assemblies
            // then defer to the AggregatingTestRunner, which will make
            // the decision on a file by file basis so that each project
            // runs with its own settings.
            if (projectCount > 1 || projectCount > 0 && assemblyCount > 0)
                return new AggregatingTestRunner(ServiceContext, package);

            // If we have a single project by itself, expand it here.
            if (projectCount > 0 && assemblyCount == 0)
            {
                var p = package.SubPackages[0];

                _projectService.ExpandProjectPackage(p);

                package = p;
            }

            // TODO: What about bad extensions?

            ProcessModel processModel = GetTargetProcessModel(package);

            switch (processModel)
            {
                default:
                case ProcessModel.Default:
                    if (package.SubPackages.Count > 1)
                        return new MultipleTestProcessRunner(this.ServiceContext, package);
                    else
                        return new ProcessRunner(this.ServiceContext, package);

                case ProcessModel.Multiple:
                    return new MultipleTestProcessRunner(this.ServiceContext, package);

                case ProcessModel.Separate:
                    return new ProcessRunner(this.ServiceContext, package);

                case ProcessModel.InProcess:
                    return base.MakeTestRunner(package);
            }
        }
Example #24
0
 public DirectTestRunner(IServiceLocator services, TestPackage package) : base(services, package)
 {
     // Bypass the resolver if not in the default AppDomain. This prevents trying to use the resolver within
     // NUnit's own automated tests (in a test AppDomain) which does not make sense anyway.
     if (AppDomain.CurrentDomain.IsDefaultAppDomain())
     {
         _assemblyResolver = new ProvidedPathsAssemblyResolver();
         _assemblyResolver.Install();
     }
 }
 public void GetByUserId_Returns_AllProjectsForUser()
 {
     const int userId = 2;
     var p = new TestPackage();
     var projects = p.Tested.GetByUserId(userId);
     foreach (var project in projects)
     {
         Assert.AreEqual(project.UserId, userId);
     }
 }
Example #26
0
        public void calling_Reserve_with_splat_should_call_SMembers()
        {
            var package = new TestPackage(new[] { "*" });
            package.RedisMock.Setup(x => x.SMembers("queues")).Returns(new[] {"RandomQueue"})
                .Verifiable();

            var job = package.UnderTest.Reserve();

            package.RedisMock.Verify();
        }
Example #27
0
        public MasterTestRunner(IServiceLocator services, TestPackage package)
        {
            _services = services;
            TestPackage = package;

            _testRunnerFactory = _services.GetService<ITestRunnerFactory>();
            _projectService = _services.GetService<IProjectService>();
            _runtimeService = _services.GetService<IRuntimeFrameworkService>();
            _extensionService = _services.GetService<ExtensionService>();
            _engineRunner = _testRunnerFactory.MakeTestRunner(package);
        }
        public void Setup()
        {
            ActionAttributeFixture.Results = new List<string>();

            TestSuiteBuilder builder = new TestSuiteBuilder();
            TestPackage package = new TestPackage(AssemblyHelper.GetAssemblyPath(typeof(ActionAttributeFixture)));
            package.TestName = typeof(ActionAttributeFixture).Namespace;

            Test suite = builder.Build(package);
            _result = suite.Run(new NullListener(), new ActionAttributeFixtureFilter());
        }
        /// <summary>
        /// Returns a test runner based on the settings in a TestPackage.
        /// Any setting that is "consumed" by the factory is removed, so
        /// that downstream runners using the factory will not repeatedly
        /// create the same type of runner.
        /// </summary>
        /// <param name="package">The TestPackage to be loaded and run</param>
        /// <returns>An ITestEngineRunner</returns>
        public virtual ITestEngineRunner MakeTestRunner(TestPackage package)
        {
            switch (package.GetSetting(PackageSettings.DomainUsage, "Default"))
            {
                case "None":
                    return new LocalTestRunner(ServiceContext, package);

                default:
                case "Single":
                    return new TestDomainRunner(ServiceContext, package);
            }
        }
Example #30
0
        /// <summary>
        /// Creates a new AppDomainSetup
        /// </summary>
        /// <param name="package"></param>
        /// <returns></returns>
        private static AppDomainSetup CreateAppDomainSetup(TestPackage package)
        {
            AppDomainSetup setup = new AppDomainSetup();

            // We need to use distinct application name, when runnin tests in parallel
            setup.ApplicationName = string.Format("Tests_{0}", Environment.TickCount);

            setup.ApplicationBase = package.GetBasePath();
            setup.ConfigurationFile = package.ConfigurationFile;

            return setup;
        }
Example #31
0
        public void PackageContainsOneSubPackagePerTestFile(params string[] testFiles)
        {
            TestPackage package = _model.MakeTestPackage(testFiles);

            Assert.That(package.SubPackages.Select(p => p.Name), Is.EqualTo(testFiles));
        }
 void IProjectService.ExpandProjectPackage(TestPackage package)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Use Mono.Cecil to get information about all assemblies and
        /// apply it to the package using special internal keywords.
        /// </summary>
        /// <param name="package"></param>
        private static void ApplyImageData(TestPackage package)
        {
            string packageName = package.FullName;

            Version targetVersion            = new Version(0, 0);
            string  frameworkName            = null;
            bool    requiresX86              = false;
            bool    requiresAssemblyResolver = false;

            // We are doing two jobs here: (1) in the else clause (below)
            // we get information about a single assembly and record it,
            // (2) in the if clause, we recursively examine all subpackages
            // and then apply policies for promulgating each setting to
            // a containing package. We could implement the policy part at
            // a higher level, but it seems simplest to do it right here.
            if (package.SubPackages.Count > 0)
            {
                foreach (var subPackage in package.SubPackages)
                {
                    ApplyImageData(subPackage);

                    // Collect the highest version required
                    Version v = subPackage.GetSetting(InternalEnginePackageSettings.ImageRuntimeVersion, new Version(0, 0));
                    if (v > targetVersion)
                    {
                        targetVersion = v;
                    }

                    // Collect highest framework name
                    // TODO: This assumes lexical ordering is valid - check it
                    string fn = subPackage.GetSetting(InternalEnginePackageSettings.ImageTargetFrameworkName, "");
                    if (fn != "")
                    {
                        if (frameworkName == null || fn.CompareTo(frameworkName) < 0)
                        {
                            frameworkName = fn;
                        }
                    }

                    // If any assembly requires X86, then the aggregate package requires it
                    if (subPackage.GetSetting(InternalEnginePackageSettings.ImageRequiresX86, false))
                    {
                        requiresX86 = true;
                    }

                    if (subPackage.GetSetting(InternalEnginePackageSettings.ImageRequiresDefaultAppDomainAssemblyResolver, false))
                    {
                        requiresAssemblyResolver = true;
                    }
                }
            }
            else if (File.Exists(packageName) && PathUtils.IsAssemblyFileType(packageName))
            {
                var assemblyDef = AssemblyDefinition.ReadAssembly(packageName);
                var module      = assemblyDef.MainModule;

                var NativeEntryPoint = (ModuleAttributes)16;
                var mask             = ModuleAttributes.Required32Bit | NativeEntryPoint;

                if (module.Architecture != TargetArchitecture.AMD64 &&
                    module.Architecture != TargetArchitecture.IA64 &&
                    (module.Attributes & mask) != 0)
                {
                    requiresX86 = true;
                    log.Debug("Assembly {0} will be run x86", packageName);
                }

                targetVersion = new Version(module.RuntimeVersion.Substring(1));
                log.Debug("Assembly {0} uses version {1}", packageName, targetVersion);

                foreach (var attr in assemblyDef.CustomAttributes)
                {
                    if (attr.AttributeType.FullName == "System.Runtime.Versioning.TargetFrameworkAttribute")
                    {
                        frameworkName = attr.ConstructorArguments[0].Value as string;
                    }
                    else if (attr.AttributeType.FullName == "NUnit.Framework.TestAssemblyDirectoryResolveAttribute")
                    {
                        requiresAssemblyResolver = true;
                    }
                }

                if (frameworkName == null)
                {
                    foreach (var reference in module.AssemblyReferences)
                    {
                        if (reference.Name == "mscorlib" && BitConverter.ToUInt64(reference.PublicKeyToken, 0) == 0xac22333d05b89d96)
                        {
                            // We assume 3.5, since that's all we are supporting
                            // Could be extended to other versions if necessary
                            // Format for FrameworkName is invented - it is not
                            // known if any compilers supporting CF use the attribute
                            frameworkName = ".NETCompactFramework,Version=3.5";
                            break;
                        }
                    }
                }
            }

            if (targetVersion.Major > 0)
            {
                package.Settings[InternalEnginePackageSettings.ImageRuntimeVersion] = targetVersion;
            }

            if (!string.IsNullOrEmpty(frameworkName))
            {
                package.Settings[InternalEnginePackageSettings.ImageTargetFrameworkName] = frameworkName;
            }

            package.Settings[InternalEnginePackageSettings.ImageRequiresX86] = requiresX86;
            if (requiresX86)
            {
                package.Settings[EnginePackageSettings.RunAsX86] = true;
            }

            package.Settings[InternalEnginePackageSettings.ImageRequiresDefaultAppDomainAssemblyResolver] = requiresAssemblyResolver;
        }
Example #34
0
        private IEnumerable <AutoTest.TestRunners.Shared.Results.TestResult> runTests(Options options, TestPackage package, TestRunner testRunner)
        {
            testRunner.Load(package);
            if (testRunner.Test == null)
            {
                testRunner.Unload();
                return(new AutoTest.TestRunners.Shared.Results.TestResult[] { ErrorHandler.GetError("NUnit", "Unable to locate fixture") });
            }

            var    harvester      = new TestHarvester(_channel);
            var    testFilter     = getTestFilter(options);
            string savedDirectory = Environment.CurrentDirectory;
            var    result         = run(testRunner, harvester, testFilter, savedDirectory);

            if (result != null)
            {
                return(harvester.Results);
            }
            return(harvester.Results);
        }
Example #35
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();

            // We always use the same application name
            setup.ApplicationName = "Tests";

            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();
            }

            string    domainName   = "domain-" + package.Name;
            Evidence  baseEvidence = AppDomain.CurrentDomain.Evidence;
            Evidence  evidence     = new Evidence(baseEvidence);
            AppDomain runnerDomain = AppDomain.CreateDomain(domainName, evidence, setup);

            // Inject assembly resolver into remote domain to help locate our assemblies
            AssemblyResolver assemblyResolver = (AssemblyResolver)runnerDomain.CreateInstanceFromAndUnwrap(
                typeof(AssemblyResolver).Assembly.CodeBase,
                typeof(AssemblyResolver).FullName);

            // Tell resolver to use our core assemblies in the test domain
            assemblyResolver.AddFile(typeof(NUnit.Core.RemoteTestRunner).Assembly.Location);
            assemblyResolver.AddFile(typeof(NUnit.Core.ITest).Assembly.Location);

// No reference to extensions, so we do it a different way
            string moduleName   = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            string nunitDirPath = Path.GetDirectoryName(moduleName);
//            string coreExtensions = Path.Combine(nunitDirPath, "nunit.core.extensions.dll");
//			assemblyResolver.AddFile( coreExtensions );
            //assemblyResolver.AddFiles( nunitDirPath, "*.dll" );

            string addinsDirPath = Path.Combine(nunitDirPath, "addins");

            assemblyResolver.AddDirectory(addinsDirPath);

            // 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);
            }

            return(runnerDomain);
        }
 public MasterTestRunner(ServiceContext services, TestPackage package) : base(services, package)
 {
 }
Example #37
0
        private int RunTests(TestPackage package, TestFilter filter)
        {
            var writer = new ColorConsoleWriter(!_options.NoColor);

            foreach (var spec in _options.ResultOutputSpecifications)
            {
                var outputPath = Path.Combine(_workDirectory, spec.OutputPath);

                IResultWriter resultWriter;

                try
                {
                    resultWriter = GetResultWriter(spec);
                }
                catch (Exception ex)
                {
                    throw new NUnitEngineException($"Error encountered in resolving output specification: {spec}", ex);
                }

                try
                {
                    var outputDirectory = Path.GetDirectoryName(outputPath);
                    Directory.CreateDirectory(outputDirectory);
                }
                catch (SystemException ex)
                {
                    writer.WriteLine(ColorStyle.Error, String.Format(
                                         "The directory in --result {0} could not be created",
                                         spec.OutputPath));
                    writer.WriteLine(ColorStyle.Error, ExceptionHelper.BuildMessage(ex));
                    return(ConsoleRunner.UNEXPECTED_ERROR);
                }

                try
                {
                    resultWriter.CheckWritability(outputPath);
                }
                catch (SystemException ex)
                {
                    throw new NUnitEngineException(
                              String.Format(
                                  "The path specified in --result {0} could not be written to",
                                  spec.OutputPath), ex);
                }
            }

            var labels = _options.DisplayTestLabels != null
                ? _options.DisplayTestLabels.ToUpperInvariant()
                : "ON";

            XmlNode result = null;
            NUnitEngineUnloadException unloadException = null;
            NUnitEngineException       engineException = null;

            try
            {
                using (new SaveConsoleOutput())
                    using (ITestRunner runner = _engine.GetRunner(package))
                        using (var output = CreateOutputWriter())
                        {
                            var eventHandler = new TestEventHandler(output, labels);

                            result = runner.Run(eventHandler, filter);
                        }
            }
            catch (NUnitEngineUnloadException ex)
            {
                unloadException = ex;
            }
            catch (NUnitEngineException ex)
            {
                engineException = ex;
            }

            if (result != null)
            {
                var reporter = new ResultReporter(result, writer, _options);
                reporter.ReportResults();

                foreach (var spec in _options.ResultOutputSpecifications)
                {
                    var outputPath = Path.Combine(_workDirectory, spec.OutputPath);
                    GetResultWriter(spec).WriteResultFile(result, outputPath);
                    writer.WriteLine("Results ({0}) saved as {1}", spec.Format, spec.OutputPath);
                }

                if (engineException != null)
                {
                    writer.WriteLine(ColorStyle.Error, Environment.NewLine + ExceptionHelper.BuildMessage(engineException));
                    return(ConsoleRunner.UNEXPECTED_ERROR);
                }

                if (unloadException != null)
                {
                    writer.WriteLine(ColorStyle.Warning, Environment.NewLine + ExceptionHelper.BuildMessage(unloadException));
                }

                if (reporter.Summary.UnexpectedError)
                {
                    return(ConsoleRunner.UNEXPECTED_ERROR);
                }

                if (reporter.Summary.InvalidAssemblies > 0)
                {
                    return(ConsoleRunner.INVALID_ASSEMBLY);
                }

                return(reporter.Summary.InvalidTestFixtures > 0
                    ? ConsoleRunner.INVALID_TEST_FIXTURE
                    : reporter.Summary.FailureCount + reporter.Summary.ErrorCount + reporter.Summary.InvalidCount);
            }

            // If we got here, it's because we had an exception, but check anyway
            if (engineException != null)
            {
                writer.WriteLine(ColorStyle.Error, ExceptionHelper.BuildMessage(engineException));
                writer.WriteLine();
                writer.WriteLine(ColorStyle.Error, ExceptionHelper.BuildMessageAndStackTrace(engineException));
            }

            return(ConsoleRunner.UNEXPECTED_ERROR);
        }
Example #38
0
 public IList <string> GetConfigNames(TestPackage package)
 {
     return(Services.ProjectService.LoadFrom(package.FullName).ConfigNames);
 }
 public AggregatingTestRunner(IServiceLocator services, TestPackage package) : base(services, package)
 {
 }
 protected virtual ITestEngineRunner CreateRunner(TestPackage package)
 {
     return(TestRunnerFactory.MakeTestRunner(package));
 }
 protected override ITestEngineRunner CreateRunner(TestPackage package)
 {
     return(new TestDomainRunner(Services, package));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MultipleTestDomainRunner"/> class.
 /// </summary>
 /// <param name="services">The services.</param>
 /// <param name="package">The package.</param>
 public MultipleTestDomainRunner(IServiceLocator services, TestPackage package) : base(services, package)
 {
 }
Example #43
0
        private Guid LaunchAgentProcess(TestPackage package)
        {
            RuntimeFramework targetRuntime;
            string           runtimeSetting = package.GetSetting(EnginePackageSettings.RuntimeFramework, "");

            if (runtimeSetting.Length > 0)
            {
                if (!RuntimeFramework.TryParse(runtimeSetting, out targetRuntime))
                {
                    throw new NUnitEngineException("Invalid or unknown framework requested: " + runtimeSetting);
                }
            }
            else
            {
                targetRuntime = RuntimeFramework.CurrentFramework;
            }

            if (targetRuntime.Runtime == RuntimeType.Any)
            {
                targetRuntime = new RuntimeFramework(RuntimeFramework.CurrentFramework.Runtime, targetRuntime.ClrVersion);
            }

            bool   useX86Agent     = package.GetSetting(EnginePackageSettings.RunAsX86, false);
            bool   debugTests      = package.GetSetting(EnginePackageSettings.DebugTests, false);
            bool   debugAgent      = package.GetSetting(EnginePackageSettings.DebugAgent, false);
            string traceLevel      = package.GetSetting(EnginePackageSettings.InternalTraceLevel, "Off");
            bool   loadUserProfile = package.GetSetting(EnginePackageSettings.LoadUserProfile, false);
            string workDirectory   = package.GetSetting(EnginePackageSettings.WorkDirectory, string.Empty);

            // Set options that need to be in effect before the package
            // is loaded by using the command line.
            string agentArgs = "--pid=" + Process.GetCurrentProcess().Id.ToString();

            if (traceLevel != "Off")
            {
                agentArgs += " --trace:" + traceLevel;
            }
            if (debugAgent)
            {
                agentArgs += " --debug-agent";
            }
            if (workDirectory != string.Empty)
            {
                agentArgs += " --work=" + workDirectory;
            }

            log.Info("Getting {0} agent for use under {1}", useX86Agent ? "x86" : "standard", targetRuntime);

            if (!targetRuntime.IsAvailable)
            {
                throw new ArgumentException(
                          string.Format("The {0} framework is not available", targetRuntime),
                          "framework");
            }

            string agentExePath = GetTestAgentExePath(useX86Agent);

            if (!File.Exists(agentExePath))
            {
                throw new FileNotFoundException(
                          $"{Path.GetFileName(agentExePath)} could not be found.", agentExePath);
            }

            log.Debug("Using nunit-agent at " + agentExePath);

            Process p = new Process();

            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow  = true;
            p.EnableRaisingEvents       = true;
            p.Exited += OnAgentExit;
            Guid   agentId = Guid.NewGuid();
            string arglist = agentId.ToString() + " " + ServerUrl + " " + agentArgs;

            targetRuntime = ServiceContext.GetService <RuntimeFrameworkService>().GetBestAvailableFramework(targetRuntime);

            switch (targetRuntime.Runtime)
            {
            case RuntimeType.Mono:
                p.StartInfo.FileName = RuntimeFramework.MonoExePath;
                string monoOptions = "--runtime=v" + targetRuntime.ClrVersion.ToString(3);
                if (debugTests || debugAgent)
                {
                    monoOptions += " --debug";
                }
                p.StartInfo.Arguments = string.Format("{0} \"{1}\" {2}", monoOptions, agentExePath, arglist);
                break;

            case RuntimeType.Net:
                p.StartInfo.FileName = agentExePath;
                // Override the COMPLUS_Version env variable, this would cause CLR meta host to run a CLR of the specific version
                string envVar = "v" + targetRuntime.ClrVersion.ToString(3);
                p.StartInfo.EnvironmentVariables["COMPLUS_Version"] = envVar;
                // Leave a marker that we have changed this variable, so that the agent could restore it for any code or child processes running within the agent
                string cpvOriginal = Environment.GetEnvironmentVariable("COMPLUS_Version");
                p.StartInfo.EnvironmentVariables["TestAgency_COMPLUS_Version_Original"] = string.IsNullOrEmpty(cpvOriginal) ? "NULL" : cpvOriginal;
                p.StartInfo.Arguments       = arglist;
                p.StartInfo.LoadUserProfile = loadUserProfile;
                break;

            default:
                p.StartInfo.FileName  = agentExePath;
                p.StartInfo.Arguments = arglist;
                break;
            }

            p.Start();
            log.Debug("Launched Agent process {0} - see nunit-agent_{0}.log", p.Id);
            log.Debug("Command line: \"{0}\" {1}", p.StartInfo.FileName, p.StartInfo.Arguments);

            _agentData.Add(new AgentRecord(agentId, p, null, AgentStatus.Starting));
            return(agentId);
        }
Example #44
0
        // This is public static for ease of testing
        public static TestPackage MakeTestPackage(ConsoleOptions options)
        {
            TestPackage package = new TestPackage(options.InputFiles);

            if (options.ProcessModelSpecified)
            {
                package.AddSetting(EnginePackageSettings.ProcessModel, options.ProcessModel);
            }

            if (options.DomainUsageSpecified)
            {
                package.AddSetting(EnginePackageSettings.DomainUsage, options.DomainUsage);
            }

            if (options.FrameworkSpecified)
            {
                package.AddSetting(EnginePackageSettings.RuntimeFramework, options.Framework);
            }

            if (options.RunAsX86)
            {
                package.AddSetting(EnginePackageSettings.RunAsX86, true);
            }

            // Console runner always sets DisposeRunners
            //if (options.DisposeRunners)
            package.AddSetting(EnginePackageSettings.DisposeRunners, true);

            if (options.ShadowCopyFiles)
            {
                package.AddSetting(EnginePackageSettings.ShadowCopyFiles, true);
            }

            if (options.LoadUserProfile)
            {
                package.AddSetting(EnginePackageSettings.LoadUserProfile, true);
            }

            if (options.SkipNonTestAssemblies)
            {
                package.AddSetting(EnginePackageSettings.SkipNonTestAssemblies, true);
            }

            if (options.DefaultTimeout >= 0)
            {
                package.AddSetting(FrameworkPackageSettings.DefaultTimeout, options.DefaultTimeout);
            }

            if (options.InternalTraceLevelSpecified)
            {
                package.AddSetting(FrameworkPackageSettings.InternalTraceLevel, options.InternalTraceLevel);
            }

            if (options.ActiveConfigSpecified)
            {
                package.AddSetting(EnginePackageSettings.ActiveConfig, options.ActiveConfig);
            }

            // Always add work directory, in case current directory is changed
            var workDirectory = options.WorkDirectory ?? Environment.CurrentDirectory;

            package.AddSetting(FrameworkPackageSettings.WorkDirectory, workDirectory);

            if (options.StopOnError)
            {
                package.AddSetting(FrameworkPackageSettings.StopOnError, true);
            }

            if (options.MaxAgentsSpecified)
            {
                package.AddSetting(EnginePackageSettings.MaxAgents, options.MaxAgents);
            }

            if (options.NumberOfTestWorkersSpecified)
            {
                package.AddSetting(FrameworkPackageSettings.NumberOfTestWorkers, options.NumberOfTestWorkers);
            }

            if (options.RandomSeedSpecified)
            {
                package.AddSetting(FrameworkPackageSettings.RandomSeed, options.RandomSeed);
            }

            if (options.DebugTests)
            {
                package.AddSetting(FrameworkPackageSettings.DebugTests, true);

                if (!options.NumberOfTestWorkersSpecified)
                {
                    package.AddSetting(FrameworkPackageSettings.NumberOfTestWorkers, 0);
                }
            }

            if (options.PauseBeforeRun)
            {
                package.AddSetting(FrameworkPackageSettings.PauseBeforeRun, true);
            }

            if (options.PrincipalPolicy != null)
            {
                package.AddSetting(EnginePackageSettings.PrincipalPolicy, options.PrincipalPolicy);
            }

#if DEBUG
            if (options.DebugAgent)
            {
                package.AddSetting(EnginePackageSettings.DebugAgent, true);
            }

            //foreach (KeyValuePair<string, object> entry in package.Settings)
            //    if (!(entry.Value is string || entry.Value is int || entry.Value is bool))
            //        throw new Exception(string.Format("Package setting {0} is not a valid type", entry.Key));
#endif

            if (options.DefaultTestNamePattern != null)
            {
                package.AddSetting(FrameworkPackageSettings.DefaultTestNamePattern, options.DefaultTestNamePattern);
            }

            if (options.TestParameters.Count != 0)
            {
                AddTestParametersSetting(package, options.TestParameters);
            }

            if (options.ConfigurationFile != null)
            {
                package.AddSetting(EnginePackageSettings.ConfigurationFile, options.ConfigurationFile);
            }

            return(package);
        }
Example #45
0
 public void CreateRunner(TestPackage testPackage)
 {
     package = testPackage;
     Runner  = TestEngine.GetRunner(package);
 }
Example #46
0
        /// <summary>
        /// Selects a target runtime framework for a TestPackage based on
        /// the settings in the package and the assemblies themselves.
        /// The package RuntimeFramework setting may be updated as a
        /// result and the selected runtime is returned.
        /// </summary>
        /// <param name="package">A TestPackage</param>
        /// <returns>The selected RuntimeFramework</returns>
        public RuntimeFramework SelectRuntimeFramework(TestPackage package)
        {
            RuntimeFramework currentFramework   = RuntimeFramework.CurrentFramework;
            string           frameworkSetting   = package.GetSetting(RunnerSettings.RuntimeFramework, "");
            RuntimeFramework requestedFramework = frameworkSetting.Length > 0
                ? RuntimeFramework.Parse(frameworkSetting)
                : new RuntimeFramework(RuntimeType.Any, RuntimeFramework.DefaultVersion);

            log.Debug("Current framework is {0}", currentFramework);
            if (requestedFramework == null)
            {
                log.Debug("No specific framework requested");
            }
            else
            {
                log.Debug("Requested framework is {0}", requestedFramework);
            }

            RuntimeType targetRuntime = requestedFramework.Runtime;
            Version     targetVersion = requestedFramework.FrameworkVersion;

            if (targetRuntime == RuntimeType.Any)
            {
                targetRuntime = currentFramework.Runtime;
            }

            if (targetVersion == RuntimeFramework.DefaultVersion)
            {
                if (ServiceContext.UserSettings.GetSetting("Options.TestLoader.RuntimeSelectionEnabled", true))
                {
                    foreach (string assembly in package.TestFiles)
                    {
                        using (AssemblyReader reader = new AssemblyReader(assembly))
                        {
                            Version v = new Version(reader.ImageRuntimeVersion.Substring(1));
                            log.Debug("Assembly {0} uses version {1}", assembly, v);
                            if (v > targetVersion)
                            {
                                targetVersion = v;
                            }
                        }
                    }
                }
                else
                {
                    targetVersion = RuntimeFramework.CurrentFramework.ClrVersion;
                }

                RuntimeFramework checkFramework = new RuntimeFramework(targetRuntime, targetVersion);
                if (!checkFramework.IsAvailable || !ServiceContext.TestAgency.IsRuntimeVersionSupported(targetVersion))
                {
                    log.Debug("Preferred version {0} is not installed or this NUnit installation does not support it", targetVersion);
                    if (targetVersion < currentFramework.FrameworkVersion)
                    {
                        targetVersion = currentFramework.FrameworkVersion;
                    }
                }
            }

            RuntimeFramework targetFramework = new RuntimeFramework(targetRuntime, targetVersion);

            package.Settings[RunnerSettings.RuntimeFramework] = targetFramework.ToString();

            log.Debug("Test will use {0} framework", targetFramework);

            return(targetFramework);
        }
        /// <summary>
        /// Use Mono.Cecil to get information about all assemblies and
        /// apply it to the package using special internal keywords.
        /// </summary>
        /// <param name="package"></param>
        private static void ApplyImageData(TestPackage package)
        {
            string packageName = package.FullName;

            Version targetVersion            = new Version(0, 0);
            string  frameworkName            = null;
            bool    requiresX86              = false;
            bool    requiresAssemblyResolver = false;

            // We are doing two jobs here: (1) in the else clause (below)
            // we get information about a single assembly and record it,
            // (2) in the if clause, we recursively examine all subpackages
            // and then apply policies for promulgating each setting to
            // a containing package. We could implement the policy part at
            // a higher level, but it seems simplest to do it right here.
            if (package.SubPackages.Count > 0)
            {
                foreach (var subPackage in package.SubPackages)
                {
                    ApplyImageData(subPackage);

                    // Collect the highest version required
                    Version v = subPackage.GetSetting(InternalEnginePackageSettings.ImageRuntimeVersion, new Version(0, 0));
                    if (v > targetVersion)
                    {
                        targetVersion = v;
                    }

                    // Collect highest framework name
                    // TODO: This assumes lexical ordering is valid - check it
                    string fn = subPackage.GetSetting(InternalEnginePackageSettings.ImageTargetFrameworkName, "");
                    if (fn != "")
                    {
                        if (frameworkName == null || fn.CompareTo(frameworkName) < 0)
                        {
                            frameworkName = fn;
                        }
                    }

                    // If any assembly requires X86, then the aggregate package requires it
                    if (subPackage.GetSetting(InternalEnginePackageSettings.ImageRequiresX86, false))
                    {
                        requiresX86 = true;
                    }

                    if (subPackage.GetSetting(InternalEnginePackageSettings.ImageRequiresDefaultAppDomainAssemblyResolver, false))
                    {
                        requiresAssemblyResolver = true;
                    }
                }
            }
            else if (File.Exists(packageName) && PathUtils.IsAssemblyFileType(packageName))
            {
                var assembly = AssemblyDefinition.ReadAssembly(packageName);

                targetVersion = assembly.GetRuntimeVersion();
                log.Debug($"Assembly {packageName} uses version {targetVersion}");

                frameworkName = assembly.GetFrameworkName();
                log.Debug($"Assembly {packageName} targets {frameworkName}");

                if (assembly.RequiresX86())
                {
                    requiresX86 = true;
                    log.Debug($"Assembly {packageName} will be run x86");
                }

                if (assembly.HasAttribute("NUnit.Framework.TestAssemblyDirectoryResolveAttribute"))
                {
                    requiresAssemblyResolver = true;
                    log.Debug($"Assembly {packageName} requires default app domain assembly resolver");
                }
            }

            if (targetVersion.Major > 0)
            {
                package.Settings[InternalEnginePackageSettings.ImageRuntimeVersion] = targetVersion;
            }

            if (!string.IsNullOrEmpty(frameworkName))
            {
                package.Settings[InternalEnginePackageSettings.ImageTargetFrameworkName] = frameworkName;
            }

            package.Settings[InternalEnginePackageSettings.ImageRequiresX86] = requiresX86;
            if (requiresX86)
            {
                package.Settings[EnginePackageSettings.RunAsX86] = true;
            }

            package.Settings[InternalEnginePackageSettings.ImageRequiresDefaultAppDomainAssemblyResolver] = requiresAssemblyResolver;
        }
Example #48
0
 public override ITestEngineRunner CreateRunner(TestPackage package)
 {
     _package = package;
     return(this);
 }
 public ITestEngineRunner CreateRunner(TestPackage package)
 {
     return(_remoteAgent.CreateRunner(package));
 }
 string IRuntimeFrameworkService.SelectRuntimeFramework(TestPackage package)
 {
     return(string.Empty);
 }
        public static Mock <TestPackageReader> CreateNuGetPackage(
            string id                     = "theId",
            string version                = "01.0.42.0",
            string title                  = "theTitle",
            string summary                = "theSummary",
            string authors                = "theFirstAuthor, theSecondAuthor",
            string owners                 = "Package owners",
            string description            = "theDescription",
            string tags                   = "theTags",
            string language               = null,
            string copyright              = "theCopyright",
            string releaseNotes           = "theReleaseNotes",
            string minClientVersion       = null,
            Uri licenseUrl                = null,
            Uri projectUrl                = null,
            Uri iconUrl                   = null,
            bool requireLicenseAcceptance = true,
            IEnumerable <PackageDependencyGroup> packageDependencyGroups = null,
            IEnumerable <NuGet.Packaging.Core.PackageType> packageTypes  = null,
            RepositoryMetadata repositoryMetadata = null,
            bool isSigned = false)
        {
            licenseUrl = licenseUrl ?? new Uri("http://thelicenseurl/");
            projectUrl = projectUrl ?? new Uri("http://theprojecturl/");
            iconUrl    = iconUrl ?? new Uri("http://theiconurl/");

            if (packageDependencyGroups == null)
            {
                packageDependencyGroups = new[]
                {
                    new PackageDependencyGroup(
                        new NuGetFramework("net40"),
                        new[]
                    {
                        new NuGet.Packaging.Core.PackageDependency(
                            "theFirstDependency",
                            VersionRange.Parse("[1.0.0, 2.0.0)")),

                        new NuGet.Packaging.Core.PackageDependency(
                            "theSecondDependency",
                            VersionRange.Parse("[1.0]")),

                        new NuGet.Packaging.Core.PackageDependency(
                            "theThirdDependency")
                    }),

                    new PackageDependencyGroup(
                        new NuGetFramework("net35"),
                        new[]
                    {
                        new NuGet.Packaging.Core.PackageDependency(
                            "theFourthDependency",
                            VersionRange.Parse("[1.0]"))
                    })
                };
            }

            if (packageTypes == null)
            {
                packageTypes = new[]
                {
                    new NuGet.Packaging.Core.PackageType("dependency", new Version("1.0.0")),
                    new NuGet.Packaging.Core.PackageType("DotNetCliTool", new Version("2.1.1"))
                };
            }

            var testPackage = TestPackage.CreateTestPackageStream(
                id, version, title, summary, authors, owners,
                description, tags, language, copyright, releaseNotes,
                minClientVersion, licenseUrl, projectUrl, iconUrl,
                requireLicenseAcceptance, packageDependencyGroups, packageTypes, repositoryMetadata,
                archive =>
            {
                if (isSigned)
                {
                    var entry = archive.CreateEntry(SigningSpecifications.V1.SignaturePath);
                    using (var stream = entry.Open())
                        using (var writer = new StreamWriter(stream))
                        {
                            writer.Write("Fake signature file.");
                        }
                }
            });

            var mock = new Mock <TestPackageReader>(testPackage);

            mock.CallBase = true;
            return(mock);
        }
 string IRuntimeFrameworkService.SelectRuntimeFramework(TestPackage package)
 {
     throw new NotImplementedException();
 }
Example #53
0
 /// <summary>
 ///  Creates a test runner
 /// </summary>
 public abstract ITestEngineRunner CreateRunner(TestPackage package);
 public ProcessRunner(ServiceContext services, TestPackage package) : base(services, package)
 {
 }
Example #55
0
        private Process LaunchAgentProcess(TestPackage package, Guid agentId)
        {
            string runtimeSetting = package.GetSetting(EnginePackageSettings.RuntimeFramework, "");

            // TEMPORARY Guard in preparation for removing Runtime.Any
            Guard.OperationValid(runtimeSetting.Length > 0, "LaunchAgentProcess called with no runtime specified");

            var targetRuntime = RuntimeFramework.Parse(runtimeSetting);

            bool   useX86Agent     = package.GetSetting(EnginePackageSettings.RunAsX86, false);
            bool   debugTests      = package.GetSetting(EnginePackageSettings.DebugTests, false);
            bool   debugAgent      = package.GetSetting(EnginePackageSettings.DebugAgent, false);
            string traceLevel      = package.GetSetting(EnginePackageSettings.InternalTraceLevel, "Off");
            bool   loadUserProfile = package.GetSetting(EnginePackageSettings.LoadUserProfile, false);
            string workDirectory   = package.GetSetting(EnginePackageSettings.WorkDirectory, string.Empty);

            var agentArgs = new StringBuilder();

            // Set options that need to be in effect before the package
            // is loaded by using the command line.
            agentArgs.Append("--pid=").Append(Process.GetCurrentProcess().Id);
            if (traceLevel != "Off")
            {
                agentArgs.Append(" --trace:").EscapeProcessArgument(traceLevel);
            }
            if (debugAgent)
            {
                agentArgs.Append(" --debug-agent");
            }
            if (workDirectory != string.Empty)
            {
                agentArgs.Append(" --work=").EscapeProcessArgument(workDirectory);
            }

            log.Info("Getting {0} agent for use under {1}", useX86Agent ? "x86" : "standard", targetRuntime);

            if (!_runtimeService.IsAvailable(targetRuntime.Id))
            {
                throw new ArgumentException(
                          string.Format("The {0} framework is not available", targetRuntime),
                          "framework");
            }

            string agentExePath = GetTestAgentExePath(useX86Agent);

            if (!File.Exists(agentExePath))
            {
                throw new FileNotFoundException(
                          $"{Path.GetFileName(agentExePath)} could not be found.", agentExePath);
            }

            log.Debug("Using testcentric-agent at " + agentExePath);

            Process p = new Process();

            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow  = true;
            p.EnableRaisingEvents       = true;
            p.Exited += (sender, e) => OnAgentExit((Process)sender, agentId);
            string arglist = agentId.ToString() + " " + ServerUrl + " " + agentArgs;

            targetRuntime = ServiceContext.GetService <RuntimeFrameworkService>().GetBestAvailableFramework(targetRuntime);

            if (targetRuntime.Runtime == Runtime.Mono)
            {
                p.StartInfo.FileName = targetRuntime.MonoExePath;
                string monoOptions = "--runtime=v" + targetRuntime.ClrVersion.ToString(3);
                if (debugTests || debugAgent)
                {
                    monoOptions += " --debug";
                }
                p.StartInfo.Arguments = string.Format("{0} \"{1}\" {2}", monoOptions, agentExePath, arglist);
            }
            else if (targetRuntime.Runtime == Runtime.Net)
            {
                p.StartInfo.FileName = agentExePath;
                // Override the COMPLUS_Version env variable, this would cause CLR meta host to run a CLR of the specific version
                string envVar = "v" + targetRuntime.ClrVersion.ToString(3);
                p.StartInfo.EnvironmentVariables["COMPLUS_Version"] = envVar;
                // Leave a marker that we have changed this variable, so that the agent could restore it for any code or child processes running within the agent
                string cpvOriginal = Environment.GetEnvironmentVariable("COMPLUS_Version");
                p.StartInfo.EnvironmentVariables["TestAgency_COMPLUS_Version_Original"] = string.IsNullOrEmpty(cpvOriginal) ? "NULL" : cpvOriginal;
                p.StartInfo.Arguments       = arglist;
                p.StartInfo.LoadUserProfile = loadUserProfile;
            }
            else
            {
                p.StartInfo.FileName  = agentExePath;
                p.StartInfo.Arguments = arglist;
            }

            p.Start();
            log.Debug("Launched Agent process {0} - see testcentric-agent_{0}.log", p.Id);
            log.Debug("Command line: \"{0}\" {1}", p.StartInfo.FileName, p.StartInfo.Arguments);

            _agents.Start(agentId, p);
            return(p);
        }
Example #56
0
 public ProcessRunner(IServiceLocator services, TestPackage package) : base(services, package)
 {
     _agency = Services.GetService <TestAgency>();
 }
        /// <summary>
        /// Selects a target runtime framework for a TestPackage based on
        /// the settings in the package and the assemblies themselves.
        /// The package RuntimeFramework setting may be updated as a result
        /// and a string representing the selected runtime is returned.
        /// </summary>
        /// <param name="package">A TestPackage</param>
        /// <returns>A string representing the selected RuntimeFramework</returns>
        public string SelectRuntimeFramework(TestPackage package)
        {
            // Evaluate package target framework
            ApplyImageData(package);

            // Examine the provided settings
            RuntimeFramework currentFramework = RuntimeFramework.CurrentFramework;
            string           frameworkSetting = package.GetSetting(EnginePackageSettings.RuntimeFramework, "");

            RuntimeFramework requestedFramework;

            if (frameworkSetting.Length > 0)
            {
                if (!RuntimeFramework.TryParse(frameworkSetting, out requestedFramework))
                {
                    throw new NUnitEngineException("Invalid or unknown framework requested: " + frameworkSetting);
                }
            }
            else
            {
                requestedFramework = new RuntimeFramework(RuntimeType.Any, RuntimeFramework.DefaultVersion);
            }

            log.Debug("Current framework is {0}", currentFramework);
            if (requestedFramework == null)
            {
                log.Debug("No specific framework requested");
            }
            else
            {
                log.Debug("Requested framework is {0}", requestedFramework);
            }

            RuntimeType targetRuntime = requestedFramework.Runtime;
            Version     targetVersion = requestedFramework.FrameworkVersion;

            if (targetRuntime == RuntimeType.Any)
            {
                targetRuntime = currentFramework.Runtime;
            }

            if (targetVersion == RuntimeFramework.DefaultVersion)
            {
                targetVersion = package.GetSetting(InternalEnginePackageSettings.ImageRuntimeVersion, currentFramework.FrameworkVersion);
            }

            if (!new RuntimeFramework(targetRuntime, targetVersion).IsAvailable)
            {
                log.Debug("Preferred version {0} is not installed or this NUnit installation does not support it", targetVersion);
                if (targetVersion < currentFramework.FrameworkVersion)
                {
                    targetVersion = currentFramework.FrameworkVersion;
                }
            }

            RuntimeFramework targetFramework = new RuntimeFramework(targetRuntime, targetVersion);

            package.Settings[EnginePackageSettings.RuntimeFramework] = targetFramework.ToString();

            log.Debug("Test will use {0} framework", targetFramework);

            return(targetFramework.ToString());
        }
Example #58
0
        private static TestRunner MakeRunnerFromCommandLine(ConsoleOptions options)
        {
            TestPackage package;

            ConsoleOptions.DomainUsage domainUsage = ConsoleOptions.DomainUsage.Default;

            if (options.IsTestProject)
            {
                NUnitProject project    = NUnitProject.LoadProject((string)options.Parameters[0]);
                string       configName = options.config;
                if (configName != null)
                {
                    project.SetActiveConfig(configName);
                }

                package          = project.ActiveConfig.MakeTestPackage();
                package.TestName = options.fixture;

                domainUsage = ConsoleOptions.DomainUsage.Single;
            }
            else if (options.Parameters.Count == 1)
            {
                package     = new TestPackage((string)options.Parameters[0]);
                domainUsage = ConsoleOptions.DomainUsage.Single;
            }
            else
            {
                package     = new TestPackage("UNNAMED", options.Parameters);
                domainUsage = ConsoleOptions.DomainUsage.Multiple;
            }

            if (options.domain != ConsoleOptions.DomainUsage.Default)
            {
                domainUsage = options.domain;
            }

            TestRunner testRunner = null;

            switch (domainUsage)
            {
            case ConsoleOptions.DomainUsage.None:
                testRunner = new NUnit.Core.RemoteTestRunner();
                // Make sure that addins are available
                CoreExtensions.Host.AddinRegistry = Services.AddinRegistry;
                break;

            case ConsoleOptions.DomainUsage.Single:
                testRunner = new TestDomain();
                break;

            case ConsoleOptions.DomainUsage.Multiple:
                testRunner = new MultipleTestDomainRunner();
                break;
            }

            package.TestName = options.fixture;
            package.Settings["ShadowCopyFiles"]   = !options.noshadow;
            package.Settings["UseThreadedRunner"] = !options.nothread;
            testRunner.Load(package);

            return(testRunner);
        }
Example #59
0
 public ITestAgent GetAgent(TestPackage package, int waitTime)
 {
     // TODO: Decide if we should reuse agents
     return(CreateRemoteAgent(package, waitTime));
 }
Example #60
0
        public AgentProcess(TestAgency agency, TestPackage package, Guid agentId)
        {
            // Get target runtime
            string runtimeSetting = package.GetSetting(EnginePackageSettings.TargetRuntimeFramework, "");

            TargetRuntime = RuntimeFramework.Parse(runtimeSetting);

            // Access other package settings
            bool   runAsX86        = package.GetSetting(EnginePackageSettings.RunAsX86, false);
            bool   debugTests      = package.GetSetting(EnginePackageSettings.DebugTests, false);
            bool   debugAgent      = package.GetSetting(EnginePackageSettings.DebugAgent, false);
            string traceLevel      = package.GetSetting(EnginePackageSettings.InternalTraceLevel, "Off");
            bool   loadUserProfile = package.GetSetting(EnginePackageSettings.LoadUserProfile, false);
            string workDirectory   = package.GetSetting(EnginePackageSettings.WorkDirectory, string.Empty);

            AgentArgs = new StringBuilder($"{agentId} {agency.ServerUrl} --pid={Process.GetCurrentProcess().Id}");

            // Set options that need to be in effect before the package
            // is loaded by using the command line.
            if (traceLevel != "Off")
            {
                AgentArgs.Append(" --trace=").EscapeProcessArgument(traceLevel);
            }
            if (debugAgent)
            {
                AgentArgs.Append(" --debug-agent");
            }
            if (workDirectory != string.Empty)
            {
                AgentArgs.Append(" --work=").EscapeProcessArgument(workDirectory);
            }

            AgentExePath = GetTestAgentExePath(TargetRuntime, runAsX86);

            log.Debug("Using nunit-agent at " + AgentExePath);

            StartInfo.UseShellExecute  = false;
            StartInfo.CreateNoWindow   = true;
            StartInfo.WorkingDirectory = Environment.CurrentDirectory;
            EnableRaisingEvents        = true;

            if (TargetRuntime.Runtime == RuntimeType.Mono)
            {
                StartInfo.FileName = RuntimeFramework.MonoExePath;
                string monoOptions = "--runtime=v" + TargetRuntime.ClrVersion.ToString(3);
                if (debugTests || debugAgent)
                {
                    monoOptions += " --debug";
                }
                StartInfo.Arguments = string.Format("{0} \"{1}\" {2}", monoOptions, AgentExePath, AgentArgs);
            }
            else if (TargetRuntime.Runtime == RuntimeType.Net)
            {
                StartInfo.FileName        = AgentExePath;
                StartInfo.Arguments       = AgentArgs.ToString();
                StartInfo.LoadUserProfile = loadUserProfile;
            }
            else
            {
                StartInfo.FileName  = AgentExePath;
                StartInfo.Arguments = AgentArgs.ToString();
            }
        }