From 1f38ce9d36a7c320037784c442a72ffc0118d63c Mon Sep 17 00:00:00 2001 From: dumbturtle Date: Wed, 10 Jun 2020 07:46:25 +0300 Subject: [PATCH 1/2] Chsnge argparse to click. Add test for testing file and directory. Branch issue/#10 Changes to be committed: modified: mr_proper/main.py new file: tests/test_check_file_or_directory.py --- mr_proper/main.py | 27 ++++++++------------------- tests/test_check_file_or_directory.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 tests/test_check_file_or_directory.py diff --git a/mr_proper/main.py b/mr_proper/main.py index a714fd1..786e8c5 100644 --- a/mr_proper/main.py +++ b/mr_proper/main.py @@ -1,18 +1,11 @@ +import click import os import sys -import argparse from mr_proper.public_api import is_function_pure from mr_proper.utils.ast import get_ast_tree, get_all_funcdefs_from -def parse_args() -> argparse.Namespace: - parser = argparse.ArgumentParser() - parser.add_argument('file_or_directory', type=str) - parser.add_argument('--recursive', action='store_true') - return parser.parse_args() - - def check_file(path_to_file: str, recursive: bool) -> None: ast_tree = get_ast_tree(path_to_file) if not ast_tree: @@ -36,17 +29,13 @@ def check_file(path_to_file: str, recursive: bool) -> None: sys.stdout.write(f'\t{error}\n') -def main() -> None: - recursive = False - args = parse_args() - if not os.path.exists(args.file_or_directory): - sys.stdout.write('File or directory not exist!\n') - return - path_to_files = [args.file_or_directory] - if args.recursive: - recursive = args.recursive - if os.path.isdir(args.file_or_directory): - directoty_path = os.path.abspath(args.file_or_directory) +@click.command() +@click.option('--recursive', is_flag=True, help='Recursive') +@click.argument('file_or_directory', type=click.Path(exists=True)) +def main(file_or_directory: str, recursive: bool) -> None: + path_to_files = [file_or_directory] + if os.path.isdir(file_or_directory): + directoty_path = os.path.abspath(file_or_directory) path_to_files = [ os.path.join(root, filename) for root, dirs, files in os.walk(directoty_path) for filename in files ] diff --git a/tests/test_check_file_or_directory.py b/tests/test_check_file_or_directory.py new file mode 100644 index 0000000..db2b31c --- /dev/null +++ b/tests/test_check_file_or_directory.py @@ -0,0 +1,19 @@ +import pytest + +from click.testing import CliRunner +from mr_proper.main import main + + +@pytest.mark.parametrize( + 'file_or_directory_path, check_result', + [ + ('/test/test_files/dir_not_exist/', 'does not exist'), + ('/test/test_files/file_not_exist.py', 'does not exist'), + ('tests/test_files/test.py', 'tests/test_files/test.py:' and 'foo is pure!'), + ('tests/test_files/errored.py', 'tests/test_files/errored.py:' and 'Error parsing ast tree'), + ], +) +def test_is_not_exist(file_or_directory_path, check_result): + runner = CliRunner() + result = runner.invoke(main, file_or_directory_path) + assert check_result in result.output From e7897e80d4d0a3c99ba1427608fc42d8864217d6 Mon Sep 17 00:00:00 2001 From: DumbTurtle Date: Wed, 10 Jun 2020 08:11:52 +0300 Subject: [PATCH 2/2] Chsnge argparse to click. Add test for testing file and directory. Branch issue/#10 Changes to be committed: modified: mr_proper/main.py new file: tests/test_check_file_or_directory.py --- mr_proper/main.py | 27 ++++++++------------------- tests/test_check_file_or_directory.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 tests/test_check_file_or_directory.py diff --git a/mr_proper/main.py b/mr_proper/main.py index a714fd1..786e8c5 100644 --- a/mr_proper/main.py +++ b/mr_proper/main.py @@ -1,18 +1,11 @@ +import click import os import sys -import argparse from mr_proper.public_api import is_function_pure from mr_proper.utils.ast import get_ast_tree, get_all_funcdefs_from -def parse_args() -> argparse.Namespace: - parser = argparse.ArgumentParser() - parser.add_argument('file_or_directory', type=str) - parser.add_argument('--recursive', action='store_true') - return parser.parse_args() - - def check_file(path_to_file: str, recursive: bool) -> None: ast_tree = get_ast_tree(path_to_file) if not ast_tree: @@ -36,17 +29,13 @@ def check_file(path_to_file: str, recursive: bool) -> None: sys.stdout.write(f'\t{error}\n') -def main() -> None: - recursive = False - args = parse_args() - if not os.path.exists(args.file_or_directory): - sys.stdout.write('File or directory not exist!\n') - return - path_to_files = [args.file_or_directory] - if args.recursive: - recursive = args.recursive - if os.path.isdir(args.file_or_directory): - directoty_path = os.path.abspath(args.file_or_directory) +@click.command() +@click.option('--recursive', is_flag=True, help='Recursive') +@click.argument('file_or_directory', type=click.Path(exists=True)) +def main(file_or_directory: str, recursive: bool) -> None: + path_to_files = [file_or_directory] + if os.path.isdir(file_or_directory): + directoty_path = os.path.abspath(file_or_directory) path_to_files = [ os.path.join(root, filename) for root, dirs, files in os.walk(directoty_path) for filename in files ] diff --git a/tests/test_check_file_or_directory.py b/tests/test_check_file_or_directory.py new file mode 100644 index 0000000..90ef8a7 --- /dev/null +++ b/tests/test_check_file_or_directory.py @@ -0,0 +1,19 @@ +import pytest + +from click.testing import CliRunner +from mr_proper.main import main + + +@pytest.mark.parametrize( + 'file_or_directory_path, check_result', + [ + ('/test/test_files/dir_not_exist/', 'does not exist'), + ('/test/test_files/file_not_exist.py', 'does not exist'), + ('tests/test_files/test.py', 'tests/test_files/test.py:' and 'foo is pure!'), + ('tests/test_files/errored.py', 'tests/test_files/errored.py:' and 'Error parsing ast tree'), + ], +) +def test_is_not_exist(file_or_directory_path, check_result): + runner = CliRunner() + test_result = runner.invoke(main, file_or_directory_path) + assert check_result in test_result.output