forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpvsc_utils.py
More file actions
351 lines (280 loc) · 11.2 KB
/
pvsc_utils.py
File metadata and controls
351 lines (280 loc) · 11.2 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import argparse
import atexit
import enum
import inspect
import json
import os
import pathlib
import sys
import unittest
from typing import List, Optional, Tuple, Union, Dict, Literal, TypedDict
script_dir = pathlib.Path(__file__).parent.parent
sys.path.append(os.fspath(script_dir))
sys.path.append(os.fspath(script_dir / "lib" / "python"))
from testing_tools import socket_manager # noqa: E402
from typing_extensions import NotRequired # noqa: E402
# Types
# Inherit from str so it's JSON serializable.
class TestNodeTypeEnum(str, enum.Enum):
class_ = "class"
file = "file"
folder = "folder"
test = "test"
class TestData(TypedDict):
name: str
path: str
type_: TestNodeTypeEnum
id_: str
class TestItem(TestData):
lineno: str
runID: str
class TestNode(TestData):
children: "List[TestNode | TestItem]"
class TestExecutionStatus(str, enum.Enum):
error = "error"
success = "success"
class VSCodeUnittestError(Exception):
"""A custom exception class for unittest errors."""
def __init__(self, message):
super().__init__(message)
class DiscoveryPayloadDict(TypedDict):
cwd: str
status: Literal["success", "error"]
tests: Optional[TestNode]
error: NotRequired[List[str]]
class ExecutionPayloadDict(TypedDict):
cwd: str
status: TestExecutionStatus
result: Optional[Dict[str, Dict[str, Optional[str]]]]
not_found: NotRequired[List[str]]
error: NotRequired[str]
class EOTPayloadDict(TypedDict):
"""A dictionary that is used to send a end of transmission post request to the server."""
command_type: Union[Literal["discovery"], Literal["execution"]]
eot: bool
# Helper functions for data retrieval.
def get_test_case(suite):
"""Iterate through a unittest test suite and return all test cases."""
for test in suite:
if isinstance(test, unittest.TestCase):
yield test
else:
for test_case in get_test_case(test):
yield test_case
def get_source_line(obj) -> str:
"""Get the line number of a test case start line."""
try:
sourcelines, lineno = inspect.getsourcelines(obj)
except Exception:
try:
# tornado-specific, see https://github.com/microsoft/vscode-python/issues/17285.
sourcelines, lineno = inspect.getsourcelines(obj.orig_method)
except Exception:
return "*"
# Return the line number of the first line of the test case definition.
for i, v in enumerate(sourcelines):
if v.strip().startswith(("def", "async def")):
return str(lineno + i)
return "*"
# Helper functions for test tree building.
def build_test_node(path: str, name: str, type_: TestNodeTypeEnum) -> TestNode:
"""Build a test node with no children. A test node can be a folder, a file or a class."""
## figure out if we are folder, file, or class
id_gen = path
if type_ == TestNodeTypeEnum.folder or type_ == TestNodeTypeEnum.file:
id_gen = path
else:
# means we have to build test node for class
id_gen = path + "\\" + name
return {"path": path, "name": name, "type_": type_, "children": [], "id_": id_gen}
def get_child_node(name: str, path: str, type_: TestNodeTypeEnum, root: TestNode) -> TestNode:
"""Find a child node in a test tree given its name, type and path. If the node doesn't exist, create it.
Path is required to distinguish between nodes with the same name and type."""
try:
result = next(
node
for node in root["children"]
if node["name"] == name and node["type_"] == type_ and node["path"] == path
)
except StopIteration:
result = build_test_node(path, name, type_)
root["children"].append(result)
return result # type:ignore
def build_test_tree(
suite: unittest.TestSuite, top_level_directory: str
) -> Tuple[Union[TestNode, None], List[str]]:
"""Build a test tree from a unittest test suite.
This function returns the test tree, and any errors found by unittest.
If no tests were discovered, return `None` and a list of errors (if any).
Test tree structure:
{
"path": <test_directory path>,
"type": "folder",
"name": <folder name>,
"children": [
{ files and folders }
...
{
"path": <file path>,
"name": filename.py,
"type_": "file",
"children": [
{
"path": <class path>,
"name": <class name>,
"type_": "class",
"children": [
{
"path": <test path>,
"name": <test name>,
"type_": "test",
"lineno": <line number>
"id_": <test case id following format in line 196>,
}
],
"id_": <class path path following format after path>
}
],
"id_": <file path>
}
],
"id_": <test_directory path>
}
"""
error = []
directory_path = pathlib.PurePath(top_level_directory)
root = build_test_node(top_level_directory, directory_path.name, TestNodeTypeEnum.folder)
for test_case in get_test_case(suite):
test_id = test_case.id()
if test_id.startswith("unittest.loader._FailedTest"):
error.append(str(test_case._exception)) # type: ignore
elif test_id.startswith("unittest.loader.ModuleSkipped"):
components = test_id.split(".")
class_name = f"{components[-1]}.py"
# Find/build class node.
file_path = os.fsdecode(os.path.join(directory_path, class_name))
current_node = get_child_node(class_name, file_path, TestNodeTypeEnum.file, root)
else:
# Get the static test path components: filename, class name and function name.
components = test_id.split(".")
*folders, filename, class_name, function_name = components
py_filename = f"{filename}.py"
current_node = root
# Find/build nodes for the intermediate folders in the test path.
for folder in folders:
current_node = get_child_node(
folder,
os.fsdecode(pathlib.PurePath(current_node["path"], folder)),
TestNodeTypeEnum.folder,
current_node,
)
# Find/build file node.
path_components = [top_level_directory] + folders + [py_filename]
file_path = os.fsdecode(pathlib.PurePath("/".join(path_components)))
current_node = get_child_node(
py_filename, file_path, TestNodeTypeEnum.file, current_node
)
# Find/build class node.
current_node = get_child_node(
class_name, file_path, TestNodeTypeEnum.class_, current_node
)
# Get test line number.
test_method = getattr(test_case, test_case._testMethodName)
lineno = get_source_line(test_method)
# Add test node.
test_node: TestItem = {
"name": function_name,
"path": file_path,
"lineno": lineno,
"type_": TestNodeTypeEnum.test,
"id_": file_path + "\\" + class_name + "\\" + function_name,
"runID": test_id,
} # concatenate class name and function test name
current_node["children"].append(test_node)
if not root["children"]:
root = None
return root, error
def parse_unittest_args(
args: List[str],
) -> Tuple[str, str, Union[str, None], int, Union[bool, None], Union[bool, None]]:
"""Parse command-line arguments that should be forwarded to unittest to perform discovery.
Valid unittest arguments are: -v, -s, -p, -t and their long-form counterparts,
however we only care about the last three.
The returned tuple contains the following items
- start_directory: The directory where to start discovery, defaults to .
- pattern: The pattern to match test files, defaults to test*.py
- top_level_directory: The top-level directory of the project, defaults to None,
and unittest will use start_directory behind the scenes.
"""
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("--start-directory", "-s", default=".")
arg_parser.add_argument("--pattern", "-p", default="test*.py")
arg_parser.add_argument("--top-level-directory", "-t", default=None)
arg_parser.add_argument("--failfast", "-f", action="store_true", default=None)
arg_parser.add_argument("--verbose", "-v", action="store_true", default=None)
arg_parser.add_argument("-q", "--quiet", action="store_true", default=None)
arg_parser.add_argument("--locals", action="store_true", default=None)
parsed_args, _ = arg_parser.parse_known_args(args)
verbosity: int = 1
if parsed_args.quiet:
verbosity = 0
elif parsed_args.verbose:
verbosity = 2
return (
parsed_args.start_directory,
parsed_args.pattern,
parsed_args.top_level_directory,
verbosity,
parsed_args.failfast,
parsed_args.locals,
)
__writer = None
atexit.register(lambda: __writer.close() if __writer else None)
def send_post_request(
payload: Union[ExecutionPayloadDict, DiscoveryPayloadDict, EOTPayloadDict],
test_run_pipe: str,
):
"""
Sends a post request to the server.
Keyword arguments:
payload -- the payload data to be sent.
test_run_pipe -- the name of the pipe to send the data to.
"""
if not test_run_pipe:
error_msg = (
"UNITTEST ERROR: TEST_RUN_PIPE is not set at the time of unittest trying to send data. "
"Please confirm this environment variable is not being changed or removed "
"as it is required for successful test discovery and execution."
f"TEST_RUN_PIPE = {test_run_pipe}\n"
)
print(error_msg, file=sys.stderr)
raise VSCodeUnittestError(error_msg)
global __writer
if __writer is None:
try:
__writer = socket_manager.PipeManager(test_run_pipe)
__writer.connect()
except Exception as error:
error_msg = f"Error attempting to connect to extension named pipe {test_run_pipe}[vscode-unittest]: {error}"
__writer = None
raise VSCodeUnittestError(error_msg)
rpc = {
"jsonrpc": "2.0",
"params": payload,
}
data = json.dumps(rpc)
try:
if __writer:
__writer.write(data)
else:
print(
f"Connection error[vscode-unittest], writer is None \n[vscode-unittest] data: \n{data} \n",
file=sys.stderr,
)
except Exception as error:
print(
f"Exception thrown while attempting to send data[vscode-unittest]: {error} \n[vscode-unittest] data: \n{data}\n",
file=sys.stderr,
)