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

Add ability to change settings via environment variables

This commit is contained in:
nvbn
2015-04-22 20:18:53 +02:00
parent b4b599df80
commit 69a9516477
8 changed files with 233 additions and 61 deletions

75
tests/test_conf.py Normal file
View File

@@ -0,0 +1,75 @@
from mock import patch, Mock
from thefuck.main import Rule
from thefuck import conf
def test_rules_list():
assert conf.RulesList(['bash', 'lisp']) == ['bash', 'lisp']
assert conf.RulesList(['bash', 'lisp']) == conf.RulesList(['bash', 'lisp'])
assert Rule('lisp', None, None, False) in conf.RulesList(['lisp'])
assert Rule('bash', None, None, False) not in conf.RulesList(['lisp'])
def test_default():
assert Rule('test', None, None, True) in conf.DEFAULT
assert Rule('test', None, None, False) not in conf.DEFAULT
assert Rule('test', None, None, False) in (conf.DEFAULT + ['test'])
def test_settings_defaults():
with patch('thefuck.conf.load_source', return_value=object()), \
patch('thefuck.conf.os.environ', new_callable=lambda: {}):
for key, val in conf.Settings.defaults.items():
assert getattr(conf.Settings(Mock()), key) == val
def test_settings_from_file():
with patch('thefuck.conf.load_source', return_value=Mock(rules=['test'],
wait_command=10,
require_confirmation=True,
no_colors=True)), \
patch('thefuck.conf.os.environ', new_callable=lambda: {}):
settings = conf.Settings(Mock())
assert settings.rules == ['test']
assert settings.wait_command == 10
assert settings.require_confirmation is True
assert settings.no_colors is True
def test_settings_from_file_with_DEFAULT():
with patch('thefuck.conf.load_source', return_value=Mock(rules=conf.DEFAULT + ['test'],
wait_command=10,
require_confirmation=True,
no_colors=True)), \
patch('thefuck.conf.os.environ', new_callable=lambda: {}):
settings = conf.Settings(Mock())
assert settings.rules == conf.DEFAULT + ['test']
def test_settings_from_env():
with patch('thefuck.conf.load_source', return_value=Mock(rules=['test'],
wait_command=10)), \
patch('thefuck.conf.os.environ',
new_callable=lambda: {'THEFUCK_RULES': 'bash:lisp',
'THEFUCK_WAIT_COMMAND': '55',
'THEFUCK_REQUIRE_CONFIRMATION': 'true',
'THEFUCK_NO_COLORS': 'false'}):
settings = conf.Settings(Mock())
assert settings.rules == ['bash', 'lisp']
assert settings.wait_command == 55
assert settings.require_confirmation is True
assert settings.no_colors is False
def test_settings_from_env_with_DEFAULT():
with patch('thefuck.conf.load_source', return_value=Mock()), \
patch('thefuck.conf.os.environ', new_callable=lambda: {'THEFUCK_RULES': 'DEFAULT:bash:lisp'}):
settings = conf.Settings(Mock())
assert settings.rules == conf.DEFAULT + ['bash', 'lisp']
def test_update_settings():
settings = conf.BaseSettings({'key': 'val'})
new_settings = settings.update(key='new-val')
assert new_settings.key == 'new-val'
assert settings.key == 'val'

View File

@@ -1,25 +1,7 @@
from subprocess import PIPE
from pathlib import PosixPath, Path
from mock import patch, Mock
from thefuck import main
def test_get_settings():
with patch('thefuck.main.load_source', return_value=Mock(rules=['bash'])):
assert main.get_settings(Path('/')).rules == ['bash']
with patch('thefuck.main.load_source', return_value=Mock(spec=[])):
assert main.get_settings(Path('/')).rules is None
def test_is_rule_enabled():
assert main.is_rule_enabled(Mock(rules=None),
main.Rule('bash', None, None, True))
assert not main.is_rule_enabled(Mock(rules=None),
main.Rule('bash', None, None, False))
assert main.is_rule_enabled(Mock(rules=['bash']),
main.Rule('bash', None, None, True))
assert not main.is_rule_enabled(Mock(rules=['bash']),
main.Rule('lisp', None, None, True))
from thefuck import main, conf
def test_load_rule():
@@ -43,14 +25,16 @@ def test_get_rules():
glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]
assert list(main.get_rules(
Path('~'),
Mock(rules=None))) == [main.Rule('bash', 'bash', 'bash', True),
main.Rule('lisp', 'lisp', 'lisp', True),
main.Rule('bash', 'bash', 'bash', True),
main.Rule('lisp', 'lisp', 'lisp', True)]
Mock(rules=conf.DEFAULT))) \
== [main.Rule('bash', 'bash', 'bash', True),
main.Rule('lisp', 'lisp', 'lisp', True),
main.Rule('bash', 'bash', 'bash', True),
main.Rule('lisp', 'lisp', 'lisp', True)]
assert list(main.get_rules(
Path('~'),
Mock(rules=['bash']))) == [main.Rule('bash', 'bash', 'bash', True),
main.Rule('bash', 'bash', 'bash', True)]
Mock(rules=conf.RulesList(['bash'])))) \
== [main.Rule('bash', 'bash', 'bash', True),
main.Rule('bash', 'bash', 'bash', True)]
def test_get_command():

View File

@@ -1,6 +1,15 @@
from mock import Mock
from thefuck.utils import sudo_support
from thefuck.utils import sudo_support, wrap_settings
from thefuck.main import Command
from thefuck.conf import BaseSettings
def test_wrap_settings():
fn = lambda _, settings: settings._conf
assert wrap_settings({'key': 'val'})(fn)(None, BaseSettings({})) \
== {'key': 'val'}
assert wrap_settings({'key': 'new-val'})(fn)(
None, BaseSettings({'key': 'val'})) == {'key': 'new-val'}
def test_sudo_support():