Ejemplo n.º 1
0
        /// <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()
        {
            log.Info("Loading " + TestPackage.Name);
            Unload();

            string frameworkSetting = TestPackage.GetSetting(RunnerSettings.RuntimeFramework, "");
            this.RuntimeFramework = frameworkSetting != ""
                ? RuntimeFramework.Parse(frameworkSetting)
                : RuntimeFramework.CurrentFramework;

            bool enableDebug = TestPackage.GetSetting("AgentDebug", false);
            bool verbose = TestPackage.GetSetting("Verbose", false);
            string agentArgs = string.Empty;
            if (enableDebug) agentArgs += " --pause";
            if (verbose) agentArgs += " --verbose";

            try
            {
                CreateAgentAndRunner(enableDebug, agentArgs);

                return _remoteRunner.Load();
            }
            catch(Exception)
            {
                // TODO: Check if this is really needed
                // Clean up if the load failed
                Unload();
                throw;
            }
        }
 public void CanCreateUsingFrameworkVersion(FrameworkData data)
 {
     RuntimeFramework framework = new RuntimeFramework(data.runtime, data.frameworkVersion);
     Assert.AreEqual(data.runtime, framework.Runtime);
     Assert.AreEqual(data.frameworkVersion, framework.FrameworkVersion);
     Assert.AreEqual(data.clrVersion, framework.ClrVersion);
 }
Ejemplo n.º 3
0
		private void CheckRuntimePlatforms( RuntimeFramework runtimeFramework, 
			string expectedPlatforms )
		{
			CheckPlatforms(
				new PlatformHelper( OSPlatform.CurrentPlatform, runtimeFramework ),
				expectedPlatforms,
				PlatformHelper.RuntimePlatforms + ",NET-1.0,NET-1.1,NET-2.0,NET-3.0,NET-3.5,NET-4.0,MONO-1.0,MONO-2.0,MONO-3.0,MONO-3.5,MONO-4.0" );
		}
        public void CanCreateUsingClrVersion(FrameworkData data)
        {
            Assume.That(data.frameworkVersion.Major != 3);

            RuntimeFramework framework = new RuntimeFramework(data.runtime, data.clrVersion);
            Assert.AreEqual(data.runtime, framework.Runtime);
            Assert.AreEqual(data.frameworkVersion, framework.FrameworkVersion);
            Assert.AreEqual(data.clrVersion, framework.ClrVersion);
        }
Ejemplo n.º 5
0
        public void CanCreateUsingClrVersion(FrameworkData data)
        {
            // Can't create using CLR version if we expect version 3.0, 3.5 or 4.5
            Assume.That(data.frameworkVersion.Major != 3);
            if (data.frameworkVersion.Major == 4)
                Assume.That(data.frameworkVersion.Minor != 5);

            RuntimeFramework framework = new RuntimeFramework(data.runtime, data.clrVersion);
            Assert.AreEqual(data.runtime, framework.Runtime);
            Assert.AreEqual(data.frameworkVersion, framework.FrameworkVersion);
            Assert.AreEqual(data.clrVersion, framework.ClrVersion);
        }
        /// <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;
        }
Ejemplo n.º 7
0
        private static bool FrameworksMatch(RuntimeFramework f1, RuntimeFramework f2)
        {
            var rt1 = f1.Runtime;
            var rt2 = f2.Runtime;

            if (rt1 != RuntimeType.Any && rt2 != RuntimeType.Any && rt1 != rt2)
                return false;

            var v1 = f1.ClrVersion;
            var v2 = f2.ClrVersion;

            if (v1 == AnyVersion || v2 == AnyVersion)
                return true;

            return v1.Major == v2.Major &&
                   v1.Minor == v2.Minor &&
                   (v1.Build < 0 || v2.Build < 0 || v1.Build == v2.Build) &&
                   (v1.Revision < 0 || v2.Revision < 0 || v1.Revision == v2.Revision) &&
                   f1.FrameworkVersion.Major == f2.FrameworkVersion.Major &&
                   f1.FrameworkVersion.Minor == f2.FrameworkVersion.Minor;
        }
