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

perf: move no-capture check to the inside of the ConfigWrapper constructor; avoid when only --help #2376

Merged
merged 3 commits into from
Nov 21, 2024
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
12 changes: 12 additions & 0 deletions src/ape/pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ class ConfigWrapper(ManagerAccessMixin):

def __init__(self, pytest_config: "PytestConfig"):
self.pytest_config = pytest_config
if not self.verbosity:
# Enable verbose output if stdout capture is disabled
self.verbosity = self.pytest_config.getoption("capture") == "no"
# else: user has already changes verbosity to an equal or higher level; avoid downgrading.

@property
def verbosity(self) -> int:
return self.pytest_config.option.verbose

@verbosity.setter
def verbosity(self, value):
self.pytest_config.option.verbose = value

@cached_property
def supports_tracing(self) -> bool:
Expand Down
5 changes: 0 additions & 5 deletions src/ape/pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ def is_module(v):
except AttributeError:
pass

if not config.option.verbose:
# Enable verbose output if stdout capture is disabled
config.option.verbose = config.getoption("capture") == "no"
# else: user has already changes verbosity to an equal or higher level; avoid downgrading.

if "--help" in config.invocation_params.args:
# perf: Don't bother setting up runner if only showing help.
return
Expand Down
27 changes: 27 additions & 0 deletions tests/functional/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from ape.exceptions import ConfigError
from ape.pytest.config import ConfigWrapper
from ape.pytest.runners import PytestApeRunner
from ape_test import ApeTestConfig
from ape_test._watch import run_with_observer
Expand All @@ -18,6 +19,32 @@ def test_balance_set_from_currency_str(self):
assert actual == expected


class TestConfigWrapper:
def test_verbosity(self, mocker):
"""
Show it returns the same as pytest_config's.
"""
pytest_cfg = mocker.MagicMock()
pytest_cfg.option.verbose = False
wrapper = ConfigWrapper(pytest_cfg)
assert wrapper.verbosity is False

def test_verbosity_when_no_capture(self, mocker):
"""
Shows we enable verbose output when no-capture is set.
"""

def get_opt(name: str):
return "no" if name == "capture" else None

pytest_cfg = mocker.MagicMock()
pytest_cfg.option.verbose = False # Start off as False
pytest_cfg.getoption.side_effect = get_opt

wrapper = ConfigWrapper(pytest_cfg)
assert wrapper.verbosity is True


def test_connect_to_mainnet_by_default(mocker):
"""
Tests the condition where mainnet is configured as the default network
Expand Down
Loading