Add str() support to LineMatcher (#8050)

This commit is contained in:
Maximilian Cosmo Sitter 2020-11-21 19:45:20 +01:00 committed by GitHub
parent 52fef811c2
commit 0cef530d10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 2 deletions

View File

@ -0,0 +1 @@
Added an ``__str__`` implementation to the :class:`~pytest.pytester.LineMatcher` class which is returned from ``pytester.run_pytest().stdout`` and similar. It returns the entire output, like the existing ``str()`` method.

View File

@ -527,6 +527,7 @@ To use it, include in your topmost ``conftest.py`` file:
.. autoclass:: LineMatcher()
:members:
:special-members: __str__
.. autoclass:: HookRecorder()
:members:

View File

@ -512,7 +512,7 @@ class RunResult:
self.stdout = LineMatcher(outlines)
""":class:`LineMatcher` of stdout.
Use e.g. :func:`stdout.str() <LineMatcher.str()>` to reconstruct stdout, or the commonly used
Use e.g. :func:`str(stdout) <LineMatcher.__str__()>` to reconstruct stdout, or the commonly used
:func:`stdout.fnmatch_lines() <LineMatcher.fnmatch_lines()>` method.
"""
self.stderr = LineMatcher(errlines)
@ -1707,6 +1707,14 @@ class LineMatcher:
self.lines = lines
self._log_output: List[str] = []
def __str__(self) -> str:
"""Return the entire original text.
.. versionadded:: 6.2
You can use :meth:`str` in older versions.
"""
return "\n".join(self.lines)
def _getlines(self, lines2: Union[str, Sequence[str], Source]) -> Sequence[str]:
if isinstance(lines2, str):
lines2 = Source(lines2)
@ -1908,4 +1916,4 @@ class LineMatcher:
def str(self) -> str:
"""Return the entire original text."""
return "\n".join(self.lines)
return str(self)

View File

@ -610,6 +610,11 @@ def test_linematcher_no_matching_after_match() -> None:
assert str(e.value).splitlines() == ["fnmatch: '*'", " with: '1'"]
def test_linematcher_string_api() -> None:
lm = LineMatcher(["foo", "bar"])
assert str(lm) == "foo\nbar"
def test_pytester_addopts_before_testdir(request, monkeypatch) -> None:
orig = os.environ.get("PYTEST_ADDOPTS", None)
monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused")