mirror of
https://github.com/nvbn/thefuck.git
synced 2025-01-31 10:11:14 +00:00
Merge pull request #119 from scorphus/initialize-settings
conf: initialize a settings file if it doesn't exist (fix #111)
This commit is contained in:
commit
03dd7eda04
@ -1,3 +1,4 @@
|
||||
import six
|
||||
from mock import patch, Mock
|
||||
from thefuck.types import Rule
|
||||
from thefuck import conf
|
||||
@ -59,3 +60,30 @@ def test_settings_from_env_with_DEFAULT():
|
||||
patch('thefuck.conf.os.environ', new_callable=lambda: {'THEFUCK_RULES': 'DEFAULT_RULES:bash:lisp'}):
|
||||
settings = conf.get_settings(Mock())
|
||||
assert settings.rules == conf.DEFAULT_RULES + ['bash', 'lisp']
|
||||
|
||||
|
||||
def test_initialize_settings_file_ignore_if_exists():
|
||||
settings_path_mock = Mock(is_file=Mock(return_value=True), open=Mock())
|
||||
user_dir_mock = Mock(joinpath=Mock(return_value=settings_path_mock))
|
||||
conf.initialize_settings_file(user_dir_mock)
|
||||
assert settings_path_mock.is_file.call_count == 1
|
||||
assert not settings_path_mock.open.called
|
||||
|
||||
|
||||
def test_initialize_settings_file_create_if_exists_not():
|
||||
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
|
||||
)),
|
||||
)
|
||||
user_dir_mock = Mock(joinpath=Mock(return_value=settings_path_mock))
|
||||
conf.initialize_settings_file(user_dir_mock)
|
||||
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()
|
||||
|
@ -35,6 +35,19 @@ ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
|
||||
'THEFUCK_NO_COLORS': 'no_colors'}
|
||||
|
||||
|
||||
SETTINGS_HEADER = u"""# ~/.thefuck/settings.py: The Fuck settings file
|
||||
#
|
||||
# The rules are defined as in the example bellow:
|
||||
#
|
||||
# rules = ['cd_parent', 'git_push', 'python_command', 'sudo']
|
||||
#
|
||||
# The default values are as follows. Uncomment and change to fit your needs.
|
||||
# See https://github.com/nvbn/thefuck#settings for more information.
|
||||
#
|
||||
|
||||
"""
|
||||
|
||||
|
||||
def _settings_from_file(user_dir):
|
||||
"""Loads settings from file."""
|
||||
settings = load_source('settings',
|
||||
@ -92,3 +105,12 @@ def get_settings(user_dir):
|
||||
conf['rules'] = types.RulesNamesList(conf['rules'])
|
||||
|
||||
return types.Settings(conf)
|
||||
|
||||
|
||||
def initialize_settings_file(user_dir):
|
||||
settings_path = user_dir.joinpath('settings.py')
|
||||
if not settings_path.is_file():
|
||||
with settings_path.open(mode='w') as settings_file:
|
||||
settings_file.write(SETTINGS_HEADER)
|
||||
for setting in DEFAULT_SETTINGS.items():
|
||||
settings_file.write(u'# {} = {}\n'.format(*setting))
|
||||
|
@ -15,7 +15,7 @@ def setup_user_dir():
|
||||
rules_dir = user_dir.joinpath('rules')
|
||||
if not rules_dir.is_dir():
|
||||
rules_dir.mkdir(parents=True)
|
||||
user_dir.joinpath('settings.py').touch()
|
||||
conf.initialize_settings_file(user_dir)
|
||||
return user_dir
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user