Wraps calls to the Executor. Used by runners to perform version-resilient test enumeration and execution.
Inheritance: IExecutorWrapper
Ejemplo n.º 1
0
        public void Run(XunitTestAssemblyTask assemblyTask, TaskProvider taskProvider)
        {
            var priorCurrentDirectory = Environment.CurrentDirectory;
            try
            {
                // Use the assembly in the folder that the user has specified, or, if not, use the assembly location
                var assemblyFolder = GetAssemblyFolder(configuration, assemblyTask);
                var assemblyPath = Path.Combine(assemblyFolder, GetFileName(assemblyTask.AssemblyLocation));

                Environment.CurrentDirectory = assemblyFolder;

                // If we just pass null for the config file, the AppDomain will load {assembly}.config if it exists,
                // or use the config file of this app domain, which will usually be JetBrains.ReSharper.TaskRunner.*.exe.config.
                // If we specify the name directly, it will just use it, or have no configuration, with no fallback.
                // This is good because it stops the TaskRunner.exe config leaking into your tests. For example, that
                // file redirects all ReSharper assemblies to the current version. When the default AppDomain loads our
                // code, the assemblies are successfully redirected, and we use the latest version. If the new AppDomain
                // uses the same redirects, and the test assembly references resharper assemblies (e.g. xunitcontrib tests!)
                // the redirects are applied, but the new AppDomain can't find the newer assemblies, and throws
                var configFile = assemblyPath + ".config";

                using (var executorWrapper = new ExecutorWrapper(assemblyPath, configFile, configuration.ShadowCopy))
                {
                    SetTempFolderPath(executorWrapper);

                    var run = new XunitTestRun(server, executorWrapper, taskProvider);
                    run.RunTests();
                }
            }
            finally
            {
                Environment.CurrentDirectory = priorCurrentDirectory;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Runs the specified test.
        /// </summary>
        /// <param name="className"></param>
        /// <param name="methodName"></param>
        /// <returns>True to indicate success, false to indicate fail</returns>
        public static bool RunExampleTest(string className, string methodName)
        {
            using (MockAssembly assembly = new MockAssembly())
            {

                XmlNode lastNode = null;
                XmlNode returnValue = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper("Coulda.Examples.dll", null, false))
                {
                    returnValue = wrapper.RunTest(className, methodName, node =>
                    {
                        lastNode = node;
                        return true;
                    });
                }
                XmlNode resultNode = ResultXmlUtility.GetResult(lastNode);
                //ResultXmlUtility.AssertAttribute(resultNode, "result", "Pass");
                if(resultNode.Attributes["result"].Value == "Pass")
                {
                    return true;
                }

            }
            return false;
        }
Ejemplo n.º 3
0
        static void Run(string className, System.Reflection.Assembly koanAssembly, ExecutorWrapper wrapper)
        {
            Type classToRun = koanAssembly.GetType(className);

            if (classToRun == null) { return; }

            string[] queue = new string[classToRun.GetMethods().Length + 1];
            int highestKoanNumber = 0;
            foreach (MethodInfo method in classToRun.GetMethods())
            {
                if (method.Name == null) { continue; }
                DotNetKoans.KoanAttribute custAttr = method.GetCustomAttributes(typeof(DotNetKoans.KoanAttribute), false).FirstOrDefault() as DotNetKoans.KoanAttribute;
                if (custAttr == null) { continue; }
                queue[custAttr.Position] = method.Name;
                if (custAttr.Position > highestKoanNumber) { highestKoanNumber = custAttr.Position; }
            }

            int numberOfTestsActuallyRun = 0;
            foreach (string test in queue)
            {
                if (String.IsNullOrEmpty(test)) { continue; }
                numberOfTestsActuallyRun += 1;
                if (TEST_FAILED != 0) { continue; }
                wrapper.RunTest(className, test, callback);
            }

            if (numberOfTestsActuallyRun != highestKoanNumber)
            {
                Console.WriteLine("!!!!WARNING - Some Koans appear disabled. The highest koan found was {0} but we ran {1} koan(s)",
                    highestKoanNumber, numberOfTestsActuallyRun);
            }
        }
Ejemplo n.º 4
0
        private static void Main(string[] args)
        {
            var exeWrapper = new ExecutorWrapper(Assembly.GetExecutingAssembly().Location, null, true);
            var builder = TestAssemblyBuilder.Build(exeWrapper);

            Console.WriteLine("AutoMapper Inheritance Extension Sample\n\n");
            builder.Run(builder.EnumerateTestMethods(), new TestRunnerCallback());
        }
Ejemplo n.º 5
0
        static int Main(string[] args)
        {
            try
            {
                Console.WriteLine("");
                Console.WriteLine("");
                Console.WriteLine("*******************************************************************");
                Console.WriteLine("*******************************************************************");
                if (args.Length == 0)
                {

                    Console.WriteLine("Need to pass as argument the path to test DLL (CSharp.dll)");
                    return TEST_FAILED;
                }
                else
                {

                    string koan_path = args[0];
                    Xunit.ExecutorWrapper wrapper = new ExecutorWrapper(koan_path, null, false);
                    System.Reflection.Assembly koans = System.Reflection.Assembly.LoadFrom(koan_path);
                    if (koans == null) { Console.WriteLine("Bad Assembly"); return -1; }
                    Type pathType = null;
                    foreach (Type type in koans.GetExportedTypes())
                    {
                        if (typeof(KoanHelpers.IAmThePathToEnlightenment).IsAssignableFrom(type))
                        {
                            pathType = type;
                            break;
                        }
                    }

                    KoanHelpers.IAmThePathToEnlightenment path = Activator.CreateInstance(pathType) as KoanHelpers.IAmThePathToEnlightenment;
                    string[] thePath = path.ThePath;

                    foreach (string koan in thePath)
                    {
                        Run(koan, koans, wrapper);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Karma has killed the runner. Exception was: " + ex.ToString());
                //return -1;
                return TEST_FAILED;
            }
            Console.WriteLine("*******************************************************************");
            Console.WriteLine("*******************************************************************");
            Console.WriteLine("");
            Console.WriteLine("");
            // Add prompt so you can view the console results
            Console.WriteLine("Press <Enter> to continue, because \"like gravity, karma is so basic");
            Console.WriteLine("we often don't even notice it.\" - Sakyong Mipham");
            Console.ReadLine();
            return TEST_FAILED;
        }
Ejemplo n.º 6
0
 public SessionResults RunAssembly(Assembly assembly)
 {
     var sessionResults = new SessionResults();
     var logger = new GilesXunitLogger();
     using (var exWrapper = new XunitFx.ExecutorWrapper(new Uri(assembly.CodeBase).LocalPath, null, false)) {
         var runner = new XunitFx.TestRunner(exWrapper, logger);
         var result = runner.RunAssembly();
     }
     return logger.SessionResults;
 }
		/// <summary>
		/// Run all the unit tests in the class
		/// </summary>
		/// <param name="assemblyPath">The assembly that contains the unit test.</param>
		/// <param name="className">The full name of the class that contains the unit test.</param>
		override public void RunTests(string assemblyPath, string assemblyName)
		{
			System.Xml.XmlNode returnValue = null;

			using (ExecutorWrapper wrapper = new ExecutorWrapper(assemblyPath, null, true))
			{
				returnValue = wrapper.RunAssembly(node => true);
			}

			ParseResults(returnValue);
		}
Ejemplo n.º 8
0
        public SessionResults RunAssembly(Assembly assembly)
        {
            var sessionResults = new SessionResults();
            var logger         = new GilesXunitLogger();

            using (var exWrapper = new XunitFx.ExecutorWrapper(new Uri(assembly.CodeBase).LocalPath, null, false)) {
                var runner = new XunitFx.TestRunner(exWrapper, logger);
                var result = runner.RunAssembly();
            }
            return(logger.SessionResults);
        }
Ejemplo n.º 9
0
        public SessionResults RunAssembly(Assembly assembly, IEnumerable<string> filters)
        {
            var logger = new GilesXunitLogger();

            using (var exWrapper = new XunitFx.ExecutorWrapper(new Uri(assembly.CodeBase).LocalPath, null, false)) {
                var runner = new XunitFx.TestRunner(exWrapper, logger);
                if (filters.Count() == 0)
                    runner.RunAssembly();
                else
                    filters.Each(x => runner.RunClass(x));
            }

            return logger.SessionResults;
        }
        public void GetProviderServices_returns_null_when_unknown()
        {
            var domain = AppDomain.CreateDomain("ExecutorWrapperTests", null, AppDomain.CurrentDomain.SetupInformation);
            try
            {
                var executor = new ExecutorWrapper(
                    domain,
                    Path.GetFileName(GetType().Assembly.CodeBase));

                Assert.Null(executor.GetProviderServices("My.Fake.Provider"));
            }
            finally
            {
                AppDomain.Unload(domain);
            }
        }
Ejemplo n.º 11
0
        public void GetProviderServices_returns_null_when_no_project_assembly()
        {
            var domain = AppDomain.CreateDomain("ExecutorWrapperTests", null, AppDomain.CurrentDomain.SetupInformation);
            try
            {
                var executor = new ExecutorWrapper(
                    domain,
                    "UnknownProject.dll");

                Assert.Null(executor.GetProviderServices("System.Data.SqlClient"));
            }
            finally
            {
                AppDomain.Unload(domain);
            }
        }
Ejemplo n.º 12
0
        // Tell ReSharper the cache folder being used for shadow copy, so that if
        // someone kills this process (e.g. user aborts), ReSharper can delete it.
        // xunit doesn't expose this information, so we'll grab it via (sorry) reflection
        private void SetTempFolderPath(ExecutorWrapper executorWrapper)
        {
            if (!configuration.ShadowCopy)
                return;

            var fieldInfo = executorWrapper.GetType().GetField("appDomain", BindingFlags.NonPublic | BindingFlags.Instance);
            if (fieldInfo == null)
                throw new InvalidOperationException("Expected ExecutorWrapped to contain private field \"appDomain\"");

            var appDomain = fieldInfo.GetValue(executorWrapper) as AppDomain;
            if (appDomain != null)
            {
                var cachePath = appDomain.SetupInformation.CachePath;
                if (!string.IsNullOrEmpty(cachePath))
                    server.SetTempFolderPath(cachePath);
            }
        }
Ejemplo n.º 13
0
        public void Should_be_able_to_run_the_simple_example()
        {
            using (MockAssembly assembly = new MockAssembly())
            {

                XmlNode lastNode = null;
                XmlNode returnValue = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper("Coulda.Examples.dll", null, false))
                    returnValue = wrapper.RunClass("Coulda.Examples.SimpleExample", node => { lastNode = node; return true; });

                XmlNode resultNode = ResultXmlUtility.GetResult(lastNode);
                ResultXmlUtility.AssertAttribute(resultNode, "name", "My example test should be able to assert");
                ResultXmlUtility.AssertAttribute(resultNode, "type", "Coulda.Examples.SimpleExample");
                ResultXmlUtility.AssertAttribute(resultNode, "method", "A_simple_example");
                ResultXmlUtility.AssertAttribute(resultNode, "result", "Pass");
            }
        }
        public void GetProviderServices_returns_assembly_qualified_type_name()
        {
            var domain = AppDomain.CreateDomain("ExecutorWrapperTests", null, AppDomain.CurrentDomain.SetupInformation);
            try
            {
                var executor = new ExecutorWrapper(
                    domain,
                    Path.GetFileName(GetType().Assembly.CodeBase));

                var typeName = executor.GetProviderServices("System.Data.SqlClient");

                Assert.Equal(typeof(SqlProviderServices).AssemblyQualifiedName, typeName);
            }
            finally
            {
                AppDomain.Unload(domain);
            }
        }
Ejemplo n.º 15
0
        public void Should_be_able_to_have_a_failing_test()
        {
            using (MockAssembly assembly = new MockAssembly())
            {

                XmlNode lastNode = null;
                XmlNode returnValue = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper("Coulda.Examples.dll", null, false))
                    returnValue = wrapper.RunClass("Coulda.Examples.FailingExample", node => { lastNode = node; return true; });

                XmlNode resultNode = ResultXmlUtility.GetResult(lastNode);
                ResultXmlUtility.AssertAttribute(resultNode, "name", "A test with an assertion should be able to fail");
                ResultXmlUtility.AssertAttribute(resultNode, "type", "Coulda.Examples.FailingExample");
                ResultXmlUtility.AssertAttribute(resultNode, "method", "A_failing_example");
                ResultXmlUtility.AssertAttribute(resultNode, "result", "Fail");
            }
        }
Ejemplo n.º 16
0
        public SessionResults RunAssembly(Assembly assembly, IEnumerable <string> filters)
        {
            var logger = new GilesXunitLogger();

            using (var exWrapper = new XunitFx.ExecutorWrapper(new Uri(assembly.CodeBase).LocalPath, null, false))
            {
                var runner = new XunitFx.TestRunner(exWrapper, logger);
                if (filters.Count() == 0)
                {
                    runner.RunAssembly();
                }
                else
                {
                    filters.Each(x => runner.RunClass(x));
                }
            }

            return(logger.SessionResults);
        }
        public override void ExecuteRecursive(TaskExecutionNode node)
        {
            var priorCurrentDirectory = Environment.CurrentDirectory;
            try
            {
                var assemblyTask = (XunitTestAssemblyTask) node.RemoteTask;
                var assemblyLocation = assemblyTask.AssemblyLocation;
                var assemblyFolder = Path.GetDirectoryName(assemblyLocation);
                var assemblyPath = Path.Combine(assemblyFolder, Path.GetFileName(assemblyLocation));
                var config = assemblyPath + ".config";
                Environment.CurrentDirectory = assemblyFolder;

                var shadowCopy = TaskExecutor.Configuration != null && TaskExecutor.Configuration.ShadowCopy;
                using (var executorWrapper = new ExecutorWrapper(assemblyLocation, config, shadowCopy))
                {
                    foreach (var childNode in node.Children)
                    {
                        var classTask = (XunitTestClassTask) childNode.RemoteTask;

                        var runnerLogger = new ReSharperRunnerLogger(Server, classTask);
                        runnerLogger.ClassStart();

                        var tasks = childNode.Children
                                             .Select(methodNode => (XunitTestMethodTask) methodNode.RemoteTask)
                                             .ToList();

                        runnerLogger.SetMethodTasks(tasks);

                        var methodNames = tasks
                            .Select(methodTask => methodTask.ShortName)
                            .ToList();

                        new TestRunner(executorWrapper, runnerLogger).RunTests(classTask.TypeName, methodNames);

                        runnerLogger.ClassFinished();
                    }
                }
            }
            finally
            {
                Environment.CurrentDirectory = priorCurrentDirectory;
            }
        }
Ejemplo n.º 18
0
		static int Main(string[] args)
		{
			StringBuilder progress = new StringBuilder();
			try
			{
				Console.WriteLine("");
				Console.WriteLine("");
				Console.WriteLine("*******************************************************************");
				Console.WriteLine("*******************************************************************");
				string koan_path = args[0];
				Xunit.ExecutorWrapper wrapper = new ExecutorWrapper(koan_path, null, false);
				System.Reflection.Assembly koans = System.Reflection.Assembly.LoadFrom(koan_path);
				if (koans == null) { Console.WriteLine("Bad Assembly"); return -1; }
				Type pathType = null;
				foreach (Type type in koans.GetExportedTypes())
				{
					if (typeof(KoanHelpers.IAmThePathToEnlightenment).IsAssignableFrom(type))
					{
						pathType = type;
						break;
					}
				}

				KoanHelpers.IAmThePathToEnlightenment path = Activator.CreateInstance(pathType) as KoanHelpers.IAmThePathToEnlightenment;
				string[] thePath = path.ThePath;

				foreach (string koan in thePath)
				{
					progress.AppendFormat("{0},", Run(koan, koans, wrapper));
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine("Karma has killed the runner. Exception was: " + ex.ToString());
				return -1;
			}
			Console.WriteLine("Koan progress:{0}", progress.ToString());
			Console.WriteLine("*******************************************************************");
			Console.WriteLine("*******************************************************************");
			Console.WriteLine("");
			Console.WriteLine("");
			return TEST_FAILED;
		}
Ejemplo n.º 19
0
        static void Run(string className, System.Reflection.Assembly koanAssembly, ExecutorWrapper wrapper)
        {
            Type classToRun = koanAssembly.GetType(className);

            if (classToRun == null) { return; }

            string[] queue = new string[classToRun.GetMethods().Length + 1];
            foreach (MethodInfo method in classToRun.GetMethods())
            {
                if (method.Name == null) { continue; }
                DotNetKoans.KoanAttribute custAttr = method.GetCustomAttributes(typeof(DotNetKoans.KoanAttribute), false).FirstOrDefault() as DotNetKoans.KoanAttribute;
                if (custAttr == null) { continue; }
                queue[custAttr.Position] = method.Name;
            }
            foreach (string test in queue)
            {
                if (String.IsNullOrEmpty(test)) { continue; }
                if (TEST_FAILED != 0) { continue; }
                wrapper.RunTest(className, test, callback);
            }
        }
Ejemplo n.º 20
0
        public void GetProviderServices_returns_null_when_no_EntityFramework_assembly()
        {
            var domain = AppDomain.CreateDomain(
                "ExecutorWrapperTests",
                null,
                new AppDomainSetup
                    {
                        // NOTE: This will cause assembly resolution for EntityFramework to fail
                        ApplicationBase = Path.GetTempPath()
                    });
            try
            {
                var executor = new ExecutorWrapper(
                    domain,
                    Path.GetFileName(GetType().Assembly.CodeBase));

                Assert.Null(executor.GetProviderServices("System.Data.SqlClient"));
            }
            finally
            {
                AppDomain.Unload(domain);
            }
        }
Ejemplo n.º 21
0
        public void AcceptanceTest()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        [Fact] public void TestMethod1() {}
                        [Fact] public void TestMethod2() {}
                        [Fact] public void TestMethod3() {}
                    }";

            using (MockAssembly assembly = new MockAssembly())
            {
                assembly.Compile(code);

                XmlNode lastNode = null;
                XmlNode returnValue = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    returnValue = wrapper.RunTests("TestClass",
                                                   new List<string> { "TestMethod1", "TestMethod2" },
                                                   node => { lastNode = node; return true; });

                Assert.Equal(returnValue, lastNode);
                Assert.Equal(2, lastNode.ChildNodes.Count); // Two test results
                XmlNode result0 = ResultXmlUtility.GetResult(lastNode, 0);
                Assert.Equal("Pass", result0.Attributes["result"].Value);
                XmlNode result1 = ResultXmlUtility.GetResult(lastNode, 1);
                Assert.Equal("Pass", result1.Attributes["result"].Value);
            }
        }
