From f47c313f91efee74b1ba7940572e42fc8dc47f40 Mon Sep 17 00:00:00 2001 From: RobertoRoos Date: Tue, 24 Dec 2024 14:59:29 +0100 Subject: [PATCH] Expanded docs and tests --- docs/pages/config.rst | 11 +++++++---- src/tctools/common.py | 13 ++++++++++--- tests/test_common.py | 23 ++++++++++++++++++++++- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/docs/pages/config.rst b/docs/pages/config.rst index f2469ed..d3198f6 100644 --- a/docs/pages/config.rst +++ b/docs/pages/config.rst @@ -6,12 +6,14 @@ The tools mostly share the same configuration methods. The following priority is given to options: #. Command line arguments -#. Sections of config file ``tctools.toml`` -#. Sections of general config file ``pyproject.toml`` +#. Config file ``tctools.toml`` +#. General config file ``pyproject.toml`` #. Extra config files (e.g. ``.editorconfig``) -In case options are available in multiple files, only the section with the highest priority is considered. -I.e. options are not merged down to the smallest level. +In case options are available in multiple files, only the one with the highest priority is considered. +I.e. options are not merged across levels: having entries in both ``tctools.toml`` and in ``pyproject.toml`` makes the ones in ``pyproject.toml`` be ignored. + +The exception is for the :ref:`auto_formatter`, where the ``.editorconfig`` file(s) are still always considered. Recommended =========== @@ -36,3 +38,4 @@ Add options under a section: Substitute ``tool`` for a specific tool and ``option`` and ``"value"`` for meaningful entries. See pages about each tool for available options. +The options are named the same as the command line arguments, except with any dashes (``-``) replaced by underscores (``_``). diff --git a/src/tctools/common.py b/src/tctools/common.py index 26b600d..2476e0f 100644 --- a/src/tctools/common.py +++ b/src/tctools/common.py @@ -45,6 +45,8 @@ def __init__(self, *args): # Pre-create the arguments output and insert any configured values: self.args = Namespace() + self.config_file: Optional[Path] = None + config = self.make_config() if self.CONFIG_KEY: config = config.get(self.CONFIG_KEY, {}) @@ -58,8 +60,13 @@ def __init__(self, *args): # Now parse CLI options on top of file configurations: parser.parse_args(args, self.args) + # Only after parsing all arguments we can establish a logger, self.logger = self.get_logger() + if self.config_file: + # Use property for config file path so we can still log it now: + self.logger.debug(f"Loading from config: {self.config_file}") + @classmethod def get_argument_parser(cls) -> ArgumentParser: parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) @@ -87,13 +94,13 @@ def set_arguments(cls, parser): def make_config(self) -> Dict[str, Any]: """Get configuration from possible files.""" config = {} - config_file = self._find_files_upwards( + self.config_file = self._find_files_upwards( Path.cwd(), ["tctools.toml", "pyproject.toml"] ) - if not config_file: + if not self.config_file: return config - with open(config_file, "rb") as fh: + with open(self.config_file, "rb") as fh: data = tomllib.load(fh) if "tctools" in data: config = data["tctools"] diff --git a/tests/test_common.py b/tests/test_common.py index 00c5214..dc6170a 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -51,4 +51,25 @@ def test_config_file(self, tmp_path, monkeypatch): tool = MyTool() assert tool.args.my_option == "xyz123" - # TODO: Add test for section priority + def test_config_file_priority(self, tmp_path, monkeypatch): + conf_dir = tmp_path / "project" + work_dir = conf_dir / "subdir1" / "subdir2" + work_dir.mkdir(parents=True) + + conf_file2 = conf_dir / "pyproject.toml" + conf_file2.write_text( + """[tctools.dummy] +my_option = "xyz123" +""" + ) + conf_file = conf_dir / "tctools.toml" + conf_file.write_text( + """[tctools.dummy] +my_option = "abc987" +""" + ) + + monkeypatch.chdir(work_dir) + + tool = MyTool() + assert tool.args.my_option == "abc987"