Skip to content

Commit 81a55d1

Browse files
committed
refactor(test): make re_lines (et al) look like re.search
and also replace some calls with just-plain re.search.
1 parent e36475a commit 81a55d1

File tree

7 files changed

+68
-74
lines changed

7 files changed

+68
-74
lines changed

tests/helpers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def wrapper(self, filename, *args, **kwargs):
133133
return ret
134134

135135

136-
def re_lines(text, pat, match=True):
136+
def re_lines(pat, text, match=True):
137137
"""Return a list of lines selected by `pat` in the string `text`.
138138
139139
If `match` is false, the selection is inverted: only the non-matching
@@ -146,18 +146,18 @@ def re_lines(text, pat, match=True):
146146
return [l for l in text.splitlines() if bool(re.search(pat, l)) == match]
147147

148148

149-
def re_lines_text(text, pat, match=True):
149+
def re_lines_text(pat, text, match=True):
150150
"""Return the multi-line text of lines selected by `pat`."""
151-
return "".join(l + "\n" for l in re_lines(text, pat, match=match))
151+
return "".join(l + "\n" for l in re_lines(pat, text, match=match))
152152

153153

154-
def re_line(text, pat):
154+
def re_line(pat, text):
155155
"""Return the one line in `text` that matches regex `pat`.
156156
157157
Raises an AssertionError if more than one, or less than one, line matches.
158158
159159
"""
160-
lines = re_lines(text, pat)
160+
lines = re_lines(pat, text)
161161
assert len(lines) == 1
162162
return lines[0]
163163

tests/test_debug.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,44 +126,39 @@ def test_debug_trace(self):
126126

127127
# We should have a line like "Tracing 'f1.py'", perhaps with an
128128
# absolute path.
129-
f1 = re_lines(out_lines, r"Tracing '.*f1.py'")
130-
assert f1
129+
assert re.search(r"Tracing '.*f1.py'", out_lines)
131130

132131
# We should have lines like "Not tracing 'collector.py'..."
133-
coverage_lines = re_lines(
134-
out_lines,
135-
r"^Not tracing .*: is part of coverage.py$"
136-
)
137-
assert coverage_lines
132+
assert re_lines(r"^Not tracing .*: is part of coverage.py$", out_lines)
138133

139134
def test_debug_trace_pid(self):
140135
out_lines = self.f1_debug_output(["trace", "pid"])
141136

142137
# Now our lines are always prefixed with the process id.
143138
pid_prefix = r"^%5d\.[0-9a-f]{4}: " % os.getpid()
144-
pid_lines = re_lines_text(out_lines, pid_prefix)
139+
pid_lines = re_lines_text(pid_prefix, out_lines)
145140
assert pid_lines == out_lines
146141

147142
# We still have some tracing, and some not tracing.
148-
assert re_lines(out_lines, pid_prefix + "Tracing ")
149-
assert re_lines(out_lines, pid_prefix + "Not tracing ")
143+
assert re_lines(pid_prefix + "Tracing ", out_lines)
144+
assert re_lines(pid_prefix + "Not tracing ", out_lines)
150145

151146
def test_debug_callers(self):
152147
out_lines = self.f1_debug_output(["pid", "dataop", "dataio", "callers"])
153148
# For every real message, there should be a stack trace with a line like
154149
# "f1_debug_output : /Users/ned/coverage/tests/test_debug.py @71"
155-
real_messages = re_lines(out_lines, r":\d+", match=False)
150+
real_messages = re_lines(r":\d+", out_lines, match=False)
156151
frame_pattern = r"\s+f1_debug_output : .*tests[/\\]test_debug.py:\d+$"
157-
frames = re_lines(out_lines, frame_pattern)
152+
frames = re_lines(frame_pattern, out_lines)
158153
assert len(real_messages) == len(frames)
159154

160155
last_line = out_lines.splitlines()[-1]
161156

162157
# The details of what to expect on the stack are empirical, and can change
163158
# as the code changes. This test is here to ensure that the debug code
164159
# continues working. It's ok to adjust these details over time.
165-
assert re.search(r"^\s*\d+\.\w{4}: Adding file tracers: 0 files", real_messages[-1])
166-
assert re.search(r"\s+add_file_tracers : .*coverage[/\\]sqldata.py:\d+$", last_line)
160+
assert re_lines(r"^\s*\d+\.\w{4}: Adding file tracers: 0 files", real_messages[-1])
161+
assert re_lines(r"\s+add_file_tracers : .*coverage[/\\]sqldata.py:\d+$", last_line)
167162

168163
def test_debug_config(self):
169164
out_lines = self.f1_debug_output(["config"])
@@ -178,7 +173,7 @@ def test_debug_config(self):
178173
for label in labels:
179174
label_pat = r"^\s*%s: " % label
180175
msg = "Incorrect lines for %r" % label
181-
assert 1 == len(re_lines(out_lines, label_pat)), msg
176+
assert 1 == len(re_lines(label_pat, out_lines)), msg
182177

183178
def test_debug_sys(self):
184179
out_lines = self.f1_debug_output(["sys"])
@@ -192,11 +187,11 @@ def test_debug_sys(self):
192187
for label in labels:
193188
label_pat = r"^\s*%s: " % label
194189
msg = "Incorrect lines for %r" % label
195-
assert 1 == len(re_lines(out_lines, label_pat)), msg
190+
assert 1 == len(re_lines(label_pat, out_lines)), msg
196191

197192
def test_debug_sys_ctracer(self):
198193
out_lines = self.f1_debug_output(["sys"])
199-
tracer_line = re_line(out_lines, r"CTracer:").strip()
194+
tracer_line = re_line(r"CTracer:", out_lines).strip()
200195
if env.C_TRACER:
201196
expected = "CTracer: available"
202197
else:

tests/test_goldtest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ def test_bad(self):
6969
stdout = self.stdout()
7070
assert "- Four score" in stdout
7171
assert "+ Five score" in stdout
72-
assert re_line(stdout, rf"^:::: diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'")
73-
assert re_line(stdout, rf"^:::: end diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'")
72+
assert re_line(rf"^:::: diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'", stdout)
73+
assert re_line(rf"^:::: end diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'", stdout)
7474
assert " D/D/D, Gxxx, Pennsylvania" in stdout
7575

7676
# The actual file was saved.

tests/test_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,5 +502,5 @@ def test_ast_dump():
502502
assert result[0] == "<Module"
503503
assert result[-1] == ">"
504504
result_text = "\n".join(result)
505-
assert len(re_lines(result_text, r"^\s+>")) > num_lines
506-
assert len(re_lines(result_text, r"<Name @ \d+,\d+(:\d+)? id: '\w+'>")) > num_lines // 2
505+
assert len(re_lines(r"^\s+>", result_text)) > num_lines
506+
assert len(re_lines(r"<Name @ \d+,\d+(:\d+)? id: '\w+'>", result_text)) > num_lines // 2

tests/test_phystokens.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from coverage.python import get_python_source
1616

1717
from tests.coveragetest import CoverageTest, TESTS_DIR
18-
from tests.helpers import re_lines
1918

2019

2120
# A simple program and its token stream.
@@ -102,11 +101,11 @@ def test_stress(self):
102101
stress = os.path.join(TESTS_DIR, "stress_phystoken.tok")
103102
self.check_file_tokenization(stress)
104103
with open(stress) as fstress:
105-
assert re_lines(fstress.read(), r"\s$"), f"{stress} needs a trailing space."
104+
assert re.search(r"\s$", fstress.read()), f"{stress} needs a trailing space."
106105
stress = os.path.join(TESTS_DIR, "stress_phystoken_dos.tok")
107106
self.check_file_tokenization(stress)
108107
with open(stress) as fstress:
109-
assert re_lines(fstress.read(), r"\s$"), f"{stress} needs a trailing space."
108+
assert re.search(r"\s$", fstress.read()), f"{stress} needs a trailing space."
110109

111110

112111
@pytest.mark.skipif(not env.PYBEHAVIOR.soft_keywords, reason="Soft keywords are new in Python 3.10")

tests/test_process.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def f2():
501501
out2 = self.run_command("python throw.py")
502502
if env.PYPY:
503503
# Pypy has an extra frame in the traceback for some reason
504-
out2 = re_lines_text(out2, "toplevel", match=False)
504+
out2 = re_lines_text("toplevel", out2, match=False)
505505
assert out == out2
506506

507507
# But also make sure that the output is what we expect.
@@ -870,8 +870,8 @@ def assert_tryexecfile_output(self, expected, actual):
870870

871871
if env.JYTHON: # pragma: only jython
872872
# Argv0 is different for Jython, remove that from the comparison.
873-
expected = re_lines_text(expected, r'\s+"argv0":', match=False)
874-
actual = re_lines_text(actual, r'\s+"argv0":', match=False)
873+
expected = re_lines_text(r'\s+"argv0":', expected, match=False)
874+
actual = re_lines_text(r'\s+"argv0":', actual, match=False)
875875

876876
assert actual == expected
877877

@@ -907,8 +907,8 @@ def test_coverage_run_dir_is_like_python_dir(self):
907907
# the comparison also...
908908
if env.PYPY:
909909
ignored = re.escape(os.getcwd())
910-
expected = re_lines_text(expected, ignored, match=False)
911-
actual = re_lines_text(actual, ignored, match=False)
910+
expected = re_lines_text(ignored, expected, match=False)
911+
actual = re_lines_text(ignored, actual, match=False)
912912
self.assert_tryexecfile_output(expected, actual)
913913

914914
def test_coverage_run_dashm_dir_no_init_is_like_python(self):
@@ -1807,13 +1807,13 @@ def test_third_party_venv_isnt_measured(self, coverage_command):
18071807
# --source refers to a file.
18081808
debug_out = self.get_trace_output()
18091809
assert re_lines(
1810+
r"^Not tracing .*\bexecfile.py': inside --source, but is third-party",
18101811
debug_out,
1811-
r"^Not tracing .*\bexecfile.py': inside --source, but is third-party"
18121812
)
1813-
assert re_lines(debug_out, r"^Tracing .*\bmyproduct.py")
1813+
assert re_lines(r"^Tracing .*\bmyproduct.py", debug_out)
18141814
assert re_lines(
1815+
r"^Not tracing .*\bcolorsys.py': falls outside the --source spec",
18151816
debug_out,
1816-
r"^Not tracing .*\bcolorsys.py': falls outside the --source spec"
18171817
)
18181818

18191819
out = run_in_venv("python -m coverage report")
@@ -1830,17 +1830,17 @@ def test_us_in_venv_isnt_measured(self, coverage_command):
18301830
# --source refers to a module.
18311831
debug_out = self.get_trace_output()
18321832
assert re_lines(
1833-
debug_out,
18341833
r"^Not tracing .*\bexecfile.py': " +
1835-
"module 'coverage.execfile' falls outside the --source spec"
1834+
"module 'coverage.execfile' falls outside the --source spec",
1835+
debug_out,
18361836
)
18371837
assert re_lines(
1838+
r"^Not tracing .*\bmyproduct.py': module 'myproduct' falls outside the --source spec",
18381839
debug_out,
1839-
r"^Not tracing .*\bmyproduct.py': module 'myproduct' falls outside the --source spec"
18401840
)
18411841
assert re_lines(
1842+
r"^Not tracing .*\bcolorsys.py': module 'colorsys' falls outside the --source spec",
18421843
debug_out,
1843-
r"^Not tracing .*\bcolorsys.py': module 'colorsys' falls outside the --source spec"
18441844
)
18451845

18461846
out = run_in_venv("python -m coverage report")
@@ -1854,9 +1854,9 @@ def test_venv_isnt_measured(self, coverage_command):
18541854
assert out == self.expected_stdout
18551855

18561856
debug_out = self.get_trace_output()
1857-
assert re_lines(debug_out, r"^Not tracing .*\bexecfile.py': is part of coverage.py")
1858-
assert re_lines(debug_out, r"^Tracing .*\bmyproduct.py")
1859-
assert re_lines(debug_out, r"^Not tracing .*\bcolorsys.py': is in the stdlib")
1857+
assert re_lines(r"^Not tracing .*\bexecfile.py': is part of coverage.py", debug_out)
1858+
assert re_lines(r"^Tracing .*\bmyproduct.py", debug_out)
1859+
assert re_lines(r"^Not tracing .*\bcolorsys.py': is in the stdlib", debug_out)
18601860

18611861
out = run_in_venv("python -m coverage report")
18621862
assert "myproduct.py" in out
@@ -1894,17 +1894,17 @@ def test_installed_namespace_packages(self, coverage_command):
18941894
# --source refers to a file.
18951895
debug_out = self.get_trace_output()
18961896
assert re_lines(
1897-
debug_out,
18981897
r"^Not tracing .*\bexecfile.py': " +
1899-
"module 'coverage.execfile' falls outside the --source spec"
1898+
"module 'coverage.execfile' falls outside the --source spec",
1899+
debug_out,
19001900
)
19011901
assert re_lines(
1902+
r"^Not tracing .*\bmyproduct.py': module 'myproduct' falls outside the --source spec",
19021903
debug_out,
1903-
r"^Not tracing .*\bmyproduct.py': module 'myproduct' falls outside the --source spec"
19041904
)
19051905
assert re_lines(
1906+
r"^Not tracing .*\bcolorsys.py': module 'colorsys' falls outside the --source spec",
19061907
debug_out,
1907-
r"^Not tracing .*\bcolorsys.py': module 'colorsys' falls outside the --source spec"
19081908
)
19091909

19101910
out = run_in_venv("python -m coverage report")

tests/test_testing.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ def test_sub_python_is_this_python(self):
183183
# Try it with a "coverage debug sys" command.
184184
out = self.run_command("coverage debug sys")
185185

186-
executable = re_line(out, "executable:")
186+
executable = re_line("executable:", out)
187187
executable = executable.split(":", 1)[1].strip()
188188
assert _same_python_executable(executable, sys.executable)
189189

190190
# "environment: COV_FOOBAR = XYZZY" or "COV_FOOBAR = XYZZY"
191-
environ = re_line(out, "COV_FOOBAR")
191+
environ = re_line("COV_FOOBAR", out)
192192
_, _, environ = environ.rpartition(":")
193193
assert environ.strip() == "COV_FOOBAR = XYZZY"
194194

@@ -296,37 +296,37 @@ class ReLinesTest(CoverageTest):
296296

297297
run_in_temp_dir = False
298298

299-
@pytest.mark.parametrize("text, pat, result", [
300-
("line1\nline2\nline3\n", "line", "line1\nline2\nline3\n"),
301-
("line1\nline2\nline3\n", "[13]", "line1\nline3\n"),
302-
("line1\nline2\nline3\n", "X", ""),
299+
@pytest.mark.parametrize("pat, text, result", [
300+
("line", "line1\nline2\nline3\n", "line1\nline2\nline3\n"),
301+
("[13]", "line1\nline2\nline3\n", "line1\nline3\n"),
302+
("X", "line1\nline2\nline3\n", ""),
303303
])
304-
def test_re_lines(self, text, pat, result):
305-
assert re_lines_text(text, pat) == result
306-
assert re_lines(text, pat) == result.splitlines()
307-
308-
@pytest.mark.parametrize("text, pat, result", [
309-
("line1\nline2\nline3\n", "line", ""),
310-
("line1\nline2\nline3\n", "[13]", "line2\n"),
311-
("line1\nline2\nline3\n", "X", "line1\nline2\nline3\n"),
304+
def test_re_lines(self, pat, text, result):
305+
assert re_lines_text(pat, text) == result
306+
assert re_lines(pat, text) == result.splitlines()
307+
308+
@pytest.mark.parametrize("pat, text, result", [
309+
("line", "line1\nline2\nline3\n", ""),
310+
("[13]", "line1\nline2\nline3\n", "line2\n"),
311+
("X", "line1\nline2\nline3\n", "line1\nline2\nline3\n"),
312312
])
313-
def test_re_lines_inverted(self, text, pat, result):
314-
assert re_lines_text(text, pat, match=False) == result
315-
assert re_lines(text, pat, match=False) == result.splitlines()
313+
def test_re_lines_inverted(self, pat, text, result):
314+
assert re_lines_text(pat, text, match=False) == result
315+
assert re_lines(pat, text, match=False) == result.splitlines()
316316

317-
@pytest.mark.parametrize("text, pat, result", [
318-
("line1\nline2\nline3\n", "2", "line2"),
317+
@pytest.mark.parametrize("pat, text, result", [
318+
("2", "line1\nline2\nline3\n", "line2"),
319319
])
320-
def test_re_line(self, text, pat, result):
321-
assert re_line(text, pat) == result
320+
def test_re_line(self, pat, text, result):
321+
assert re_line(pat, text) == result
322322

323-
@pytest.mark.parametrize("text, pat", [
324-
("line1\nline2\nline3\n", "line"), # too many matches
325-
("line1\nline2\nline3\n", "X"), # no matches
323+
@pytest.mark.parametrize("pat, text", [
324+
("line", "line1\nline2\nline3\n"), # too many matches
325+
("X", "line1\nline2\nline3\n"), # no matches
326326
])
327-
def test_re_line_bad(self, text, pat):
327+
def test_re_line_bad(self, pat, text):
328328
with pytest.raises(AssertionError):
329-
re_line(text, pat)
329+
re_line(pat, text)
330330

331331

332332
def _same_python_executable(e1, e2):

0 commit comments

Comments
 (0)