Ejemplo n.º 22
0
        public IEnumerable<AutoTest.TestRunners.Shared.Results.TestResult> Run(RunSettings settings)
        {
            var logger = new XUnitLogger();
            XunitProject project = new XunitProject();

            var runner = settings.Assembly;
            // Set assembly externally as XUnit screws up the casing
            logger.SetCurrentAssembly(runner.Assembly);

            XunitProjectAssembly assembly = new XunitProjectAssembly
            {
                AssemblyFilename = runner.Assembly,
                ConfigFilename = null,
                ShadowCopy = false
            };
            project.AddAssembly(assembly);

            foreach (XunitProjectAssembly asm in project.Assemblies)
            {
                using (ExecutorWrapper wrapper = new ExecutorWrapper(asm.AssemblyFilename, asm.ConfigFilename, asm.ShadowCopy))
                {
                    try
                    {
                        var xunitRunner = new TestRunner(wrapper, logger);
                        //Run all tests
                        if (runner.Tests.Count() == 0 && runner.Members.Count() == 0 && runner.Namespaces.Count() == 0)
                            xunitRunner.RunAssembly();
                        //Run tests
                        if (runner.Tests.Count() > 0)
                        {
                            foreach (var test in runner.Tests)
                                xunitRunner.RunTest(test.Substring(0, test.LastIndexOf(".")), test.Substring(test.LastIndexOf(".") + 1, test.Length - (test.LastIndexOf(".") + 1)));
                        }
                        //Run members
                        if (runner.Members.Count() > 0)
                        {
                            foreach (var member in runner.Members)
                                xunitRunner.RunClass(member);
                        }
                        //Run namespaces
                        if (runner.Namespaces.Count() > 0)
                        {
                            var loadedAssembly = Assembly.LoadFrom(runner.Assembly);
                            var types = loadedAssembly.GetExportedTypes();
                            loadedAssembly = null;
                            foreach (var ns in runner.Namespaces)
                            {
                                foreach (Type type in types)
                                    if (ns == null || type.Namespace == ns)
                                        xunitRunner.RunClass(type.FullName);
                            }
                        }
                    }
                    catch (ArgumentException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            return logger.Results;
        }
Ejemplo n.º 23
0
        public void DoRunTests()
        {
            _isCancelRequested = false;

            if (Directory.Exists("./Screenshots"))
            {
                var screenshotDirectory = new DirectoryInfo("./Screenshots");
                foreach (var file in screenshotDirectory.GetFiles("*.png"))
                {
                    file.Delete();
                }
            }

            var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();

            try
            {
                using (var wrapper = new ExecutorWrapper(executingAssembly.Location, null, true))
                {
                    var assembly = TestAssemblyBuilder.Build(wrapper);
                    var methods = assembly
                        .EnumerateTestMethods()
                        .ToList();

                    var activeMethods = methods
                        .Where(m =>
                               _viewModel.UseFirefox && m.TestClass.TypeName.Contains("Firefox") ||
                               _viewModel.UseChrome && m.TestClass.TypeName.Contains("Chrome") ||
                               _viewModel.UseInternetExplorer && m.TestClass.TypeName.Contains("Internet") ||
                               _viewModel.UsePhantomJs && m.TestClass.TypeName.Contains("Phantom")
                        )
                        .ToList();

                    if (activeMethods.Count > 0)
                    {
                        assembly.Run(activeMethods, this);
                    }
                    else
                    {
                        _eventAggregator.GetEvent<TestRunFinishedEvent>().Publish(true);

                        _viewModel.IsRunning = false;
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            finally
            {
                Application.Current.Dispatcher.BeginInvoke((Action)(DoDone));
            }
        }
Ejemplo n.º 24
0
        public void TestMethodWithNonTestMethod()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        [Fact] public void TestMethod1() {}
                        [Fact] public void TestMethod2() {}
                        [Fact] public void TestMethod3() {}
                        public void NonTestMethod() {}
                    }";

            using (MockAssembly assembly = new MockAssembly())
            {
                assembly.Compile(code);

                XmlNode lastNode = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    wrapper.RunTests("TestClass",
                                     new List<string> { "TestMethod1", "NonTestMethod" },
                                     node => { lastNode = node; return true; });

                Assert.Single(lastNode.ChildNodes); // Only the test method
                XmlNode result = ResultXmlUtility.GetResult(lastNode, 0);
                Assert.Equal("Pass", result.Attributes["result"].Value);
            }
        }
Ejemplo n.º 25
0
        public void NonPublicTestMethod()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        [Fact] void NonPublicTestMethod() {}
                    }";

            using (MockAssembly assembly = new MockAssembly())
            {
                assembly.Compile(code);

                XmlNode returnValue = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    returnValue = wrapper.RunTests("TestClass",
                                                   new List<string> { "NonPublicTestMethod" },
                                                   node => { return true; });

                Assert.Single(returnValue.ChildNodes);
                XmlNode result = ResultXmlUtility.GetResult(returnValue, 0);
                Assert.Equal("Pass", result.Attributes["result"].Value);
            }
        }
Ejemplo n.º 26
0
 /// <inheritdoc/>
 public void Dispose()
 {
     ExecutorWrapper.Dispose();
 }
Ejemplo n.º 27
0
        static void Run(string className, System.Reflection.Assembly koanAssembly, ExecutorWrapper wrapper)
        {
            Type classToRun = koanAssembly.GetType(className);

            if (classToRun == null)
            {
                Console.WriteLine("Class {0} not found", className);
            }

            object koans = Activator.CreateInstance(classToRun);

            MethodInfo[] queue = new MethodInfo[classToRun.GetMethods().Length + 1];
            int highestKoanNumber = 0;
            foreach (MethodInfo method in classToRun.GetMethods())
            {
                if (method.Name == null) { continue; }
                DotNetKoans.KoanAttribute custAttr = method.GetCustomAttributes(typeof(DotNetKoans.KoanAttribute), false).FirstOrDefault() as DotNetKoans.KoanAttribute;
                if (custAttr == null) { continue; }
                if (queue[custAttr.Position] != null)
                    Console.WriteLine("More than one koan in {0} has the position {1}", className, custAttr.Position);
                queue[custAttr.Position] = method;
                if (custAttr.Position > highestKoanNumber) { highestKoanNumber = custAttr.Position; }
            }

            int numberOfKoansRunInThisCollection = 0;
            int numberOfKoansPassedInThisCollection = 0;

            BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public;
            foreach (MethodInfo test in queue)
            {
                if (test == null)
                    continue;

                ++numberKoansProcessed;
                numberOfKoansRunInThisCollection++;
                if (aKoanHasFailed)
                    continue;
                try
                {
                    Type attType = typeof(AsyncStateMachineAttribute);

                    // Obtain the custom attribute for the method.
                    // The value returned contains the StateMachineType property.
                    // Null is returned if the attribute isn't present for the method.
                    var attrib = (AsyncStateMachineAttribute)test.GetCustomAttribute(attType);

                    if (attrib != null)
                    {
                        Task t = (classToRun.InvokeMember(test.Name, flags, null, koans, new Object[] { }) as Task);
                        t.Wait();
                    }
                    else
                    {
                        classToRun.InvokeMember(test.Name, flags, null, koans, new Object[] { });
                    }
                }
                catch (Exception e)
                {
                    aKoanHasFailed = true;
                    firstFailingKoan = numberKoansProcessed - 1;
                    Exception ie = e.InnerException;
                    if (ie == null)
                        ie = e;
                    string st = ie.StackTrace;
                    string[] stLines = st.Split(new char[] { '\n' });
                    // uncomment if there are problems parsing the stack trace:
                    /*foreach (string line in stLines)
                        Console.WriteLine(line);*/
                    string filenameAndLine = "";
                    foreach (string s in stLines)
                    {
                        filenameAndLine = GetFilenameAndLine(s);
                        if (filenameAndLine != null)
                            break;
                    }

                    // make the message a single line, so that it is completely seen if the Error List pane is selected instead of the Output pane:
                    string message = ie.Message.Replace("\n", " \\ ").Replace("\r", "");

                    Console.WriteLine("The test {0} has damaged your karma.", test);
                    // Use this format: "file(linenr): warning: " followed by anything, so it shows up as a warning in the Error List pane.
                    // In both the Error List pane and Output pane clicking it jumps to the line where a fix is needed.
                    // Replacing 'warning' by 'error' would work too, but also give a distracting extra error message.
                    Console.WriteLine(filenameAndLine + ": warning: {0}", message);

                }
                if (!aKoanHasFailed)
                    numberOfKoansPassedInThisCollection++;
            }

            if (numberOfKoansRunInThisCollection != highestKoanNumber)
            {
                Console.WriteLine("!!!!WARNING - Some Koans in {0} appear disabled. The highest koan found was {1} but we ran {2} koan(s)",
                    className, highestKoanNumber, numberOfKoansRunInThisCollection);
            }
        }
Ejemplo n.º 28
0
        public void NonTestMethodInClassWithTestMethod()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        public void NonTestMethod()
                        {
                        }

                        [Fact]
                        public void TestMethod()
                        {
                        }
                    }";

            using (MockAssembly assembly = new MockAssembly())
            {
                assembly.Compile(code);

                XmlNode lastNode = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    wrapper.RunTest("TestClass", "NonTestMethod", node => { lastNode = node; return true; });

                Assert.Equal("class", lastNode.Name);
                Assert.Equal(0, lastNode.ChildNodes.Count);   // Empty class node
            }
        }
