mirror of
				https://github.com/nvbn/thefuck.git
				synced 2025-10-30 22:54:14 +00:00 
			
		
		
		
	Remove obscure RulesNamesList and DefaultRulesNames
				
					
				
			This commit is contained in:
		| @@ -2,15 +2,6 @@ import pytest | |||||||
| import six | import six | ||||||
| from mock import Mock | from mock import Mock | ||||||
| from thefuck import conf | from thefuck import conf | ||||||
| from tests.utils import Rule |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @pytest.mark.parametrize('enabled, rules, result', [ |  | ||||||
|     (True, conf.DEFAULT_RULES, True), |  | ||||||
|     (False, conf.DEFAULT_RULES, False), |  | ||||||
|     (False, conf.DEFAULT_RULES + ['test'], True)]) |  | ||||||
| def test_default(enabled, rules, result): |  | ||||||
|     assert (Rule('test', enabled_by_default=enabled) in rules) == result |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @pytest.fixture | @pytest.fixture | ||||||
|   | |||||||
| @@ -1,9 +1,9 @@ | |||||||
| import pytest | import pytest | ||||||
| from pathlib import PosixPath, Path | from pathlib import PosixPath, Path | ||||||
| from mock import Mock | from mock import Mock | ||||||
| from thefuck import corrector, conf, types | from thefuck import corrector, conf | ||||||
| from tests.utils import Rule, Command, CorrectedCommand | from tests.utils import Rule, Command, CorrectedCommand | ||||||
| from thefuck.corrector import make_corrected_commands, get_corrected_commands | from thefuck.corrector import make_corrected_commands, get_corrected_commands, is_rule_enabled | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_load_rule(mocker): | def test_load_rule(mocker): | ||||||
| @@ -21,6 +21,23 @@ def test_load_rule(mocker): | |||||||
|     load_source.assert_called_once_with('bash', '/rules/bash.py') |     load_source.assert_called_once_with('bash', '/rules/bash.py') | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @pytest.mark.parametrize('rules, exclude_rules, rule, is_enabled', [ | ||||||
|  |     (conf.DEFAULT_RULES, [], Rule('git', enabled_by_default=True), True), | ||||||
|  |     (conf.DEFAULT_RULES, [], Rule('git', enabled_by_default=False), False), | ||||||
|  |     ([], [], Rule('git', enabled_by_default=False), False), | ||||||
|  |     ([], [], Rule('git', enabled_by_default=True), False), | ||||||
|  |     (conf.DEFAULT_RULES + ['git'], [], Rule('git', enabled_by_default=False), True), | ||||||
|  |     (['git'], [], Rule('git', enabled_by_default=False), True), | ||||||
|  |     (conf.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=True), False), | ||||||
|  |     (conf.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=False), False), | ||||||
|  |     ([], ['git'], Rule('git', enabled_by_default=True), False), | ||||||
|  |     ([], ['git'], Rule('git', enabled_by_default=False), False)]) | ||||||
|  | def test_is_rule_enabled(settings, rules, exclude_rules, rule, is_enabled): | ||||||
|  |     settings.update(rules=rules, | ||||||
|  |                     exclude_rules=exclude_rules) | ||||||
|  |     assert is_rule_enabled(rule) == is_enabled | ||||||
|  |  | ||||||
|  |  | ||||||
| class TestGetRules(object): | class TestGetRules(object): | ||||||
|     @pytest.fixture |     @pytest.fixture | ||||||
|     def glob(self, mocker): |     def glob(self, mocker): | ||||||
| @@ -37,12 +54,6 @@ class TestGetRules(object): | |||||||
|     def _compare_names(self, rules, names): |     def _compare_names(self, rules, names): | ||||||
|         assert {r.name for r in rules} == set(names) |         assert {r.name for r in rules} == set(names) | ||||||
|  |  | ||||||
|     def _prepare_rules(self, rules): |  | ||||||
|         if rules == conf.DEFAULT_RULES: |  | ||||||
|             return rules |  | ||||||
|         else: |  | ||||||
|             return types.RulesNamesList(rules) |  | ||||||
|  |  | ||||||
|     @pytest.mark.parametrize('paths, conf_rules, exclude_rules, loaded_rules', [ |     @pytest.mark.parametrize('paths, conf_rules, exclude_rules, loaded_rules', [ | ||||||
|         (['git.py', 'bash.py'], conf.DEFAULT_RULES, [], ['git', 'bash']), |         (['git.py', 'bash.py'], conf.DEFAULT_RULES, [], ['git', 'bash']), | ||||||
|         (['git.py', 'bash.py'], ['git'], [], ['git']), |         (['git.py', 'bash.py'], ['git'], [], ['git']), | ||||||
| @@ -51,9 +62,9 @@ class TestGetRules(object): | |||||||
|     def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules, |     def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules, | ||||||
|                        loaded_rules): |                        loaded_rules): | ||||||
|         glob([PosixPath(path) for path in paths]) |         glob([PosixPath(path) for path in paths]) | ||||||
|         settings.update(rules=self._prepare_rules(conf_rules), |         settings.update(rules=conf_rules, | ||||||
|                         priority={}, |                         priority={}, | ||||||
|                         exclude_rules=self._prepare_rules(exclude_rules)) |                         exclude_rules=exclude_rules) | ||||||
|         rules = corrector.get_rules() |         rules = corrector.get_rules() | ||||||
|         self._compare_names(rules, loaded_rules) |         self._compare_names(rules, loaded_rules) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,13 +1,5 @@ | |||||||
| from thefuck.types import RulesNamesList, Settings, \ | from thefuck.types import SortedCorrectedCommandsSequence | ||||||
|     SortedCorrectedCommandsSequence | from tests.utils import CorrectedCommand | ||||||
| from tests.utils import Rule, CorrectedCommand |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_rules_names_list(): |  | ||||||
|     assert RulesNamesList(['bash', 'lisp']) == ['bash', 'lisp'] |  | ||||||
|     assert RulesNamesList(['bash', 'lisp']) == RulesNamesList(['bash', 'lisp']) |  | ||||||
|     assert Rule('lisp') in RulesNamesList(['lisp']) |  | ||||||
|     assert Rule('bash') not in RulesNamesList(['lisp']) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class TestSortedCorrectedCommandsSequence(object): | class TestSortedCorrectedCommandsSequence(object): | ||||||
|   | |||||||
| @@ -2,25 +2,10 @@ from imp import load_source | |||||||
| import os | import os | ||||||
| import sys | import sys | ||||||
| from six import text_type | from six import text_type | ||||||
| from .types import RulesNamesList, Settings | from .types import Settings | ||||||
|  |  | ||||||
|  | ALL_ENABLED = object() | ||||||
| class _DefaultRulesNames(RulesNamesList): | DEFAULT_RULES = [ALL_ENABLED] | ||||||
|     def __add__(self, items): |  | ||||||
|         return _DefaultRulesNames(list(self) + items) |  | ||||||
|  |  | ||||||
|     def __contains__(self, item): |  | ||||||
|         return item.enabled_by_default or \ |  | ||||||
|                super(_DefaultRulesNames, self).__contains__(item) |  | ||||||
|  |  | ||||||
|     def __eq__(self, other): |  | ||||||
|         if isinstance(other, _DefaultRulesNames): |  | ||||||
|             return super(_DefaultRulesNames, self).__eq__(other) |  | ||||||
|         else: |  | ||||||
|             return False |  | ||||||
|  |  | ||||||
|  |  | ||||||
| DEFAULT_RULES = _DefaultRulesNames([]) |  | ||||||
| DEFAULT_PRIORITY = 1000 | DEFAULT_PRIORITY = 1000 | ||||||
|  |  | ||||||
| DEFAULT_SETTINGS = {'rules': DEFAULT_RULES, | DEFAULT_SETTINGS = {'rules': DEFAULT_RULES, | ||||||
| @@ -121,12 +106,6 @@ def init_settings(user_dir): | |||||||
|     except Exception: |     except Exception: | ||||||
|         exception("Can't load settings from env", sys.exc_info()) |         exception("Can't load settings from env", sys.exc_info()) | ||||||
|  |  | ||||||
|     if not isinstance(settings['rules'], RulesNamesList): |  | ||||||
|         settings.rules = RulesNamesList(settings['rules']) |  | ||||||
|  |  | ||||||
|     if not isinstance(settings.exclude_rules, RulesNamesList): |  | ||||||
|         settings.exclude_rules = RulesNamesList(settings.exclude_rules) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def initialize_settings_file(user_dir): | def initialize_settings_file(user_dir): | ||||||
|     settings_path = user_dir.joinpath('settings.py') |     settings_path = user_dir.joinpath('settings.py') | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| import sys | import sys | ||||||
| from imp import load_source | from imp import load_source | ||||||
| from pathlib import Path | from pathlib import Path | ||||||
| from .conf import settings, DEFAULT_PRIORITY | from .conf import settings, DEFAULT_PRIORITY, ALL_ENABLED | ||||||
| from .types import Rule, CorrectedCommand, SortedCorrectedCommandsSequence | from .types import Rule, CorrectedCommand, SortedCorrectedCommandsSequence | ||||||
| from .utils import compatibility_call | from .utils import compatibility_call | ||||||
| from . import logs | from . import logs | ||||||
| @@ -21,13 +21,24 @@ def load_rule(rule): | |||||||
|                 getattr(rule_module, 'requires_output', True)) |                 getattr(rule_module, 'requires_output', True)) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def is_rule_enabled(rule): | ||||||
|  |     """Returns `True` when rule enabled.""" | ||||||
|  |     if rule.name in settings.exclude_rules: | ||||||
|  |         return False | ||||||
|  |     elif rule.name in settings.rules: | ||||||
|  |         return True | ||||||
|  |     elif rule.enabled_by_default and ALL_ENABLED in settings.rules: | ||||||
|  |         return True | ||||||
|  |     else: | ||||||
|  |         return False | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_loaded_rules(rules): | def get_loaded_rules(rules): | ||||||
|     """Yields all available rules.""" |     """Yields all available rules.""" | ||||||
|     for rule in rules: |     for rule in rules: | ||||||
|         if rule.name != '__init__.py': |         if rule.name != '__init__.py': | ||||||
|             loaded_rule = load_rule(rule) |             loaded_rule = load_rule(rule) | ||||||
|             if loaded_rule in settings.rules and \ |             if is_rule_enabled(loaded_rule): | ||||||
|                             loaded_rule not in settings.exclude_rules: |  | ||||||
|                 yield loaded_rule |                 yield loaded_rule | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -30,13 +30,6 @@ class CorrectedCommand(object): | |||||||
|             self.script, self.side_effect, self.priority) |             self.script, self.side_effect, self.priority) | ||||||
|  |  | ||||||
|  |  | ||||||
| class RulesNamesList(list): |  | ||||||
|     """Wrapper a top of list for storing rules names.""" |  | ||||||
|  |  | ||||||
|     def __contains__(self, item): |  | ||||||
|         return super(RulesNamesList, self).__contains__(item.name) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Settings(dict): | class Settings(dict): | ||||||
|     def __getattr__(self, item): |     def __getattr__(self, item): | ||||||
|         return self.get(item) |         return self.get(item) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user