Skip to content

Commit

Permalink
fix: issue where couldn't use ape test -vvv (#2293)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Sep 19, 2024
1 parent 99df05f commit a561cde
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/ape/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def __init__(
self.info = _logger.info
self.debug = _logger.debug
self._logger = _logger
self._did_parse_sys_argv = False
self._load_from_sys_argv()
self.fmt = fmt

Expand All @@ -146,22 +147,34 @@ def _load_from_sys_argv(self, default: Optional[Union[str, int, LogLevel]] = Non
"""
Load from sys.argv to beat race condition with `click`.
"""
if self._did_parse_sys_argv:
# Already parsed.
return

log_level = _get_level(level=default)
level_names = [lvl.name for lvl in LogLevel]
for arg_i in range(len(sys.argv) - 1):

# Minus 2 because if `-v` is the last arg, it is not our verbosity `-v`.
num_args = len(sys.argv) - 2

for arg_i in range(num_args):
if sys.argv[arg_i] == "-v" or sys.argv[arg_i] == "--verbosity":
level = _get_level(sys.argv[arg_i + 1].upper())
try:
level = _get_level(sys.argv[arg_i + 1].upper())
except Exception:
# Let it fail in a better spot, or is not our level.
continue

if level in level_names:
self._sys_argv = level
log_level = level
break
else:
names_str = f"{', '.join(level_names[:-1])}, or {level_names[-1]}"
self._logger.error(f"Must be one of '{names_str}', not '{level}'.")
sys.exit(2)
# Not our level.
continue

self.set_level(log_level)
self._did_parse_sys_argv = True

@property
def level(self) -> int:
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_info_level_higher(simple_runner):
def cmd(cli_ctx):
cli_ctx.logger.info("this is a test")

logger._did_parse_sys_argv = False
result = simple_runner.invoke(group_for_testing, ("cmd", "-v", "WARNING"))

# You don't get INFO log when log level is higher
Expand All @@ -57,6 +58,7 @@ def test_warning_level_higher(simple_runner):
def cmd(cli_ctx):
cli_ctx.logger.warning("this is a test")

logger._did_parse_sys_argv = False
result = simple_runner.invoke(group_for_testing, ("cmd", "-v", "ERROR"))
assert "WARNING" not in result.output
assert "this is a test" not in result.output
Expand All @@ -71,6 +73,7 @@ def test_success(simple_runner):
def cmd(cli_ctx):
cli_ctx.logger.success("this is a test")

logger._did_parse_sys_argv = False
result = simple_runner.invoke(group_for_testing, "cmd")
assert "SUCCESS" in result.output
assert "this is a test" in result.output
Expand All @@ -82,6 +85,7 @@ def test_success_level_higher(simple_runner):
def cmd(cli_ctx):
cli_ctx.logger.success("this is a test")

logger._did_parse_sys_argv = False
result = simple_runner.invoke(group_for_testing, ("cmd", "-v", "WARNING"))
assert "SUCCESS" not in result.output
assert "this is a test" not in result.output
Expand All @@ -97,6 +101,7 @@ def cmd(cli_ctx):
finally:
cli_ctx.logger.format()

logger._did_parse_sys_argv = False
result = simple_runner.invoke(group_for_testing, ("cmd", "-v", "SUCCESS"))
assert "SUCCESS" not in result.output

Expand Down
10 changes: 10 additions & 0 deletions tests/integration/cli/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ def test_verbosity(runner, ape_cli):
assert result.exit_code == 0, result.output


@skip_projects_except("test")
def test_vvv(runner, ape_cli):
"""
Showing you can somehow use pytest's -v flag without
messing up Ape.
"""
result = runner.invoke(ape_cli, ("test", "-vvv", "--fixtures"), catch_exceptions=False)
assert result.exit_code == 0, result.output


@skip_projects_except("test", "with-contracts")
def test_fixture_docs(setup_pytester, integ_project, pytester, eth_tester_provider):
_ = eth_tester_provider # Ensure using EthTester for this test.
Expand Down

0 comments on commit a561cde

Please sign in to comment.