Ejemplo n.º 29
0
        public void CanCancelBetweenTestMethodRuns()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        [Fact]
                        public void TestMethod1()
                        {
                        }

                        [Fact]
                        public void TestMethod2()
                        {
                        }
                    }";

            using (MockAssembly assembly = new MockAssembly())
            {
                assembly.Compile(code);

                XmlNode lastNode = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    wrapper.RunClass("TestClass", node => { lastNode = node; return false; });

                Assert.Equal(0, lastNode.ChildNodes.Count);   // Cancels from the start of the first test
            }
        }
Ejemplo n.º 30
0
        static int Main(string[] args)
        {
            try
            {
                Console.WriteLine("");
                Console.WriteLine("");
                Console.WriteLine("*******************************************************************");
                Console.WriteLine("*******************************************************************");

                ReadProgress();

                string koan_path = args[0];
                Xunit.ExecutorWrapper wrapper = new ExecutorWrapper(koan_path, null, false);
                System.Reflection.Assembly koans = System.Reflection.Assembly.LoadFrom(koan_path);
                if (koans == null) { Console.WriteLine("Bad Assembly"); return -1; }
                Type pathType = null;
                foreach (Type type in koans.GetExportedTypes())
                {
                    if (typeof(KoanHelpers.IAmThePathToEnlightenment).IsAssignableFrom(type))
                    {
                        pathType = type;
                        break;
                    }
                }

                KoanHelpers.IAmThePathToEnlightenment path = Activator.CreateInstance(pathType) as KoanHelpers.IAmThePathToEnlightenment;
                string thePath = path.ThePath;
                string[] theKoanNames = KoanNames;

                foreach (string koanName in theKoanNames)
                {
                    Run(thePath + "." + koanName, koans, wrapper);
                }

                Console.WriteLine("{0}", Encouragement());
                progress.Add(firstFailingKoan);
                WriteProgress();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Karma has killed the runner. Exception was: " + ex.ToString());
                return -1;
            }
            if (!aKoanHasFailed)
                firstFailingKoan = numberKoansProcessed;
            Console.WriteLine("Koan progress: {0}/{1}", firstFailingKoan, numberKoansProcessed);
            Console.WriteLine("*******************************************************************");
            Console.WriteLine("*******************************************************************");
            Console.WriteLine("");
            Console.WriteLine("");
            return 0;
        }