Ejemplo n.º 8
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 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)
        {
            // Start by examining the provided settings
            RuntimeFramework currentFramework = RuntimeFramework.CurrentFramework;
            string frameworkSetting = package.GetSetting(EnginePackageSettings.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;

            // Examine the package 
            ApplyImageData(package);

            // Modify settings if necessary
            targetVersion = package.GetSetting(InternalEnginePackageSettings.ImageRuntimeVersion, targetVersion);
            RuntimeFramework checkFramework = new RuntimeFramework(targetRuntime, targetVersion);
            if (!checkFramework.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();
        }
Ejemplo n.º 9
0
        public void WellKnownClrVersions_SupportEquivalentFrameworkVersions(string s)
        {
            RuntimeFramework f1 = RuntimeFramework.Parse(s);
            RuntimeFramework f2 = new RuntimeFramework(f1.Runtime, f1.FrameworkVersion);

            Assert.That(f1.Runtime, Is.EqualTo(f2.Runtime));
            Assert.That(f1.FrameworkVersion, Is.EqualTo(f2.FrameworkVersion));
            Assert.That(f1.ClrVersion, Is.EqualTo(f2.ClrVersion));

            Assert.That(f1.Supports(f2));
            Assert.That(f2.Supports(f1));
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
0
 public bool CanMatchRuntimes(RuntimeFramework f1, RuntimeFramework f2)
 {
     return(f1.Supports(f2));
 }
Ejemplo n.º 12
0
 private void CheckRuntimePlatforms( RuntimeFramework runtimeFramework, 
     string expectedPlatforms)
 {
     CheckPlatforms(
         new PlatformHelper( Environment.OSVersion, runtimeFramework ),
         expectedPlatforms,
         PlatformHelper.RuntimePlatforms + ",NET-1.0,NET-1.1,NET-2.0,MONO-1.0,MONO-2.0" );
 }
Ejemplo n.º 13
0
 public TestAgent GetAgent(RuntimeFramework framework, int waitTime)
 {
     return(GetAgent(framework, waitTime, false));
 }
Ejemplo n.º 14
0
 public void SameRuntime_LowerVersion_DifferentCLR_NotSupported(RuntimeFramework f1, RuntimeFramework f2)
 {
     Assert.False(f1.Supports(f2));
 }
Ejemplo n.º 15
0
        public void UnspecifiedRuntime_SameVersion_Supported(RuntimeFramework f1)
        {
            // NOTE: Mono 1.0 has a 1.1 ClrVersion, so this doesn't work.
            // Since we're phasing out support for 1.x versions, we
            // aren't planning to fix this.
            Assume.That(f1.ToString() != "mono-1.0");

            RuntimeFramework f2 = new RuntimeFramework(RuntimeType.Any, f1.FrameworkVersion);
            Assert.That(f1.Supports(f2));
            Assert.That(f2.Supports(f1));
        }
Ejemplo n.º 16
0
 public void SameRuntime_HigherVersion_NotSupported(RuntimeFramework f1, RuntimeFramework f2)
 {
     Assert.False(f1.Supports(f2));
 }
Ejemplo n.º 17
0
 public void SameRuntime_LowerVersion_SameCLR_Supported(RuntimeFramework f1, RuntimeFramework f2)
 {
     Assert.That(f1.Supports(f2));
 }
Ejemplo n.º 18
0
 public void AnyFrameworkSupportsItself(RuntimeFramework framework)
 {
     Assert.That(framework.Supports(framework));
 }
Ejemplo n.º 19
0
        //public void DestroyAgent( ITestAgent agent )
        //{
        //    AgentRecord r = agentData[agent.Id];
        //    if ( r != null )
        //    {
        //        if( !r.Process.HasExited )
        //            r.Agent.Stop();
        //        agentData[r.Id] = null;
        //    }
        //}
        #endregion

        #region Helper Methods
        private Guid LaunchAgentProcess(TestPackage package)
        {
            RuntimeFramework targetRuntime  = RuntimeFramework.CurrentFramework;
            string           runtimeSetting = package.GetSetting(EnginePackageSettings.RuntimeFramework, "");

            if (runtimeSetting != "")
            {
                targetRuntime = RuntimeFramework.Parse(runtimeSetting);
            }

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

            // Set options that need to be in effect before the package
            // is loaded by using the command line.
            string agentArgs = string.Empty;

            if (debugAgent)
            {
                agentArgs += " --debug-agent";
            }
            if (traceLevel != "Off")
            {
                agentArgs += " --trace:" + traceLevel;
            }

            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(targetRuntime.ClrVersion, useX86Agent);

            if (agentExePath == null)
            {
                throw new ArgumentException(
                          string.Format("NUnit components for version {0} of the CLR are not installed",
                                        targetRuntime.ClrVersion.ToString()), "targetRuntime");
            }

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

            Process p = new Process();

            p.StartInfo.UseShellExecute = false;
            Guid   agentId = Guid.NewGuid();
            string arglist = agentId.ToString() + " " + ServerUrl + " " + agentArgs;

            if (targetRuntime.ClrVersion.Build < 0)
            {
                targetRuntime = RuntimeFramework.GetBestAvailableFramework(targetRuntime);
            }

            switch (targetRuntime.Runtime)
            {
            case RuntimeType.Mono:
                p.StartInfo.FileName = NUnitConfiguration.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.Exited += new EventHandler(OnProcessExit);
            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);
        }
Ejemplo n.º 20
0
        public int Execute(ConsoleOptions options)
        {
            this.workDir = options.work;
            if (workDir == null || workDir == string.Empty)
            {
                workDir = Environment.CurrentDirectory;
            }
            else
            {
                workDir = Path.GetFullPath(workDir);
                if (!Directory.Exists(workDir))
                {
                    Directory.CreateDirectory(workDir);
                }
            }

            TextWriter outWriter      = Console.Out;
            bool       redirectOutput = options.output != null && options.output != string.Empty;

            if (redirectOutput)
            {
                StreamWriter outStreamWriter = new StreamWriter(Path.Combine(workDir, options.output));
                outStreamWriter.AutoFlush = true;
                outWriter = outStreamWriter;
            }

            TextWriter errorWriter   = Console.Error;
            bool       redirectError = options.err != null && options.err != string.Empty;

            if (redirectError)
            {
                StreamWriter errorStreamWriter = new StreamWriter(Path.Combine(workDir, options.err));
                errorStreamWriter.AutoFlush = true;
                errorWriter = errorStreamWriter;
            }

            TestPackage package = MakeTestPackage(options);

            ProcessModel processModel = package.Settings.Contains("ProcessModel")
                ? (ProcessModel)package.Settings["ProcessModel"]
                : ProcessModel.Default;

            DomainUsage domainUsage = package.Settings.Contains("DomainUsage")
                ? (DomainUsage)package.Settings["DomainUsage"]
                : DomainUsage.Default;

            RuntimeFramework framework = package.Settings.Contains("RuntimeFramework")
                ? (RuntimeFramework)package.Settings["RuntimeFramework"]
                : RuntimeFramework.CurrentFramework;

#if CLR_2_0 || CLR_4_0
            Console.WriteLine("ProcessModel: {0}    DomainUsage: {1}", processModel, domainUsage);

            Console.WriteLine("Execution Runtime: {0}", framework);
#else
            Console.WriteLine("DomainUsage: {0}", domainUsage);

            if (processModel != ProcessModel.Default && processModel != ProcessModel.Single)
            {
                Console.WriteLine("Warning: Ignoring project setting 'processModel={0}'", processModel);
            }

            if (!RuntimeFramework.CurrentFramework.Supports(framework))
            {
                Console.WriteLine("Warning: Ignoring project setting 'runtimeFramework={0}'", framework);
            }
#endif

            using (TestRunner testRunner = new DefaultTestRunnerFactory().MakeTestRunner(package))
            {
                testRunner.Load(package);

                if (testRunner.Test == null)
                {
                    testRunner.Unload();
                    Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
                    return(FIXTURE_NOT_FOUND);
                }

                EventCollector collector = new EventCollector(options, outWriter, errorWriter);

                TestFilter       testFilter = TestFilter.Empty;
                SimpleNameFilter nameFilter = new SimpleNameFilter();

                if (options.run != null && options.run != string.Empty)
                {
                    Console.WriteLine("Selected test(s): " + options.run);
                    foreach (string name in TestNameParser.Parse(options.run))
                    {
                        nameFilter.Add(name);
                    }
                    testFilter = nameFilter;
                }

                if (options.runlist != null && options.runlist != string.Empty)
                {
                    Console.WriteLine("Run list: " + options.runlist);
                    using (StreamReader rdr = new StreamReader(options.runlist))
                    {
                        // NOTE: We can't use rdr.EndOfStream because it's
                        // not present in .NET 1.x.
                        string line = rdr.ReadLine();
                        while (line != null)
                        {
                            if (line[0] != '#')
                            {
                                nameFilter.Add(line);
                            }
                            line = rdr.ReadLine();
                        }
                    }
                    testFilter = nameFilter;
                }

                if (options.include != null && options.include != string.Empty)
                {
                    TestFilter includeFilter = new CategoryExpression(options.include).Filter;
                    Console.WriteLine("Included categories: " + includeFilter.ToString());

                    if (testFilter.IsEmpty)
                    {
                        testFilter = includeFilter;
                    }
                    else
                    {
                        testFilter = new AndFilter(testFilter, includeFilter);
                    }
                }

                if (options.exclude != null && options.exclude != string.Empty)
                {
                    TestFilter excludeFilter = new NotFilter(new CategoryExpression(options.exclude).Filter);
                    Console.WriteLine("Excluded categories: " + excludeFilter.ToString());

                    if (testFilter.IsEmpty)
                    {
                        testFilter = excludeFilter;
                    }
                    else if (testFilter is AndFilter)
                    {
                        ((AndFilter)testFilter).Add(excludeFilter);
                    }
                    else
                    {
                        testFilter = new AndFilter(testFilter, excludeFilter);
                    }
                }

                if (testFilter is NotFilter)
                {
                    ((NotFilter)testFilter).TopLevel = true;
                }

                TestResult result         = null;
                string     savedDirectory = Environment.CurrentDirectory;
                TextWriter savedOut       = Console.Out;
                TextWriter savedError     = Console.Error;

                try
                {
                    result = testRunner.Run(collector, testFilter, false, LoggingThreshold.Off);
                }
                finally
                {
                    outWriter.Flush();
                    errorWriter.Flush();

                    if (redirectOutput)
                    {
                        outWriter.Close();
                    }
                    if (redirectError)
                    {
                        errorWriter.Close();
                    }

                    Environment.CurrentDirectory = savedDirectory;
                    Console.SetOut(savedOut);
                    Console.SetError(savedError);
                }

                Console.WriteLine();

                int returnCode = UNEXPECTED_ERROR;

                if (result != null)
                {
                    string           xmlOutput = CreateXmlOutput(result);
                    ResultSummarizer summary   = new ResultSummarizer(result);

                    if (options.xmlConsole)
                    {
                        Console.WriteLine(xmlOutput);
                    }
                    else
                    {
                        WriteSummaryReport(summary);

                        bool hasErrors = summary.Errors > 0 || summary.Failures > 0 || result.IsError || result.IsFailure;

                        if (options.stoponerror && (hasErrors || summary.NotRunnable > 0))
                        {
                            Console.WriteLine("Test run was stopped after first error, as requested.");
                            Console.WriteLine();
                        }

                        if (hasErrors)
                        {
                            WriteErrorsAndFailuresReport(result);
                        }

                        if (summary.TestsNotRun > 0)
                        {
                            WriteNotRunReport(result);
                        }

                        if (!options.noresult)
                        {
                            // Write xml output here
                            string xmlResultFile = options.result == null || options.result == string.Empty
                                ? "TestResult.xml" : options.result;

                            using (StreamWriter writer = new StreamWriter(Path.Combine(workDir, xmlResultFile)))
                            {
                                writer.Write(xmlOutput);
                            }
                        }
                    }

                    returnCode = summary.Errors + summary.Failures + summary.NotRunnable;
                }

                if (collector.HasExceptions)
                {
                    collector.WriteExceptions();
                    returnCode = UNEXPECTED_ERROR;
                }

                return(returnCode);
            }
        }
Ejemplo n.º 21
0
 public void SameRuntime_LowerVersion_SameCLR_Supported(RuntimeFramework f1, RuntimeFramework f2)
 {
     Assert.That(f1.Supports(f2));
 }
Ejemplo n.º 22
0
 private RuntimeFramework Framework(string representation)
 {
     return(RuntimeFramework.Parse(representation));
 }
Ejemplo n.º 23
0
        public void DifferentRuntimes_NotSupported(RuntimeFramework f1)
        {
            RuntimeFramework f2 = new RuntimeFramework(RuntimeType.Net, f1.FrameworkVersion);

            Assert.False(f1.Supports(f2));
            Assert.False(f2.Supports(f1));
        }
Ejemplo n.º 24
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 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)
        {
            RuntimeFramework currentFramework = RuntimeFramework.CurrentFramework;
            string frameworkSetting = package.GetSetting(PackageSettings.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)
            {
                var packages = package.SubPackages;
                if (packages.Count == 0)
                    packages.Add(package);

                foreach (var subPackage in packages)
                {
                    var assembly = subPackage.FullName;

                    // If the file is not an assembly or doesn't exist, then it can't
                    // contribute any information to the decision, so we skip it.
                    if (PathUtils.IsAssemblyFileType(assembly) && File.Exists(assembly))
                    {
                        var module = AssemblyDefinition.ReadAssembly(assembly).MainModule;
                        var NativeEntryPoint = (ModuleAttributes)16;
                        var mask = ModuleAttributes.Required32Bit | NativeEntryPoint;
                        if (module.Architecture != TargetArchitecture.AMD64 && 
                            module.Architecture != TargetArchitecture.IA64 &&
                           (module.Attributes & mask) != 0)
                        {
                            package.Settings[PackageSettings.RunAsX86] = true;
                            log.Debug("Assembly {0} will be run x86", assembly);
                        }

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

                        // TODO: We are doing two jobs here: (1) getting the
                        // target version and (2) applying a policy that says
                        // we run under the highest version of all assemblies.
                        // We should implement the policy at a higher level.
                        if (v > targetVersion) targetVersion = v;
                    }
                }
                
                RuntimeFramework checkFramework = new RuntimeFramework(targetRuntime, targetVersion);
                if (!checkFramework.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[PackageSettings.RuntimeFramework] = targetFramework.ToString();

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

            return targetFramework.ToString();
        }
Ejemplo n.º 25
0
 public void UnspecifiedRuntimeAndVersion_Supported(RuntimeFramework f1)
 {
     RuntimeFramework f2 = new RuntimeFramework(RuntimeType.Any, RuntimeFramework.DefaultVersion);
     Assert.That(f1.Supports(f2));
     Assert.That(f2.Supports(f1));
 }
Ejemplo n.º 26
0
        public static string GetTestAgentExePath(RuntimeFramework targetRuntime, bool requires32Bit)
        {
            string engineDir = NUnitConfiguration.EngineDirectory;

            if (engineDir == null)
            {
                return(null);
            }

            string agentName = requires32Bit
                ? "testcentric-agent-x86"
                : "testcentric-agent";

            string agentPath = null;

            switch (targetRuntime.Runtime.FrameworkIdentifier)
            {
            case FrameworkIdentifiers.NetFramework:
                switch (targetRuntime.FrameworkVersion.Major)
                {
                case 2:
                case 3:
                    agentPath = Path.Combine(engineDir, "agents/net20/" + agentName + ".exe");
                    break;

                case 4:
                    agentPath = Path.Combine(engineDir, "agents/net40/" + agentName + ".exe");
                    break;
                }
                break;

            case FrameworkIdentifiers.NetCoreApp:
                switch (targetRuntime.FrameworkVersion.Major)
                {
                case 1:
                // TODO: For now, we don't have a .NET Core 1.1 agent and default to 2.1
                //agentPath = Path.Combine(engineDir, "agents/netcoreapp1.1/" + agentName + ".dll");
                //break;

                case 2:
                    agentPath = Path.Combine(engineDir, "agents/netcoreapp2.1/" + agentName + ".dll");
                    break;

                case 3:
                    agentPath = Path.Combine(engineDir, "agents/netcoreapp3.1/" + agentName + ".dll");
                    break;
                }
                break;
            }

            if (agentPath == null)
            {
                throw new InvalidOperationException($"Unsupported runtime: {targetRuntime.Runtime}");
            }

            // TODO: Temporarily leaving out this check because it breaks some AgentProcessTests
            //if (!File.Exists(agentPath))
            //    throw new FileNotFoundException($"Agent not found: {agentPath}");

            return(agentPath);
        }
Ejemplo n.º 27
0
        public int Execute(ConsoleOptions options)
        {
            this.workDir = options.work;
            if (workDir == null || workDir == string.Empty)
            {
                workDir = Environment.CurrentDirectory;
            }
            else
            {
                workDir = Path.GetFullPath(workDir);
                if (!Directory.Exists(workDir))
                {
                    Directory.CreateDirectory(workDir);
                }
            }

            TextWriter outWriter      = Console.Out;
            bool       redirectOutput = options.output != null && options.output != string.Empty;

            if (redirectOutput)
            {
                StreamWriter outStreamWriter = new StreamWriter(Path.Combine(workDir, options.output));
                outStreamWriter.AutoFlush = true;
                outWriter = outStreamWriter;
            }

            TextWriter errorWriter   = Console.Error;
            bool       redirectError = options.err != null && options.err != string.Empty;

            if (redirectError)
            {
                StreamWriter errorStreamWriter = new StreamWriter(Path.Combine(workDir, options.err));
                errorStreamWriter.AutoFlush = true;
                errorWriter = errorStreamWriter;
            }

            TestPackage package = MakeTestPackage(options);

            ProcessModel processModel = package.Settings.Contains("ProcessModel")
                ? (ProcessModel)package.Settings["ProcessModel"]
                : ProcessModel.Default;

            DomainUsage domainUsage = package.Settings.Contains("DomainUsage")
                ? (DomainUsage)package.Settings["DomainUsage"]
                : DomainUsage.Default;

            RuntimeFramework framework = package.Settings.Contains("RuntimeFramework")
                ? (RuntimeFramework)package.Settings["RuntimeFramework"]
                : RuntimeFramework.CurrentFramework;

#if CLR_2_0 || CLR_4_0
            Console.WriteLine("ProcessModel: {0}    DomainUsage: {1}", processModel, domainUsage);

            Console.WriteLine("Execution Runtime: {0}", framework);
#else
            Console.WriteLine("DomainUsage: {0}", domainUsage);

            if (processModel != ProcessModel.Default && processModel != ProcessModel.Single)
            {
                Console.WriteLine("Warning: Ignoring project setting 'processModel={0}'", processModel);
            }

            if (!RuntimeFramework.CurrentFramework.Supports(framework))
            {
                Console.WriteLine("Warning: Ignoring project setting 'runtimeFramework={0}'", framework);
            }
#endif

            using (TestRunner testRunner = new DefaultTestRunnerFactory().MakeTestRunner(package))
            {
                if (options.compatibility)
                {
                    Compatibility.Initialize(workDir);
                }

                testRunner.Load(package);

                if (testRunner.Test == null)
                {
                    testRunner.Unload();
                    Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
                    return(FIXTURE_NOT_FOUND);
                }

                EventCollector collector = new EventCollector(options, outWriter, errorWriter);

                TestFilter testFilter;

                if (!CreateTestFilter(options, out testFilter))
                {
                    return(INVALID_ARG);
                }

                TestResult result         = null;
                string     savedDirectory = Environment.CurrentDirectory;
                TextWriter savedOut       = Console.Out;
                TextWriter savedError     = Console.Error;

                try
                {
                    result = testRunner.Run(collector, testFilter, true, LoggingThreshold.Off);
                }
                finally
                {
                    outWriter.Flush();
                    errorWriter.Flush();

                    if (redirectOutput)
                    {
                        outWriter.Close();
                    }

                    if (redirectError)
                    {
                        errorWriter.Close();
                    }

                    Environment.CurrentDirectory = savedDirectory;
                    Console.SetOut(savedOut);
                    Console.SetError(savedError);
                }

                Console.WriteLine();

                int returnCode = UNEXPECTED_ERROR;

                if (result != null)
                {
                    string xmlOutput = CreateXmlOutput(result);
                    if (options.xmlConsole)
                    {
                        Console.WriteLine(xmlOutput);
                    }
                    else
                    {
                        new ResultReporter(result, options).ReportResults();

                        // Check both new and old option forms
                        if (!options.noresult && !options.noxml)
                        {
                            // Write xml output here
                            string xmlResultFile = !string.IsNullOrEmpty(options.result)
                                ? options.result
                                : !string.IsNullOrEmpty(options.xml)
                                    ? options.xml
                                    : "TestResult.xml";

                            using (StreamWriter writer = new StreamWriter(Path.Combine(workDir, xmlResultFile)))
                            {
                                writer.Write(xmlOutput);
                            }
                        }
                    }

                    ResultSummarizer summary = new ResultSummarizer(result);
                    returnCode = summary.Errors + summary.Failures + summary.NotRunnable;
                }

                if (collector.HasExceptions)
                {
                    collector.WriteExceptions();
                    returnCode = UNEXPECTED_ERROR;
                }

                return(returnCode);
            }
        }
Ejemplo n.º 28
0
        private RuntimeFramework SelectRuntimeFrameworkInner(TestPackage package)
        {
            foreach (var subPackage in package.SubPackages)
            {
                SelectRuntimeFrameworkInner(subPackage);
            }

            // Examine the provided settings
            RuntimeFramework currentFramework = RuntimeFramework.CurrentFramework;

            log.Debug("Current framework is " + currentFramework);

            string frameworkSetting = package.GetSetting(EnginePackageSettings.RequestedRuntimeFramework, "");

            RuntimeFramework requestedFramework;

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

                log.Debug($"Requested framework for {package.Name} is {requestedFramework}");

                if (!IsAvailable(frameworkSetting))
                {
                    throw new NUnitEngineException("Requested framework is not available: " + frameworkSetting);
                }

                package.Settings[EnginePackageSettings.TargetRuntimeFramework] = frameworkSetting;

                return(requestedFramework);
            }

            log.Debug($"No specific framework requested for {package.Name}");

            string      imageTargetFrameworkNameSetting = package.GetSetting(InternalEnginePackageSettings.ImageTargetFrameworkName, "");
            RuntimeType targetRuntime;
            Version     targetVersion;

            if (string.IsNullOrEmpty(imageTargetFrameworkNameSetting))
            {
                // Assume .NET Framework
                targetRuntime = currentFramework.Runtime;
                targetVersion = package.GetSetting(InternalEnginePackageSettings.ImageRuntimeVersion, new Version(2, 0));
            }
            else
            {
                FrameworkName frameworkName = new FrameworkName(imageTargetFrameworkNameSetting);

                switch (frameworkName.Identifier)
                {
                case ".NETFramework":
                    targetRuntime = RuntimeType.Net;
                    break;

                case ".NETCoreApp":
                    targetRuntime = RuntimeType.NetCore;
                    break;

                default:
                    throw new NUnitEngineException("Unsupported Target Framework: " + imageTargetFrameworkNameSetting);
                }

                targetVersion = frameworkName.Version;
            }

            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.TargetRuntimeFramework] = targetFramework.ToString();

            log.Debug($"Test will use {targetFramework} for {package.Name}");
            return(targetFramework);
        }
Ejemplo n.º 29
0
        //private void OnProcessExit(object sender, EventArgs e)
        //{
        //    Process p = sender as Process;
        //    if (p != null)
        //        agentData.Remove(p.Id);
        //}

        //private AgentRecord FindAvailableAgent()
        //{
        //    foreach( AgentRecord r in agentData )
        //        if ( r.Status == AgentStatus.Ready)
        //        {
        //            log.Debug( "Reusing agent {0}", r.Id );
        //            r.Status = AgentStatus.Busy;
        //            return r;
        //        }

        //    return null;
        //}

        private ITestAgent CreateRemoteAgent(RuntimeFramework framework, int waitTime, bool enableDebug, string agentArgs)
        {
            Guid agentId = LaunchAgentProcess(framework, enableDebug, agentArgs);

            log.Debug( "Waiting for agent {0} to register", agentId.ToString("B") );

            int pollTime = 200;
            bool infinite = waitTime == Timeout.Infinite;

            while( infinite || waitTime > 0 )
            {
                Thread.Sleep( pollTime );
                if ( !infinite ) waitTime -= pollTime;
                ITestAgent agent = agentData[agentId].Agent;
                if ( agent != null )
                {
                    log.Debug( "Returning new agent {0}", agentId.ToString("B") );
                    return agent;
                }
            }

            return null;
        }
Ejemplo n.º 30
0
        //public void DestroyAgent( ITestAgent agent )
        //{
        //    AgentRecord r = agentData[agent.Id];
        //    if ( r != null )
        //    {
        //        if( !r.Process.HasExited )
        //            r.Agent.Stop();
        //        agentData[r.Id] = null;
        //    }
        //}
        #endregion

        #region Helper Methods
        private Guid LaunchAgentProcess(RuntimeFramework targetRuntime)
        {
            string agentExePath = GetTestAgentExePath(targetRuntime.ClrVersion);

            if (agentExePath == null)
            {
                throw new ArgumentException(
                          string.Format("NUnit components for version {0} of the CLR are not installed",
                                        targetRuntime.ClrVersion.ToString()), "targetRuntime");
            }

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

            Process p = new Process();

            p.StartInfo.UseShellExecute = false;
            Guid   agentId = Guid.NewGuid();
            string arglist = agentId.ToString() + " " + ServerUrl;

            if (Debugger.IsAttached)
            {
                arglist += " --launch-debugger";
            }

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

            case RuntimeType.Net:
                p.StartInfo.FileName = agentExePath;

                if (targetRuntime.ClrVersion.Build < 0)
                {
                    targetRuntime = RuntimeFramework.GetBestAvailableFramework(targetRuntime);
                }

                string envVar = "v" + targetRuntime.ClrVersion.ToString(3);
                p.StartInfo.EnvironmentVariables["COMPLUS_Version"] = envVar;

                p.StartInfo.Arguments = arglist;
                break;

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

            //p.Exited += new EventHandler(OnProcessExit);
            p.Start();
            log.Info("Launched Agent process {0} - see nunit-agent_{0}.log", p.Id);
            log.Info("Command line: \"{0}\" {1}", p.StartInfo.FileName, p.StartInfo.Arguments);

            agentData.Add(new AgentRecord(agentId, p, null, AgentStatus.Starting));
            return(agentId);
        }
Ejemplo n.º 31
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();
            }
        }
