From 41ac11ab3e0ff474693e470bc0e2fd7f92805cf4 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Thu, 19 Sep 2024 10:58:48 -0500 Subject: [PATCH] fix: ape test -vvv --- src/ape/logging.py | 23 ++++++++++++++++++----- tests/integration/cli/test_test.py | 10 ++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/ape/logging.py b/src/ape/logging.py index fd4161c90d..a41049f04e 100644 --- a/src/ape/logging.py +++ b/src/ape/logging.py @@ -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 @@ -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 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: diff --git a/tests/integration/cli/test_test.py b/tests/integration/cli/test_test.py index eadaefc443..1c2d092aba 100644 --- a/tests/integration/cli/test_test.py +++ b/tests/integration/cli/test_test.py @@ -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.