Ejemplo n.º 31
0
        public void CallbackIncludesStartMessages()
        {
            const string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        [Fact] public void TestMethod1() {}
                        [Fact] public void TestMethod2() {}
                        [Fact] public void TestMethod3() {}
                    }";

            using (MockAssembly assembly = new MockAssembly())
            {
                assembly.Compile(code);

                List<XmlNode> nodes = new List<XmlNode>();

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    wrapper.RunTests("TestClass",
                                     new List<string> { "TestMethod1" },
                                     node => { nodes.Add(node); return true; });

                Assert.Equal(3, nodes.Count);
                Assert.Equal("start", nodes[0].Name);  // <start>
                ResultXmlUtility.AssertAttribute(nodes[0], "name", "TestClass.TestMethod1");
                ResultXmlUtility.AssertAttribute(nodes[0], "type", "TestClass");
                ResultXmlUtility.AssertAttribute(nodes[0], "method", "TestMethod1");
                Assert.Equal("test", nodes[1].Name);
                Assert.Equal("class", nodes[2].Name);
            }
        }
Ejemplo n.º 32
0
        public XmlNode Run(string configFile)
        {
            using (ExecutorWrapper wrapper = new ExecutorWrapper(FileName, configFile, false))
            {
                XmlNode result = null;

                wrapper.RunAssembly(node =>
                                    {
                                        if (node.Name == "assembly")
                                            result = node;

                                        return true;
                                    });

                return result;
            }
        }
Ejemplo n.º 33
0
        public void InvalidMethodName()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        public void DummyMethod() {}
                    }";

            using (MockAssembly assembly = new MockAssembly())
            {
                assembly.Compile(code);

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    Assert.Throws<ArgumentException>(
                        () => wrapper.RunTests("TestClass",
                                               new List<string> { "DummyMethod", "DummyMethod2" },
                                               null));
            }
        }