{{ message }}
Repository navigation
Expand file tree
/
Copy pathhelpers.py
More file actions
401 lines (303 loc) · 12.6 KB
/
Copy pathhelpers.py
File metadata and controls
401 lines (303 loc) · 12.6 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt
"""Helpers for coverage.py tests."""
# pragma: enforce 100% coverage
from __future__ import annotations
import collections
import contextlib
import dis
import io
import locale
import os
import os.path
import re
import shutil
import subprocess
import sys
import textwrap
import warnings
from collections.abc import Callable, Iterable, Iterator
from pathlib import Path
from typing import Any, NoReturn, TypeVar, cast
from coverage import env
from coverage.debug import DebugControl
from coverage.exceptions import CoverageWarning
from coverage.types import TArc
def _correct_encoding() -> str:
"""Determine the right encoding to use for subprocesses."""
# Type checking trick due to "unreachable" being set
_locale_type_erased: Any = locale
encoding = os.device_encoding(1) or (
_locale_type_erased.getpreferredencoding()
if sys.version_info < (3, 11)
else _locale_type_erased.getencoding()
)
return encoding
def subprocess_popen(cmd: str) -> subprocess.Popen[bytes]:
"""Run a command in a subprocess.
Returns the Popen object.
"""
# Subprocesses are expensive, but convenient, and so may be over-used in
# the test suite. Use these lines to get a list of the tests using them:
if 0: # pragma: debugging
pth = "/tmp/processes.txt" # type: ignore[unreachable]
with open(pth, "a", encoding="utf-8") as proctxt:
print(os.getenv("PYTEST_CURRENT_TEST", "unknown"), file=proctxt, flush=True)
# In some strange cases (PyPy3 in a virtualenv!?) the stdout encoding of
# the subprocess is set incorrectly to ascii. Use an environment variable
# to force the encoding to be the same as ours.
sub_env = dict(os.environ)
sub_env["PYTHONIOENCODING"] = _correct_encoding()
proc = subprocess.Popen(
cmd,
shell=True,
env=sub_env,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
return proc
def run_command(cmd: str) -> tuple[int, str]:
"""Run a command in a subprocess.
Returns the exit status code and the combined stdout and stderr.
"""
proc = subprocess_popen(cmd)
output, _ = proc.communicate()
status = proc.returncode
# Get the output, and canonicalize it to strings with newlines.
output_str = output.decode(_correct_encoding()).replace("\r", "")
return status, output_str
# $set_env.py: COVERAGE_DIS - Disassemble test code to /tmp/dis
SHOW_DIS = bool(int(os.getenv("COVERAGE_DIS", "0")))
def make_file(
filename: str,
text: str = "",
bytes: bytes = b"",
newline: str | None = None,
) -> str:
"""Create a file for testing.
`filename` is the relative path to the file, including directories if
desired, which will be created if need be.
`text` is the text content to create in the file, or `bytes` are the
bytes to write.
If `newline` is provided, it is a string that will be used as the line
endings in the created file, otherwise the line endings are as provided
in `text`.
Returns `filename`.
"""
# pylint: disable=redefined-builtin # bytes
if bytes:
data = bytes
else:
text = textwrap.dedent(text)
if newline:
text = text.replace("\n", newline)
data = text.encode("utf-8")
# Make sure the directories are available.
dirs, basename = os.path.split(filename)
if dirs:
os.makedirs(dirs, exist_ok=True)
# Create the file.
with open(filename, "wb") as f:
f.write(data)
if text and basename.endswith(".py") and SHOW_DIS: # pragma: debugging
os.makedirs("/tmp/dis", exist_ok=True)
with open(f"/tmp/dis/{basename}.dis", "w", encoding="utf-8") as fdis:
print(f"# {os.path.abspath(filename)}", file=fdis)
cur_test = os.getenv("PYTEST_CURRENT_TEST", "unknown")
print(f"# PYTEST_CURRENT_TEST = {cur_test}", file=fdis)
kwargs = {}
if env.PYVERSION >= (3, 13):
kwargs["show_offsets"] = True
try:
dis.dis(text, file=fdis, **kwargs)
except Exception as exc:
# Some tests make .py files that aren't Python, so dis will
# fail, which is expected.
print(f"#! {exc!r}", file=fdis)
# For debugging, enable this to show the contents of files created.
if 0: # pragma: debugging
print(f" ───┬──┤ {filename} ├───────────────────────") # type: ignore[unreachable]
for lineno, line in enumerate(data.splitlines(), start=1):
print(f"{lineno:6}│ {line.rstrip().decode()}")
print()
return filename
def nice_file(*fparts: str) -> str:
"""Canonicalize the file name composed of the parts in `fparts`."""
fname = os.path.join(*fparts)
return os.path.normcase(os.path.abspath(os.path.realpath(fname)))
def os_sep(s: str) -> str:
"""Replace slashes in `s` with the correct separator for the OS."""
return s.replace("/", os.sep)
class CheckUniqueFilenames:
"""Asserts the uniqueness of file names passed to a function."""
def __init__(self, wrapped: Callable[..., Any]) -> None:
self.filenames: set[str] = set()
self.wrapped = wrapped
@classmethod
def hook(cls, obj: Any, method_name: str) -> CheckUniqueFilenames:
"""Replace a method with our checking wrapper.
The method must take a string as a first argument. That argument
will be checked for uniqueness across all the calls to this method.
The values don't have to be file names actually, just strings, but
we only use it for filename arguments.
"""
method = getattr(obj, method_name)
hook = cls(method)
setattr(obj, method_name, hook.wrapper)
return hook
def wrapper(self, filename: str, *args: Any, **kwargs: Any) -> Any:
"""The replacement method. Check that we don't have dupes."""
assert filename not in self.filenames, (
f"File name {filename!r} passed to {self.wrapped!r} twice"
)
self.filenames.add(filename)
return self.wrapped(filename, *args, **kwargs)
def re_lines(pat: str, text: str, match: bool = True) -> list[str]:
"""Return a list of lines selected by `pat` in the string `text`.
If `match` is false, the selection is inverted: only the non-matching
lines are included.
Returns a list, the selected lines, without line endings.
"""
assert len(pat) < 200, "It's super-easy to swap the arguments to re_lines"
return [line for line in text.splitlines() if bool(re.search(pat, line)) == match]
def re_lines_text(pat: str, text: str, match: bool = True) -> str:
"""Return the multi-line text of lines selected by `pat`."""
return "".join(line + "\n" for line in re_lines(pat, text, match=match))
def re_line(pat: str, text: str) -> str:
"""Return the one line in `text` that matches regex `pat`.
Raises an AssertionError if more than one, or less than one, line matches.
"""
lines = re_lines(pat, text)
assert len(lines) == 1
return lines[0]
def remove_tree(dirname: str) -> None:
"""Remove a directory tree.
It's fine for the directory to not exist in the first place.
"""
if os.path.exists(dirname):
shutil.rmtree(dirname)
# Map chars to numbers for arcz_to_arcs
_arcz_map = {".": -1}
_arcz_map.update({c: ord(c) - ord("0") for c in "123456789"})
_arcz_map.update({c: 10 + ord(c) - ord("A") for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"})
def arcz_to_arcs(arcz: str) -> list[TArc]:
"""Convert a compact textual representation of arcs to a list of pairs.
The text has space-separated pairs of letters. Period is -1, 1-9 are
1-9, A-Z are 10 through 36. The resulting list is sorted regardless of
the order of the input pairs.
".1 12 2." --> [(-1,1), (1,2), (2,-1)]
Minus signs can be included in the pairs:
"-11, 12, 2-5" --> [(-1,1), (1,2), (2,-5)]
"""
# The `type: ignore[misc]` here are to suppress "Unpacking a string is
# disallowed".
a: str
b: str
arcs = []
for pair in arcz.split():
asgn = bsgn = 1
if len(pair) == 2:
a, b = pair # type: ignore[str-unpack]
else:
assert len(pair) == 3
if pair[0] == "-":
_, a, b = pair # type: ignore[str-unpack]
asgn = -1
else:
assert pair[1] == "-"
a, _, b = pair # type: ignore[str-unpack]
bsgn = -1
arcs.append((asgn * _arcz_map[a], bsgn * _arcz_map[b]))
return sorted(arcs)
T = TypeVar("T")
def assert_count_equal(
a: Iterable[T] | None,
b: Iterable[T] | None,
) -> None:
"""
A pytest-friendly implementation of assertCountEqual.
Assert that `a` and `b` have the same elements, but maybe in different order.
This only works for hashable elements.
"""
assert a is not None
assert b is not None
assert collections.Counter(list(a)) == collections.Counter(list(b))
def get_coverage_warnings(warns: Iterable[warnings.WarningMessage]) -> list[str]:
"""Extract the text of CoverageWarnings."""
warns = [w for w in warns if issubclass(w.category, CoverageWarning)]
texts = [cast(Warning, w.message).args[0] for w in warns]
return texts
def assert_coverage_warnings(
warns: Iterable[warnings.WarningMessage],
*msgs: str | re.Pattern[str],
) -> None:
"""
Assert that the CoverageWarning's in `warns` have `msgs` as messages.
Each msg can be a string compared for equality, or a compiled regex used to
search the text.
"""
actuals = get_coverage_warnings(warns)
assert msgs # don't call this without some messages.
assert len(msgs) == len(actuals)
for actual, expected in zip(actuals, msgs):
if hasattr(expected, "search"):
assert expected.search(actual), f"{actual!r} didn't match {expected!r}"
else:
actual = actual.partition("; see ")[0]
assert actual == expected
@contextlib.contextmanager
def swallow_warnings(
message: str = r".",
category: type[Warning] = CoverageWarning,
) -> Iterator[None]:
"""Swallow particular warnings.
It's OK if they happen, or if they don't happen. Just ignore them.
"""
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=category, message=message)
yield
class FailingProxy:
"""A proxy for another object, but one method will fail a few times before working."""
def __init__(self, obj: Any, methname: str, fails: list[Exception]) -> None:
"""Create the failing proxy.
`obj` is the object to proxy. `methname` is the method that will fail
a few times. `fails` are the exceptions to fail with. Once used up,
the method will proxy correctly.
"""
self.obj = obj
self.methname = methname
self.fails = fails
def __getattr__(self, name: str) -> Any:
if name == self.methname and self.fails:
meth = self._make_failing_method(self.fails[0])
del self.fails[0]
else:
meth = getattr(self.obj, name)
return meth
def _make_failing_method(self, exc: Exception) -> Callable[..., NoReturn]:
"""Return a function that will raise `exc`."""
def _meth(*args: Any, **kwargs: Any) -> NoReturn:
raise exc
return _meth
class DebugControlString(DebugControl):
"""A `DebugControl` that writes to a StringIO, for testing."""
def __init__(self, options: Iterable[str]) -> None:
self.io = io.StringIO()
super().__init__(options, self.io)
def get_output(self) -> str:
"""Get the output text from the `DebugControl`."""
return self.io.getvalue()
def all_our_source_files() -> Iterable[tuple[Path, str]]:
"""Iterate over all of our own source files.
This is used in tests that need a bunch of Python code to analyze, so we
might as well use our own source code as the subject.
Produces a stream of (filename, file contents) tuples.
"""
cov_dir = Path(__file__).parent.parent
# To run against all the files in the tox venvs:
# for source_file in cov_dir.rglob("*.py"):
for sub in [".", "ci", "coverage", "lab", "tests"]:
assert (cov_dir / sub).is_dir()
for source_file in (cov_dir / sub).glob("*.py"):
yield (source_file, source_file.read_text(encoding="utf-8"))
You can’t perform that action at this time.
