Ejemplo n.º 1
0
 public bool SetDefaultExceptionHitTreatment(ExceptionHitTreatment exceptionTreatment) {
     if (_defaultExceptionTreatment != exceptionTreatment) {
         _defaultExceptionTreatment = exceptionTreatment;
         return true;
     }
     return false;
 }
Ejemplo n.º 2
0
 public bool SetDefaultExceptionHitTreatment(ExceptionHitTreatment exceptionTreatment)
 {
     if (this._defaultExceptionTreatment != exceptionTreatment)
     {
         this._defaultExceptionTreatment = exceptionTreatment;
         return(true);
     }
     return(false);
 }
Ejemplo n.º 3
0
        public void GetExceptionHitTreatmentForUnknownError()
        {
            // Arrange
            var exceptionHandler = new ExceptionHandler();

            // Act
            ExceptionHitTreatment result = exceptionHandler.GetExceptionHitTreatment("Error(MY)");

            // Assert
            Assert.AreEqual(ExceptionHitTreatment.BreakNever, result);
        }
Ejemplo n.º 4
0
        public void GetExceptionHitTreatmentForUnknownErrorAfterChangingDefaults()
        {
            // Arrange
            var exceptionHandler = new ExceptionHandler();
            const ExceptionHitTreatment newDefault = ExceptionHitTreatment.BreakAlways;

            // Act
            exceptionHandler.SetDefaultExceptionHitTreatment(newDefault);
            ExceptionHitTreatment result = exceptionHandler.GetExceptionHitTreatment("Error(MY)");

            // Assert
            Assert.AreEqual(newDefault, result);
        }
Ejemplo n.º 5
0
        public void SetNewExceptionTreatments()
        {
            // Arrange
            var          exceptionHandler        = new ExceptionHandler();
            const string exceptionName           = "Error";
            const ExceptionHitTreatment newValue = ExceptionHitTreatment.BreakOnUnhandled;
            ExceptionHitTreatment       initial  = exceptionHandler.GetExceptionHitTreatment(exceptionName);

            // Act
            bool updated = exceptionHandler.SetExceptionTreatments(new Dictionary <string, ExceptionHitTreatment> {
                { exceptionName, newValue }
            });
            ExceptionHitTreatment changed = exceptionHandler.GetExceptionHitTreatment(exceptionName);

            // Assert
            Assert.AreEqual(ExceptionHitTreatment.BreakNever, initial);
            Assert.IsTrue(updated);
            Assert.AreEqual(newValue, changed);
        }
