From c7af0e458f118a47ea0de7c6afb1b038f493441e Mon Sep 17 00:00:00 2001 From: Daniel Mach Date: Wed, 24 Jan 2024 09:49:24 +0100 Subject: [PATCH] Use Field.get_callback to handle quiet/verbose and http_debug/http_full_debug options --- osc/conf.py | 8 ++++++-- tests/test_conf.py | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/osc/conf.py b/osc/conf.py index 0d914da7..998ebae1 100644 --- a/osc/conf.py +++ b/osc/conf.py @@ -564,7 +564,7 @@ class Options(OscOptions): description=textwrap.dedent( """ Reduce amount of printed information to bare minimum. - Takes priority over ``verbose``. + If enabled, automatically sets ``verbose`` to ``False``. """ ), ) # type: ignore[assignment] @@ -574,8 +574,10 @@ class Options(OscOptions): description=textwrap.dedent( """ Increase amount of printed information to stdout. + Automatically set to ``False`` when ``quiet`` is enabled. """ ), + get_callback=lambda conf, value: False if conf.quiet else value, ) # type: ignore[assignment] debug: bool = Field( @@ -592,8 +594,10 @@ class Options(OscOptions): description=textwrap.dedent( """ Print HTTP traffic to stderr. + Automatically set to ``True`` when``http_full_debug`` is enabled. """ ), + get_callback=lambda conf, value: True if conf.http_full_debug else value, ) # type: ignore[assignment] http_full_debug: bool = Field( @@ -601,6 +605,7 @@ class Options(OscOptions): description=textwrap.dedent( """ [CAUTION!] Print HTTP traffic incl. authentication data to stderr. + If enabled, automatically sets ``http_debug`` to ``True``. """ ), ) # type: ignore[assignment] @@ -1821,7 +1826,6 @@ def get_config(override_conffile=None, overrides["http_debug"] = override_http_debug if override_http_full_debug is not None: - overrides["http_debug"] = override_http_full_debug or overrides["http_debug"] overrides["http_full_debug"] = override_http_full_debug if override_traceback is not None: diff --git a/tests/test_conf.py b/tests/test_conf.py index bfe1c327..0f4db601 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -434,6 +434,33 @@ class TestExampleConfig(unittest.TestCase): self.assertEqual(self.config["apiurl_aliases"], expected) +class TestOverrides(unittest.TestCase): + def test_verbose(self): + self.options = osc.conf.Options() + self.assertEqual(self.options.quiet, False) + self.assertEqual(self.options.verbose, False) + + self.options.quiet = True + self.options.verbose = True + self.assertEqual(self.options.quiet, True) + # ``verbose`` is forced to ``False`` by the ``quiet`` option + self.assertEqual(self.options.verbose, False) + + self.options.quiet = False + self.assertEqual(self.options.quiet, False) + self.assertEqual(self.options.verbose, True) + + def test_http_debug(self): + self.options = osc.conf.Options() + self.assertEqual(self.options.http_debug, False) + self.assertEqual(self.options.http_full_debug, False) + + self.options.http_full_debug = True + # ``http_debug`` forced to ``True`` by the ``http_full_debug`` option + self.assertEqual(self.options.http_debug, True) + self.assertEqual(self.options.http_full_debug, True) + + class TestFromParent(unittest.TestCase): def setUp(self): self.options = osc.conf.Options()