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 dedicated exit code for when we find dead code #319

Merged
merged 5 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 2.9 (unreleased)
* Use exit code 3 when dead code is found (whosayn, #319).

# 2.8 (2023-08-10)

* Add `UnicodeEncodeError` exception handling to `core.py` (milanbalazs, #299).
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ codes.
| Exit code | Description |
| --------- | ------------------------------------------------------------- |
| 0 | No dead code found |
| 1 | Dead code found |
| 1 | Invalid input (file missing, syntax error, wrong encoding) |
| 2 | Invalid command line arguments |
| 3 | Dead code found |

## Similar programs

Expand Down
9 changes: 5 additions & 4 deletions tests/test_encoding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import codecs

from . import v
from vulture.utils import ExitCode

assert v # Silence pyflakes.

Expand All @@ -12,7 +13,7 @@ def test_encoding1(v):
pass
"""
)
assert not v.found_dead_code_or_error
assert v.exit_code == ExitCode.NoDeadCode


def test_encoding2(v):
Expand All @@ -23,7 +24,7 @@ def test_encoding2(v):
pass
"""
)
assert not v.found_dead_code_or_error
assert v.exit_code == ExitCode.NoDeadCode


def test_non_utf8_encoding(v, tmp_path):
Expand All @@ -34,7 +35,7 @@ def test_non_utf8_encoding(v, tmp_path):
f.write(codecs.BOM_UTF16_LE)
f.write(code.encode("utf_16_le"))
v.scavenge([non_utf_8_file])
assert v.found_dead_code_or_error
assert v.exit_code == ExitCode.InvalidInput


def test_utf8_with_bom(v, tmp_path):
Expand All @@ -43,4 +44,4 @@ def test_utf8_with_bom(v, tmp_path):
# utf8_sig prepends the BOM to the file.
filepath.write_text("", encoding="utf-8-sig")
v.scavenge([filepath])
assert not v.found_dead_code_or_error
assert v.exit_code == ExitCode.NoDeadCode
14 changes: 11 additions & 3 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import pytest

from . import v
from . import v, call_vulture
from vulture.utils import ExitCode

assert v # Silence pyflakes.


def test_syntax_error(v):
v.scan("foo bar")
assert int(v.report()) == 1
assert int(v.report()) == ExitCode.InvalidInput


def test_null_byte(v):
v.scan("\x00")
assert int(v.report()) == 1
assert int(v.report()) == ExitCode.InvalidInput


def test_confidence_range(v):
Expand All @@ -24,3 +25,10 @@ def foo():
)
with pytest.raises(ValueError):
v.get_unused_code(min_confidence=150)


def test_invalid_cmdline_args():
assert (
call_vulture(["vulture/", "--invalid-argument"])
== ExitCode.InvalidCmdlineArguments
)
7 changes: 4 additions & 3 deletions tests/test_scavenging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from . import check, v
from vulture.utils import ExitCode

assert v # Silence pyflakes.

Expand Down Expand Up @@ -765,7 +766,7 @@ def __init__(self):
check(v.unused_imports, ["Any", "Dict", "List", "Text", "Tuple"])
else:
check(v.unused_imports, [])
assert not v.found_dead_code_or_error
assert v.exit_code == ExitCode.NoDeadCode


def test_invalid_type_comment(v):
Expand All @@ -779,9 +780,9 @@ def bad():
)

if sys.version_info < (3, 8):
assert not v.found_dead_code_or_error
assert v.exit_code == ExitCode.NoDeadCode
else:
assert v.found_dead_code_or_error
assert v.exit_code == ExitCode.InvalidInput


def test_unused_args_with_del(v):
Expand Down
42 changes: 30 additions & 12 deletions tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,49 @@
import sys

from . import call_vulture, REPO, WHITELISTS
from vulture.utils import ExitCode


def test_module_with_explicit_whitelists():
assert call_vulture(["vulture/"] + WHITELISTS) == 0
assert call_vulture(["vulture/"] + WHITELISTS) == ExitCode.NoDeadCode


def test_module_with_implicit_whitelists():
assert call_vulture(["vulture/"]) == 0
assert call_vulture(["vulture/"]) == ExitCode.NoDeadCode


def test_module_without_whitelists():
assert call_vulture(["vulture/", "--exclude", "whitelists"]) == 1
assert (
call_vulture(["vulture/", "--exclude", "whitelists"])
== ExitCode.DeadCode
)


