Exemple #1
0
        internal static ScenarioTextWithParameters GetScenarioTextWithParameters(MethodInfo testMethod, string callerMember)
        {
            var stackFrames = new StackTrace().GetFrames() ?? Array.Empty <StackFrame>();

            var stepAttributeFrame = stackFrames.FirstOrDefault(it => GetScenarioTextAttribute(it) != null);

            if (stepAttributeFrame != null)
            {
                return(new ScenarioTextWithParameters(new ScenarioText(GetScenarioTextAttribute(stepAttributeFrame)), GetParameters(stepAttributeFrame)));
            }

            var callingFrame = stackFrames.FirstOrDefault(it => it.GetMethod().Name == callerMember);

            if (callingFrame != null)
            {
                return(new ScenarioTextWithParameters(new ScenarioText(callingFrame.GetMethod().Name.Humanize()), GetParameters(callingFrame)));
            }

            var scenarioTextAttribute = testMethod?.GetCustomAttribute <ScenarioTextAttribute>();

            if (scenarioTextAttribute != null)
            {
                return(new ScenarioTextWithParameters(new ScenarioText(scenarioTextAttribute.Text),
                                                      Enumerable.Empty <string>()));
            }

            return(new ScenarioTextWithParameters(new ScenarioText("No Scenario Text found (Use attribute [ScenarioText(\"...\")] on your tests"), Array.Empty <string>()));
        }
        internal static BDTestRetryAttribute GetBDTestRetryAttribute(Scenario scenario)
        {
            var scenarioMethod       = scenario?.GetScenarioMethod();
            var bdTestRetryAttribute = scenarioMethod?.GetCustomAttribute <BDTestRetryAttribute>();

            if (bdTestRetryAttribute != null)
            {
                return(bdTestRetryAttribute);
            }

            var stackFrames = new StackTrace().GetFrames() ?? Array.Empty <StackFrame>();

            var stepAttributeFrame = stackFrames.FirstOrDefault(it => GetBDTestRetryAttribute(it) != null);

            if (stepAttributeFrame != null)
            {
                return(GetBDTestRetryAttribute(stepAttributeFrame));
            }

            var storyClass = scenario?.GetStoryClass();

            bdTestRetryAttribute = storyClass?.GetCustomAttribute <BDTestRetryAttribute>();
            if (bdTestRetryAttribute != null)
            {
                return(bdTestRetryAttribute);
            }

            var callingFrame = stackFrames.FirstOrDefault(it => it.GetMethod().Name == scenario?.RuntimeInformation?.CallerMember);

            return(callingFrame?.GetMethod()?.GetCustomAttribute <BDTestRetryAttribute>());
        }
        static void Initialize()
        {
            if (intialized)
            {
                return;
            }
            try
            {
                var entryAssembly = Assembly.GetEntryAssembly();
                if (entryAssembly != null && entryAssembly.EntryPoint != null)
                {
                    entryType = entryAssembly.EntryPoint.ReflectedType;
                    return;
                }

                StackFrame targetFrame = null;

                StackFrame[] stackFrames = new StackTrace().GetFrames();
                if (stackFrames != null)
                {
                    targetFrame =
                        stackFrames.FirstOrDefault(
                            f => typeof(HttpApplication).IsAssignableFrom(f.GetMethod().DeclaringType));
                }

                if (targetFrame != null)
                {
                    entryType = targetFrame.GetMethod().ReflectedType;
                    return;
                }

                if (StackTraceToExamine != null)
                {
                    stackFrames = StackTraceToExamine.GetFrames();
                    if (stackFrames != null)
                    {
                        targetFrame =
                            stackFrames.FirstOrDefault(
                                f => f.GetMethod().DeclaringType != typeof(NServiceBus.Configure));
                    }
                }

                if (targetFrame == null)
                {
                    targetFrame = stackFrames.FirstOrDefault(
                        f => f.GetMethod().DeclaringType.Assembly != typeof(NServiceBus.Configure).Assembly);
                }

                if (targetFrame != null)
                {
                    entryType = targetFrame.GetMethod().ReflectedType;
                    return;
                }
            }
            finally
            {
                intialized = true;
            }
        }
        static Type GetEntryType()
        {
            var stackTraceToExamine = new StackTrace();
            var entryAssembly       = Assembly.GetEntryAssembly();

            if (entryAssembly?.EntryPoint != null)
            {
                return(entryAssembly.EntryPoint.ReflectedType);
            }

            StackFrame targetFrame = null;

            var stackFrames = new StackTrace().GetFrames();

            if (stackFrames != null)
            {
                targetFrame =
                    stackFrames.FirstOrDefault(
                        f => typeof(HttpApplication).IsAssignableFrom(f.GetMethod().DeclaringType));
            }

            if (targetFrame != null)
            {
                return(targetFrame.GetMethod().ReflectedType);
            }

            stackFrames = stackTraceToExamine.GetFrames();
            if (stackFrames != null)
            {
                targetFrame =
                    stackFrames.FirstOrDefault(
                        f =>
                {
                    var declaringType = f.GetMethod().DeclaringType;
                    return(declaringType != typeof(EndpointConfiguration));
                });
            }

            if (targetFrame == null)
            {
                targetFrame = stackFrames.FirstOrDefault(
                    f => f.GetMethod().DeclaringType.Assembly != typeof(EndpointConfiguration).Assembly);
            }

            if (targetFrame != null)
            {
                return(targetFrame.GetMethod().ReflectedType);
            }
            throw new Exception("Could not derive EndpointName");
        }
        /// <summary>
        /// Gets the name of this endpoint
        /// </summary>
        /// <returns></returns>
        public static string Get()
        {
            var entryAssembly = Assembly.GetEntryAssembly();

            if (entryAssembly != null && entryAssembly.EntryPoint != null)
            {
                return(entryAssembly.EntryPoint.ReflectedType.Namespace ??
                       entryAssembly.EntryPoint.ReflectedType.Assembly.GetName().Name);
            }

            var        stackFrames = new StackTrace().GetFrames();
            StackFrame targetFrame = null;

            if (stackFrames != null)
            {
                targetFrame =
                    stackFrames.FirstOrDefault(
                        f => typeof(HttpApplication).IsAssignableFrom(f.GetMethod().DeclaringType));
            }

            if (targetFrame != null)
            {
                return(targetFrame.GetMethod().ReflectedType.Namespace ??
                       targetFrame.GetMethod().ReflectedType.Assembly.GetName().Name);
            }

            throw new InvalidOperationException(
                      "No endpoint name could be generated, please specify your own convention using Configure.DefineEndpointName(...)");
        }
        private static Assembly GetWebEntryAssembly()
        {
            var frames = new StackTrace().GetFrames();
            var i      = frames.FirstOrDefault(c => Assembly.GetAssembly(c.GetMethod().DeclaringType).FullName != Assembly.GetExecutingAssembly().FullName).GetMethod().DeclaringType;

            return(Assembly.GetAssembly(i));
        }
