Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #62 from Yelp/refactors-argv
Browse files Browse the repository at this point in the history
Refactors to avoid patching argv in tests
  • Loading branch information
ajm188 committed Oct 14, 2016
2 parents 7fa6d4e + 15993fb commit f53d9c4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
17 changes: 5 additions & 12 deletions tests/cmd/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,19 @@ def test_process_file_raises_exception(mock_process, mock_load_text):
def test_no_file(mock_process, mock_load_patterns):
fake_patterns = mock.MagicMock()
mock_load_patterns.return_value = fake_patterns
args = ['undebt', '-p', 'blah']
with mock.patch('sys.argv', args):
main.main()
args = ['-p', 'blah']
main.main(args)
mock_process.assert_called_once_with(fake_patterns, None, False)


def test_single_file():
args = [
"undebt",
"-p",
method_to_function.__name__,
method_to_function_input_path,
"--verbose",
]
with mock.patch("sys.argv", args):
main.main()
main.main(args)
assert (
_read_input_file() ==
method_to_function_output_contents ==
Expand All @@ -151,14 +148,12 @@ def test_single_file():

def test_loading_pattern_with_module_name():
args = [
"undebt",
"-p",
method_to_function.__name__,
method_to_function_input_path,
"--verbose",
]
with mock.patch("sys.argv", args):
main.main()
main.main(args)
assert (
_read_input_file() ==
method_to_function_output_contents ==
Expand All @@ -168,15 +163,13 @@ def test_loading_pattern_with_module_name():

def test_dry_run(capsys):
args = [
"undebt",
"-p",
method_to_function.__name__,
"--dry-run",
method_to_function_input_path,
"--verbose",
]
with mock.patch("sys.argv", args):
main.main()
main.main(args)
out, err = capsys.readouterr()
assert err == '>>> {}\n'.format(method_to_function_input_path)
assert out == method_to_function_output_contents
Expand Down
10 changes: 5 additions & 5 deletions undebt/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _write_result_text(result_text, path, dry_run):


@_exit_fail_upon_error
def _handle_arguments():
def _handle_arguments(args):
parser = argparse.ArgumentParser(prog='undebt')
parser.add_argument(
'files', nargs='*', metavar='FILE',
Expand All @@ -69,7 +69,7 @@ def _handle_arguments():
'--dry-run', '-d', action='store_true', default=False,
help='only print to stdout; do not overwrite files',
)
return parser.parse_args()
return parser.parse_args(args)


@_exit_fail_upon_error
Expand All @@ -93,9 +93,9 @@ def process(patterns, text_file, dry_run):
return True


def main():
"""Handle and process arguments from sys.argv."""
args = _handle_arguments()
def main(args=sys.argv[1:]):
"""Handle and process arguments from args."""
args = _handle_arguments(args)

logger.setup(args.verbose)
patterns = load_patterns(args.pattern)
Expand Down

0 comments on commit f53d9c4

Please sign in to comment.