Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incompatibility with the 'flaky' plugin #50

Merged
merged 2 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ optional-dependencies.lint = [
optional-dependencies.test = [
"covdefaults>=2.2",
"pytest>=7.1.2",
"coverage>=6.4.4"
"coverage>=6.4.4",
"flaky>=3.7.0",
]
dynamic = ["version"]
classifiers = [
Expand Down
21 changes: 14 additions & 7 deletions src/pytest_memray/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,21 @@ def _build_bin_path() -> Path:

@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> object | None:
result_file = _build_bin_path()
with Tracker(result_file):
result: object | None = func(*args, **kwargs)
try:
metadata = FileReader(result_file).metadata
except OSError:
return None
self.results[pyfuncitem.nodeid] = Result(metadata, result_file)
result_file = _build_bin_path()
with Tracker(result_file):
result: object | None = func(*args, **kwargs)
try:
metadata = FileReader(result_file).metadata
except OSError:
return None
self.results[pyfuncitem.nodeid] = Result(metadata, result_file)
finally:
# Restore the original function. This is needed because some
# pytest plugins (e.g. flaky) will call our pytest_pyfunc_call
# hook again with whatever is here, which will cause the wrapper
# to be wrapped again.
pyfuncitem.obj = func
return result

pyfuncitem.obj = wrapper
Expand Down
29 changes: 25 additions & 4 deletions tests/test_pytest_memray.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ def test_bar():

assert "results for test_memray_report.py::test_foo" in output
assert "Total memory allocated: 2.0KiB" in output
assert "valloc:src/memray/_memray_test_utils.pyx" in output
assert "valloc:" in output
assert "-> 2.0KiB" in output

assert "results for test_memray_report.py::test_bar" in output
assert "Total memory allocated: 1.0KiB" in output
assert "valloc:src/memray/_memray_test_utils.pyx" in output
assert "valloc:" in output
assert "-> 1.0KiB" in output


Expand Down Expand Up @@ -223,12 +223,12 @@ def test_bar():

assert "results for test_memray_report.py::test_foo" not in output
assert "Total memory allocated: 2.0KiB" not in output
assert "valloc:src/memray/_memray.pyx" not in output
assert "valloc:" not in output
assert "-> 2.0KiB" not in output

assert "results for test_memray_report.py::test_bar" not in output
assert "Total memory allocated: 1.0KiB" not in output
assert "valloc:src/memray/_memray.pyx" not in output
assert "valloc:" not in output
assert "-> 1.0KiB" not in output


Expand Down Expand Up @@ -368,3 +368,24 @@ def test_t():

assert result.ret == ExitCode.OK
assert bin_path.exists()


def test_plugin_works_with_the_flaky_plugin(pytester: Pytester) -> None:
pytester.makepyfile(
"""
from flaky import flaky

@flaky
def test_hello_world():
1/0
"""
)

with patch("pytest_memray.plugin.Tracker") as mock:
result = pytester.runpytest("--memray")

# Ensure that flaky has only called our Tracker once per retry (2 times)
# and not more times because it has incorrectly wrapped our plugin and
# called it multiple times per retry.
assert mock.call_count == 2
assert result.ret == ExitCode.TESTS_FAILED