diff --git a/tests/conftest.py b/tests/conftest.py index b3525f43..811fd078 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 diff --git a/tests/rules/test_yarn_help.py b/tests/rules/test_yarn_help.py index 18a607fb..76485644 100644 --- a/tests/rules/test_yarn_help.py +++ b/tests/rules/test_yarn_help.py @@ -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) diff --git a/tests/shells/test_fish.py b/tests/shells/test_fish.py index ce7858fb..836a648d 100644 --- a/tests/shells/test_fish.py +++ b/tests/shells/test_fish.py @@ -18,17 +18,14 @@ class TestFish(object): 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'} diff --git a/tests/test_conf.py b/tests/test_conf.py index f89d4e26..c9f0b18d 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -10,14 +10,6 @@ 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() @@ -25,7 +17,6 @@ def test_settings_defaults(load_source, settings): 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'], @@ -54,15 +45,15 @@ class TestSettingsFromFile(object): @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'] @@ -73,8 +64,8 @@ class TestSettingsFromEnv(object): 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'] @@ -116,15 +107,15 @@ class TestInitializeSettingsFile(object): (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) diff --git a/tests/test_types.py b/tests/test_types.py index 5ddb489a..6b3d81ff 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -115,11 +115,10 @@ class TestCommand(object): @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( @@ -129,7 +128,7 @@ class TestCommand(object): stdin=PIPE, stdout=PIPE, stderr=PIPE, - env={}) + env=os_environ) @pytest.mark.parametrize('script, result', [ ([''], None), diff --git a/tests/test_utils.py b/tests/test_utils.py index ac45a7b1..5e271e49 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -206,8 +206,7 @@ class TestGetValidHistoryWithoutCurrent(object): 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)