Skip to content

Commit

Permalink
Inform if no custom dir when requesting to edit
Browse files Browse the repository at this point in the history
Add checks to inform the user and do nothing if they request to edit
custom examples but no directory has been set.
  • Loading branch information
srsudar committed Jan 23, 2017
1 parent 7ccb0de commit 13dfd23
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
25 changes: 25 additions & 0 deletions eg/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,36 @@
ALIAS_FILE_NAME = 'aliases.json'


def _inform_cannot_edit_no_custom_dir():
"""
Inform the user that a custom dir has not be specified, and thus an edit
cannot be performed.
"""
msg = """
You've requested to edit custom examples for a command, but no custom
directory has been found. The value must be set and the directory must
exist.
Custom examples are shown before the default examples and are under your
control. You must specify where to save them before editing them. This can
be done at the command line (with the `-c` or `--custom-dir` flags) or via
the `custom-dir` option in your egrc. A minimal egrc might look like:
[eg-config]
custom-dir = ~/.eg/
"""
print(msg)


def edit_custom_examples(program, config):
"""
Edit custom examples for the given program, creating the file if it does
not exist.
"""
if (not config.custom_dir) or (not os.path.exists(config.custom_dir)):
_inform_cannot_edit_no_custom_dir()
return

# resolve aliases
resolved_program = get_resolved_program(program, config)
custom_file_path = get_file_path_for_program(
Expand Down
58 changes: 51 additions & 7 deletions test/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ def test_handle_program_no_entries():

def test_handle_program_finds_paths_and_calls_open_pager_no_alias():
"""
If there are entries for the program, handle_program needs to get the paths,
get the contents, format the contents, and page the resulting string.
If there are entries for the program, handle_program needs to get the
paths, get the contents, format the contents, and page the resulting
string.
"""
program = 'mv'

Expand Down Expand Up @@ -378,8 +379,9 @@ def return_correct_path(*args, **kwargs):

def test_handle_program_finds_paths_and_calls_open_pager_with_alias():
"""
If there are entries for the program, handle_program needs to get the paths,
get the contents, format the contents, and page the resulting string.
If there are entries for the program, handle_program needs to get the
paths, get the contents, format the contents, and page the resulting
string.
"""
alias_for_program = 'link'
resolved_program = 'ln'
Expand Down Expand Up @@ -853,8 +855,8 @@ def _helper_assert_formatted_contents(
formatted_result
):
"""
Helper method to assist in asserting things about the get_formatted_contents
method.
Helper method to assist in asserting things about the
get_formatted_contents method.
starting_contents: the starting string that we are working with
use_color: True if we should use color
Expand Down Expand Up @@ -1276,13 +1278,17 @@ def test_can_parse_alias_file():
assert_equal(alias_dict['link'], 'ln')


@patch('os.path.exists')
@patch('eg.util._inform_cannot_edit_no_custom_dir')
@patch('eg.util.get_resolved_program')
@patch('eg.util.get_file_path_for_program')
@patch('subprocess.call')
def test_edit_custom_examples_correct(
def test_edit_custom_examples_correct_with_custom_dir(
mock_call,
mock_get_path,
mock_get_program,
mock_inform,
mock_exists,
):
"""
We should resolve aliases, get the custom file path, and call subprocess.
Expand All @@ -1294,9 +1300,47 @@ def test_edit_custom_examples_correct(

mock_get_program.return_value = resolved_program
mock_get_path.return_value = path
mock_exists.return_value = True

util.edit_custom_examples(program, config)

mock_get_program.assert_called_once_with(program, config)
mock_get_path.assert_called_once_with(resolved_program, config.custom_dir)
mock_call.assert_called_once_with([config.editor_cmd, path])
assert_equal(mock_inform.call_count, 0)


@patch('os.path.exists')
@patch('eg.util._inform_cannot_edit_no_custom_dir')
@patch('eg.util.get_resolved_program')
@patch('eg.util.get_file_path_for_program')
@patch('subprocess.call')
def test_edit_custom_examples_informs_if_no_custom_dir(
mock_call,
mock_get_path,
mock_get_program,
mock_inform,
mock_exists,
):
"""
We should inform the user if they are trying to edit with no custom dir.
This should be true if it is not set and if the path does not exist.
"""
program = 'awk'

# First with no custom dir set.
config = _create_config(editor_cmd='vi -e')
mock_exists.return_value = True
util.edit_custom_examples(program, config)
assert_equal(mock_inform.call_count, 1)

# And now with it set but a nonexistent path.
config = _create_config(custom_dir='/path/to/custom', editor_cmd='vi -e')
mock_exists.return_value = False
util.edit_custom_examples(program, config)
assert_equal(mock_inform.call_count, 2)

assert_equal(mock_call.call_count, 0)
assert_equal(mock_get_path.call_count, 0)
assert_equal(mock_get_program.call_count, 0)

0 comments on commit 13dfd23

Please sign in to comment.