From 124623007775942035f71617fce6a9859e8e4f57 Mon Sep 17 00:00:00 2001 From: Russell Martin Date: Fri, 29 Sep 2023 13:37:27 -0400 Subject: [PATCH] Enable debug cookiecutter logging for deep debug --- src/briefcase/commands/base.py | 3 ++ .../base/test_cookiecutter_logging.py | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/commands/base/test_cookiecutter_logging.py diff --git a/src/briefcase/commands/base.py b/src/briefcase/commands/base.py index 7125f1d22..86a180f8a 100644 --- a/src/briefcase/commands/base.py +++ b/src/briefcase/commands/base.py @@ -13,6 +13,7 @@ from pathlib import Path from cookiecutter import exceptions as cookiecutter_exceptions +from cookiecutter.log import configure_logger as configure_cookiecutter_logger from cookiecutter.repository import is_repo_url from platformdirs import PlatformDirs @@ -911,6 +912,8 @@ def generate_template(self, template, branch, output_path, extra_context): template=template, branch=branch ) + configure_cookiecutter_logger("DEBUG" if self.logger.is_deep_debug else "INFO") + try: # Unroll the template self.tools.cookiecutter( diff --git a/tests/commands/base/test_cookiecutter_logging.py b/tests/commands/base/test_cookiecutter_logging.py new file mode 100644 index 000000000..34be8b83b --- /dev/null +++ b/tests/commands/base/test_cookiecutter_logging.py @@ -0,0 +1,37 @@ +import logging + +import pytest + +cookiecutter_logger = logging.getLogger("cookiecutter") + + +@pytest.fixture +def base_command(base_command): + # Mock actual templating commands as no-ops + base_command.update_cookiecutter_cache = lambda *a, **kw: None + base_command.tools.cookiecutter = lambda *a, **kw: None + return base_command + + +@pytest.mark.parametrize( + "verbosity, log_level", + [ + (0, logging.INFO), + (1, logging.INFO), + (2, logging.DEBUG), + ], +) +def test_cookiecutter_logging_config(base_command, verbosity, log_level): + """The loggers for cookiecutter are configured as expected.""" + base_command.logger.verbosity = verbosity + + base_command.generate_template( + template="", branch="", output_path="", extra_context={} + ) + # call multiple times to ensure only 1 handler ever exists + base_command.generate_template( + template="", branch="", output_path="", extra_context={} + ) + + assert len(cookiecutter_logger.handlers) == 1 + assert cookiecutter_logger.handlers[0].level == log_level