Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --config parameter to specify pyproject.toml path #352

Merged
merged 12 commits into from
Mar 19, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from io import BytesIO
import pathlib
from textwrap import dedent

import pytest
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/toml/mock_pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"]
9 changes: 8 additions & 1 deletion vulture/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#: Possible configuration options and their respective defaults
DEFAULTS = {
"config": "pyproject.toml",
"min_confidence": 0,
"paths": [],
"exclude": [],
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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)
Expand Down
Loading