Skip to content

Commit

Permalink
#N/A: Reset environment variables in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nvbn committed Aug 3, 2017
1 parent 2bbba9a commit 754bb3e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 38 deletions.
8 changes: 7 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ def set_shell(monkeypatch, request):
def _set(cls):
shell = cls()
monkeypatch.setattr('thefuck.shells.shell', shell)
request.addfinalizer()
return shell

return _set


@pytest.fixture(autouse=True)
def os_environ(monkeypatch):
env = {'PATH': ''}
monkeypatch.setattr('os.environ', env)
return env
8 changes: 4 additions & 4 deletions tests/rules/test_yarn_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def test_match(command):
assert match(command)


@pytest.mark.parametrize('command, new_command', [
@pytest.mark.parametrize('command, url', [
(Command('yarn help clean', stdout=stdout_clean),
open_command('https://yarnpkg.com/en/docs/cli/clean'))])
def test_get_new_command(command, new_command):
assert get_new_command(command) == new_command
'https://yarnpkg.com/en/docs/cli/clean')])
def test_get_new_command(command, url):
assert get_new_command(command) == open_command(url)
7 changes: 2 additions & 5 deletions tests/shells/test_fish.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ def Popen(self, mocker):
b'man\nmath\npopd\npushd\nruby')
return mock

@pytest.fixture
def os_environ(self, monkeypatch, key, value):
monkeypatch.setattr('os.environ', {key: value})

@pytest.mark.parametrize('key, value', [
('TF_OVERRIDDEN_ALIASES', 'cut,git,sed'), # legacy
('THEFUCK_OVERRIDDEN_ALIASES', 'cut,git,sed'),
('THEFUCK_OVERRIDDEN_ALIASES', 'cut, git, sed'),
('THEFUCK_OVERRIDDEN_ALIASES', ' cut,\tgit,sed\n'),
('THEFUCK_OVERRIDDEN_ALIASES', '\ncut,\n\ngit,\tsed\r')])
def test_get_overridden_aliases(self, shell, os_environ):
def test_get_overridden_aliases(self, shell, os_environ, key, value):
os_environ[key] = value
assert shell._get_overridden_aliases() == {'cd', 'cut', 'git', 'grep',
'ls', 'man', 'open', 'sed'}

Expand Down
37 changes: 14 additions & 23 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,13 @@ def load_source(mocker):
return mocker.patch('thefuck.conf.load_source')


@pytest.fixture
def environ(monkeypatch):
data = {}
monkeypatch.setattr('thefuck.conf.os.environ', data)
return data


@pytest.mark.usefixture('environ')
def test_settings_defaults(load_source, settings):
load_source.return_value = object()
settings.init()
for key, val in const.DEFAULT_SETTINGS.items():
assert getattr(settings, key) == val


@pytest.mark.usefixture('environ')
class TestSettingsFromFile(object):
def test_from_file(self, load_source, settings):
load_source.return_value = Mock(rules=['test'],
Expand Down Expand Up @@ -54,15 +45,15 @@ def test_from_file_with_DEFAULT(self, load_source, settings):

@pytest.mark.usefixture('load_source')
class TestSettingsFromEnv(object):
def test_from_env(self, environ, settings):
environ.update({'THEFUCK_RULES': 'bash:lisp',
'THEFUCK_EXCLUDE_RULES': 'git:vim',
'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'})
def test_from_env(self, os_environ, settings):
os_environ.update({'THEFUCK_RULES': 'bash:lisp',
'THEFUCK_EXCLUDE_RULES': 'git:vim',
'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']
Expand All @@ -73,8 +64,8 @@ def test_from_env(self, environ, settings):
assert settings.wait_slow_command == 999
assert settings.slow_commands == ['lein', 'react-native', './gradlew']

def test_from_env_with_DEFAULT(self, environ, settings):
environ.update({'THEFUCK_RULES': 'DEFAULT_RULES:bash:lisp'})
def test_from_env_with_DEFAULT(self, os_environ, settings):
os_environ.update({'THEFUCK_RULES': 'DEFAULT_RULES:bash:lisp'})
settings.init()
assert settings.rules == const.DEFAULT_RULES + ['bash', 'lisp']

Expand Down Expand Up @@ -116,15 +107,15 @@ def test_create_if_doesnt_exists(self, settings):
(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,
def test_get_user_dir_path(mocker, os_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
os_environ['XDG_CONFIG_HOME'] = xdg_config_home
else:
environ.pop('XDG_CONFIG_HOME', None)
os_environ.pop('XDG_CONFIG_HOME', None)

path = settings._get_user_dir_path().as_posix()
assert path == os.path.expanduser(result)
5 changes: 2 additions & 3 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ def Popen(self, monkeypatch):

@pytest.fixture(autouse=True)
def prepare(self, monkeypatch):
monkeypatch.setattr('thefuck.types.os.environ', {})
monkeypatch.setattr('thefuck.types.Command._wait_output',
staticmethod(lambda *_: True))

def test_from_script_calls(self, Popen, settings):
def test_from_script_calls(self, Popen, settings, os_environ):
settings.env = {}
assert Command.from_raw_script(
['apt-get', 'search', 'vim']) == Command(
Expand All @@ -129,7 +128,7 @@ def test_from_script_calls(self, Popen, settings):
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
env={})
env=os_environ)

@pytest.mark.parametrize('script, result', [
([''], None),
Expand Down
3 changes: 1 addition & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ def alias(self, mocker):
return_value='fuck')

@pytest.fixture(autouse=True)
def bins(self, mocker, monkeypatch):
monkeypatch.setattr('thefuck.conf.os.environ', {'PATH': 'path'})
def bins(self, mocker):
callables = list()
for name in ['diff', 'ls', 'café']:
bin_mock = mocker.Mock(name=name)
Expand Down

0 comments on commit 754bb3e

Please sign in to comment.