2015-05-02 04:29:55 +02:00
|
|
|
import pytest
|
2015-04-23 23:01:25 -03:00
|
|
|
import six
|
2015-05-02 04:29:55 +02:00
|
|
|
from mock import Mock
|
2015-04-22 20:18:53 +02:00
|
|
|
from thefuck import conf
|
|
|
|
|
|
|
|
|
2015-05-02 04:29:55 +02:00
|
|
|
@pytest.fixture
|
2015-05-07 13:42:52 +02:00
|
|
|
def load_source(mocker):
|
|
|
|
return mocker.patch('thefuck.conf.load_source')
|
2015-04-22 20:18:53 +02:00
|
|
|
|
|
|
|
|
2015-05-02 04:29:55 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def environ(monkeypatch):
|
|
|
|
data = {}
|
|
|
|
monkeypatch.setattr('thefuck.conf.os.environ', data)
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixture('environ')
|
2015-09-07 12:12:16 +03:00
|
|
|
def test_settings_defaults(load_source, settings):
|
2015-05-02 04:29:55 +02:00
|
|
|
load_source.return_value = object()
|
2015-09-08 15:15:53 +03:00
|
|
|
settings.init()
|
2015-05-02 04:29:55 +02:00
|
|
|
for key, val in conf.DEFAULT_SETTINGS.items():
|
2015-09-07 12:12:16 +03:00
|
|
|
assert getattr(settings, key) == val
|
2015-05-02 04:29:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixture('environ')
|
|
|
|
class TestSettingsFromFile(object):
|
2015-09-07 12:12:16 +03:00
|
|
|
def test_from_file(self, load_source, settings):
|
2015-05-02 04:29:55 +02:00
|
|
|
load_source.return_value = Mock(rules=['test'],
|
|
|
|
wait_command=10,
|
|
|
|
require_confirmation=True,
|
2015-05-07 13:11:45 +02:00
|
|
|
no_colors=True,
|
2015-09-06 12:55:59 +03:00
|
|
|
priority={'vim': 100},
|
|
|
|
exclude_rules=['git'])
|
2015-09-08 15:15:53 +03:00
|
|
|
settings.init()
|
2015-04-22 20:18:53 +02:00
|
|
|
assert settings.rules == ['test']
|
|
|
|
assert settings.wait_command == 10
|
|
|
|
assert settings.require_confirmation is True
|
|
|
|
assert settings.no_colors is True
|
2015-05-07 13:11:45 +02:00
|
|
|
assert settings.priority == {'vim': 100}
|
2015-09-06 12:55:59 +03:00
|
|
|
assert settings.exclude_rules == ['git']
|
2015-04-22 20:18:53 +02:00
|
|
|
|
2015-09-07 12:12:16 +03:00
|
|
|
def test_from_file_with_DEFAULT(self, load_source, settings):
|
2015-05-02 04:29:55 +02:00
|
|
|
load_source.return_value = Mock(rules=conf.DEFAULT_RULES + ['test'],
|
|
|
|
wait_command=10,
|
2015-09-06 12:55:59 +03:00
|
|
|
exclude_rules=[],
|
2015-05-02 04:29:55 +02:00
|
|
|
require_confirmation=True,
|
|
|
|
no_colors=True)
|
2015-09-08 15:15:53 +03:00
|
|
|
settings.init()
|
2015-04-22 22:37:11 +02:00
|
|
|
assert settings.rules == conf.DEFAULT_RULES + ['test']
|
2015-04-22 20:18:53 +02:00
|
|
|
|
|
|
|
|
2015-05-02 04:29:55 +02:00
|
|
|
@pytest.mark.usefixture('load_source')
|
|
|
|
class TestSettingsFromEnv(object):
|
2015-09-07 12:12:16 +03:00
|
|
|
def test_from_env(self, environ, settings):
|
2015-05-02 04:29:55 +02:00
|
|
|
environ.update({'THEFUCK_RULES': 'bash:lisp',
|
2015-09-06 12:55:59 +03:00
|
|
|
'THEFUCK_EXCLUDE_RULES': 'git:vim',
|
2015-05-02 04:29:55 +02:00
|
|
|
'THEFUCK_WAIT_COMMAND': '55',
|
|
|
|
'THEFUCK_REQUIRE_CONFIRMATION': 'true',
|
2015-05-07 13:11:45 +02:00
|
|
|
'THEFUCK_NO_COLORS': 'false',
|
|
|
|
'THEFUCK_PRIORITY': 'bash=10:lisp=wrong:vim=15'})
|
2015-09-08 15:15:53 +03:00
|
|
|
settings.init()
|
2015-04-22 20:18:53 +02:00
|
|
|
assert settings.rules == ['bash', 'lisp']
|
2015-09-06 12:55:59 +03:00
|
|
|
assert settings.exclude_rules == ['git', 'vim']
|
2015-04-22 20:18:53 +02:00
|
|
|
assert settings.wait_command == 55
|
|
|
|
assert settings.require_confirmation is True
|
|
|
|
assert settings.no_colors is False
|
2015-05-07 13:11:45 +02:00
|
|
|
assert settings.priority == {'bash': 10, 'vim': 15}
|
2015-04-22 20:18:53 +02:00
|
|
|
|
2015-09-07 12:12:16 +03:00
|
|
|
def test_from_env_with_DEFAULT(self, environ, settings):
|
2015-05-02 04:29:55 +02:00
|
|
|
environ.update({'THEFUCK_RULES': 'DEFAULT_RULES:bash:lisp'})
|
2015-09-08 15:15:53 +03:00
|
|
|
settings.init()
|
2015-04-22 22:37:11 +02:00
|
|
|
assert settings.rules == conf.DEFAULT_RULES + ['bash', 'lisp']
|
2015-04-23 23:01:25 -03:00
|
|
|
|
|
|
|
|
2015-05-02 04:29:55 +02:00
|
|
|
class TestInitializeSettingsFile(object):
|
2015-09-08 15:15:53 +03:00
|
|
|
def test_ignore_if_exists(self, settings):
|
2015-05-02 04:29:55 +02:00
|
|
|
settings_path_mock = Mock(is_file=Mock(return_value=True), open=Mock())
|
2015-09-08 15:15:53 +03:00
|
|
|
settings.user_dir = Mock(joinpath=Mock(return_value=settings_path_mock))
|
|
|
|
settings._init_settings_file()
|
2015-05-02 04:29:55 +02:00
|
|
|
assert settings_path_mock.is_file.call_count == 1
|
|
|
|
assert not settings_path_mock.open.called
|
|
|
|
|
2015-09-08 15:15:53 +03:00
|
|
|
def test_create_if_doesnt_exists(self, settings):
|
2015-05-02 04:29:55 +02: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)))
|
2015-09-08 15:15:53 +03:00
|
|
|
settings.user_dir = Mock(joinpath=Mock(return_value=settings_path_mock))
|
|
|
|
settings._init_settings_file()
|
2015-05-02 04:29:55 +02:00
|
|
|
settings_file_contents = settings_file.getvalue()
|
|
|
|
assert settings_path_mock.is_file.call_count == 1
|
|
|
|
assert settings_path_mock.open.call_count == 1
|
|
|
|
assert conf.SETTINGS_HEADER in settings_file_contents
|
|
|
|
for setting in conf.DEFAULT_SETTINGS.items():
|
|
|
|
assert '# {} = {}\n'.format(*setting) in settings_file_contents
|
|
|
|
settings_file.close()
|