Skip to content

Commit

Permalink
Fix --most-allocations with the value of 0
Browse files Browse the repository at this point in the history
In the docs we say that a value of 0 will show all entries but
currently the plugin crashes because it interprets the value of 0 as a
string and is not properly propagated to show all entries.
  • Loading branch information
pablogsal authored and godlygeek committed Sep 25, 2023
1 parent 2d9358b commit 8c263ec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/pytest_memray/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ def pytest_terminal_summary(
)

max_results = cast(int, value_or_ini(self.config, "most_allocations"))
if max_results == 0:
max_results = len(total_sizes)

for test_id, total_size in total_sizes.most_common(max_results):
result = self.results[test_id]
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_memray/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def parse_memory_string(mem_str: str) -> float:

def value_or_ini(config: Config, key: str) -> object:
value = config.getvalue(key)
if value:
if value is not None:
return value
try:
return config.getini(key)
Expand Down
33 changes: 33 additions & 0 deletions tests/test_pytest_memray.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,39 @@ def test_bar():
assert "results for test_memray_report_limit.py::test_bar" in output


def test_memray_report_limit_without_limit(pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest
from memray._test import MemoryAllocator
allocator = MemoryAllocator()
def allocating_func1():
allocator.valloc(1024)
allocator.free()
def allocating_func2():
allocator.valloc(1024*2)
allocator.free()
def test_foo():
allocating_func1()
def test_bar():
allocating_func2()
"""
)

result = pytester.runpytest("--memray", "--most-allocations=0")

assert result.ret == ExitCode.OK

output = result.stdout.str()

assert "results for test_memray_report_limit_without_limit.py::test_foo" in output
assert "results for test_memray_report_limit_without_limit.py::test_bar" in output


def test_failing_tests_are_not_reported(pytester: Pytester) -> None:
pytester.makepyfile(
"""
Expand Down

0 comments on commit 8c263ec

Please sign in to comment.