1
0
mirror of https://github.com/nvbn/thefuck.git synced 2024-10-06 10:51:11 +01:00
thefuck/tests/test_conf.py

124 lines
5.0 KiB
Python
Raw Normal View History

2015-05-02 03:29:55 +01:00
import pytest
import six
2016-08-22 03:45:27 +01:00
import os
2015-05-02 03:29:55 +01:00
from mock import Mock
2016-02-22 15:40:28 +00:00
from thefuck import const
2015-05-02 03:29:55 +01:00
@pytest.fixture
2015-05-07 12:42:52 +01:00
def load_source(mocker):
return mocker.patch('thefuck.conf.load_source')
2015-05-02 03:29:55 +01:00
@pytest.fixture
def environ(monkeypatch):
data = {}
monkeypatch.setattr('thefuck.conf.os.environ', data)
return data
@pytest.mark.usefixture('environ')
2015-09-07 10:12:16 +01:00
def test_settings_defaults(load_source, settings):
2015-05-02 03:29:55 +01:00
load_source.return_value = object()
settings.init()
2016-02-22 15:40:28 +00:00
for key, val in const.DEFAULT_SETTINGS.items():
2015-09-07 10:12:16 +01:00
assert getattr(settings, key) == val
2015-05-02 03:29:55 +01:00
@pytest.mark.usefixture('environ')
class TestSettingsFromFile(object):
2015-09-07 10:12:16 +01:00
def test_from_file(self, load_source, settings):
2015-05-02 03:29:55 +01:00
load_source.return_value = Mock(rules=['test'],
wait_command=10,
require_confirmation=True,
no_colors=True,
priority={'vim': 100},
exclude_rules=['git'])
settings.init()
assert settings.rules == ['test']
assert settings.wait_command == 10
assert settings.require_confirmation is True
assert settings.no_colors is True
assert settings.priority == {'vim': 100}
assert settings.exclude_rules == ['git']
2015-09-07 10:12:16 +01:00
def test_from_file_with_DEFAULT(self, load_source, settings):
2016-02-22 15:40:28 +00:00
load_source.return_value = Mock(rules=const.DEFAULT_RULES + ['test'],
2015-05-02 03:29:55 +01:00
wait_command=10,
exclude_rules=[],
2015-05-02 03:29:55 +01:00
require_confirmation=True,
no_colors=True)
settings.init()
2016-02-22 15:40:28 +00:00
assert settings.rules == const.DEFAULT_RULES + ['test']
2015-05-02 03:29:55 +01:00
@pytest.mark.usefixture('load_source')
class TestSettingsFromEnv(object):
2015-09-07 10:12:16 +01:00
def test_from_env(self, environ, settings):
2015-05-02 03:29:55 +01:00
environ.update({'THEFUCK_RULES': 'bash:lisp',
'THEFUCK_EXCLUDE_RULES': 'git:vim',
2015-05-02 03:29:55 +01:00
'THEFUCK_WAIT_COMMAND': '55',
'THEFUCK_REQUIRE_CONFIRMATION': 'true',
'THEFUCK_NO_COLORS': 'false',
'THEFUCK_PRIORITY': 'bash=10:lisp=wrong:vim=15',
'THEFUCK_WAIT_SLOW_COMMAND': '999',
'THEFUCK_SLOW_COMMANDS': 'lein:react-native:./gradlew'})
settings.init()
assert settings.rules == ['bash', 'lisp']
assert settings.exclude_rules == ['git', 'vim']
assert settings.wait_command == 55
assert settings.require_confirmation is True
assert settings.no_colors is False
assert settings.priority == {'bash': 10, 'vim': 15}
assert settings.wait_slow_command == 999
assert settings.slow_commands == ['lein', 'react-native', './gradlew']
2015-09-07 10:12:16 +01:00
def test_from_env_with_DEFAULT(self, environ, settings):
2015-05-02 03:29:55 +01:00
environ.update({'THEFUCK_RULES': 'DEFAULT_RULES:bash:lisp'})
settings.init()
2016-02-22 15:40:28 +00:00
assert settings.rules == const.DEFAULT_RULES + ['bash', 'lisp']
2015-05-02 03:29:55 +01:00
class TestInitializeSettingsFile(object):
def test_ignore_if_exists(self, settings):
2015-05-02 03:29:55 +01:00
settings_path_mock = Mock(is_file=Mock(return_value=True), open=Mock())
settings.user_dir = Mock(joinpath=Mock(return_value=settings_path_mock))
settings._init_settings_file()
2015-05-02 03:29:55 +01:00
assert settings_path_mock.is_file.call_count == 1
assert not settings_path_mock.open.called
def test_create_if_doesnt_exists(self, settings):
2015-05-02 03:29:55 +01:00
settings_file = six.StringIO()
settings_path_mock = Mock(
is_file=Mock(return_value=False),
open=Mock(return_value=Mock(
__exit__=lambda *args: None, __enter__=lambda *args: settings_file)))
settings.user_dir = Mock(joinpath=Mock(return_value=settings_path_mock))
settings._init_settings_file()
2015-05-02 03:29:55 +01:00
settings_file_contents = settings_file.getvalue()
assert settings_path_mock.is_file.call_count == 1
assert settings_path_mock.open.call_count == 1
2016-02-22 15:40:28 +00:00
assert const.SETTINGS_HEADER in settings_file_contents
for setting in const.DEFAULT_SETTINGS.items():
2015-05-02 03:29:55 +01:00
assert '# {} = {}\n'.format(*setting) in settings_file_contents
settings_file.close()
2016-08-22 03:45:27 +01:00
@pytest.mark.parametrize('legacy_dir_exists, xdg_config_home, result', [
(False, '~/.config', '~/.config/thefuck'),
(False, '/user/test/config/', '/user/test/config/thefuck'),
(True, '~/.config', '~/.thefuck'),
(True, '/user/test/config/', '~/.thefuck')])
def test_get_user_dir_path(mocker, environ, settings, legacy_dir_exists,
xdg_config_home, result):
mocker.patch('thefuck.conf.Path.is_dir',
return_value=legacy_dir_exists)
if xdg_config_home is not None:
environ['XDG_CONFIG_HOME'] = xdg_config_home
else:
environ.pop('XDG_CONFIG_HOME', None)
path = settings._get_user_dir_path().as_posix()
assert path == os.path.expanduser(result)