Skip to content

Commit

Permalink
fix: ape test -vvv
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Sep 19, 2024
1 parent a2e2dd4 commit 712c2a5
Show file tree
Hide file tree
Showing 2 changed files with 28 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 @@ -127,6 +127,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 @@ -145,22 +146,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 is 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
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"))
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 712c2a5

Please sign in to comment.