Ejemplo n.º 6
0
        public void ClearExceptionTreatments()
        {
            // Arrange
            var          exceptionHandler = new ExceptionHandler();
            const string exceptionName    = "SyntaxError";

            exceptionHandler.SetExceptionTreatments(new Dictionary <string, ExceptionHitTreatment> {
                { exceptionName, ExceptionHitTreatment.BreakAlways }
            });
            ExceptionHitTreatment initial = exceptionHandler.GetExceptionHitTreatment(exceptionName);

            // Act
            bool updated = exceptionHandler.ClearExceptionTreatments(new Dictionary <string, ExceptionHitTreatment> {
                { exceptionName, ExceptionHitTreatment.BreakOnUnhandled }
            });
            ExceptionHitTreatment changed = exceptionHandler.GetExceptionHitTreatment(exceptionName);

            // Assert
            Assert.AreEqual(ExceptionHitTreatment.BreakAlways, initial);
            Assert.IsTrue(updated);
            Assert.AreEqual(ExceptionHitTreatment.BreakNever, changed);
        }
        public void ClearExceptionTreatment(
            ExceptionHitTreatment? defaultExceptionTreatment,
            ICollection<KeyValuePair<string, ExceptionHitTreatment>> exceptionTreatments
        ) {
            DebuggerClient.RunWithRequestExceptionsHandled(async () => {
                bool updated = false;

                if (defaultExceptionTreatment.HasValue) {
                    updated |= _exceptionHandler.SetDefaultExceptionHitTreatment(ExceptionHitTreatment.BreakNever);
                }

                updated |= _exceptionHandler.ClearExceptionTreatments(exceptionTreatments);

                if (updated) {
                    var tokenSource = new CancellationTokenSource(_timeout);
                    await SetExceptionBreakAsync(tokenSource.Token).ConfigureAwait(false);
                }
            });
        }
        internal void TestDebuggerSteps(
            NodeDebugger process,
            NodeThread thread,
            string filename,
            IEnumerable<TestStep> steps,
            ExceptionHitTreatment? defaultExceptionTreatment = null,
            ICollection<KeyValuePair<string, ExceptionHitTreatment>> exceptionTreatments = null,
            bool waitForExit = false
        ) {
            if (!Path.IsPathRooted(filename)) {
                filename = DebuggerTestPath + filename;
            }

            // Since Alpha does not support break on unhandled, and the commonly used Express module has handled SyntaxError exceptions,
            // for alpha we have SyntaxErrors set to BreakNever by default.  Here we set it to BreakAlways so unit tests can run
            // assuming BreakAlways is the default.            
            // TODO: Remove once exception treatment is updated for just my code support when it is added after Alpha
            process.SetExceptionTreatment(null, CollectExceptionTreatments(ExceptionHitTreatment.BreakAlways, "SyntaxError"));

            if (defaultExceptionTreatment != null || exceptionTreatments != null) {
                process.SetExceptionTreatment(defaultExceptionTreatment, exceptionTreatments);
            }

            Dictionary<Breakpoint, NodeBreakpoint> breakpoints = new Dictionary<Breakpoint, NodeBreakpoint>();

            AutoResetEvent entryPointHit = new AutoResetEvent(false);
            process.EntryPointHit += (sender, e) => {
                Console.WriteLine("EntryPointHit");
                Assert.AreEqual(e.Thread, thread);
                entryPointHit.Set();
            };

            AutoResetEvent breakpointBound = new AutoResetEvent(false);
            process.BreakpointBound += (sender, e) => {
                Console.WriteLine("BreakpointBound {0} {1}", e.BreakpointBinding.Position.FileName, e.BreakpointBinding.Position.Line);
                breakpointBound.Set();
            };

            AutoResetEvent breakpointUnbound = new AutoResetEvent(false);
            process.BreakpointUnbound += (sender, e) => {
                Console.WriteLine("BreakpointUnbound");
                breakpointUnbound.Set();
            };

            AutoResetEvent breakpointBindFailure = new AutoResetEvent(false);
            process.BreakpointBindFailure += (sender, e) => {
                Console.WriteLine("BreakpointBindFailure");
                breakpointBindFailure.Set();
            };

            AutoResetEvent breakpointHit = new AutoResetEvent(false);
            process.BreakpointHit += (sender, e) => {
                Console.WriteLine("BreakpointHit {0}", e.BreakpointBinding.Target.Line);
                Assert.AreEqual(e.Thread, thread);
                Assert.AreEqual(e.BreakpointBinding.Target.Line, thread.Frames.First().Line);
                breakpointHit.Set();
            };

            AutoResetEvent stepComplete = new AutoResetEvent(false);
            process.StepComplete += (sender, e) => {
                Console.WriteLine("StepComplete");
                Assert.AreEqual(e.Thread, thread);
                stepComplete.Set();
            };

            AutoResetEvent exceptionRaised = new AutoResetEvent(false);
            NodeException exception = null;
            process.ExceptionRaised += (sender, e) => {
                Console.WriteLine("ExceptionRaised");
                Assert.AreEqual(e.Thread, thread);
                exception = e.Exception;
                exceptionRaised.Set();
            };

            AutoResetEvent processExited = new AutoResetEvent(false);
            int exitCode = 0;
            process.ProcessExited += (sender, e) => {
                Console.WriteLine("ProcessExited {0}", e.ExitCode);
                exitCode = e.ExitCode;
                processExited.Set();
            };

            Console.WriteLine("-----------------------------------------");
            Console.WriteLine("Begin debugger step test");
            foreach (var step in steps) {
                Console.WriteLine("Step: {0}", step._action);
                Assert.IsFalse(
                    ((step._expectedEntryPointHit != null ? 1 : 0) +
                     (step._expectedBreakpointHit != null ? 1 : 0) +
                     (step._expectedStepComplete != null ? 1 : 0) +
                     (step._expectedExceptionRaised != null ? 1 : 0)) > 1);
                bool wait = false;
                NodeBreakpoint nodeBreakpoint;
                switch (step._action) {
                    case TestAction.None:
                        break;
                    case TestAction.Wait:
                        wait = true;
                        break;
                    case TestAction.ResumeThread:
                        thread.Resume();
                        wait = true;
                        break;
                    case TestAction.ResumeProcess:
                        process.Resume();
                        wait = true;
                        break;
                    case TestAction.StepOver:
                        thread.StepOver();
                        wait = true;
                        break;
                    case TestAction.StepInto:
                        thread.StepInto();
                        wait = true;
                        break;
                    case TestAction.StepOut:
                        thread.StepOut();
                        wait = true;
                        break;
                    case TestAction.AddBreakpoint:
                        string breakpointFileName = step._targetBreakpointFile;
                        if (breakpointFileName != null) {
                            if (!step._builtin && !Path.IsPathRooted(breakpointFileName)) {
                                breakpointFileName = DebuggerTestPath + breakpointFileName;
                            }
                        } else {
                            breakpointFileName = filename;
                        }
                        int breakpointLine = step._targetBreakpoint.Value;
                        int breakpointColumn = step._targetBreakpointColumn.HasValue ? step._targetBreakpointColumn.Value : 0;
                        Breakpoint breakpoint = new Breakpoint(breakpointFileName, breakpointLine, breakpointColumn);
                        Assert.IsFalse(breakpoints.TryGetValue(breakpoint, out nodeBreakpoint));
                        breakpoints[breakpoint] =
                            AddBreakPoint(
                                process,
                                breakpointFileName,
                                breakpointLine,
                                breakpointColumn,
                                step._enabled ?? true,
                                step._breakOn ?? new BreakOn(),
                                step._condition
                            );
                        if (step._expectFailure) {
                            AssertWaited(breakpointBindFailure);
                            AssertNotSet(breakpointBound);
                            breakpointBindFailure.Reset();
                        } else {
                            AssertWaited(breakpointBound);
                            AssertNotSet(breakpointBindFailure);
                            breakpointBound.Reset();
                        }
                        break;
                    case TestAction.RemoveBreakpoint:
                        breakpointFileName = step._targetBreakpointFile ?? filename;
                        breakpointLine = step._targetBreakpoint.Value;
                        breakpointColumn = step._targetBreakpointColumn.HasValue ? step._targetBreakpointColumn.Value : 0;
                        breakpoint = new Breakpoint(breakpointFileName, breakpointLine, breakpointColumn);
                        breakpoints[breakpoint].Remove().WaitAndUnwrapExceptions();
                        breakpoints.Remove(breakpoint);
                        AssertWaited(breakpointUnbound);
                        breakpointUnbound.Reset();
                        break;
                    case TestAction.UpdateBreakpoint:
                        breakpointFileName = step._targetBreakpointFile ?? filename;
                        breakpointLine = step._targetBreakpoint.Value;
                        breakpointColumn = step._targetBreakpointColumn.HasValue ? step._targetBreakpointColumn.Value : 0;
                        breakpoint = new Breakpoint(breakpointFileName, breakpointLine, breakpointColumn);
                        nodeBreakpoint = breakpoints[breakpoint];
                        foreach (var breakpointBinding in nodeBreakpoint.GetBindings()) {
                            if (step._hitCount != null) {
                                Assert.IsTrue(breakpointBinding.SetHitCountAsync(step._hitCount.Value).WaitAndUnwrapExceptions());
                            }
                            if (step._enabled != null) {
                                Assert.IsTrue(breakpointBinding.SetEnabledAsync(step._enabled.Value).WaitAndUnwrapExceptions());
                            }
                            if (step._breakOn != null) {
                                Assert.IsTrue(breakpointBinding.SetBreakOnAsync(step._breakOn.Value).WaitAndUnwrapExceptions());
                            }
                            if (step._condition != null) {
                                Assert.IsTrue(breakpointBinding.SetConditionAsync(step._condition).WaitAndUnwrapExceptions());
                            }
                        }
                        break;
                    case TestAction.KillProcess:
                        process.Terminate();
                        break;
                    case TestAction.Detach:
                        process.Detach();
                        break;
                }

                if (wait) {
                    if (step._expectedEntryPointHit != null) {
                        AssertWaited(entryPointHit);
                        AssertNotSet(breakpointHit);
                        AssertNotSet(stepComplete);
                        AssertNotSet(exceptionRaised);
                        Assert.IsNull(exception);
                        entryPointHit.Reset();
                    } else if (step._expectedBreakpointHit != null) {
                        if (step._expectReBind) {
                            AssertWaited(breakpointUnbound);
                            AssertWaited(breakpointBound);
                            breakpointUnbound.Reset();
                            breakpointBound.Reset();
                        }
                        AssertWaited(breakpointHit);
                        AssertNotSet(entryPointHit);
                        AssertNotSet(stepComplete);
                        AssertNotSet(exceptionRaised);
                        Assert.IsNull(exception);
                        breakpointHit.Reset();
                    } else if (step._expectedStepComplete != null) {
                        AssertWaited(stepComplete);
                        AssertNotSet(entryPointHit);
                        AssertNotSet(breakpointHit);
                        AssertNotSet(exceptionRaised);
                        Assert.IsNull(exception);
                        stepComplete.Reset();
                    } else if (step._expectedExceptionRaised != null) {
                        AssertWaited(exceptionRaised);
                        AssertNotSet(entryPointHit);
                        AssertNotSet(breakpointHit);
                        AssertNotSet(stepComplete);
                        exceptionRaised.Reset();
                    } else {
                        AssertNotSet(entryPointHit);
                        AssertNotSet(breakpointHit);
                        AssertNotSet(stepComplete);
                        AssertNotSet(exceptionRaised);
                        Assert.IsNull(exception);
                    }
                }

                if (step._expectedEntryPointHit != null) {
                    Assert.AreEqual(step._expectedEntryPointHit.Value, thread.Frames.First().Line);
                } else if (step._expectedBreakpointHit != null) {
                    Assert.AreEqual(step._expectedBreakpointHit.Value, thread.Frames.First().Line);
                } else if (step._expectedStepComplete != null) {
                    Assert.AreEqual(step._expectedStepComplete.Value, thread.Frames.First().Line);
                } else if (step._expectedExceptionRaised != null) {
                    Assert.AreEqual(step._expectedExceptionRaised.TypeName, exception.TypeName);
                    Assert.AreEqual(step._expectedExceptionRaised.Description, exception.Description);
                    if (step._expectedExceptionRaised.LineNo != null) {
                        Assert.AreEqual(step._expectedExceptionRaised.LineNo.Value, thread.Frames[0].Line);
                    }
                    exception = null;
                }
                var expectedBreakFile = step._expectedBreakFile;
                if (expectedBreakFile != null) {
                    if (!step._builtin && !Path.IsPathRooted(expectedBreakFile)) {
                        expectedBreakFile = DebuggerTestPath + expectedBreakFile;
                    }
                    Assert.AreEqual(expectedBreakFile, thread.Frames.First().FileName);
                }
                var expectedBreakFunction = step._expectedBreakFunction;
                if (expectedBreakFunction != null) {
                    Assert.AreEqual(expectedBreakFunction, thread.Frames.First().FunctionName);
                }

                if (step._expectedHitCount != null) {
                    string breakpointFileName = step._targetBreakpointFile ?? filename;
                    if (!step._builtin && !Path.IsPathRooted(breakpointFileName)) {
                        breakpointFileName = DebuggerTestPath + breakpointFileName;
                    }
                    int breakpointLine = step._targetBreakpoint.Value;
                    int breakpointColumn = step._targetBreakpointColumn.HasValue ? step._targetBreakpointColumn.Value : 0;
                    var breakpoint = new Breakpoint(breakpointFileName, breakpointLine, breakpointColumn);
                    nodeBreakpoint = breakpoints[breakpoint];
                    foreach (var breakpointBinding in nodeBreakpoint.GetBindings()) {
                        Assert.AreEqual(step._expectedHitCount.Value, breakpointBinding.GetHitCount());
                    }
                }

                if (step._validation != null) {
                    step._validation(process, thread);
                }

                if (step._expectedExitCode != null) {
                    AssertWaited(processExited);
                    Assert.AreEqual(step._expectedExitCode.Value, exitCode);
                }
            }

            if (waitForExit) {
                process.WaitForExit(10000);
            }

            AssertNotSet(entryPointHit);
            AssertNotSet(breakpointHit);
            AssertNotSet(stepComplete);
            AssertNotSet(exceptionRaised);
            Assert.IsNull(exception);
        }
        internal void TestDebuggerSteps(
            string filename,
            IEnumerable<TestStep> steps,
            string interpreterOptions = null,
            Action<NodeDebugger> onProcessCreated = null,
            ExceptionHitTreatment? defaultExceptionTreatment = null,
            ICollection<KeyValuePair<string, ExceptionHitTreatment>> exceptionTreatments = null,
            string scriptArguments = null
        ) {
            if (!Path.IsPathRooted(filename)) {
                filename = DebuggerTestPath + filename;
            }

            NodeThread thread = null;
            using (var process = DebugProcess(
                filename,
                onProcessCreated: onProcessCreated,
                onLoadComplete: (newproc, newthread) => {
                    thread = newthread;
                },
                interpreterOptions: interpreterOptions,
                arguments: scriptArguments,
                resumeOnProcessLoad: false
            )) {
                TestDebuggerSteps(
                    process,
                    thread,
                    filename,
                    steps,
                    defaultExceptionTreatment,
                    exceptionTreatments,
                    waitForExit: true);
            }
        }
 internal void TestExceptions(
     string filename,
     ExceptionHitTreatment? defaultExceptionTreatment,
     ICollection<KeyValuePair<string, ExceptionHitTreatment>> exceptionTreatments,
     int expectedExitCode,
     params ExceptionInfo[] exceptions
 ) {
     var steps = new List<TestStep>();
     foreach (var exception in exceptions) {
         steps.Add(new TestStep(action: TestAction.ResumeProcess, expectedExceptionRaised: exception));
     }
     steps.Add(new TestStep(action: TestAction.ResumeProcess, expectedExitCode: expectedExitCode));
     TestDebuggerSteps(
         filename,
         steps,
         defaultExceptionTreatment: defaultExceptionTreatment,
         exceptionTreatments: exceptionTreatments
         );
 }
 internal ICollection<KeyValuePair<string, ExceptionHitTreatment>> CollectExceptionTreatments(ExceptionHitTreatment exceptionTreatment = ExceptionHitTreatment.BreakAlways, params string[] exceptionNames) {
     return exceptionNames.Select(
         name => new KeyValuePair<string, ExceptionHitTreatment>(name, exceptionTreatment)
     ).ToArray();
 }
Ejemplo n.º 12
0
 internal ICollection <KeyValuePair <string, ExceptionHitTreatment> > CollectExceptionTreatments(ExceptionHitTreatment exceptionTreatment = ExceptionHitTreatment.BreakAlways, params string[] exceptionNames)
 {
     return(exceptionNames.Select(
                name => new KeyValuePair <string, ExceptionHitTreatment>(name, exceptionTreatment)
                ).ToArray());
 }