diff options
-rw-r--r-- | tests/helpers.py | 10 | ||||
-rw-r--r-- | tests/test_debug.py | 29 | ||||
-rw-r--r-- | tests/test_goldtest.py | 4 | ||||
-rw-r--r-- | tests/test_parser.py | 4 | ||||
-rw-r--r-- | tests/test_phystokens.py | 5 | ||||
-rw-r--r-- | tests/test_process.py | 38 | ||||
-rw-r--r-- | tests/test_testing.py | 52 |
7 files changed, 68 insertions, 74 deletions
diff --git a/tests/helpers.py b/tests/helpers.py index 7e6594ac..82d8b18f 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -133,7 +133,7 @@ class CheckUniqueFilenames: return ret -def re_lines(text, pat, match=True): +def re_lines(pat, text, match=True): """Return a list of lines selected by `pat` in the string `text`. If `match` is false, the selection is inverted: only the non-matching @@ -146,18 +146,18 @@ def re_lines(text, pat, match=True): return [l for l in text.splitlines() if bool(re.search(pat, l)) == match] -def re_lines_text(text, pat, match=True): +def re_lines_text(pat, text, 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)) + return "".join(l + "\n" for l in re_lines(pat, text, match=match)) -def re_line(text, pat): +def re_line(pat, text): """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(text, pat) + lines = re_lines(pat, text) assert len(lines) == 1 return lines[0] diff --git a/tests/test_debug.py b/tests/test_debug.py index 564c2c29..1ea72e82 100644 --- a/tests/test_debug.py +++ b/tests/test_debug.py @@ -126,35 +126,30 @@ class DebugTraceTest(CoverageTest): # We should have a line like "Tracing 'f1.py'", perhaps with an # absolute path. - f1 = re_lines(out_lines, r"Tracing '.*f1.py'") - assert f1 + assert re.search(r"Tracing '.*f1.py'", out_lines) # We should have lines like "Not tracing 'collector.py'..." - coverage_lines = re_lines( - out_lines, - r"^Not tracing .*: is part of coverage.py$" - ) - assert coverage_lines + assert re_lines(r"^Not tracing .*: is part of coverage.py$", out_lines) def test_debug_trace_pid(self): out_lines = self.f1_debug_output(["trace", "pid"]) # Now our lines are always prefixed with the process id. pid_prefix = r"^%5d\.[0-9a-f]{4}: " % os.getpid() - pid_lines = re_lines_text(out_lines, pid_prefix) + pid_lines = re_lines_text(pid_prefix, out_lines) assert pid_lines == out_lines # We still have some tracing, and some not tracing. - assert re_lines(out_lines, pid_prefix + "Tracing ") - assert re_lines(out_lines, pid_prefix + "Not tracing ") + assert re_lines(pid_prefix + "Tracing ", out_lines) + assert re_lines(pid_prefix + "Not tracing ", out_lines) def test_debug_callers(self): 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) + real_messages = re_lines(r":\d+", out_lines, match=False) frame_pattern = r"\s+f1_debug_output : .*tests[/\\]test_debug.py:\d+$" - frames = re_lines(out_lines, frame_pattern) + frames = re_lines(frame_pattern, out_lines) assert len(real_messages) == len(frames) last_line = out_lines.splitlines()[-1] @@ -162,8 +157,8 @@ class DebugTraceTest(CoverageTest): # The details of what to expect on the stack are empirical, and can change # as the code changes. This test is here to ensure that the debug code # continues working. It's ok to adjust these details over time. - assert re.search(r"^\s*\d+\.\w{4}: Adding file tracers: 0 files", real_messages[-1]) - assert re.search(r"\s+add_file_tracers : .*coverage[/\\]sqldata.py:\d+$", last_line) + assert re_lines(r"^\s*\d+\.\w{4}: Adding file tracers: 0 files", real_messages[-1]) + assert re_lines(r"\s+add_file_tracers : .*coverage[/\\]sqldata.py:\d+$", last_line) def test_debug_config(self): out_lines = self.f1_debug_output(["config"]) @@ -178,7 +173,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)), msg + assert 1 == len(re_lines(label_pat, out_lines)), msg def test_debug_sys(self): out_lines = self.f1_debug_output(["sys"]) @@ -192,11 +187,11 @@ 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)), msg + assert 1 == len(re_lines(label_pat, out_lines)), msg def test_debug_sys_ctracer(self): out_lines = self.f1_debug_output(["sys"]) - tracer_line = re_line(out_lines, r"CTracer:").strip() + tracer_line = re_line(r"CTracer:", out_lines).strip() if env.C_TRACER: expected = "CTracer: available" else: diff --git a/tests/test_goldtest.py b/tests/test_goldtest.py index 5086fa15..73e0e188 100644 --- a/tests/test_goldtest.py +++ b/tests/test_goldtest.py @@ -69,8 +69,8 @@ class CompareTest(CoverageTest): stdout = self.stdout() assert "- Four score" in stdout assert "+ Five score" in stdout - assert re_line(stdout, rf"^:::: diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'") - assert re_line(stdout, rf"^:::: end diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'") + assert re_line(rf"^:::: diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'", stdout) + assert re_line(rf"^:::: end diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'", stdout) assert " D/D/D, Gxxx, Pennsylvania" in stdout # The actual file was saved. diff --git a/tests/test_parser.py b/tests/test_parser.py index 3197ffb4..48963358 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -502,5 +502,5 @@ def test_ast_dump(): assert result[0] == "<Module" assert result[-1] == ">" result_text = "\n".join(result) - assert len(re_lines(result_text, r"^\s+>")) > num_lines - assert len(re_lines(result_text, r"<Name @ \d+,\d+(:\d+)? id: '\w+'>")) > num_lines // 2 + assert len(re_lines(r"^\s+>", result_text)) > num_lines + assert len(re_lines(r"<Name @ \d+,\d+(:\d+)? id: '\w+'>", result_text)) > num_lines // 2 diff --git a/tests/test_phystokens.py b/tests/test_phystokens.py index fea581a5..f3985437 100644 --- a/tests/test_phystokens.py +++ b/tests/test_phystokens.py @@ -15,7 +15,6 @@ from coverage.phystokens import neuter_encoding_declaration, compile_unicode from coverage.python import get_python_source from tests.coveragetest import CoverageTest, TESTS_DIR -from tests.helpers import re_lines # A simple program and its token stream. @@ -102,11 +101,11 @@ class PhysTokensTest(CoverageTest): stress = os.path.join(TESTS_DIR, "stress_phystoken.tok") self.check_file_tokenization(stress) with open(stress) as fstress: - assert re_lines(fstress.read(), r"\s$"), f"{stress} needs a trailing space." + assert re.search(r"\s$", fstress.read()), f"{stress} needs a trailing space." stress = os.path.join(TESTS_DIR, "stress_phystoken_dos.tok") self.check_file_tokenization(stress) with open(stress) as fstress: - assert re_lines(fstress.read(), r"\s$"), f"{stress} needs a trailing space." + assert re.search(r"\s$", fstress.read()), f"{stress} needs a trailing space." @pytest.mark.skipif(not env.PYBEHAVIOR.soft_keywords, reason="Soft keywords are new in Python 3.10") diff --git a/tests/test_process.py b/tests/test_process.py index f5ffee51..1b17160f 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -501,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_text(out2, "toplevel", match=False) + out2 = re_lines_text("toplevel", out2, match=False) assert out == out2 # But also make sure that the output is what we expect. @@ -870,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_text(expected, r'\s+"argv0":', match=False) - actual = re_lines_text(actual, r'\s+"argv0":', match=False) + expected = re_lines_text(r'\s+"argv0":', expected, match=False) + actual = re_lines_text(r'\s+"argv0":', actual, match=False) assert actual == expected @@ -907,8 +907,8 @@ class EnvironmentTest(CoverageTest): # the comparison also... if env.PYPY: ignored = re.escape(os.getcwd()) - expected = re_lines_text(expected, ignored, match=False) - actual = re_lines_text(actual, ignored, match=False) + expected = re_lines_text(ignored, expected, match=False) + actual = re_lines_text(ignored, actual, match=False) self.assert_tryexecfile_output(expected, actual) def test_coverage_run_dashm_dir_no_init_is_like_python(self): @@ -1807,13 +1807,13 @@ class VirtualenvTest(CoverageTest): # --source refers to a file. debug_out = self.get_trace_output() assert re_lines( + r"^Not tracing .*\bexecfile.py': inside --source, but is third-party", debug_out, - r"^Not tracing .*\bexecfile.py': inside --source, but is third-party" ) - assert re_lines(debug_out, r"^Tracing .*\bmyproduct.py") + assert re_lines(r"^Tracing .*\bmyproduct.py", debug_out) assert re_lines( + r"^Not tracing .*\bcolorsys.py': falls outside the --source spec", debug_out, - r"^Not tracing .*\bcolorsys.py': falls outside the --source spec" ) out = run_in_venv("python -m coverage report") @@ -1830,17 +1830,17 @@ class VirtualenvTest(CoverageTest): # --source refers to a module. debug_out = self.get_trace_output() assert re_lines( - debug_out, r"^Not tracing .*\bexecfile.py': " + - "module 'coverage.execfile' falls outside the --source spec" + "module 'coverage.execfile' falls outside the --source spec", + debug_out, ) assert re_lines( + r"^Not tracing .*\bmyproduct.py': module 'myproduct' falls outside the --source spec", debug_out, - r"^Not tracing .*\bmyproduct.py': module 'myproduct' falls outside the --source spec" ) assert re_lines( + r"^Not tracing .*\bcolorsys.py': module 'colorsys' falls outside the --source spec", debug_out, - r"^Not tracing .*\bcolorsys.py': module 'colorsys' falls outside the --source spec" ) out = run_in_venv("python -m coverage report") @@ -1854,9 +1854,9 @@ class VirtualenvTest(CoverageTest): assert out == self.expected_stdout debug_out = self.get_trace_output() - assert re_lines(debug_out, r"^Not tracing .*\bexecfile.py': is part of coverage.py") - assert re_lines(debug_out, r"^Tracing .*\bmyproduct.py") - assert re_lines(debug_out, r"^Not tracing .*\bcolorsys.py': is in the stdlib") + assert re_lines(r"^Not tracing .*\bexecfile.py': is part of coverage.py", debug_out) + assert re_lines(r"^Tracing .*\bmyproduct.py", debug_out) + assert re_lines(r"^Not tracing .*\bcolorsys.py': is in the stdlib", debug_out) out = run_in_venv("python -m coverage report") assert "myproduct.py" in out @@ -1894,17 +1894,17 @@ class VirtualenvTest(CoverageTest): # --source refers to a file. debug_out = self.get_trace_output() assert re_lines( - debug_out, r"^Not tracing .*\bexecfile.py': " + - "module 'coverage.execfile' falls outside the --source spec" + "module 'coverage.execfile' falls outside the --source spec", + debug_out, ) assert re_lines( + r"^Not tracing .*\bmyproduct.py': module 'myproduct' falls outside the --source spec", debug_out, - r"^Not tracing .*\bmyproduct.py': module 'myproduct' falls outside the --source spec" ) assert re_lines( + r"^Not tracing .*\bcolorsys.py': module 'colorsys' falls outside the --source spec", debug_out, - r"^Not tracing .*\bcolorsys.py': module 'colorsys' falls outside the --source spec" ) out = run_in_venv("python -m coverage report") diff --git a/tests/test_testing.py b/tests/test_testing.py index 8d559fe4..e1d3a345 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -183,12 +183,12 @@ class CoverageTestTest(CoverageTest): # Try it with a "coverage debug sys" command. out = self.run_command("coverage debug sys") - executable = re_line(out, "executable:") + executable = re_line("executable:", out) executable = executable.split(":", 1)[1].strip() assert _same_python_executable(executable, sys.executable) # "environment: COV_FOOBAR = XYZZY" or "COV_FOOBAR = XYZZY" - environ = re_line(out, "COV_FOOBAR") + environ = re_line("COV_FOOBAR", out) _, _, environ = environ.rpartition(":") assert environ.strip() == "COV_FOOBAR = XYZZY" @@ -296,37 +296,37 @@ class ReLinesTest(CoverageTest): run_in_temp_dir = False - @pytest.mark.parametrize("text, pat, result", [ - ("line1\nline2\nline3\n", "line", "line1\nline2\nline3\n"), - ("line1\nline2\nline3\n", "[13]", "line1\nline3\n"), - ("line1\nline2\nline3\n", "X", ""), + @pytest.mark.parametrize("pat, text, result", [ + ("line", "line1\nline2\nline3\n", "line1\nline2\nline3\n"), + ("[13]", "line1\nline2\nline3\n", "line1\nline3\n"), + ("X", "line1\nline2\nline3\n", ""), ]) - def test_re_lines(self, 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", ""), - ("line1\nline2\nline3\n", "[13]", "line2\n"), - ("line1\nline2\nline3\n", "X", "line1\nline2\nline3\n"), + def test_re_lines(self, pat, text, result): + assert re_lines_text(pat, text) == result + assert re_lines(pat, text) == result.splitlines() + + @pytest.mark.parametrize("pat, text, result", [ + ("line", "line1\nline2\nline3\n", ""), + ("[13]", "line1\nline2\nline3\n", "line2\n"), + ("X", "line1\nline2\nline3\n", "line1\nline2\nline3\n"), ]) - def test_re_lines_inverted(self, text, pat, result): - assert re_lines_text(text, pat, match=False) == result - assert re_lines(text, pat, match=False) == result.splitlines() + def test_re_lines_inverted(self, pat, text, result): + assert re_lines_text(pat, text, match=False) == result + assert re_lines(pat, text, match=False) == result.splitlines() - @pytest.mark.parametrize("text, pat, result", [ - ("line1\nline2\nline3\n", "2", "line2"), + @pytest.mark.parametrize("pat, text, result", [ + ("2", "line1\nline2\nline3\n", "line2"), ]) - def test_re_line(self, text, pat, result): - assert re_line(text, pat) == result + def test_re_line(self, pat, text, result): + assert re_line(pat, text) == result - @pytest.mark.parametrize("text, pat", [ - ("line1\nline2\nline3\n", "line"), # too many matches - ("line1\nline2\nline3\n", "X"), # no matches + @pytest.mark.parametrize("pat, text", [ + ("line", "line1\nline2\nline3\n"), # too many matches + ("X", "line1\nline2\nline3\n"), # no matches ]) - def test_re_line_bad(self, text, pat): + def test_re_line_bad(self, pat, text): with pytest.raises(AssertionError): - re_line(text, pat) + re_line(pat, text) def _same_python_executable(e1, e2): |