Skip to content

Commit

Permalink
fix: ValueError: Unknown level: 'LOGLEVEL.WARNING' (#2249)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Aug 27, 2024
1 parent 228ab19 commit 4ff4a39
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/ape/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ def _create_verbosity_kwargs(
cli_logger = _logger or logger

def set_level(ctx, param, value):
cli_logger._load_from_sys_argv(default=value.upper() if isinstance(value, str) else value)
if isinstance(value, str):
value = value.upper()
if value.startswith("LOGLEVEL."):
value = value.split(".")[-1].strip()

cli_logger._load_from_sys_argv(default=value)

level_names = [lvl.name for lvl in LogLevel]
names_str = f"{', '.join(level_names[:-1])}, or {level_names[-1]}"
Expand Down
9 changes: 7 additions & 2 deletions src/ape/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,11 @@ def set_level(self, level: Union[str, int, LogLevel]):
"""
if level == self._logger.level:
return

elif isinstance(level, LogLevel):
level = level.value
elif isinstance(level, str) and level.lower().startswith("loglevel."):
# Seen in some environments.
level = level.split(".")[-1].strip()

self._logger.setLevel(level)
for _logger in self._extra_loggers.values():
Expand Down Expand Up @@ -274,8 +276,11 @@ def _get_level(level: Optional[Union[str, int, LogLevel]] = None) -> str:
return DEFAULT_LOG_LEVEL
elif isinstance(level, LogLevel):
return level.name
elif isinstance(level, int) or level.isnumeric():
elif isinstance(level, int) or (isinstance(level, str) and level.isnumeric()):
return LogLevel(int(level)).name
elif isinstance(level, str) and level.lower().startswith("loglevel."):
# Handle 'LogLevel.' prefix.
return level.split(".")[-1].strip()

return level

Expand Down
31 changes: 29 additions & 2 deletions src/ape_test/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from watchdog.observers import Observer

from ape.cli import ape_cli_context
from ape.logging import LogLevel
from ape.logging import LogLevel, _get_level
from ape.utils import ManagerAccessMixin, cached_property

# Copied from https://github.com/olzhasar/pytest-watcher/blob/master/pytest_watcher/watcher.py
Expand Down Expand Up @@ -75,12 +75,38 @@ def _run_main_loop(delay: float, pytest_args: Sequence[str]) -> None:
time.sleep(delay)


def _validate_pytest_args(*pytest_args) -> list[str]:
threshold = len(pytest_args) - 1
args_iter = iter(pytest_args)
valid_args = []
for idx, argument in enumerate(args_iter):
if idx >= threshold:
# If the last arg is -v without a value, it is a valid
# pytest arg.
valid_args.append(argument)
break

elif argument == "-v":
# Ensure this is a pytest -v and not ape's -v.
next_arg = next(args_iter)
lvl_name = _get_level(next_arg)
if lvl_name in [x.name for x in LogLevel]:
# Ape log level found, cannot use.
continue

else:
valid_args.append(argument)

return valid_args


@click.command(
add_help_option=False, # NOTE: This allows pass-through to pytest's help
short_help="Launches pytest and runs the tests for a project",
context_settings=dict(ignore_unknown_options=True),
)
@ape_cli_context(default_log_level=LogLevel.WARNING)
# NOTE: Using '.value' because more performant.
@ape_cli_context(default_log_level=LogLevel.WARNING.value)
@click.option(
"-w",
"--watch",
Expand Down Expand Up @@ -119,6 +145,7 @@ def cli(cli_ctx, watch, watch_folders, watch_delay, pytest_args):
cli_ctx.logger.warning(f"Folder '{folder}' doesn't exist or isn't a folder.")

observer.start()
pytest_args = _validate_pytest_args(pytest_args)

try:
_run_ape_test(pytest_args)
Expand Down
6 changes: 4 additions & 2 deletions tests/functional/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,13 @@ def test_verbosity_option(runner):
def cmd():
click.echo(f"__expected_{logger.level}")

result = runner.invoke(cmd, ["--verbosity", logger.level])
result = runner.invoke(cmd, ("--verbosity", logger.level))
assert f"__expected_{logger.level}" in result.output


@pytest.mark.parametrize("level", (LogLevel.WARNING, LogLevel.WARNING.name, LogLevel.WARNING.value))
@pytest.mark.parametrize(
"level", (LogLevel.WARNING, LogLevel.WARNING.name, LogLevel.WARNING.value, "LogLevel.WARNING")
)
def test_verbosity_option_change_default(runner, level):
@click.command()
@verbosity_option(default=level)
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def cmd(cli_ctx):
assert "SUCCESS" not in result.output


@pytest.mark.parametrize("level", (LogLevel.INFO, LogLevel.INFO.value, LogLevel.INFO.name))
@pytest.mark.parametrize(
"level", (LogLevel.INFO, LogLevel.INFO.value, LogLevel.INFO.name, "LogLevel.INFO")
)
def test_set_level(level):
logger.set_level(level)
assert logger.level == LogLevel.INFO.value

0 comments on commit 4ff4a39

Please sign in to comment.