-
Notifications
You must be signed in to change notification settings - Fork 2
/
pytest_edit.py
203 lines (149 loc) · 4.99 KB
/
pytest_edit.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from __future__ import annotations
import os
import shutil
import subprocess
import pytest
DEFAULTS = {
"posix": "vi",
"nt": "notepad",
}
def choose_editor() -> str | None:
user_editor = os.getenv("EDITOR")
if user_editor is not None:
return user_editor
return DEFAULTS.get(os.name, None)
def no_lineno(path: str, _: int) -> list[str]:
return [path]
def vi_style_linemark(path: str, lineno: int) -> list[str]:
return [path, f"+{lineno}"]
def emacsclient_linemark(path: str, lineno: int) -> list[str]:
return [f"+{lineno}:0", path]
def notepad_plus_plus_n_opt(path: str, lineno: int) -> list[str]:
return [path, f"-n{lineno}"]
def vscode_goto(path: str, lineno: int) -> list[str]:
return ["--goto", f"{path}:{lineno}:0"]
def pycharm_line_opt(path: str, lineno: int) -> list[str]:
return ["--line", str(lineno), path]
def hx_style_linemark(path: str, lineno: int) -> list[str]:
return [f"{path}:{lineno}"]
OPT_GENERATOR = {
"vi": vi_style_linemark,
"vim": vi_style_linemark,
"nvim": vi_style_linemark,
"nano": vi_style_linemark,
"gedit": vi_style_linemark,
"emacsclient": emacsclient_linemark,
"code": vscode_goto,
"notepad": no_lineno,
"notepad++": notepad_plus_plus_n_opt,
"pycharm64": pycharm_line_opt,
"pycharm": pycharm_line_opt,
"hx": hx_style_linemark,
}
def call_tty_child(argv):
subprocess.call(argv)
def call_detached(argv):
if os.name == "nt":
subprocess.Popen(
argv,
creationflags=subprocess.DETACHED_PROCESS
| subprocess.CREATE_NEW_PROCESS_GROUP,
)
else:
subprocess.Popen(argv, start_new_session=True)
USES_TTY = {
"vi": True,
"vim": True,
"nvim": True,
"nano": True,
"gedit": False,
"emacsclient": True,
"code": False,
"notepad": False,
"notepad++": False,
"pycharm64": False,
"pycharm": False,
}
def open_editor(path, lineno: int | None = None, editor: str | None = None) -> str:
if editor is None:
editor = choose_editor()
if editor is None:
print("No '$EDITOR' value set and no default available.")
pytest.exit(returncode=1)
assert editor is not None
editor_name, _ = os.path.splitext(editor)
if lineno is None:
opts = [path]
else:
opt_generator = OPT_GENERATOR.get(editor_name, no_lineno)
opts = opt_generator(path, lineno)
exec_path = shutil.which(editor)
if exec_path is None:
print(f"Executable specified via '$EDITOR' could not be found: {editor!r}.")
pytest.exit(returncode=1)
argv = [exec_path] + opts
call = call_tty_child if USES_TTY.get(editor_name, True) else call_detached
call(argv)
return editor
CACHE_KEY = f"{__name__}/failure_locations"
NOT_GIVEN = object()
NO_FAILED_TESTS_MSG = """\
No tests recorded as failed by pytest-edit.
If you just installed this plugin, rerun your tests to let it record the edit locations.
"""
@pytest.hookimpl
def pytest_addoption(parser):
parser.addoption(
"--edit",
action="store",
help=(
"Open failed test in the editor specified via $EDITOR environment "
"variable. Choose the test to open by specifying a number, e.g. "
"'--edit=2'. This number will be interpretted as the index in the short "
"summary list of failed tests, starting from 0."
"Omitting this number will open editor on the last failed test."
),
default=NOT_GIVEN,
nargs="?",
)
@pytest.hookimpl
def pytest_sessionstart(session):
edit_choice = session.config.option.edit
if edit_choice is NOT_GIVEN:
# User didn't specify the `--edit` flag.
return
failed = session.config.cache.get(CACHE_KEY, [])
if not failed:
print(NO_FAILED_TESTS_MSG)
pytest.exit(returncode=0)
if edit_choice is None:
edit_idx = -1
else:
try:
edit_idx = int(edit_choice)
except ValueError:
print(
f"Wrong value specified for --edit: {edit_choice!r}.\n"
f"Value needs to be a valid integer."
)
pytest.exit(returncode=1)
try:
path, lineno, nodeid = failed[edit_idx]
except IndexError:
print(
f"Test index specified for --edit is out of bounds: {edit_idx}.\n"
f"There are {len(failed)} failed tests."
)
pytest.exit(returncode=1)
# Editors count from 1, pytest counts lines from 0
lineno += 1
opened_editor = open_editor(path, lineno)
print(f"Opened {path} at line {lineno} in editor {opened_editor!r}.")
pytest.exit(returncode=0)
@pytest.hookimpl
def pytest_terminal_summary(terminalreporter, exitstatus, config):
if "failed" in terminalreporter.stats:
failure_reports = terminalreporter.stats["failed"]
# path, lineno, nodeid
location_info = [report.location for report in failure_reports]
config.cache.set(CACHE_KEY, location_info)