From 9014a944cee1b55447f2944083a3ee600b549c8e Mon Sep 17 00:00:00 2001 From: Glen Robertson Date: Tue, 19 Mar 2024 03:00:35 -0400 Subject: [PATCH] Add --config parameter to specify pyproject.toml path (#352) If you have a specific path to pyproject.toml in your project (e.g. not in the root), this allows Vulture to read a config from that path. --------- Co-authored-by: Jendrik Seipp --- CHANGELOG.md | 1 + README.md | 4 ++++ tests/test_config.py | 20 ++++++++++++++++++++ tests/toml/mock_pyproject.toml | 5 +++++ vulture/config.py | 9 ++++++++- 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/toml/mock_pyproject.toml diff --git a/CHANGELOG.md b/CHANGELOG.md index 39ebcbf1..a4c4a9f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Use `ruff` for linting (Anh Trinh, #347). * Use `ruff` for formatting (Anh Trinh, #349). * Replace `tox` by `pre-commit` for linting and formatting (Anh Trinh, #349). +* Add `--config` flag to specify path to pyproject.toml configuration file (Glen Robertson #352). # 2.11 (2024-01-06) diff --git a/README.md b/README.md index 99b2ec81..7275f6cd 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,10 @@ sort_by_size = true verbose = true ``` +Vulture will automatically look for a `pyproject.toml` in the current working directory. + +To use a `pyproject.toml` in another directory, you can use the `--config path/to/pyproject.toml` flag. + ## Version control integration You can use a [pre-commit](https://pre-commit.com/#install) hook to run diff --git a/tests/test_config.py b/tests/test_config.py index 04ce7259..2d55612b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -3,6 +3,7 @@ """ from io import BytesIO +import pathlib from textwrap import dedent import pytest @@ -33,6 +34,7 @@ def test_cli_args(): exclude=["file*.py", "dir/"], ignore_decorators=["deco1", "deco2"], ignore_names=["name1", "name2"], + config="pyproject.toml", make_whitelist=True, min_confidence=10, sort_by_size=True, @@ -164,6 +166,7 @@ def test_config_merging(): exclude=["cli_exclude"], ignore_decorators=["cli_deco"], ignore_names=["cli_name"], + config="pyproject.toml", make_whitelist=True, min_confidence=20, sort_by_size=True, @@ -172,6 +175,23 @@ def test_config_merging(): assert result == expected +def test_toml_config_custom_path(): + """ + Ensure that TOML pyproject.toml files can be read from a custom path, + other than the current working directory. + + Test file is in tests/toml/mock_pyproject.toml + """ + here = pathlib.Path(__file__).parent + tomlfile_path = here.joinpath("toml", "mock_pyproject.toml") + cliargs = [ + f"--config={tomlfile_path}", + "cli_path", + ] + result = make_config(cliargs) + assert result["ignore_names"] == ["name_from_toml_file"] + + def test_config_merging_missing(): """ If we have set a boolean value in the TOML file, but not on the CLI, we diff --git a/tests/toml/mock_pyproject.toml b/tests/toml/mock_pyproject.toml new file mode 100644 index 00000000..b26a6d55 --- /dev/null +++ b/tests/toml/mock_pyproject.toml @@ -0,0 +1,5 @@ +# This file exists for the test case: test_config::test_toml_config_custom_path + +[tool.vulture] +verbose = true +ignore_names = ["name_from_toml_file"] diff --git a/vulture/config.py b/vulture/config.py index 4e193fe2..87dd5739 100644 --- a/vulture/config.py +++ b/vulture/config.py @@ -14,6 +14,7 @@ #: Possible configuration options and their respective defaults DEFAULTS = { + "config": "pyproject.toml", "min_confidence": 0, "paths": [], "exclude": [], @@ -158,6 +159,12 @@ def csv(exclude): default=missing, help="Sort unused functions and classes by their lines of code.", ) + parser.add_argument( + "--config", + type=str, + default="pyproject.toml", + help="Path to pyproject.toml config file.", + ) parser.add_argument( "-v", "--verbose", action="store_true", default=missing ) @@ -195,7 +202,7 @@ def make_config(argv=None, tomlfile=None): config = _parse_toml(tomlfile) detected_toml_path = str(tomlfile) else: - toml_path = pathlib.Path("pyproject.toml").resolve() + toml_path = pathlib.Path(cli_config["config"]).resolve() if toml_path.is_file(): with open(toml_path, "rb") as fconfig: config = _parse_toml(fconfig)