Exemple #7
0
        /// <summary>
        /// Gets the test-data directory for the current test project.
        /// </summary>
        /// <returns>System.String.</returns>
        /// <exception cref="System.Exception">
        /// Could not get stacktrace.
        /// or
        /// Could not determine the test method.
        /// or
        /// Could not find test class type.
        /// </exception>
        public static string GetDataDir()
        {
            var stackFrames = new StackTrace().GetFrames();

            if (stackFrames == null)
            {
                throw new Exception("Could not get stacktrace.");
            }

            var testMethod = stackFrames.FirstOrDefault(f => f.GetMethod().GetCustomAttributes(typeof(TestAttribute), true).Any() ||
                                                        f.GetMethod().GetCustomAttributes(typeof(SetUpAttribute), true).Any() ||
                                                        f.GetMethod().GetCustomAttributes(typeof(TestFixtureSetUpAttribute), true).Any() ||
                                                        f.GetMethod().DeclaringType.GetCustomAttributes(typeof(TestFixtureAttribute), true).Any());

            if (testMethod == null)
            {
                throw new Exception("Could not determine the test method.");
            }

            var testClassType = testMethod.GetMethod().DeclaringType;

            if (testClassType == null)
            {
                throw new Exception("Could not find test class type.");
            }

            return(Path.GetFullPath(string.Format("test-data\\{0}", testClassType.Assembly.GetName().Name)));
        }
        private static string BuildMessage(string message, string filePath, int lineNumber)
        {
            var frames = new StackTrace().GetFrames();

            if (frames == null)
            {
                return(message);
            }

            try
            {
                filePath = Path.GetFileName(filePath);
            }
            catch (ArgumentException)
            {
            }

            var frame = frames.FirstOrDefault(f => f.GetMethod().ReflectedType != typeof(SelfCheck));
            var sb    = new StringBuilder()
                        .Append('[')
                        .Append(frame?.GetType().Assembly.GetName().Name ?? "<unknown>");

            if (!string.IsNullOrEmpty(filePath))
            {
                sb.Append(": ").Append(filePath);
                if (lineNumber > 0)
                {
                    sb.Append('(').Append(lineNumber).Append(')');
                }
            }

            return(sb.Append("] ").Append(message).ToString());
        }
        private StackFrame GetDiagnosticsCallerStackFrame()
        {
            var fl  = new StackTrace(true).GetFrames().ToArray();
            var r   = fl.FirstOrDefault(frame => !frame.GetMethod().Name.ToLowerInvariant().Contains("diag"));
            var idx = Array.IndexOf(fl, r);

            return(fl[idx]);
        }
