Repository navigation
Expand file tree
/
Copy pathtest_debug.py
More file actions
119 lines (90 loc) · 4.64 KB
/
Copy pathtest_debug.py
File metadata and controls
119 lines (90 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""Tests for vscode_common_python_lsp.debug."""
import os
from unittest.mock import MagicMock, patch
import pytest
from vscode_common_python_lsp.debug import setup_debugpy
PATCH_UPDATE = "vscode_common_python_lsp.debug.update_sys_path"
def test_no_env_var_does_nothing():
"""When USE_DEBUGPY is not set, debugpy is not loaded."""
with patch.dict(os.environ, {}, clear=True):
os.environ.pop("USE_DEBUGPY", None)
os.environ.pop("DEBUGPY_PATH", None)
setup_debugpy()
@pytest.mark.parametrize("value", ["False", "false", "0", "no", ""])
def test_disabled_values_do_nothing(value):
"""Non-truthy USE_DEBUGPY values skip debugpy setup."""
with patch.dict(os.environ, {"USE_DEBUGPY": value}, clear=False):
setup_debugpy()
@pytest.mark.parametrize("value", ["True", "TRUE", "1", "T"])
def test_enabled_without_path_does_nothing(value):
"""USE_DEBUGPY enabled but no DEBUGPY_PATH skips setup."""
env = {"USE_DEBUGPY": value}
with patch.dict(os.environ, env, clear=False):
os.environ.pop("DEBUGPY_PATH", None)
setup_debugpy()
def test_enabled_with_path_connects():
"""USE_DEBUGPY + DEBUGPY_PATH triggers debugpy.connect."""
mock_debugpy = MagicMock()
debugger_dir = os.path.dirname(__file__)
env = {"USE_DEBUGPY": "True", "DEBUGPY_PATH": debugger_dir}
with patch.dict(os.environ, env, clear=False):
with patch.dict("sys.modules", {"debugpy": mock_debugpy}):
with patch(PATCH_UPDATE) as mock_update:
setup_debugpy(port=9999)
mock_update.assert_called_once_with(debugger_dir, "fromEnvironment")
mock_debugpy.connect.assert_called_once_with(9999)
mock_debugpy.breakpoint.assert_called_once()
def test_debugpy_path_ending_with_debugpy_strips_suffix():
"""DEBUGPY_PATH ending with 'debugpy' uses parent directory."""
test_parent = os.path.dirname(__file__)
debugpy_path = os.path.join(test_parent, "debugpy")
env = {"USE_DEBUGPY": "True", "DEBUGPY_PATH": debugpy_path}
mock_debugpy = MagicMock()
with patch.dict(os.environ, env, clear=False):
with patch.dict("sys.modules", {"debugpy": mock_debugpy}):
with patch(PATCH_UPDATE) as mock_update:
setup_debugpy()
mock_update.assert_called_once_with(test_parent, "fromEnvironment")
def test_debugpy_path_with_similar_name_not_stripped():
"""DEBUGPY_PATH like 'mydebugpy' should NOT be stripped."""
test_parent = os.path.dirname(__file__)
debugpy_path = os.path.join(test_parent, "mydebugpy")
env = {"USE_DEBUGPY": "True", "DEBUGPY_PATH": debugpy_path}
mock_debugpy = MagicMock()
with patch.dict(os.environ, env, clear=False):
with patch.dict("sys.modules", {"debugpy": mock_debugpy}):
with patch(PATCH_UPDATE) as mock_update:
setup_debugpy()
# Should NOT strip — "mydebugpy" basename != "debugpy"
mock_update.assert_called_once_with(debugpy_path, "fromEnvironment")
# ---------------------------------------------------------------------------
# require_opt_in=False tests (for repos without USE_DEBUGPY guard)
# ---------------------------------------------------------------------------
def test_no_opt_in_skips_use_debugpy_check():
"""With require_opt_in=False, USE_DEBUGPY is not checked."""
mock_debugpy = MagicMock()
debugger_dir = os.path.dirname(__file__)
env = {"DEBUGPY_PATH": debugger_dir}
with patch.dict(os.environ, env, clear=True):
with patch.dict("sys.modules", {"debugpy": mock_debugpy}):
with patch(PATCH_UPDATE):
setup_debugpy(require_opt_in=False)
mock_debugpy.connect.assert_called_once_with(5678)
mock_debugpy.breakpoint.assert_called_once()
def test_no_opt_in_still_requires_debugpy_path():
"""With require_opt_in=False but no DEBUGPY_PATH, nothing happens."""
with patch.dict(os.environ, {}, clear=True):
setup_debugpy(require_opt_in=False)
@pytest.mark.parametrize("value", ["False", "0", "no"])
def test_no_opt_in_ignores_use_debugpy_value(value):
"""With require_opt_in=False, USE_DEBUGPY value is irrelevant."""
mock_debugpy = MagicMock()
debugger_dir = os.path.dirname(__file__)
env = {"USE_DEBUGPY": value, "DEBUGPY_PATH": debugger_dir}
with patch.dict(os.environ, env, clear=False):
with patch.dict("sys.modules", {"debugpy": mock_debugpy}):
with patch(PATCH_UPDATE):
setup_debugpy(require_opt_in=False)
mock_debugpy.connect.assert_called_once()