def test_missing_file():
assert call_vulture(["missing.py"]) == 1
assert call_vulture(["missing.py"]) == ExitCode.InvalidInput


def test_tests():
assert call_vulture(["tests/"]) == 0
assert call_vulture(["tests/"]) == ExitCode.NoDeadCode


def test_whitelists_with_python():
for whitelist in WHITELISTS:
assert subprocess.call([sys.executable, whitelist], cwd=REPO) == 0
assert (
subprocess.call([sys.executable, whitelist], cwd=REPO)
== ExitCode.NoDeadCode
)


def test_pyc():
assert call_vulture(["missing.pyc"]) == 1


def test_sort_by_size():
assert call_vulture(["vulture/utils.py", "--sort-by-size"]) == 1
assert (
call_vulture(["vulture/utils.py", "--sort-by-size"])
== ExitCode.DeadCode
)


def test_min_confidence():
Expand All @@ -50,7 +60,7 @@ def test_min_confidence():
"100",
]
)
== 0
== ExitCode.NoDeadCode
)


Expand All @@ -61,15 +71,23 @@ def get_csv(paths):
def call_vulture_with_excludes(excludes):
return call_vulture(["vulture/", "--exclude", get_csv(excludes)])

assert call_vulture_with_excludes(["core.py", "utils.py"]) == 1
assert call_vulture_with_excludes(glob.glob("vulture/*.py")) == 0
assert (
call_vulture_with_excludes(["core.py", "utils.py"])
== ExitCode.DeadCode
)
assert (
call_vulture_with_excludes(glob.glob("vulture/*.py"))
== ExitCode.NoDeadCode
)


def test_make_whitelist():
assert (
call_vulture(
["vulture/", "--make-whitelist", "--exclude", "whitelists"]
)
== 1
== ExitCode.DeadCode
)
assert (
call_vulture(["vulture/", "--make-whitelist"]) == ExitCode.NoDeadCode
)
assert call_vulture(["vulture/", "--make-whitelist"]) == 0
6 changes: 5 additions & 1 deletion vulture/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import toml

from .version import __version__
from vulture.utils import ExitCode

#: Possible configuration options and their respective defaults
DEFAULTS = {
Expand Down Expand Up @@ -192,7 +193,10 @@ def make_config(argv=None, tomlfile=None):
else:
config = {}

cli_config = _parse_args(argv)
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
13 changes: 7 additions & 6 deletions vulture/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from vulture import noqa
from vulture import utils
from vulture.config import make_config
from vulture.utils import ExitCode


DEFAULT_CONFIDENCE = 60
Expand Down Expand Up @@ -217,7 +218,7 @@ def get_list(typ):

self.filename = Path()
self.code = []
self.found_dead_code_or_error = False
self.exit_code = ExitCode.NoDeadCode

def scan(self, code, filename=""):
filename = Path(filename)
Expand All @@ -232,7 +233,7 @@ def handle_syntax_error(e):
file=sys.stderr,
force=True,
)
self.found_dead_code_or_error = True
self.exit_code = ExitCode.InvalidInput

try:
node = (
Expand All @@ -251,7 +252,7 @@ def handle_syntax_error(e):
file=sys.stderr,
force=True,
)
self.found_dead_code_or_error = True
self.exit_code = ExitCode.InvalidInput
else:
# When parsing type comments, visiting can throw SyntaxError.
try:
Expand Down Expand Up @@ -287,7 +288,7 @@ def exclude_path(path):
file=sys.stderr,
force=True,
)
self.found_dead_code_or_error = True
self.exit_code = ExitCode.InvalidInput
else:
self.scan(module_string, filename=module)

Expand Down Expand Up @@ -353,8 +354,8 @@ def report(
else item.get_report(add_size=sort_by_size),
force=True,
)
self.found_dead_code_or_error = True
return self.found_dead_code_or_error
self.exit_code = ExitCode.DeadCode
return self.exit_code

@property
def unused_classes(self):
Expand Down
8 changes: 8 additions & 0 deletions vulture/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ast
from enum import IntEnum
import pathlib
import sys
import tokenize
Expand All @@ -8,6 +9,13 @@ class VultureInputException(Exception):
pass


class ExitCode(IntEnum):
NoDeadCode = 0
InvalidInput = 1
InvalidCmdlineArguments = 2
DeadCode = 3


def _safe_eval(node, default):
"""
Safely evaluate the Boolean expression under the given AST node.
Expand Down