-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_source_code_handler.py
More file actions
77 lines (57 loc) · 2.24 KB
/
test_source_code_handler.py
File metadata and controls
77 lines (57 loc) · 2.24 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
import os
from backtracepython.source_code_handler import SourceCodeHandler
def write_file(path, content):
with open(str(path), "w") as f:
f.write(content)
def make_report(source_paths):
"""Build a minimal report whose main thread stack references the given file paths."""
stack = [
{"sourceCode": path, "line": 10, "funcName": "test"} for path in source_paths
]
return {
"mainThread": "main",
"threads": {
"main": {"stack": stack},
},
}
def test_collect_removes_unreadable_sources_without_runtime_error():
"""Reproduces RuntimeError: dictionary changed size during iteration.
When every source file in the stack is unreadable, collect() used to pop
entries from the source_code dict while iterating over it.
"""
handler = SourceCodeHandler(tab_width=4, context_line_count=3)
report = make_report(
[
"/nonexistent/path/a.py",
"/nonexistent/path/b.py",
]
)
# Before the fix this raised:
# RuntimeError: dictionary changed size during iteration
result = handler.collect(report)
assert result["sourceCode"] == {}
def test_collect_keeps_readable_sources(tmp_path):
"""Verify that readable source files are collected normally."""
source_file = tmp_path / "real.py"
write_file(source_file, "foobarbaz")
handler = SourceCodeHandler(tab_width=4, context_line_count=1)
report = make_report([str(source_file)])
result = handler.collect(report)
assert str(source_file) in result["sourceCode"]
assert "text" in result["sourceCode"][str(source_file)]
def test_collect_mixed_readable_and_unreadable(tmp_path):
"""Mix of existing and missing files"""
source_file = tmp_path / "exists.py"
write_file(source_file, "foobarbaz")
handler = SourceCodeHandler(tab_width=4, context_line_count=3)
report = make_report(
[
"/nonexistent/path/missing.py",
str(source_file),
"/another/missing/file.py",
]
)
result = handler.collect(report)
assert str(source_file) in result["sourceCode"]
assert "/nonexistent/path/missing.py" not in result["sourceCode"]
assert "/another/missing/file.py" not in result["sourceCode"]