Exemple #10
0
        public static string GetCurrentPositionText()
        {
            try
            {
                var frames = new StackTrace(true).GetFrames();
                if (frames != null)
                {
                    var featureFileFrame = frames.FirstOrDefault(f =>
                    {
                        var fileName = f.GetFileName();
                        return(fileName != null && fileName.EndsWith(".feature"));
                    });

                    if (featureFileFrame != null)
                    {
                        var       lines       = File.ReadAllLines(featureFileFrame.GetFileName());
                        const int frameSize   = 20;
                        var       currentLine = featureFileFrame.GetFileLineNumber() - 1;
                        var       minLine     = Math.Max(0, currentLine - frameSize);
                        var       maxLine     = Math.Min(lines.Length - 1, currentLine + frameSize);

                        for (var lineNo = currentLine - 1; lineNo >= minLine; lineNo--)
                        {
                            if (!lines[lineNo].TrimStart().StartsWith("Scenario:"))
                            {
                                continue;
                            }
                            minLine = lineNo + 1;
                            break;
                        }

                        for (var lineNo = currentLine + 1; lineNo <= maxLine; lineNo++)
                        {
                            if (!lines[lineNo].TrimStart().StartsWith("Scenario:"))
                            {
                                continue;
                            }
                            maxLine = lineNo - 1;
                            break;
                        }

                        for (var lineNo = minLine; lineNo <= maxLine; lineNo++)
                        {
                            if (lineNo == currentLine)
                            {
                                return(string.Format("->" + lines[lineNo]));
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }

            return("(Unable to detect current step)");
        }
Exemple #11
0
        private SocketPurpose GetSocketPurpose()
        {
            var stackFrames = new StackTrace(1).GetFrames();

            if (stackFrames.FirstOrDefault(sf => sf.GetMethod().Name == CreateClusterMonitorSendingSocketMethod) != null)
            {
                return(SocketPurpose.ClusterMonitorSendingSocket);
            }
            if (stackFrames.FirstOrDefault(sf => sf.GetMethod().Name == CreateClusterMonitorSubscriptionSocketMethod) != null)
            {
                return(SocketPurpose.ClusterMonitorSubscriptionSocket);
            }
            if (stackFrames.FirstOrDefault(sf => sf.GetMethod().Name == CreateRouterCommunicationSocketMethod) != null)
            {
                return(SocketPurpose.RouterCommunicationSocket);
            }

            throw new Exception($"Socket creation method is unexpected: {string.Join(" @ ", stackFrames.Select(sf => sf.ToString()))}");
        }
Exemple #12
0
        private void SetScenarioText()
        {
            var stackFrames = new StackTrace().GetFrames();

            var stepAttributeFrame = stackFrames.FirstOrDefault(it => GetScenarioTextAttribute(it) != null);

            if (stepAttributeFrame != null)
            {
                SetParameters(stepAttributeFrame);
                ScenarioText = new ScenarioText($"{GetScenarioTextAttribute(stepAttributeFrame)}");
                return;
            }

            var callingFrame = stackFrames.FirstOrDefault(it => it.GetMethod().Name == _callerMember);

            if (callingFrame != null)
            {
                SetParameters(callingFrame);
                ScenarioText = new ScenarioText($"{callingFrame.GetMethod().Name.Humanize()}");
                return;
            }

            ScenarioText = new ScenarioText("No Scenario Text found (Use attribute [ScenarioText\"...\")] on your tests");
        }
Exemple #13
0
        private static int GetCurrentPositionText(StringBuilder messageBoxText)
        {
            int currentPositionText = 0;

            try
            {
                var frames = new StackTrace(true).GetFrames();
                if (frames != null)
                {
                    var featureFileFrame = frames.FirstOrDefault(f =>
                                                                 f.GetFileName() != null &&
                                                                 f.GetFileName().EndsWith(".feature"));

                    if (featureFileFrame != null)
                    {
                        var       lines       = File.ReadAllLines(featureFileFrame.GetFileName());
                        const int frameSize   = 20;
                        int       currentLine = featureFileFrame.GetFileLineNumber() - 1;
                        int       minLine     = Math.Max(0, currentLine - frameSize);
                        int       maxLine     = Math.Min(lines.Length - 1, currentLine + frameSize);

                        for (int lineNo = currentLine - 1; lineNo >= minLine; lineNo--)
                        {
                            if (lines[lineNo].TrimStart().StartsWith("Scenario:"))
                            {
                                minLine = lineNo + 1;
                                break;
                            }
                        }

                        for (int lineNo = currentLine + 1; lineNo <= maxLine; lineNo++)
                        {
                            if (lines[lineNo].TrimStart().StartsWith("Scenario:"))
                            {
                                maxLine = lineNo - 1;
                                break;
                            }
                        }

                        for (int lineNo = minLine; lineNo <= maxLine; lineNo++)
                        {
                            messageBoxText.Append(lineNo == currentLine ? "->  " : "    ");
                            messageBoxText.AppendLine(lines[lineNo]);
                            if (lineNo == currentLine)
                            {
                                currentPositionText = lineNo - minLine;
                            }
                        }

                        return(currentPositionText);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex, "GetCurrentPositionText");
            }

            messageBoxText.AppendLine("(Unable to detect current step)");

            return(currentPositionText);
        }
        /// <summary>
        /// Gets the text of the currently executing step.
        /// </summary>
        /// <remarks>
        /// Logic provided by Gaspar Nagy on StackOverflow
        /// </remarks>
        /// <returns>The step text.</returns>
        public string GetCurrentStepText()
        {
            int currentPositionText = 0;

            try
            {
                var frames = new StackTrace(true).GetFrames();
                if (frames != null)
                {
                    var featureFileFrame = frames.FirstOrDefault(f =>
                                                                 f.GetFileName() != null &&
                                                                 f.GetFileName().EndsWith(".feature"));

                    if (featureFileFrame != null)
                    {
                        var       lines       = File.ReadAllLines(featureFileFrame.GetFileName());
                        const int frameSize   = 20;
                        int       currentLine = featureFileFrame.GetFileLineNumber() - 1;
                        int       minLine     = Math.Max(0, currentLine - frameSize);
                        int       maxLine     = Math.Min(lines.Length - 1, currentLine + frameSize);

                        for (int lineNo = currentLine - 1; lineNo >= minLine; lineNo--)
                        {
                            if (lines[lineNo].TrimStart().StartsWith("Scenario:"))
                            {
                                minLine = lineNo + 1;
                                break;
                            }
                        }

                        for (int lineNo = currentLine + 1; lineNo <= maxLine; lineNo++)
                        {
                            if (lines[lineNo].TrimStart().StartsWith("Scenario:"))
                            {
                                maxLine = lineNo - 1;
                                break;
                            }
                        }

                        for (int lineNo = minLine; lineNo <= maxLine; lineNo++)
                        {
                            if (lineNo == currentLine)
                            {
                                currentPositionText = lineNo - minLine;
                                var result = new StringBuilder(lines[lineNo]);
                                for (int i = lineNo + 1; i < lines.Length; i++)
                                {
                                    if (!lines[i].TrimStart().StartsWith("|"))
                                    {
                                        break;
                                    }

                                    result.AppendLine();
                                    result.Append(lines[i]);
                                }

                                return(result.ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }

            return("(Unable to detect current step)");
        }