From ab184377c0638f43cdb8087b4975f5f60cd43946 Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Tue, 6 Aug 2024 15:03:59 +0200 Subject: [PATCH] Move the `TMT_DEBUG` to click Also make the bootstrap_logger failsafe Signed-off-by: Cristian Le --- tests/core/env/test.sh | 2 +- tmt/log.py | 38 ++++++++++++-------------------------- tmt/options.py | 2 +- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/tests/core/env/test.sh b/tests/core/env/test.sh index ed910f94a3..28a038b32d 100755 --- a/tests/core/env/test.sh +++ b/tests/core/env/test.sh @@ -12,7 +12,7 @@ rlJournalStart rlRun -s "TMT_DEBUG=3 tmt plan show" rlAssertGrep "Using the 'DiscoverFmf' plugin" $rlRun_LOG rlRun -s "TMT_DEBUG=weird tmt plan show" 2 - rlAssertGrep "Invalid debug level" $rlRun_LOG + rlAssertGrep "Invalid value.*'weird' is not a valid integer" $rlRun_LOG -E rlPhaseEnd for execute in 'tmt'; do diff --git a/tmt/log.py b/tmt/log.py index 753e8e3902..1de96e78ed 100644 --- a/tmt/log.py +++ b/tmt/log.py @@ -108,21 +108,6 @@ def create_decolorizer(apply_colors: bool) -> Callable[[str], str]: return tmt.utils.remove_color -def _debug_level_from_global_envvar() -> int: - import tmt.utils - - raw_value = os.getenv('TMT_DEBUG', None) - - if raw_value is None: - return 0 - - try: - return int(raw_value) - - except ValueError: - raise tmt.utils.GeneralError(f"Invalid debug level '{raw_value}', use an integer.") - - def decide_colorization(no_color: bool, force_color: bool) -> tuple[bool, bool]: """ Decide whether the output and logging should be colorized. @@ -626,19 +611,13 @@ def apply_verbosity_options( else: self.verbosity_level = verbosity_level - debug_level_from_global_envvar = _debug_level_from_global_envvar() + debug_level_from_option = cast(Optional[int], actual_kwargs.get('debug', None)) - if debug_level_from_global_envvar not in (None, 0): - self.debug_level = debug_level_from_global_envvar + if debug_level_from_option is None or debug_level_from_option == 0: + pass else: - debug_level_from_option = cast(Optional[int], actual_kwargs.get('debug', None)) - - if debug_level_from_option is None or debug_level_from_option == 0: - pass - - else: - self.debug_level = debug_level_from_option + self.debug_level = debug_level_from_option quietness_level = actual_kwargs.get('quiet', False) @@ -868,7 +847,14 @@ def get_bootstrap_logger(cls) -> 'Logger': # Stay away of our future main logger actual_logger = Logger._normalize_logger(logging.getLogger('_tmt_bootstrap')) - cls._bootstrap_logger = Logger.create(actual_logger=actual_logger) + # The environment variables are usually handled at the click cli stage + # Here we enable safe parsing of those variables. + try: + debug = int(os.getenv('TMT_DEBUG', 0)) + except ValueError: + debug = None + + cls._bootstrap_logger = Logger.create(actual_logger=actual_logger, debug=debug) cls._bootstrap_logger.add_console_handler() return cls._bootstrap_logger diff --git a/tmt/options.py b/tmt/options.py index 04f5569804..b755398510 100644 --- a/tmt/options.py +++ b/tmt/options.py @@ -132,7 +132,7 @@ def option( '-v', '--verbose', count=True, default=0, help='Show more details. Use multiple times to raise verbosity.'), option( - '-d', '--debug', count=True, default=0, + '-d', '--debug', count=True, default=0, envvar="TMT_DEBUG", help='Provide debugging information. Repeat to see more details.'), option( '-q', '--quiet', is_flag=True,