Ejemplo n.º 32
0
        //public void DestroyAgent( ITestAgent agent )
        //{
        //    AgentRecord r = agentData[agent.Id];
        //    if ( r != null )
        //    {
        //        if( !r.Process.HasExited )
        //            r.Agent.Stop();
        //        agentData[r.Id] = null;
        //    }
        //}
        #endregion

        #region Helper Methods
        private Guid LaunchAgentProcess(TestPackage package)
        {
            RuntimeFramework targetRuntime = RuntimeFramework.CurrentFramework;
            string runtimeSetting = package.GetSetting(PackageSettings.RuntimeFramework, "");
            if (runtimeSetting != "")
                targetRuntime = RuntimeFramework.Parse(runtimeSetting);

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

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

            // Set options that need to be in effect before the package
            // is loaded by using the command line.
            string agentArgs = string.Empty;
            if (debugAgent)
                agentArgs += " --debug-agent";
            if (traceLevel != "Off")
                agentArgs += " --trace:" + traceLevel;

            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(targetRuntime.ClrVersion, useX86Agent);

            if (agentExePath == null)
                throw new ArgumentException(
                    string.Format("NUnit components for version {0} of the CLR are not installed",
                    targetRuntime.ClrVersion.ToString()), "targetRuntime");

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

            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            Guid agentId = Guid.NewGuid();
            string arglist = agentId.ToString() + " " + ServerUrl + " " + agentArgs;

            if (targetRuntime.ClrVersion.Build < 0)
                targetRuntime = RuntimeFramework.GetBestAvailableFramework(targetRuntime);

            switch( targetRuntime.Runtime )
            {
                case RuntimeType.Mono:
                    p.StartInfo.FileName = NUnitConfiguration.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.Exited += new EventHandler(OnProcessExit);
            p.Start();
            log.Info("Launched Agent process {0} - see nunit-agent_{0}.log", p.Id);
            log.Info("Command line: \"{0}\" {1}", p.StartInfo.FileName, p.StartInfo.Arguments);

            _agentData.Add( new AgentRecord( agentId, p, null, AgentStatus.Starting ) );
            return agentId;
        }
Ejemplo n.º 33
0
 public void CurrentFrameworkMustBeAvailable()
 {
     Assert.That(RuntimeFramework.IsAvailable(RuntimeFramework.CurrentFramework));
 }
Ejemplo n.º 34
0
        public ITestAgent GetAgent(RuntimeFramework framework, int waitTime, bool enableDebug, string agentArgs)
        {
            log.Info("Getting agent for use under {0}", framework);
 
            if (!framework.IsAvailable)
                throw new ArgumentException(
                    string.Format("The {0} framework is not available", framework),
                    "framework");

            // TODO: Decide if we should reuse agents
            //AgentRecord r = FindAvailableRemoteAgent(type);
            //if ( r == null )
            //    r = CreateRemoteAgent(type, framework, waitTime);
            return CreateRemoteAgent(framework, waitTime, enableDebug, agentArgs);
        }
Ejemplo n.º 35
0
 public bool CanLoad(RuntimeFramework f1, RuntimeFramework f2)
 {
     return(f1.CanLoad(f2));
 }
Ejemplo n.º 36
0
        //public void DestroyAgent( ITestAgent agent )
        //{
        //    AgentRecord r = agentData[agent.Id];
        //    if ( r != null )
        //    {
        //        if( !r.Process.HasExited )
        //            r.Agent.Stop();
        //        agentData[r.Id] = null;
        //    }
        //}
        #endregion

        #region Helper Methods
        private Guid LaunchAgentProcess(TestPackage package)
        {
            string           runtimeSetting = package.GetSetting(PackageSettings.RuntimeFramework, "");
            RuntimeFramework targetRuntime  = RuntimeFramework.Parse(
                runtimeSetting != ""
                    ? runtimeSetting
                    : ServiceContext.RuntimeFrameworkService.SelectRuntimeFramework(package));

            bool   useX86Agent = package.GetSetting(PackageSettings.RunAsX86, false);
            bool   enableDebug = package.GetSetting("AgentDebug", false);
            bool   verbose     = package.GetSetting("Verbose", false);
            string agentArgs   = string.Empty;

            if (enableDebug)
            {
                agentArgs += " --pause";
            }
            if (verbose)
            {
                agentArgs += " --verbose";
            }

            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(targetRuntime.ClrVersion, useX86Agent);

            if (agentExePath == null)
            {
                throw new ArgumentException(
                          string.Format("NUnit components for version {0} of the CLR are not installed",
                                        targetRuntime.ClrVersion.ToString()), "targetRuntime");
            }

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

            Process p = new Process();

            p.StartInfo.UseShellExecute = false;
            Guid   agentId = Guid.NewGuid();
            string arglist = agentId.ToString() + " " + ServerUrl + " " + agentArgs;

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

            case RuntimeType.Net:
                p.StartInfo.FileName = agentExePath;

                if (targetRuntime.ClrVersion.Build < 0)
                {
                    targetRuntime = RuntimeFramework.GetBestAvailableFramework(targetRuntime);
                }

                string envVar = "v" + targetRuntime.ClrVersion.ToString(3);
                p.StartInfo.EnvironmentVariables["COMPLUS_Version"] = envVar;

                p.StartInfo.Arguments = arglist;
                break;

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

            //p.Exited += new EventHandler(OnProcessExit);
            p.Start();
            log.Info("Launched Agent process {0} - see nunit-agent_{0}.log", p.Id);
            log.Info("Command line: \"{0}\" {1}", p.StartInfo.FileName, p.StartInfo.Arguments);

            agentData.Add(new AgentRecord(agentId, p, null, AgentStatus.Starting));
            return(agentId);
        }
Ejemplo n.º 37
0
        public void Mono11IsSynonymousWithMono10()
        {
            RuntimeFramework mono10 = new RuntimeFramework(RuntimeType.Mono, new Version(1, 0));
            RuntimeFramework mono11 = new RuntimeFramework(RuntimeType.Mono, new Version(1, 1));

            Assert.That(mono11.ToString(), Is.EqualTo("mono-1.0"));
            Assert.That(mono11.DisplayName, Is.EqualTo("Mono 1.0"));
            Assert.That(mono11.Runtime, Is.EqualTo(RuntimeType.Mono));
            Assert.That(mono11.FrameworkVersion, Is.EqualTo(new Version(1, 0)));

            Assert.That(mono11.Supports(mono10));
            Assert.That(mono10.Supports(mono11));
        }
Ejemplo n.º 38
0
        public void LoadTest(string testName)
        {
            log.Info("Loading tests for " + Path.GetFileName(TestFileName));

            long startTime = DateTime.Now.Ticks;

            try
            {
                events.FireTestLoading(TestFileName);

                TestPackage package = MakeTestPackage(testName);
                if (testRunner != null)
                {
                    testRunner.Dispose();
                }
                testRunner = factory.MakeTestRunner(package);

                bool loaded = testRunner.Load(package);

                loadedTestName = testName;
                testResult     = null;
                reloadPending  = false;

                if (Services.UserSettings.GetSetting("Options.TestLoader.ReloadOnChange", true))
                {
                    InstallWatcher( );
                }

                if (loaded)
                {
                    this.currentFramework = package.Settings.Contains("RuntimeFramework")
                        ? package.Settings["RuntimeFramework"] as RuntimeFramework
                        : RuntimeFramework.CurrentFramework;

                    testProject.HasChangesRequiringReload = false;
                    events.FireTestLoaded(TestFileName, LoadedTest);
                }
                else
                {
                    lastException = new ApplicationException(string.Format("Unable to find test {0} in assembly", testName));
                    events.FireTestLoadFailed(TestFileName, lastException);
                }
            }
            catch (FileNotFoundException exception)
            {
                log.Error("File not found", exception);
                lastException = exception;

                foreach (string assembly in TestProject.ActiveConfig.Assemblies)
                {
                    if (Path.GetFileNameWithoutExtension(assembly) == exception.FileName &&
                        !PathUtils.SamePathOrUnder(testProject.ActiveConfig.BasePath, assembly))
                    {
                        lastException = new ApplicationException(string.Format("Unable to load {0} because it is not located under the AppBase", exception.FileName), exception);
                        break;
                    }
                }

                events.FireTestLoadFailed(TestFileName, lastException);

                double loadTime = (double)(DateTime.Now.Ticks - startTime) / (double)TimeSpan.TicksPerSecond;
                log.Info("Load completed in {0} seconds", loadTime);
            }
            catch (Exception exception)
            {
                log.Error("Failed to load test", exception);

                lastException = exception;
                events.FireTestLoadFailed(TestFileName, exception);
            }
        }
Ejemplo n.º 39
0
 public void SameRuntime_HigherVersion_NotSupported(RuntimeFramework f1, RuntimeFramework f2)
 {
     Assert.False(f1.Supports(f2));
 }
Ejemplo n.º 40
0
        public AgentProcess(TestAgency agency, TestPackage package, Guid agentId)
        {
            // Get target runtime
            string runtimeSetting = package.GetSetting(EnginePackageSettings.TargetRuntimeFramework, "");

            log.Debug($"TargetRuntime = {runtimeSetting}");
            TargetRuntime = RuntimeFramework.Parse(runtimeSetting);

            // Access other package settings
            bool   runAsX86        = package.GetSetting(EnginePackageSettings.RunAsX86, 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);

            string agencyUrl = TargetRuntime.Runtime == RuntimeType.NetCore ? agency.TcpEndPoint : agency.RemotingUrl;

            AgentArgs = new StringBuilder($"{agentId} {agencyUrl} --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);
                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 if (TargetRuntime.Runtime == RuntimeType.NetCore)
            {
                StartInfo.FileName        = "dotnet";
                StartInfo.Arguments       = $"{AgentExePath} {AgentArgs}";
                StartInfo.LoadUserProfile = loadUserProfile;

                // TODO: Remove the windows limitation and the use of a hard-coded path.
                if (runAsX86)
                {
                    if (Path.DirectorySeparatorChar != '\\')
                    {
                        throw new Exception("Running .NET Core as X86 is currently only supported on Windows");
                    }

                    var x86_dotnet_exe = @"C:\Program Files (x86)\dotnet\dotnet.exe";
                    if (!File.Exists(x86_dotnet_exe))
                    {
                        throw new Exception("The X86 version of dotnet.exe is not installed");
                    }

                    StartInfo.FileName = x86_dotnet_exe;
                }
            }
            else
            {
                StartInfo.FileName  = AgentExePath;
                StartInfo.Arguments = AgentArgs.ToString();
            }
        }
Ejemplo n.º 41
0
 public void SameRuntime_LowerVersion_DifferentCLR_NotSupported(RuntimeFramework f1, RuntimeFramework f2)
 {
     Assert.False(f1.Supports(f2));
 }
Ejemplo n.º 42
0
        private static TestPackage MakeTestPackage(ConsoleOptions options)
        {
            TestPackage      package;
            DomainUsage      domainUsage  = DomainUsage.Default;
            ProcessModel     processModel = ProcessModel.Default;
            RuntimeFramework framework    = RuntimeFramework.CurrentFramework;

            if (options.IsTestProject)
            {
                NUnitProject project =
                    Services.ProjectService.LoadProject((string)options.Parameters[0]);

                string configName = options.config;
                if (configName != null)
                {
                    project.SetActiveConfig(configName);
                }

                package      = project.ActiveConfig.MakeTestPackage();
                processModel = project.ProcessModel;
                domainUsage  = project.DomainUsage;
                framework    = project.ActiveConfig.RuntimeFramework;
            }
            else if (options.Parameters.Count == 1)
            {
                package     = new TestPackage((string)options.Parameters[0]);
                domainUsage = DomainUsage.Single;
            }
            else
            {
                // TODO: Figure out a better way to handle "anonymous" packages
                package             = new TestPackage(null, options.Parameters);
                package.AutoBinPath = true;
                domainUsage         = DomainUsage.Multiple;
            }

            if (options.process != ProcessModel.Default)
            {
                processModel = options.process;
            }

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

            if (options.framework != null)
            {
                framework = RuntimeFramework.Parse(options.framework);
            }

            package.TestName = options.fixture;

            package.Settings["ProcessModel"] = processModel;
            package.Settings["DomainUsage"]  = domainUsage;
            if (framework != null)
            {
                package.Settings["RuntimeFramework"] = framework;
            }



            if (domainUsage == DomainUsage.None)
            {
                // Make sure that addins are available
                CoreExtensions.Host.AddinRegistry = Services.AddinRegistry;
            }

            package.Settings["ShadowCopyFiles"]   = !options.noshadow;
            package.Settings["UseThreadedRunner"] = !options.nothread;
            package.Settings["DefaultTimeout"]    = options.timeout;

            return(package);
        }
Ejemplo n.º 43
0
        public void SameRuntimes_DifferentBuilds_NotSupported()
        {
            RuntimeFramework f1 = new RuntimeFramework(RuntimeType.Net, new Version(2, 0, 50727));
            RuntimeFramework f2 = new RuntimeFramework(RuntimeType.Net, new Version(2, 0, 40607));

            Assert.False(f1.Supports(f2));
            Assert.False(f2.Supports(f1));
        }
Ejemplo n.º 44
0
 public ITestAgent GetAgent(RuntimeFramework framework, int waitTime)
 {
     return(GetAgent(framework, waitTime, false, string.Empty));
 }
Ejemplo n.º 45
0
 public void UnspecifiedVersion_SameRuntime_Supported(RuntimeFramework f1)
 {
     RuntimeFramework f2 = new RuntimeFramework(f1.Runtime, RuntimeFramework.DefaultVersion);
     Assert.That(f1.Supports(f2));
     Assert.That(f2.Supports(f1));
 }
        // TODO: See if this can be unified with the Gui's MakeTestPackage
        private TestPackage MakeTestPackage(ConsoleOptions options)
        {
            TestPackage      package;
            DomainUsage      domainUsage  = DomainUsage.Default;
            ProcessModel     processModel = ProcessModel.Default;
            RuntimeFramework framework    = null;

            string[] parameters = new string[options.ParameterCount];
            for (int i = 0; i < options.ParameterCount; i++)
            {
                parameters[i] = Path.GetFullPath((string)options.Parameters[i]);
            }

            if (options.IsTestProject)
            {
                NUnitProject project =
                    Services.ProjectService.LoadProject(parameters[0]);

                string configName = options.config;
                if (configName != null)
                {
                    project.SetActiveConfig(configName);
                }

                package      = project.ActiveConfig.MakeTestPackage();
                processModel = project.ProcessModel;
                domainUsage  = project.DomainUsage;
                framework    = project.ActiveConfig.RuntimeFramework;
            }
            else if (parameters.Length == 1)
            {
                package     = new TestPackage(parameters[0]);
                domainUsage = DomainUsage.Single;
            }
            else
            {
                // TODO: Figure out a better way to handle "anonymous" packages
                package             = new TestPackage(null, parameters);
                package.AutoBinPath = true;
                domainUsage         = DomainUsage.Multiple;
            }

            if (options.basepath != null && options.basepath != string.Empty)
            {
                package.BasePath = options.basepath;
            }

            if (options.privatebinpath != null && options.privatebinpath != string.Empty)
            {
                package.AutoBinPath    = false;
                package.PrivateBinPath = options.privatebinpath;
            }

#if CLR_2_0 || CLR_4_0
            if (options.framework != null)
            {
                framework = RuntimeFramework.Parse(options.framework);
            }

            if (options.process != ProcessModel.Default)
            {
                processModel = options.process;
            }
#endif

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

            package.TestName = options.fixture;

            package.Settings["ProcessModel"] = processModel;
            package.Settings["DomainUsage"]  = domainUsage;

            if (framework != null)
            {
                package.Settings["RuntimeFramework"] = framework;
            }

            if (domainUsage == DomainUsage.None)
            {
                // Make sure that addins are available
                CoreExtensions.Host.AddinRegistry = Services.AddinRegistry;
            }

            package.Settings["ShadowCopyFiles"]   = !options.noshadow;
            package.Settings["UseThreadedRunner"] = !options.nothread;
            package.Settings["DefaultTimeout"]    = options.timeout;
            package.Settings["WorkDirectory"]     = this.workDir;
            package.Settings["StopOnError"]       = options.stoponerror;

            if (options.apartment != System.Threading.ApartmentState.Unknown)
            {
                package.Settings["ApartmentState"] = options.apartment;
            }

            return(package);
        }
Ejemplo n.º 47
0
 static int PairCount(RuntimeFramework[] array)
 {
     return array.Length * (array.Length - 1) / 2;
 }
        public int Execute(ExtendedConsoleOptions options)
        {
            this.workDir = options.work;
            if (workDir == null || workDir == string.Empty)
            {
                workDir = Environment.CurrentDirectory;
            }
            else
            {
                workDir = Path.GetFullPath(workDir);
                if (!Directory.Exists(workDir))
                {
                    Directory.CreateDirectory(workDir);
                }
            }

            TextWriter outWriter      = Console.Out;
            bool       redirectOutput = options.output != null && options.output != string.Empty;

            if (redirectOutput)
            {
                StreamWriter outStreamWriter = new StreamWriter(Path.Combine(workDir, options.output));
                outStreamWriter.AutoFlush = true;
                outWriter = outStreamWriter;
            }

            TextWriter errorWriter   = Console.Error;
            bool       redirectError = options.err != null && options.err != string.Empty;

            if (redirectError)
            {
                StreamWriter errorStreamWriter = new StreamWriter(Path.Combine(workDir, options.err));
                errorStreamWriter.AutoFlush = true;
                errorWriter = errorStreamWriter;
            }

            TextWriter testResultWriter = null;

            if (options.UsePipe)
            {
                var namedPipe = new NamedPipeClientStream(".", options.pipe, PipeDirection.Out, PipeOptions.WriteThrough);
                namedPipe.Connect();
                testResultWriter = new StreamWriter(namedPipe, Encoding.UTF8);
                ((StreamWriter)testResultWriter).AutoFlush = true;
            }

            TestPackage package = MakeTestPackage(options);

            ProcessModel processModel = package.Settings.Contains("ProcessModel")
                ? (ProcessModel)package.Settings["ProcessModel"]
                : ProcessModel.Default;

            DomainUsage domainUsage = package.Settings.Contains("DomainUsage")
                ? (DomainUsage)package.Settings["DomainUsage"]
                : DomainUsage.Default;

            RuntimeFramework framework = package.Settings.Contains("RuntimeFramework")
                ? (RuntimeFramework)package.Settings["RuntimeFramework"]
                : RuntimeFramework.CurrentFramework;

#if CLR_2_0 || CLR_4_0
            Console.WriteLine("ProcessModel: {0}    DomainUsage: {1}", processModel, domainUsage);

            Console.WriteLine("Execution Runtime: {0}", framework);
#else
            Console.WriteLine("DomainUsage: {0}", domainUsage);

            if (processModel != ProcessModel.Default && processModel != ProcessModel.Single)
            {
                Console.WriteLine("Warning: Ignoring project setting 'processModel={0}'", processModel);
            }

            if (!RuntimeFramework.CurrentFramework.Supports(framework))
            {
                Console.WriteLine("Warning: Ignoring project setting 'runtimeFramework={0}'", framework);
            }
#endif

            using (TestRunner testRunner = new DefaultTestRunnerFactory().MakeTestRunner(package))
            {
                testRunner.Load(package);

                if (testRunner.Test == null)
                {
                    testRunner.Unload();
                    Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
                    return(FIXTURE_NOT_FOUND);
                }

                ExtendedEventCollector collector = new ExtendedEventCollector(options, outWriter, errorWriter, testResultWriter);

                TestFilter testFilter;

                if (!CreateTestFilter(options, out testFilter))
                {
                    return(INVALID_ARG);
                }

                TestResult result         = null;
                string     savedDirectory = Environment.CurrentDirectory;
                TextWriter savedOut       = Console.Out;
                TextWriter savedError     = Console.Error;

                try
                {
                    result = testRunner.Run(collector, testFilter, true, LoggingThreshold.Off);
                }
                finally
                {
                    outWriter.Flush();
                    errorWriter.Flush();

                    if (redirectOutput)
                    {
                        outWriter.Close();
                    }

                    if (redirectError)
                    {
                        errorWriter.Close();
                    }

                    if (testResultWriter != null)
                    {
                        testResultWriter.Close();
                    }

                    Environment.CurrentDirectory = savedDirectory;
                    Console.SetOut(savedOut);
                    Console.SetError(savedError);
                }

                Console.WriteLine();

                int returnCode = UNEXPECTED_ERROR;

                if (result != null)
                {
                    string           xmlOutput = CreateXmlOutput(result);
                    ResultSummarizer summary   = new ResultSummarizer(result);

                    if (options.xmlConsole)
                    {
                        Console.WriteLine(xmlOutput);
                    }
                    else
                    {
                        WriteSummaryReport(summary);

                        bool hasErrors = summary.Errors > 0 || summary.Failures > 0 || result.IsError || result.IsFailure;

                        if (options.stoponerror && (hasErrors || summary.NotRunnable > 0))
                        {
                            Console.WriteLine("Test run was stopped after first error, as requested.");
                            Console.WriteLine();
                        }

                        if (hasErrors)
                        {
                            WriteErrorsAndFailuresReport(result);
                        }

                        if (summary.TestsNotRun > 0)
                        {
                            WriteNotRunReport(result);
                        }

                        if (!options.noresult)
                        {
                            // Write xml output here
                            string xmlResultFile = options.result == null || options.result == string.Empty
                                ? "TestResult.xml" : options.result;

                            using (StreamWriter writer = new StreamWriter(Path.Combine(workDir, xmlResultFile)))
                            {
                                writer.Write(xmlOutput);
                            }
                        }
                    }

                    returnCode = summary.Errors + summary.Failures + summary.NotRunnable;
                }

                if (collector.HasExceptions)
                {
                    collector.WriteExceptions();
                    returnCode = UNEXPECTED_ERROR;
                }

                return(returnCode);
            }
        }
Ejemplo n.º 49
0
 public void AnyFrameworkSupportsItself(RuntimeFramework framework)
 {
     Assert.That(framework.Supports(framework));
 }
Ejemplo n.º 50
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(PackageSettings.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)
                    {
                        // If the file is not an assembly or doesn't exist, then it can't
                        // contribute any information to the decision, so we skip it.
                        if (PathUtils.IsAssemblyFileType(assembly) && File.Exists(assembly))
                        {
                            using (var reader = new AssemblyReader(assembly))
                            {
                                if (!reader.IsValidPeFile)
                                {
                                    log.Debug("{0} is not a valid PE file", assembly);
                                }
                                else if (!reader.IsDotNetFile)
                                {
                                    log.Debug("{0} is not a managed assembly", assembly);
                                }
                                else
                                {
                                    if (reader.ShouldRun32Bit)
                                    {
                                        package.Settings[PackageSettings.RunAsX86] = true;
                                        log.Debug("Assembly {0} will be run x86", assembly);
                                    }

                                    var imageRuntimeVersion = reader.ImageRuntimeVersion;
                                    if (imageRuntimeVersion != null)
                                    {
                                        var v = new Version(imageRuntimeVersion.Substring(1));
                                        log.Debug("Assembly {0} uses version {1}", assembly, v);

                                        // TODO: We are doing two jobs here: (1) getting the
                                        // target version and (2) applying a policy that says
                                        // we run under the highest version of all assemblies.
                                        // We should implement the policy at a higher level.
                                        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[PackageSettings.RuntimeFramework] = targetFramework.ToString();

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

            return(targetFramework);
        }
 public void CanDisplayFrameworkAsString(FrameworkData data)
 {
     RuntimeFramework framework = new RuntimeFramework(data.runtime, data.frameworkVersion);
     Assert.AreEqual(data.representation, framework.ToString());
     Assert.AreEqual(data.displayName, framework.DisplayName);
 }
Ejemplo n.º 52
0
        //public void DestroyAgent( ITestAgent agent )
        //{
        //    AgentRecord r = agentData[agent.Id];
        //    if ( r != null )
        //    {
        //        if( !r.Process.HasExited )
        //            r.Agent.Stop();
        //        agentData[r.Id] = null;
        //    }
        //}
        #endregion

        #region Helper Methods
        private Guid LaunchAgentProcess(TestPackage package)
        {
            string runtimeSetting = package.GetSetting(PackageSettings.RuntimeFramework, "");
            RuntimeFramework targetRuntime = RuntimeFramework.Parse(
                runtimeSetting != ""
                    ? runtimeSetting
                    : _runtimeService.SelectRuntimeFramework(package));

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

            bool useX86Agent = package.GetSetting(PackageSettings.RunAsX86, false);
            bool enableDebug = package.GetSetting("AgentDebug", false);
            bool verbose = package.GetSetting("Verbose", false);
            string agentArgs = string.Empty;
            if (enableDebug) agentArgs += " --pause";
            if (verbose) agentArgs += " --verbose";

            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(targetRuntime.ClrVersion, useX86Agent);

            if (agentExePath == null)
                throw new ArgumentException(
                    string.Format("NUnit components for version {0} of the CLR are not installed",
                    targetRuntime.ClrVersion.ToString()), "targetRuntime");

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

            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            Guid agentId = Guid.NewGuid();
            string arglist = agentId.ToString() + " " + ServerUrl + " " + agentArgs;

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

                    if (targetRuntime.ClrVersion.Build < 0)
                        targetRuntime = RuntimeFramework.GetBestAvailableFramework(targetRuntime);

                    string envVar = "v" + targetRuntime.ClrVersion.ToString(3);
                    p.StartInfo.EnvironmentVariables["COMPLUS_Version"] = envVar;

                    p.StartInfo.Arguments = arglist;
                    break;
                default:
                    p.StartInfo.FileName = agentExePath;
                    p.StartInfo.Arguments = arglist;
                    break;
            }
            
            //p.Exited += new EventHandler(OnProcessExit);
            p.Start();
            log.Info("Launched Agent process {0} - see nunit-agent_{0}.log", p.Id);
            log.Info("Command line: \"{0}\" {1}", p.StartInfo.FileName, p.StartInfo.Arguments);

            _agentData.Add( new AgentRecord( agentId, p, null, AgentStatus.Starting ) );
            return agentId;
        }
Ejemplo n.º 53
0
        static CEEInfoOffset()
        {
            RuntimeFramework framework = RuntimeFramework.Framework;

            ReadOffset(framework.IsCore, framework.FrameworkVersion);
        }
Ejemplo n.º 54
0
        //public void DestroyAgent( ITestAgent agent )
        //{
        //    AgentRecord r = agentData[agent.Id];
        //    if ( r != null )
        //    {
        //        if( !r.Process.HasExited )
        //            r.Agent.Stop();
        //        agentData[r.Id] = null;
        //    }
        //}
        #endregion

        #region Helper Methods
        private Guid LaunchAgentProcess(RuntimeFramework targetRuntime, bool enableDebug, string agentArgs)
        {
            string agentExePath = GetTestAgentExePath(targetRuntime.ClrVersion);

            if (agentExePath == null)
                throw new ArgumentException(
                    string.Format("NUnit components for version {0} of the CLR are not installed",
                    targetRuntime.ClrVersion.ToString()), "targetRuntime");

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

            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            Guid agentId = Guid.NewGuid();
            string arglist = agentId.ToString() + " " + ServerUrl + " " + agentArgs;

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

                    if (targetRuntime.ClrVersion.Build < 0)
                        targetRuntime = RuntimeFramework.GetBestAvailableFramework(targetRuntime);

                    string envVar = "v" + targetRuntime.ClrVersion.ToString(3);
                    p.StartInfo.EnvironmentVariables["COMPLUS_Version"] = envVar;

                    p.StartInfo.Arguments = arglist;
                    break;
                default:
                    p.StartInfo.FileName = agentExePath;
                    p.StartInfo.Arguments = arglist;
                    break;
            }
            
            //p.Exited += new EventHandler(OnProcessExit);
            p.Start();
            log.Info("Launched Agent process {0} - see nunit-agent_{0}.log", p.Id);
            log.Info("Command line: \"{0}\" {1}", p.StartInfo.FileName, p.StartInfo.Arguments);

            agentData.Add( new AgentRecord( agentId, p, null, AgentStatus.Starting ) );
            return agentId;
        }
Ejemplo n.º 55
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 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());
        }
Ejemplo n.º 56
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);
        }
 public bool CanMatchRuntimes(RuntimeFramework f1, RuntimeFramework f2)
 {
     return f1.Supports(f2);
 }
Ejemplo n.º 58
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);
        }
Ejemplo n.º 59
0
 public static bool IsCurrentFrameworkCompatibleWith(RuntimeFramework framework)
 {
     return _currentFrameworkCompatibleWith.Contains(framework);
 }
Ejemplo n.º 60
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;
            RuntimeFramework requestedFramework = package.Settings["RuntimeFramework"] as RuntimeFramework;

            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 == null
                ? RuntimeType.Any
                : requestedFramework.Runtime;
            Version targetVersion = requestedFramework == null
                ? RuntimeFramework.DefaultVersion
                : requestedFramework.FrameworkVersion;

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

            if (targetVersion == RuntimeFramework.DefaultVersion)
            {
                foreach (string assembly in package.Assemblies)
                {
                    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;
                        }
                    }
                }

                RuntimeFramework checkFramework = new RuntimeFramework(targetRuntime, targetVersion);
                if (!checkFramework.IsAvailable || NUnitConfiguration.GetTestAgentExePath(targetVersion) == null)
                {
                    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["RuntimeFramework"] = targetFramework;

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

            return(targetFramework);
        }