summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-10-27 08:02:44 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-10-27 08:50:31 -0400
commit15aff0ad7d6c92851bc21fc39863a3949edbb9c6 (patch)
tree4c1bbef4e9e6d9d3f789fb3b52649c42100ccbf5
parent93c9ca9f1b2e5d0b45dbf4b82c77aaf05b458bac (diff)
downloadpython-coveragepy-git-15aff0ad7d6c92851bc21fc39863a3949edbb9c6.tar.gz
refactor(test): re_lines is more useful if it returns a list
-rw-r--r--tests/helpers.py14
-rw-r--r--tests/test_debug.py12
-rw-r--r--tests/test_process.py13
-rw-r--r--tests/test_testing.py8
4 files changed, 28 insertions, 19 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index 185f20dd..7e6594ac 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -134,15 +134,21 @@ class CheckUniqueFilenames:
def re_lines(text, pat, match=True):
- """Return the text of lines that match `pat` in the string `text`.
+ """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 string, the text of only the selected lines.
+ Returns a list, the selected lines, without line endings.
"""
- return "".join(l for l in text.splitlines(True) if bool(re.search(pat, l)) == match)
+ assert len(pat) < 200, "It's super-easy to swap the arguments to re_lines"
+ return [l for l in text.splitlines() if bool(re.search(pat, l)) == match]
+
+
+def re_lines_text(text, pat, match=True):
+ """Return the multi-line text of lines selected by `pat`."""
+ return "".join(l + "\n" for l in re_lines(text, pat, match=match))
def re_line(text, pat):
@@ -151,7 +157,7 @@ def re_line(text, pat):
Raises an AssertionError if more than one, or less than one, line matches.
"""
- lines = re_lines(text, pat).splitlines()
+ lines = re_lines(text, pat)
assert len(lines) == 1
return lines[0]
diff --git a/tests/test_debug.py b/tests/test_debug.py
index 740bd617..564c2c29 100644
--- a/tests/test_debug.py
+++ b/tests/test_debug.py
@@ -15,7 +15,7 @@ from coverage.debug import filter_text, info_formatter, info_header, short_id, s
from coverage.debug import clipped_repr
from tests.coveragetest import CoverageTest
-from tests.helpers import re_line, re_lines
+from tests.helpers import re_line, re_lines, re_lines_text
class InfoFormatterTest(CoverageTest):
@@ -141,7 +141,7 @@ class DebugTraceTest(CoverageTest):
# Now our lines are always prefixed with the process id.
pid_prefix = r"^%5d\.[0-9a-f]{4}: " % os.getpid()
- pid_lines = re_lines(out_lines, pid_prefix)
+ pid_lines = re_lines_text(out_lines, pid_prefix)
assert pid_lines == out_lines
# We still have some tracing, and some not tracing.
@@ -152,9 +152,9 @@ class DebugTraceTest(CoverageTest):
out_lines = self.f1_debug_output(["pid", "dataop", "dataio", "callers"])
# For every real message, there should be a stack trace with a line like
# "f1_debug_output : /Users/ned/coverage/tests/test_debug.py @71"
- real_messages = re_lines(out_lines, r":\d+", match=False).splitlines()
+ real_messages = re_lines(out_lines, r":\d+", match=False)
frame_pattern = r"\s+f1_debug_output : .*tests[/\\]test_debug.py:\d+$"
- frames = re_lines(out_lines, frame_pattern).splitlines()
+ frames = re_lines(out_lines, frame_pattern)
assert len(real_messages) == len(frames)
last_line = out_lines.splitlines()[-1]
@@ -178,7 +178,7 @@ class DebugTraceTest(CoverageTest):
for label in labels:
label_pat = r"^\s*%s: " % label
msg = "Incorrect lines for %r" % label
- assert 1 == len(re_lines(out_lines, label_pat).splitlines()), msg
+ assert 1 == len(re_lines(out_lines, label_pat)), msg
def test_debug_sys(self):
out_lines = self.f1_debug_output(["sys"])
@@ -192,7 +192,7 @@ class DebugTraceTest(CoverageTest):
for label in labels:
label_pat = r"^\s*%s: " % label
msg = "Incorrect lines for %r" % label
- assert 1 == len(re_lines(out_lines, label_pat).splitlines()), msg
+ assert 1 == len(re_lines(out_lines, label_pat)), msg
def test_debug_sys_ctracer(self):
out_lines = self.f1_debug_output(["sys"])
diff --git a/tests/test_process.py b/tests/test_process.py
index 65a6e75d..f5ffee51 100644
--- a/tests/test_process.py
+++ b/tests/test_process.py
@@ -23,7 +23,8 @@ from coverage.data import line_counts
from coverage.files import abs_file, python_reported_file
from tests.coveragetest import CoverageTest, TESTS_DIR
-from tests.helpers import change_dir, make_file, nice_file, os_sep, re_lines, run_command
+from tests.helpers import change_dir, make_file, nice_file, os_sep
+from tests.helpers import re_lines, re_lines_text, run_command
class ProcessTest(CoverageTest):
@@ -500,7 +501,7 @@ class ProcessTest(CoverageTest):
out2 = self.run_command("python throw.py")
if env.PYPY:
# Pypy has an extra frame in the traceback for some reason
- out2 = re_lines(out2, "toplevel", match=False)
+ out2 = re_lines_text(out2, "toplevel", match=False)
assert out == out2
# But also make sure that the output is what we expect.
@@ -869,8 +870,8 @@ class EnvironmentTest(CoverageTest):
if env.JYTHON: # pragma: only jython
# Argv0 is different for Jython, remove that from the comparison.
- expected = re_lines(expected, r'\s+"argv0":', match=False)
- actual = re_lines(actual, r'\s+"argv0":', match=False)
+ expected = re_lines_text(expected, r'\s+"argv0":', match=False)
+ actual = re_lines_text(actual, r'\s+"argv0":', match=False)
assert actual == expected
@@ -906,8 +907,8 @@ class EnvironmentTest(CoverageTest):
# the comparison also...
if env.PYPY:
ignored = re.escape(os.getcwd())
- expected = re_lines(expected, ignored, match=False)
- actual = re_lines(actual, ignored, match=False)
+ expected = re_lines_text(expected, ignored, match=False)
+ actual = re_lines_text(actual, ignored, match=False)
self.assert_tryexecfile_output(expected, actual)
def test_coverage_run_dashm_dir_no_init_is_like_python(self):
diff --git a/tests/test_testing.py b/tests/test_testing.py
index 37130074..8d559fe4 100644
--- a/tests/test_testing.py
+++ b/tests/test_testing.py
@@ -19,7 +19,7 @@ from coverage.files import actual_path
from tests.coveragetest import CoverageTest
from tests.helpers import (
arcs_to_arcz_repr, arcz_to_arcs, assert_count_equal, assert_coverage_warnings,
- CheckUniqueFilenames, re_lines, re_line, without_module,
+ CheckUniqueFilenames, re_lines, re_lines_text, re_line, without_module,
)
@@ -302,7 +302,8 @@ class ReLinesTest(CoverageTest):
("line1\nline2\nline3\n", "X", ""),
])
def test_re_lines(self, text, pat, result):
- assert re_lines(text, pat) == result
+ assert re_lines_text(text, pat) == result
+ assert re_lines(text, pat) == result.splitlines()
@pytest.mark.parametrize("text, pat, result", [
("line1\nline2\nline3\n", "line", ""),
@@ -310,7 +311,8 @@ class ReLinesTest(CoverageTest):
("line1\nline2\nline3\n", "X", "line1\nline2\nline3\n"),
])
def test_re_lines_inverted(self, text, pat, result):
- assert re_lines(text, pat, match=False) == result
+ assert re_lines_text(text, pat, match=False) == result
+ assert re_lines(text, pat, match=False) == result.splitlines()
@pytest.mark.parametrize("text, pat, result", [
("line1\nline2\nline3\n", "2", "line2"),