| 1 | import pytest |
| 2 | |
| 3 | class AppTestBreakpoint: |
| 4 | def setup_class(cls): |
| 5 | cls.w_import_mock_pdb = cls.space.appexec([], """(): |
| 6 | import sys |
| 7 | from contextlib import contextmanager |
| 8 | from types import ModuleType |
| 9 | |
| 10 | @contextmanager |
| 11 | def w_import_mock_pdb(): |
| 12 | try: |
| 13 | mock_pdb = ModuleType('pdb') |
| 14 | mock_pdb.set_trace = None |
| 15 | sys.modules['pdb'] = mock_pdb |
| 16 | yield mock_pdb |
| 17 | finally: |
| 18 | del sys.modules['pdb'] |
| 19 | |
| 20 | return w_import_mock_pdb |
| 21 | """) |
| 22 | |
| 23 | cls.w_mock_function = cls.space.appexec([], """(): |
| 24 | from contextlib import contextmanager |
| 25 | |
| 26 | class MockedCallable: |
| 27 | def __init__(self): |
| 28 | self.called = 0 |
| 29 | |
| 30 | def __call__(self, *args, **kwargs): |
| 31 | self.called += 1 |
| 32 | self.last_call = (args, kwargs) |
| 33 | |
| 34 | @contextmanager |
| 35 | def mock_function(scope, attr, delete=False): |
| 36 | old = getattr(scope, attr) |
| 37 | try: |
| 38 | if not delete: |
| 39 | new = MockedCallable() |
| 40 | setattr(scope, attr, new) |
| 41 | yield new |
| 42 | else: |
| 43 | delattr(scope, attr) |
| 44 | yield |
| 45 | finally: |
| 46 | setattr(scope, attr, old) |
| 47 | |
| 48 | return mock_function |
| 49 | """) |
| 50 | |
| 51 | |
| 52 | def test_default(self): |
| 53 | import sys |
| 54 | assert sys.breakpointhook is sys.__breakpointhook__ |
| 55 | |
| 56 | import os |
| 57 | assert 'PYTHONBREAKPOINT' not in os.environ |
| 58 | |
| 59 | with self.import_mock_pdb() as pdb: |
| 60 | with self.mock_function(pdb, 'set_trace') as mocked: |
| 61 | breakpoint() |
| 62 | assert mocked.called == 1 |
| 63 | |
| 64 | |
| 65 | def test_args_kwargs(self): |
| 66 | import sys |
| 67 | with self.import_mock_pdb() as pdb: |
| 68 | with self.mock_function(pdb, 'set_trace') as mocked: |
| 69 | breakpoint(1, 2, 3, x=4, y=5) |
| 70 | assert mocked.called == 1 |
| 71 | assert mocked.last_call == ((1, 2, 3), {'x': 4, 'y': 5}) |
| 72 | |
| 73 | |
| 74 | def test_breakpointhook(self): |
| 75 | import sys |
| 76 | with self.import_mock_pdb() as pdb: |
| 77 | with self.mock_function(sys, 'breakpointhook') as mocked, \ |
| 78 | self.mock_function(pdb, 'set_trace') as mocked_pdb: |
| 79 | breakpoint() |
| 80 | assert mocked.called == 1 |
| 81 | assert mocked_pdb.called == 0 |
| 82 | |
| 83 | with self.mock_function(pdb, 'set_trace') as mocked_pdb: |
| 84 | breakpoint() |
| 85 | assert mocked_pdb.called == 1 |
| 86 | |
| 87 | |
| 88 | def test_breakpointhook_lost(self): |
| 89 | import sys |
| 90 | with self.import_mock_pdb() as pdb: |
| 91 | with self.mock_function(sys, 'breakpointhook', delete=True): |
| 92 | with raises(RuntimeError) as excinfo: |
| 93 | breakpoint() |
| 94 | assert str(excinfo.value) == "lost sys.breakpointhook" |
| 95 | |
| 96 | |
| 97 | @pytest.mark.dont_track_allocations('putenv intentionally keeps strings alive') |
| 98 | def test_env_default(self): |
| 99 | import os |
| 100 | try: |
| 101 | os.environ['PYTHONBREAKPOINT'] = "" |
| 102 | |
| 103 | import sys |
| 104 | with self.import_mock_pdb() as pdb: |
| 105 | with self.mock_function(pdb, 'set_trace') as mocked: |
| 106 | breakpoint() |
| 107 | assert mocked.called == 1 |
| 108 | finally: |
| 109 | del os.environ['PYTHONBREAKPOINT'] |
| 110 | |
| 111 | |
| 112 | @pytest.mark.dont_track_allocations('putenv intentionally keeps strings alive') |
| 113 | def test_env_disable(self): |
| 114 | import os |
| 115 | try: |
| 116 | os.environ['PYTHONBREAKPOINT'] = "0" |
| 117 | |
| 118 | import sys |
| 119 | with self.import_mock_pdb() as pdb: |
| 120 | with self.mock_function(pdb, 'set_trace') as mocked: |
| 121 | breakpoint() |
| 122 | assert mocked.called == 0 |
| 123 | |
| 124 | with self.mock_function(sys, 'breakpointhook') as mocked: |
| 125 | breakpoint() |
| 126 | assert mocked.called == 1 |
| 127 | finally: |
| 128 | del os.environ['PYTHONBREAKPOINT'] |
| 129 | |
| 130 | |
| 131 | @pytest.mark.dont_track_allocations('putenv intentionally keeps strings alive') |
| 132 | def test_env_other(self): |
| 133 | import os |
| 134 | try: |
| 135 | os.environ['PYTHONBREAKPOINT'] = 'sys.exit' |
| 136 | |
| 137 | import sys |
| 138 | with self.import_mock_pdb() as pdb: |
| 139 | with self.mock_function(sys, 'exit') as mocked, \ |
| 140 | self.mock_function(pdb, 'set_trace') as mocked_pdb: |
| 141 | breakpoint() |
| 142 | assert mocked.called == 1 |
| 143 | assert mocked_pdb.called == 0 |
| 144 | finally: |
| 145 | del os.environ['PYTHONBREAKPOINT'] |
| 146 | |
| 147 | |
| 148 | @pytest.mark.dont_track_allocations('putenv intentionally keeps strings alive') |
| 149 | def test_env_nonexistent(self): |
| 150 | import os |
| 151 | import warnings |
| 152 | try: |
| 153 | os.environ['PYTHONBREAKPOINT'] = 'blah.bleh' |
| 154 | |
| 155 | import sys |
| 156 | with self.import_mock_pdb() as pdb: |
| 157 | with self.mock_function(pdb, 'set_trace') as mocked_pdb, \ |
| 158 | warnings.catch_warnings(record=True) as w: |
| 159 | breakpoint() |
| 160 | assert mocked_pdb.called == 0 |
| 161 | assert len(w) == 1 |
| 162 | assert str(w[-1].message) == 'Ignoring unimportable $PYTHONBREAKPOINT: "blah.bleh"' |
| 163 | finally: |
| 164 | del os.environ['PYTHONBREAKPOINT'] |