Exemplo n.º 1
0
        internal void StepTest(string filename, string breakFile, string arguments, int[] breakLines, Action <PythonProcess>[] breakAction, Action processLoaded, PythonDebugOptions options = PythonDebugOptions.RedirectOutput, bool waitForExit = true, params ExpectedStep[] kinds)
        {
            Console.WriteLine("--- Begin Step Test ---");
            var debugger = new PythonDebugger();

            if (breakFile == null)
            {
                breakFile = filename;
            }

            string fullPath = Path.GetFullPath(filename);
            string dir      = Path.GetDirectoryName(filename);
            var    process  = debugger.CreateProcess(Version.Version, Version.InterpreterPath, "\"" + fullPath + "\" " + (arguments ?? ""), dir, "", null, options);

            try {
                PythonThread thread = null;
                process.ThreadCreated += (sender, args) => {
                    thread = args.Thread;
                };


                AutoResetEvent processEvent = new AutoResetEvent(false);

                bool processLoad = false, stepComplete = false;
                process.ProcessLoaded += (sender, args) => {
                    foreach (var breakLine in breakLines)
                    {
                        var bp = process.AddBreakPointByFileExtension(breakLine, breakFile);
                        bp.Add();
                    }

                    processLoad = true;
                    processEvent.Set();

                    if (processLoaded != null)
                    {
                        processLoaded();
                    }
                };

                process.StepComplete += (sender, args) => {
                    stepComplete = true;
                    processEvent.Set();
                };

                int breakHits             = 0;
                ExceptionDispatchInfo edi = null;
                process.BreakpointHit += (sender, args) => {
                    try {
                        Console.WriteLine("Breakpoint hit");
                        if (breakAction != null)
                        {
                            if (breakHits >= breakAction.Length)
                            {
                                Assert.Fail("Unexpected breakpoint hit at {0}:{1}", args.Breakpoint.Filename, args.Breakpoint.LineNo);
                            }
                            breakAction[breakHits++](process);
                        }
                        stepComplete = true;
                        processEvent.Set();
                    } catch (Exception ex) {
                        edi = ExceptionDispatchInfo.Capture(ex);
                        try {
                            processEvent.Set();
                        } catch { }
                    }
                };

                process.Start();
                for (int curStep = 0; curStep < kinds.Length; curStep++)
                {
                    Console.WriteLine("Step {0} {1}", curStep, kinds[curStep].Kind);
                    // process the stepping events as they occur, we cannot callback during the
                    // event because the notificaiton happens on the debugger thread and we
                    // need to callback to get the frames.
                    AssertWaited(processEvent);
                    edi?.Throw();

                    // first time through we hit process load, each additional time we should hit step complete.
                    Debug.Assert((processLoad == true && stepComplete == false && curStep == 0) ||
                                 (stepComplete == true && processLoad == false && curStep != 0));

                    processLoad = stepComplete = false;

                    var frames   = thread.Frames;
                    var stepInfo = kinds[curStep];
                    Assert.AreEqual(stepInfo.StartLine, frames[0].LineNo, String.Format("{0} != {1} on {2} step", stepInfo.StartLine, frames[0].LineNo, curStep));

                    switch (stepInfo.Kind)
                    {
                    case StepKind.Into:
                        thread.StepInto();
                        break;

                    case StepKind.Out:
                        thread.StepOut();
                        break;

                    case StepKind.Over:
                        thread.StepOver();
                        break;

                    case StepKind.Resume:
                        process.Resume();
                        break;
                    }
                }
                if (waitForExit)
                {
                    WaitForExit(process);
                }
            } finally {
                process.Terminate();
            }
        }
Exemplo n.º 2
0
        private void StepTest(string filename, params ExpectedStep[] kinds)
        {
            var debugger = new PythonDebugger();

            string       fullPath = Path.GetFullPath(filename);
            string       dir      = Path.GetDirectoryName(filename);
            var          process  = debugger.CreateProcess(Version.Version, Version.Path, "\"" + fullPath + "\"", dir, "");
            PythonThread thread   = null;

            process.ThreadCreated += (sender, args) => {
                thread = args.Thread;
            };


            AutoResetEvent processEvent = new AutoResetEvent(false);

            bool processLoad = false, stepComplete = false;

            process.ProcessLoaded += (sender, args) => {
                processLoad = true;
                processEvent.Set();
            };

            process.StepComplete += (sender, args) => {
                stepComplete = true;
                processEvent.Set();
            };

            process.ExceptionRaised += (sender, args) => {
                args.Thread.Resume();
            };

            process.Start();

            for (int curStep = 0; curStep < kinds.Length; curStep++)
            {
                // process the stepping events as they occur, we cannot callback during the
                // event because the notificaiton happens on the debugger thread and we
                // need to callback to get the frames.
                processEvent.WaitOne();

                // first time through we hit process load, each additional time we should hit step complete.
                Debug.Assert((processLoad == true && stepComplete == false && curStep == 0) ||
                             (stepComplete == true && processLoad == false && curStep != 0));

                processLoad = stepComplete = false;

                var frames   = thread.GetFrames();
                var stepInfo = kinds[curStep];
                Assert.AreEqual(stepInfo.StartLine, frames[0].LineNo, String.Format("{0} != {1} on {2} step", stepInfo.StartLine, frames[0].LineNo, curStep));

                switch (stepInfo.Kind)
                {
                case StepKind.Into:
                    thread.StepInto();
                    break;

                case StepKind.Out:
                    thread.StepOut();
                    break;

                case StepKind.Over:
                    thread.StepOver();
                    break;

                case StepKind.Resume:
                    process.Resume();
                    break;
                }
            }

            process.WaitForExit();
        }