Skip to content

Commit

Permalink
Fix #321: use correct exit codes. (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
jendrikseipp committed Aug 21, 2023
1 parent d66f943 commit 5b87da5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
7 changes: 4 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
_parse_args,
_parse_toml,
make_config,
InputError,
)


Expand Down Expand Up @@ -173,7 +174,7 @@ def test_invalid_config_options_output():
If the config file contains unknown options we want to abort.
"""

with pytest.raises(SystemExit):
with pytest.raises(InputError):
_check_input_config({"unknown_key_1": 1})


Expand All @@ -185,13 +186,13 @@ def test_incompatible_option_type(key, value):
wrong_types = {int, str, list, bool} - {type(value)}
for wrong_type in wrong_types:
test_value = wrong_type()
with pytest.raises(SystemExit):
with pytest.raises(InputError):
_check_input_config({key: test_value})


def test_missing_paths():
"""
If the script is run without any paths, we want to abort.
"""
with pytest.raises(SystemExit):
with pytest.raises(InputError):
make_config([])
4 changes: 4 additions & 0 deletions tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ def test_make_whitelist():
assert (
call_vulture(["vulture/", "--make-whitelist"]) == ExitCode.NoDeadCode
)


def test_version():
assert call_vulture(["--version"]) == ExitCode.NoDeadCode
26 changes: 14 additions & 12 deletions vulture/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
"""
import argparse
import pathlib
import sys

import toml

from .version import __version__
from vulture.utils import ExitCode

#: Possible configuration options and their respective defaults
DEFAULTS = {
Expand All @@ -24,30 +22,35 @@
}


class InputError(Exception):
def __init__(self, message):
self.message = message


def _check_input_config(data):
"""
Checks the types of the values in *data* against the expected types of
config-values. If a value is of the wrong type it will raise a SystemExit.
config-values. If a value has the wrong type, raise an InputError.
"""
for key, value in data.items():
if key not in DEFAULTS:
sys.exit(f"Unknown configuration key: {key}")
raise InputError(f"Unknown configuration key: {key}")
# The linter suggests to use "isinstance" here but this fails to
# detect the difference between `int` and `bool`.
if type(value) is not type(DEFAULTS[key]): # noqa: E721
expected_type = type(DEFAULTS[key]).__name__
sys.exit(f"Data type for {key} must be {expected_type!r}")
raise InputError(f"Data type for {key} must be {expected_type!r}")


def _check_output_config(config):
"""
Run sanity checks on the generated config after all parsing and
preprocessing is done.
Exit the application if an error is encountered.
Raise InputError if an error is encountered.
"""
if not config["paths"]:
sys.exit("Please pass at least one file or directory")
raise InputError("Please pass at least one file or directory")


def _parse_toml(infile):
Expand Down Expand Up @@ -177,6 +180,10 @@ def make_config(argv=None, tomlfile=None):
auto-detect an existing ``pyproject.toml`` file and exists solely for
unit-testing.
"""

# Parse CLI first to skip sanity checks when --version or --help is given.
cli_config = _parse_args(argv)

# If we loaded data from a TOML file, we want to print this out on stdout
# in verbose mode so we need to keep the value around.
detected_toml_path = ""
Expand All @@ -193,11 +200,6 @@ def make_config(argv=None, tomlfile=None):
else:
config = {}

try:
cli_config = _parse_args(argv)
except SystemExit as e:
raise SystemExit(ExitCode.InvalidCmdlineArguments) from e

# Overwrite TOML options with CLI options, if given.
config.update(cli_config)

Expand Down
9 changes: 7 additions & 2 deletions vulture/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from vulture import lines
from vulture import noqa
from vulture import utils
from vulture.config import make_config
from vulture.config import InputError, make_config
from vulture.utils import ExitCode


Expand Down Expand Up @@ -727,7 +727,12 @@ def generic_visit(self, node):


def main():
config = make_config()
try:
config = make_config()
except InputError as e:
print(e, file=sys.stderr)
sys.exit(ExitCode.InvalidCmdlineArguments)

vulture = Vulture(
verbose=config["verbose"],
ignore_names=config["ignore_names"],
Expand Down

0 comments on commit 5b87da5

Please sign in to comment.