Skip to content

Commit

Permalink
Add --version option to show deadcode version
Browse files Browse the repository at this point in the history
  • Loading branch information
albertas committed Aug 9, 2024
1 parent 911a22b commit c8fb49c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
8 changes: 2 additions & 6 deletions deadcode/actions/parse_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
else:
import tomli as tomllib

from deadcode import __version__
from deadcode.data_types import Args
from deadcode.utils.flatten_lists import flatten_lists_of_comma_separated_values


def parse_arguments(args: Optional[List[str]]) -> tuple[Args | None, str | None]:
def parse_arguments(args: Optional[List[str]]) -> Args:
"""Parses arguments (execution options) for deadcode tool.
Arguments for DeadCode can be provided via:
Expand Down Expand Up @@ -204,9 +203,6 @@ def parse_arguments(args: Optional[List[str]]) -> tuple[Args | None, str | None]

parsed_args = parser.parse_args(args).__dict__

if parsed_args['version']:
return None, __version__

for arg_name, arg_value in parsed_args.items():
if isinstance(arg_value, list) and arg_name != 'paths':
parsed_args[arg_name] = flatten_lists_of_comma_separated_values(parsed_args.get(arg_name))
Expand All @@ -220,7 +216,7 @@ def parse_arguments(args: Optional[List[str]]) -> tuple[Args | None, str | None]
if parsed_args['dry']:
parsed_args['fix'] = False

return Args(**parsed_args), None
return Args(**parsed_args)


def parse_pyproject_toml() -> Dict[str, Any]:
Expand Down
9 changes: 6 additions & 3 deletions deadcode/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Optional

from deadcode import __version__
from deadcode.actions.find_python_filenames import find_python_filenames
from deadcode.actions.find_unused_names import find_unused_names
from deadcode.actions.fix_or_show_unused_code import fix_or_show_unused_code
Expand All @@ -12,9 +13,11 @@
def main(
command_line_args: Optional[List[str]] = None,
) -> Optional[str]:
args, message = parse_arguments(command_line_args)
if not args:
return message

if command_line_args and '--version' in command_line_args:
return __version__

args = parse_arguments(command_line_args)

filenames = find_python_filenames(args=args)

Expand Down
26 changes: 13 additions & 13 deletions tests/cli_args/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,72 +21,72 @@ def setUp(self):

def test_calling_with_one_paths_argument(self):
options = '.'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.paths, ['.'])
self.assertEqual(args.exclude, [])
self.assertEqual(args.ignore_names, [])
self.assertEqual(args.ignore_names_in_files, [])

def test_calling_with_several_paths_argument(self):
options = '. tests'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.paths, ['.', 'tests'])
self.assertEqual(args.exclude, [])
self.assertEqual(args.ignore_names, [])
self.assertEqual(args.ignore_names_in_files, [])

def test_calling_with_exclude(self):
options = '. --exclude=tests,venv'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.paths, ['.'])
self.assertEqual(args.exclude, ['tests', 'venv'])
self.assertEqual(args.ignore_names, [])
self.assertEqual(args.ignore_names_in_files, [])

def test_calling_with_several_exclude_options(self):
options = '. --exclude=tests,venv --exclude=migrations'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.paths, ['.'])
self.assertEqual(args.exclude, ['tests', 'venv', 'migrations'])
self.assertEqual(args.ignore_names, [])
self.assertEqual(args.ignore_names_in_files, [])

def test_calling_with_no_color_option(self):
options = '. --no-color'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.paths, ['.'])
self.assertEqual(args.no_color, True)

def test_ignore_names_and_ignore_files_command_line_argument_parsing(self):
options = '. --ignore-names-in-files=tests,venv --ignore-names=BaseTestCase,lambda_handler'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.paths, ['.'])
self.assertEqual(args.exclude, [])
self.assertEqual(args.ignore_names, ['BaseTestCase', 'lambda_handler'])
self.assertEqual(args.ignore_names_in_files, ['tests', 'venv'])

def test_verbose_flag_is_on_using_long_name(self):
options = '--verbose .'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.verbose, True)
self.assertEqual(args.paths, ['.'])

def test_verbose_flag_is_on_using_short_name(self):
options = '. -v'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.paths, ['.'])
self.assertEqual(args.verbose, True)

def test_calling_with_fix(self):
options = '. --fix'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.paths, ['.'])
self.assertEqual(args.dry, False)
self.assertEqual(args.fix, True)

def test_calling_with_dry(self):
options = '. --dry --verbose'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.paths, ['.'])
self.assertEqual(args.dry, True)
self.assertEqual(args.only, [])
Expand All @@ -95,22 +95,22 @@ def test_calling_with_dry(self):

def test_calling_with_fix_and_dry(self):
options = '. --dry --fix'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.dry, True)
self.assertEqual(args.fix, False)
self.assertEqual(args.only, [])

def test_calling_with_dry_and_single_only_filename(self):
options = '. --dry --only foo.py --fix'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.paths, ['.'])
self.assertEqual(args.dry, True)
self.assertEqual(args.only, ['foo.py'])
self.assertEqual(args.fix, False)

def test_calling_with_two_dry_filenames(self):
options = '. --fix --dry --only foo.py bar.py'
args, _ = parse_arguments(options.split())
args = parse_arguments(options.split())
self.assertEqual(args.paths, ['.'])
self.assertEqual(args.only, ['foo.py', 'bar.py'])
self.assertEqual(args.fix, False)
Expand Down
20 changes: 19 additions & 1 deletion tests/cli_args/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@


class TestVersionCliOption(BaseTestCase):
def test_version_shown(self):
def test_show_version(self):
self.files = {
'foo.py': b"""
class UnusedClass:
pass
print("Dont change this file")""",
'bar.py': b"""
def unused_function():
pass
print("Dont change this file")""",
}

result = main('--version'.split())

self.assertEqual(result, __version__)

def test_show_version_when_path_provided(self):
self.files = {
'foo.py': b"""
class UnusedClass:
Expand Down

0 comments on commit c8fb49c

Please sign in to comment.