Repository navigation
Expand file tree
/
Copy pathtest_tools.py
More file actions
235 lines (172 loc) · 6.54 KB
/
Copy pathtest_tools.py
File metadata and controls
235 lines (172 loc) · 6.54 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
"""Tests for the wrapture.tools command line: the dispatcher and the
convert command.
python -m wrapture.tools lists the commands; convert renders a
JSONLines trace file with one of the exporters, to a file or to
standard output. Parsing and behaviour are tested in process; one
subprocess test proves the -m wiring end to end.
"""
import json
import os
import subprocess
import sys
from pathlib import Path
from typing import Any
import pytest
from wrapture.tools.__main__ import main as tools_main
from wrapture.tools.convert import _parse
from wrapture.tools.convert import main as convert_main
# A small nested trace in the serialised form, written in completion
# order the way a JSONLines file would hold it.
RECORDS: list[dict[str, Any]] = [
{
"seq": 2,
"parent_id": 1,
"depth": 1,
"kind": "call",
"path": "app:Gateway.charge",
"started": 0.001,
"duration": 0.002,
"thread_id": 7,
"thread_name": "MainThread",
"result": "ch_500",
},
{
"seq": 1,
"parent_id": None,
"depth": 0,
"kind": "call",
"path": "app:Processor.process",
"started": 0.0,
"duration": 0.01,
"thread_id": 7,
"thread_name": "MainThread",
},
]
@pytest.fixture
def trace(tmp_path: Path) -> Path:
source = tmp_path / "trace.jsonl"
source.write_text("".join(json.dumps(record) + "\n" for record in RECORDS))
return source
# ---------------------------------------------------------------------------
# the dispatcher
# ---------------------------------------------------------------------------
def test_no_command_lists_the_available_commands(
capsys: pytest.CaptureFixture[str],
) -> None:
with pytest.raises(SystemExit) as caught:
tools_main([])
assert caught.value.code == 0
assert "convert" in capsys.readouterr().out
def test_an_unknown_command_is_a_usage_error(
capsys: pytest.CaptureFixture[str],
) -> None:
with pytest.raises(SystemExit) as caught:
tools_main(["transmogrify"])
assert caught.value.code == 2
assert "unknown command" in capsys.readouterr().err
# ---------------------------------------------------------------------------
# convert parsing
# ---------------------------------------------------------------------------
def test_options_and_the_trace_file_are_accepted_in_any_order() -> None:
for argv in (
["--format", "chrome", "-o", "out.json", "trace.jsonl"],
["trace.jsonl", "--format=chrome", "--output=out.json"],
["--output", "out.json", "trace.jsonl", "--format", "chrome"],
):
conversion = _parse(argv)
assert conversion.format == "chrome"
assert conversion.output == "out.json"
assert conversion.source == "trace.jsonl"
def test_the_format_is_required() -> None:
with pytest.raises(SystemExit) as caught:
_parse(["trace.jsonl"])
assert caught.value.code == 2
def test_an_unknown_format_is_a_usage_error() -> None:
with pytest.raises(SystemExit) as caught:
_parse(["--format", "interpretive-dance", "trace.jsonl"])
assert caught.value.code == 2
def test_the_trace_file_is_required() -> None:
with pytest.raises(SystemExit) as caught:
_parse(["--format", "chrome"])
assert caught.value.code == 2
def test_a_second_trace_file_is_a_usage_error() -> None:
with pytest.raises(SystemExit) as caught:
_parse(["--format", "chrome", "one.jsonl", "two.jsonl"])
assert caught.value.code == 2
def test_an_unknown_option_is_a_usage_error() -> None:
with pytest.raises(SystemExit) as caught:
_parse(["--verbose", "trace.jsonl"])
assert caught.value.code == 2
def test_help_prints_usage_and_exits_cleanly(
capsys: pytest.CaptureFixture[str],
) -> None:
with pytest.raises(SystemExit) as caught:
_parse(["--help"])
assert caught.value.code == 0
assert "usage: python -m wrapture.tools convert" in capsys.readouterr().out
# ---------------------------------------------------------------------------
# convert behaviour
# ---------------------------------------------------------------------------
def test_convert_renders_to_standard_output_by_default(
trace: Path, capsys: pytest.CaptureFixture[str]
) -> None:
convert_main(["--format", "canonical", str(trace)])
assert capsys.readouterr().out == (
"call app:Processor.process\n call app:Gateway.charge\n"
)
def test_convert_writes_the_output_file(trace: Path, tmp_path: Path) -> None:
output = tmp_path / "trace.json"
convert_main(["--format", "chrome", "-o", str(output), str(trace)])
rendered = json.loads(output.read_text())
slices = [entry for entry in rendered["traceEvents"] if entry["ph"] == "X"]
assert {entry["name"] for entry in slices} == {
"app:Processor.process",
"app:Gateway.charge",
}
def test_convert_renders_mermaid(
trace: Path, capsys: pytest.CaptureFixture[str]
) -> None:
convert_main(["--format", "mermaid", str(trace)])
out = capsys.readouterr().out
assert out.startswith("sequenceDiagram\n")
assert " P1->>+P2: charge" in out
def test_a_missing_trace_file_is_reported(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
with pytest.raises(SystemExit) as caught:
convert_main(["--format", "chrome", str(tmp_path / "absent.jsonl")])
assert caught.value.code == 1
assert "cannot read trace file" in capsys.readouterr().err
def test_a_file_that_is_not_jsonlines_is_reported(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
source = tmp_path / "trace.jsonl"
source.write_text("this is not json\n")
with pytest.raises(SystemExit) as caught:
convert_main(["--format", "chrome", str(source)])
assert caught.value.code == 1
assert "not a JSONLines trace" in capsys.readouterr().err
# ---------------------------------------------------------------------------
# end to end
# ---------------------------------------------------------------------------
def test_the_module_is_runnable(trace: Path) -> None:
completed = subprocess.run(
[
sys.executable,
"-m",
"wrapture.tools",
"convert",
"--format",
"canonical",
trace.name,
],
cwd=trace.parent,
env=dict(os.environ),
capture_output=True,
text=True,
timeout=60,
)
assert completed.returncode == 0, completed.stderr
assert completed.stdout == (
"call app:Processor.process\n call app:Gateway.charge\n"
)