Skip to content

Commit

Permalink
test: lessen amount of logs in ape's tests (#2399)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Dec 16, 2024
1 parent 9dc7098 commit ec381e7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
12 changes: 6 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class ApeCaplog:
def __init__(self, caplog_level: LogLevel = LogLevel.WARNING):
self.level = caplog_level
self.messages_at_start = list(caplog.messages)
self.set_levels(caplog_level=caplog_level)
self.set_levels(caplog_level)

@only_raise_attribute_error
def __getattr__(self, name: str) -> Any:
Expand Down Expand Up @@ -418,10 +418,10 @@ def head(self) -> str:
"""
return caplog.messages[-1] if len(caplog.messages) else ""

def set_levels(self, caplog_level: LogLevel = LogLevel.WARNING):
self.level = caplog_level
logger.set_level(LogLevel.INFO)
caplog.set_level(caplog_level)
def set_levels(self, level: LogLevel = LogLevel.WARNING):
self.level = level
logger.set_level(level)
caplog.set_level(level)

def assert_last_log(self, message: str):
assert message in self.head, self.fail_message
Expand All @@ -446,7 +446,7 @@ def assert_last_log_with_retries(

# Reset levels in case they got switched.
self.set_levels()
logger.set_level(LogLevel.INFO)
logger.set_level(LogLevel.ERROR)
caplog.set_level(LogLevel.WARNING)

pytest.fail(self.fail_message)
Expand Down
1 change: 1 addition & 0 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ def _assert_log_values(

@pytest.fixture(scope="session")
def logger():
_logger.set_level(LogLevel.ERROR)
return _logger


Expand Down
8 changes: 5 additions & 3 deletions tests/functional/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,12 +561,13 @@ def test_contract_file_paths_argument_given_subdir_relative_to_path(

@skip_if_plugin_installed("vyper")
def test_contract_file_paths_argument_missing_vyper(
project_with_source_files_contract, runner, contracts_paths_cmd
project_with_source_files_contract, runner, contracts_paths_cmd, ape_caplog
):
name = "VyperContract"
pm = project_with_source_files_contract
arguments = (name, "--project", f"{pm.path}")
result = runner.invoke(contracts_paths_cmd, arguments)
with ape_caplog.at_level(LogLevel.INFO):
result = runner.invoke(contracts_paths_cmd, arguments)

expected = (
"Missing compilers for the following file types: '.vy'. "
Expand All @@ -584,7 +585,8 @@ def test_contract_file_paths_argument_missing_solidity(
pm = project_with_source_files_contract
with pm.isolate_in_tempdir() as tmp_project:
arguments = (name, "--project", f"{tmp_project.path}")
result = runner.invoke(contracts_paths_cmd, arguments)
with logger.at_level(LogLevel.WARNING):
result = runner.invoke(contracts_paths_cmd, arguments)

expected = (
"Missing compilers for the following file types: '.sol'. "
Expand Down
11 changes: 7 additions & 4 deletions tests/functional/test_contracts_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ape import Contract
from ape.contracts import ContractInstance
from ape.exceptions import ContractNotFoundError, ConversionError
from ape.logging import LogLevel
from ape_ethereum.proxies import _make_minimal_proxy
from tests.conftest import explorer_test, skip_if_plugin_installed

Expand Down Expand Up @@ -298,11 +299,13 @@ def test_get_multiple(vyper_contract_instance, solidity_contract_instance, chain
)


def test_get_multiple_no_addresses(chain, caplog):
contract_map = chain.contracts.get_multiple([])
def test_get_multiple_no_addresses(chain, ape_caplog):
with ape_caplog.at_level(LogLevel.WARNING):
contract_map = chain.contracts.get_multiple([])
assert "WARNING" in ape_caplog.records[-1].levelname
assert "No addresses provided." in ape_caplog.messages[-1]

assert not contract_map
assert "WARNING" in caplog.records[-1].levelname
assert "No addresses provided." in caplog.messages[-1]


def test_get_multiple_include_non_contract_address(vyper_contract_instance, chain, owner):
Expand Down
18 changes: 12 additions & 6 deletions tests/functional/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def test_info(simple_runner):
@group_for_testing.command()
@ape_cli_context()
def cmd(cli_ctx):
cli_ctx.logger.info("this is a test")
with cli_ctx.logger.at_level(LogLevel.INFO):
cli_ctx.logger.info("this is a test")

result = simple_runner.invoke(group_for_testing, "cmd")
assert "INFO" in result.output
Expand All @@ -45,7 +46,8 @@ def test_warning(simple_runner):
@group_for_testing.command()
@ape_cli_context()
def cmd(cli_ctx):
cli_ctx.logger.warning("this is a test")
with cli_ctx.logger.at_level(LogLevel.WARNING):
cli_ctx.logger.warning("this is a test")

result = simple_runner.invoke(group_for_testing, "cmd")
assert "WARNING" in result.output
Expand All @@ -56,7 +58,8 @@ def test_warning_level_higher(simple_runner):
@group_for_testing.command()
@ape_cli_context()
def cmd(cli_ctx):
cli_ctx.logger.warning("this is a test")
with cli_ctx.logger.at_level(LogLevel.WARNING.value + 1):
cli_ctx.logger.warning("this is a test")

logger._did_parse_sys_argv = False
result = simple_runner.invoke(group_for_testing, ("cmd", "-v", "ERROR"))
Expand All @@ -71,7 +74,8 @@ def test_success(simple_runner):
@group_for_testing.command()
@ape_cli_context(default_log_level=LogLevel.INFO.value)
def cmd(cli_ctx):
cli_ctx.logger.success("this is a test")
with cli_ctx.logger.at_level(LogLevel.SUCCESS):
cli_ctx.logger.success("this is a test")

logger._did_parse_sys_argv = False
result = simple_runner.invoke(group_for_testing, "cmd")
Expand All @@ -83,7 +87,8 @@ def test_success_level_higher(simple_runner):
@group_for_testing.command()
@ape_cli_context()
def cmd(cli_ctx):
cli_ctx.logger.success("this is a test")
with cli_ctx.logger.at_level(LogLevel.SUCCESS.value + 1):
cli_ctx.logger.success("this is a test")

logger._did_parse_sys_argv = False
result = simple_runner.invoke(group_for_testing, ("cmd", "-v", "WARNING"))
Expand All @@ -97,7 +102,8 @@ def test_format(simple_runner):
def cmd(cli_ctx):
cli_ctx.logger.format(fmt="%(message)s")
try:
cli_ctx.logger.success("this is a test")
with cli_ctx.logger.at_level(LogLevel.SUCCESS):
cli_ctx.logger.success("this is a test")
finally:
cli_ctx.logger.format()

Expand Down
8 changes: 5 additions & 3 deletions tests/functional/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,11 @@ def test_repr_when_exception(self, mocker):
def test_handle_upgrade_result_when_upgrading_to_same_version(ape_caplog, logger):
plugin = PluginMetadata(name=THIRD_PARTY[0], version=f"0.{ape_version.minor}.0")
handler = ModifyPluginResultHandler(plugin)
ape_caplog.set_levels(LogLevel.INFO)
handler.handle_upgrade_result(0, f"0.{ape_version.minor}.0")
assert f"'{THIRD_PARTY[0]}' already has version '0.{ape_version.minor}.0'" in ape_caplog.head
with ape_caplog.at_level(LogLevel.INFO):
handler.handle_upgrade_result(0, f"0.{ape_version.minor}.0")
assert (
f"'{THIRD_PARTY[0]}' already has version '0.{ape_version.minor}.0'" in ape_caplog.head
)


class TestApeVersion:
Expand Down

0 comments on commit ec381e7

Please sign in to comment.