1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-10-30 14:44:05 +00:00

#611: Allow to configure alias automatically by calling fuck twice

This commit is contained in:
Vladimir Iakovlev
2017-03-13 21:50:13 +01:00
parent 2379573cf2
commit 14a9cd85aa
19 changed files with 336 additions and 54 deletions

View File

@@ -20,3 +20,11 @@ def history_lines(mocker):
.return_value.readlines.return_value = lines
return aux
@pytest.fixture
def config_exists(mocker):
path_mock = mocker.patch('thefuck.shells.generic.Path')
return path_mock.return_value \
.expanduser.return_value \
.exists

View File

@@ -61,3 +61,12 @@ class TestBash(object):
command = 'git log -p'
command_parts = ['git', 'log', '-p']
assert shell.split_command(command) == command_parts
def test_how_to_configure(self, shell, config_exists):
config_exists.return_value = True
assert shell.how_to_configure().can_configure_automatically
def test_how_to_configure_when_config_not_found(self, shell,
config_exists):
config_exists.return_value = False
assert not shell.how_to_configure().can_configure_automatically

View File

@@ -95,3 +95,12 @@ class TestFish(object):
shell.put_to_history(entry)
builtins_open.return_value.__enter__.return_value. \
write.assert_called_once_with(entry_utf8)
def test_how_to_configure(self, shell, config_exists):
config_exists.return_value = True
assert shell.how_to_configure().can_configure_automatically
def test_how_to_configure_when_config_not_found(self, shell,
config_exists):
config_exists.return_value = False
assert not shell.how_to_configure().can_configure_automatically

View File

@@ -37,3 +37,6 @@ class TestGeneric(object):
def test_split_command(self, shell):
assert shell.split_command('ls') == ['ls']
assert shell.split_command(u'echo café') == [u'echo', u'café']
def test_how_to_configure(self, shell):
assert shell.how_to_configure() is None

View File

@@ -17,3 +17,6 @@ class TestPowershell(object):
assert 'function fuck' in shell.app_alias('fuck')
assert 'function FUCK' in shell.app_alias('FUCK')
assert 'thefuck' in shell.app_alias('fuck')
def test_how_to_configure(self, shell):
assert not shell.how_to_configure().can_configure_automatically

View File

@@ -48,3 +48,12 @@ class TestTcsh(object):
def test_get_history(self, history_lines, shell):
history_lines(['ls', 'rm'])
assert list(shell.get_history()) == ['ls', 'rm']
def test_how_to_configure(self, shell, config_exists):
config_exists.return_value = True
assert shell.how_to_configure().can_configure_automatically
def test_how_to_configure_when_config_not_found(self, shell,
config_exists):
config_exists.return_value = False
assert not shell.how_to_configure().can_configure_automatically

View File

@@ -55,3 +55,12 @@ class TestZsh(object):
def test_get_history(self, history_lines, shell):
history_lines([': 1432613911:0;ls', ': 1432613916:0;rm'])
assert list(shell.get_history()) == ['ls', 'rm']
def test_how_to_configure(self, shell, config_exists):
config_exists.return_value = True
assert shell.how_to_configure().can_configure_automatically
def test_how_to_configure_when_config_not_found(self, shell,
config_exists):
config_exists.return_value = False
assert not shell.how_to_configure().can_configure_automatically

View File

@@ -0,0 +1,119 @@
import pytest
from thefuck.shells.generic import ShellConfiguration
from thefuck.not_configured import main
@pytest.fixture(autouse=True)
def usage_tracker(mocker):
return mocker.patch(
'thefuck.not_configured._get_not_configured_usage_tracker_path')
def _assert_tracker_updated(usage_tracker, pid):
usage_tracker.return_value \
.open.return_value \
.__enter__.return_value \
.write.assert_called_once_with(str(pid))
def _change_tracker(usage_tracker, pid):
usage_tracker.return_value.exists.return_value = True
usage_tracker.return_value \
.open.return_value \
.__enter__.return_value \
.read.return_value = str(pid)
@pytest.fixture(autouse=True)
def shell_pid(mocker):
return mocker.patch('thefuck.not_configured._get_shell_pid')
@pytest.fixture(autouse=True)
def shell(mocker):
shell = mocker.patch('thefuck.not_configured.shell')
shell.get_history.return_value = []
shell.how_to_configure.return_value = ShellConfiguration(
content='eval $(thefuck --alias)',
path='/tmp/.bashrc',
reload='bash',
can_configure_automatically=True)
return shell
@pytest.fixture(autouse=True)
def shell_config(mocker):
path_mock = mocker.patch('thefuck.not_configured.Path')
return path_mock.return_value \
.expanduser.return_value \
.open.return_value \
.__enter__.return_value
@pytest.fixture(autouse=True)
def logs(mocker):
return mocker.patch('thefuck.not_configured.logs')
def test_for_generic_shell(shell, logs):
shell.how_to_configure.return_value = None
main()
logs.how_to_configure_alias.assert_called_once()
def test_on_first_run(usage_tracker, shell_pid, logs):
shell_pid.return_value = 12
usage_tracker.return_value.exists.return_value = False
main()
_assert_tracker_updated(usage_tracker, 12)
logs.how_to_configure_alias.assert_called_once()
def test_on_run_after_other_commands(usage_tracker, shell_pid, shell, logs):
shell_pid.return_value = 12
shell.get_history.return_value = ['fuck', 'ls']
_change_tracker(usage_tracker, 12)
main()
logs.how_to_configure_alias.assert_called_once()
def test_on_first_run_from_current_shell(usage_tracker, shell_pid,
shell, logs):
shell.get_history.return_value = ['fuck']
shell_pid.return_value = 12
_change_tracker(usage_tracker, 55)
main()
_assert_tracker_updated(usage_tracker, 12)
logs.how_to_configure_alias.assert_called_once()
def test_when_cant_configure_automatically(shell_pid, shell, logs):
shell_pid.return_value = 12
shell.how_to_configure.return_value = ShellConfiguration(
content='eval $(thefuck --alias)',
path='/tmp/.bashrc',
reload='bash',
can_configure_automatically=False)
main()
logs.how_to_configure_alias.assert_called_once()
def test_when_already_configured(usage_tracker, shell_pid,
shell, shell_config, logs):
shell.get_history.return_value = ['fuck']
shell_pid.return_value = 12
_change_tracker(usage_tracker, 12)
shell_config.read.return_value = 'eval $(thefuck --alias)'
main()
logs.already_configured.assert_called_once()
def test_when_successfuly_configured(usage_tracker, shell_pid,
shell, shell_config, logs):
shell.get_history.return_value = ['fuck']
shell_pid.return_value = 12
_change_tracker(usage_tracker, 12)
shell_config.read.return_value = ''
main()
shell_config.write.assert_any_call('eval $(thefuck --alias)')
logs.configured_successfully.assert_called_once()