|
| 1 | +import sys |
| 2 | +import types |
| 3 | +import logging |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +from ckanext.pycharm_debugger.plugin import PycharmDebugger |
| 7 | + |
| 8 | + |
| 9 | +def test_update_config_debug_enabled_import_failure(monkeypatch, caplog): |
| 10 | + caplog.set_level(logging.INFO) |
| 11 | + |
| 12 | + config = { |
| 13 | + 'debug.remote': 'True', |
| 14 | + 'debug.remote.host.ip': '127.0.0.1', |
| 15 | + 'debug.remote.host.port': '1234', |
| 16 | + 'debug.remote.stdout_to_server': 'True', |
| 17 | + 'debug.remote.stderr_to_server': 'True', |
| 18 | + 'debug.remote.suspend': 'False', |
| 19 | + } |
| 20 | + |
| 21 | + sys_path_original = list(sys.path) |
| 22 | + monkeypatch.setattr(sys, 'path', list(sys_path_original)) |
| 23 | + |
| 24 | + # Mock pydevd_pycharm to simulate import failure |
| 25 | + mock_pydevd = types.SimpleNamespace(settrace=mock.Mock(side_effect=ConnectionRefusedError("Connection refused"))) |
| 26 | + monkeypatch.setitem(sys.modules, 'pydevd_pycharm', mock_pydevd) |
| 27 | + |
| 28 | + plugin = PycharmDebugger() |
| 29 | + plugin.update_config(config) |
| 30 | + |
| 31 | + assert "pydevd_pycharm is missing" in caplog.text or \ |
| 32 | + "Failed to connect to debug server" in caplog.text |
| 33 | + |
| 34 | + sys.path = sys_path_original |
| 35 | + |
| 36 | + |
| 37 | +def test_update_config_debug_enabled_mimic_missing(monkeypatch, caplog): |
| 38 | + caplog.set_level(logging.INFO) |
| 39 | + |
| 40 | + config = { |
| 41 | + 'debug.remote': 'True', |
| 42 | + 'debug.remote.egg_dir': '/dummy/dir', |
| 43 | + 'debug.remote.egg_file': 'dummy.egg', |
| 44 | + 'debug.remote.host.ip': '127.0.0.1', |
| 45 | + 'debug.remote.host.port': '1234', |
| 46 | + 'debug.remote.stdout_to_server': 'True', |
| 47 | + 'debug.remote.stderr_to_server': 'True', |
| 48 | + 'debug.remote.suspend': 'True', |
| 49 | + } |
| 50 | + |
| 51 | + sys_path_original = list(sys.path) |
| 52 | + monkeypatch.setattr(sys, 'path', list(sys_path_original)) |
| 53 | + |
| 54 | + monkeypatch.delitem(sys.modules, 'pydevd_pycharm', raising=False) |
| 55 | + |
| 56 | + plugin = PycharmDebugger() |
| 57 | + plugin.update_config(config) |
| 58 | + |
| 59 | + assert "pydevd_pycharm is missing" in caplog.text or \ |
| 60 | + "Failed to connect to debug server" in caplog.text |
| 61 | + |
| 62 | + sys.path = sys_path_original |
| 63 | + |
| 64 | + |
| 65 | +def test_update_config_see_manual_egg_injected_into_sys(monkeypatch, caplog): |
| 66 | + caplog.set_level(logging.INFO) |
| 67 | + |
| 68 | + config = { |
| 69 | + 'debug.remote': 'False', |
| 70 | + 'debug.remote.egg_dir': '/dummy/dir', |
| 71 | + 'debug.remote.egg_file': 'dummy.egg', |
| 72 | + 'debug.remote.host.ip': '127.0.0.1', |
| 73 | + 'debug.remote.host.port': '1234', |
| 74 | + 'debug.remote.stdout_to_server': 'True', |
| 75 | + 'debug.remote.stderr_to_server': 'True', |
| 76 | + 'debug.remote.suspend': 'True', |
| 77 | + } |
| 78 | + |
| 79 | + sys_path_original = list(sys.path) |
| 80 | + monkeypatch.setattr(sys, 'path', list(sys_path_original)) |
| 81 | + |
| 82 | + plugin = PycharmDebugger() |
| 83 | + plugin.update_config(config) |
| 84 | + |
| 85 | + assert "Initiating supplied egg path: /dummy/dir file: dummy.egg" in caplog.text |
| 86 | + assert '/dummy/dir/dummy.egg' in sys.path |
| 87 | + |
| 88 | + sys.path = sys_path_original |
| 89 | + |
| 90 | + |
| 91 | +def test_update_config_debug_disabled(monkeypatch, caplog): |
| 92 | + caplog.set_level(logging.INFO) |
| 93 | + |
| 94 | + config = { |
| 95 | + 'debug.remote': 'False', |
| 96 | + } |
| 97 | + |
| 98 | + sys_path_original = list(sys.path) |
| 99 | + monkeypatch.setattr(sys, 'path', list(sys_path_original)) |
| 100 | + |
| 101 | + plugin = PycharmDebugger() |
| 102 | + plugin.update_config(config) |
| 103 | + |
| 104 | + assert "PyCharm Debugger not enabled" in caplog.text |
| 105 | + |
| 106 | + sys.path = sys_path_original |
| 107 | + |
| 108 | + |
| 109 | +def test_update_config_debug_import_failure(monkeypatch, caplog): |
| 110 | + caplog.set_level(logging.INFO) |
| 111 | + |
| 112 | + config = { |
| 113 | + 'debug.remote': 'True', |
| 114 | + } |
| 115 | + |
| 116 | + sys_path_original = list(sys.path) |
| 117 | + monkeypatch.setattr(sys, 'path', list(sys_path_original)) |
| 118 | + |
| 119 | + # Mock pydevd_pycharm to raise ImportError when accessed |
| 120 | + monkeypatch.setattr('builtins.__import__', mock.Mock(side_effect=ImportError("pydevd_pycharm not found"))) |
| 121 | + |
| 122 | + plugin = PycharmDebugger() |
| 123 | + plugin.update_config(config) |
| 124 | + |
| 125 | + assert "debug.enabled set to True, but pydevd_pycharm is missing." in caplog.text |
| 126 | + |
| 127 | + sys.path = sys_path_original |
| 128 | + |
| 129 | + |
| 130 | +def test_update_config_debug_enabled_nameerror_or_importerror(monkeypatch, caplog): |
| 131 | + caplog.set_level(logging.WARNING) |
| 132 | + |
| 133 | + config = { |
| 134 | + 'debug.remote': 'True', |
| 135 | + 'debug.remote.host.ip': '127.0.0.1', |
| 136 | + 'debug.remote.host.port': '1234', |
| 137 | + 'debug.remote.stdout_to_server': 'True', |
| 138 | + 'debug.remote.stderr_to_server': 'True', |
| 139 | + 'debug.remote.suspend': 'True', |
| 140 | + } |
| 141 | + |
| 142 | + sys_path_original = list(sys.path) |
| 143 | + monkeypatch.setattr(sys, 'path', list(sys_path_original)) |
| 144 | + |
| 145 | + # Simulate ImportError by removing the module from sys.modules |
| 146 | + monkeypatch.delitem(sys.modules, 'pydevd_pycharm', raising=False) |
| 147 | + |
| 148 | + # Mock pydevd_pycharm to raise ImportError when accessed |
| 149 | + monkeypatch.setattr('builtins.__import__', mock.Mock(side_effect=ConnectionRefusedError("Can't Connect"))) |
| 150 | + |
| 151 | + plugin = PycharmDebugger() |
| 152 | + plugin.update_config(config) |
| 153 | + |
| 154 | + assert "Failed to connect to debug server; is it started?" in caplog.text |
| 155 | + |
| 156 | + sys.path = sys_path_original |
0 commit comments