Esempio n. 1
0
 def test_get_corrected_commands_with_rule_returns_list(self):
     rule = Rule(get_new_command=lambda x: [x.script + "!", x.script + "@"],
                 priority=100)
     assert list(rule.get_corrected_commands(Command("test", ""))) == [
         CorrectedCommand(script="test!", priority=100),
         CorrectedCommand(script="test@", priority=200),
     ]
Esempio n. 2
0
 def test_get_corrected_commands_with_rule_returns_list(self):
     rule = Rule(get_new_command=lambda x: [x.script + '!', x.script + '@'],
                 priority=100)
     assert (list(rule.get_corrected_commands(Command(script='test'))) == [
         CorrectedCommand(script='test!', priority=100),
         CorrectedCommand(script='test@', priority=200)
     ])
Esempio n. 3
0
class TestRule(object):
    def test_from_path(self, mocker):
        match = object()
        get_new_command = object()
        load_source = mocker.patch(
            'thefuck.types.load_source',
            return_value=Mock(match=match,
                              get_new_command=get_new_command,
                              enabled_by_default=True,
                              priority=900,
                              requires_output=True))
        rule_path = os.path.join(os.sep, 'rules', 'bash.py')
        assert (Rule.from_path(Path(rule_path))
                == Rule('bash', match, get_new_command, priority=900))
        load_source.assert_called_once_with('bash', rule_path)

    @pytest.mark.parametrize('rules, exclude_rules, rule, is_enabled', [
        (const.DEFAULT_RULES, [], Rule('git', enabled_by_default=True), True),
        (const.DEFAULT_RULES, [], Rule('git', enabled_by_default=False), False),
        ([], [], Rule('git', enabled_by_default=False), False),
        ([], [], Rule('git', enabled_by_default=True), False),
        (const.DEFAULT_RULES + ['git'], [], Rule('git', enabled_by_default=False), True),
        (['git'], [], Rule('git', enabled_by_default=False), True),
        (const.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=True), False),
        (const.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_enabled(self, settings, rules, exclude_rules, rule, is_enabled):
        settings.update(rules=rules,
                        exclude_rules=exclude_rules)
        assert rule.is_enabled == is_enabled

    def test_isnt_match(self):
        assert not Rule('', lambda _: False).is_match(
            Command('ls'))

    def test_is_match(self):
        rule = Rule('', lambda x: x.script == 'cd ..')
        assert rule.is_match(Command('cd ..'))

    @pytest.mark.usefixtures('no_colors')
    def test_isnt_match_when_rule_failed(self, capsys):
        rule = Rule('test', Mock(side_effect=OSError('Denied')),
                    requires_output=False)
        assert not rule.is_match(Command('ls'))
        assert capsys.readouterr()[1].split('\n')[0] == '[WARN] Rule test:'

    def test_get_corrected_commands_with_rule_returns_list(self):
        rule = Rule(get_new_command=lambda x: [x.script + '!', x.script + '@'],
                    priority=100)
        assert (list(rule.get_corrected_commands(Command(script='test')))
                == [CorrectedCommand(script='test!', priority=100),
                    CorrectedCommand(script='test@', priority=200)])

    def test_get_corrected_commands_with_rule_returns_command(self):
        rule = Rule(get_new_command=lambda x: x.script + '!',
                    priority=100)
        assert (list(rule.get_corrected_commands(Command(script='test')))
                == [CorrectedCommand(script='test!', priority=100)])
Esempio n. 4
0
def test_run_rule(capsys):
    with patch('oops.main.confirm', return_value=True):
        main.run_rule(Rule(get_new_command=lambda *_: 'new-command'), None,
                      None)
        assert capsys.readouterr() == ('new-command\n', '')
    with patch('oops.main.confirm', return_value=False):
        main.run_rule(Rule(get_new_command=lambda *_: 'new-command'), None,
                      None)
        assert capsys.readouterr() == ('', '')
Esempio n. 5
0
def test_get_corrected_commands(mocker):
    command = Command('test', 'test', 'test')
    rules = [Rule(match=lambda _: False),
             Rule(match=lambda _: True,
                  get_new_command=lambda x: x.script + '!', priority=100),
             Rule(match=lambda _: True,
                  get_new_command=lambda x: [x.script + '@', x.script + ';'],
                  priority=60)]
    mocker.patch('thefuck.corrector.get_rules', return_value=rules)
    assert [cmd.script for cmd in get_corrected_commands(command)] \
           == ['test!', 'test@', 'test;']
Esempio n. 6
0
def test_get_matched_rule(capsys):
    rules = [
        Rule('', lambda x, _: x.script == 'cd ..'),
        Rule('', lambda *_: False),
        Rule('rule', Mock(side_effect=OSError('Denied')))
    ]
    assert main.get_matched_rule(Command('ls'), rules,
                                 Mock(no_colors=True)) is None
    assert main.get_matched_rule(Command('cd ..'), rules,
                                 Mock(no_colors=True)) == rules[0]
    assert capsys.readouterr()[1].split('\n')[0] \
           == '[WARN] Rule rule:'
Esempio n. 7
0
 def test_from_path(self, mocker):
     match = object()
     get_new_command = object()
     load_source = mocker.patch('thefuck.types.load_source',
                                return_value=Mock(
                                    match=match,
                                    get_new_command=get_new_command,
                                    enabled_by_default=True,
                                    priority=900,
                                    requires_output=True))
     assert Rule.from_path(Path('/rules/bash.py')) \
            == Rule('bash', match, get_new_command, priority=900)
     load_source.assert_called_once_with('bash', '/rules/bash.py')
Esempio n. 8
0
 def test_from_path(self, mocker):
     match = object()
     get_new_command = object()
     load_source = mocker.patch(
         'thedick.types.load_source',
         return_value=Mock(match=match,
                           get_new_command=get_new_command,
                           enabled_by_default=True,
                           priority=900,
                           requires_output=True))
     rule_path = os.path.join(os.sep, 'rules', 'bash.py')
     assert (Rule.from_path(Path(rule_path))
             == Rule('bash', match, get_new_command, priority=900))
     load_source.assert_called_once_with('bash', rule_path)
Esempio n. 9
0
 def test_get(self, monkeypatch, glob, conf_rules, rules):
     glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]
     monkeypatch.setattr('thefuck.corrector.load_source',
                         lambda x, _: Rule(x))
     assert self._compare_names(
         corrector.get_rules(Path('~'), Mock(rules=conf_rules,
                                             priority={})), rules)
Esempio n. 10
0
 def test_with_rule_returns_list(self):
     rule = Rule(
         get_new_command=lambda x, _: [x.script + '!', x.script + '@'],
         priority=100)
     assert list(make_corrected_commands(Command(script='test'), [rule], None)) \
            == [CorrectedCommand(script='test!', priority=100),
                CorrectedCommand(script='test@', priority=200)]
Esempio n. 11
0
 def test_when_rule_failed(self, capsys):
     all(
         corrector.get_matched_rules(Command('ls'), [
             Rule('test',
                  Mock(side_effect=OSError('Denied')),
                  requires_output=False)
         ], Mock(no_colors=True, debug=False)))
     assert capsys.readouterr()[1].split('\n')[0] == '[WARN] Rule test:'
Esempio n. 12
0
 def test_run_rule_with_side_effect(self, capsys):
     side_effect = Mock()
     settings = Mock()
     command = Command()
     main.run_rule(
         Rule(get_new_command=lambda *_: 'new-command',
              side_effect=side_effect), command, settings)
     assert capsys.readouterr() == ('new-command\n', '')
     side_effect.assert_called_once_with(command, settings)
Esempio n. 13
0
def test_load_rule():
    match = object()
    get_new_command = object()
    with patch('oops.main.load_source',
               return_value=Mock(match=match,
                                 get_new_command=get_new_command,
                                 enabled_by_default=True)) as load_source:
        assert main.load_rule(Path('/rules/bash.py')) \
               == Rule('bash', match, get_new_command)
        load_source.assert_called_once_with('bash', '/rules/bash.py')
Esempio n. 14
0
 def test_from_path(self, mocker):
     match = object()
     get_new_command = object()
     load_source = mocker.patch(
         "thefuck.types.load_source",
         return_value=Mock(
             match=match,
             get_new_command=get_new_command,
             enabled_by_default=True,
             priority=900,
             requires_output=True,
         ),
     )
     rule_path = os.path.join(os.sep, "rules", "bash.py")
     assert Rule.from_path(Path(rule_path)) == Rule("bash",
                                                    match,
                                                    get_new_command,
                                                    priority=900)
     load_source.assert_called_once_with("bash", rule_path)
Esempio n. 15
0
def test_load_rule(mocker):
    match = object()
    get_new_command = object()
    load_source = mocker.patch('thefuck.main.load_source',
                               return_value=Mock(
                                   match=match,
                                   get_new_command=get_new_command,
                                   enabled_by_default=True,
                                   priority=900))
    assert main.load_rule(Path('/rules/bash.py')) \
           == Rule('bash', match, get_new_command, priority=900)
    load_source.assert_called_once_with('bash', '/rules/bash.py')
Esempio n. 16
0
 def test_from_path(self, mocker):
     match = object()
     get_new_command = object()
     load_source = mocker.patch(
         'thefuck.types.load_source',
         return_value=Mock(match=match,
                           get_new_command=get_new_command,
                           enabled_by_default=True,
                           priority=900,
                           requires_output=True))
     assert Rule.from_path(Path('/rules/bash.py')) \
            == Rule('bash', match, get_new_command, priority=900)
     load_source.assert_called_once_with('bash', '/rules/bash.py')
Esempio n. 17
0
def test_load_rule(mocker):
    match = object()
    get_new_command = object()
    load_source = mocker.patch('thefuck.corrector.load_source',
                               return_value=Mock(
                                   match=match,
                                   get_new_command=get_new_command,
                                   enabled_by_default=True,
                                   priority=900,
                                   requires_output=True))
    assert corrector.load_rule(Path('/rules/bash.py'), settings=Mock(priority={})) \
           == Rule('bash', match, get_new_command, priority=900)
    load_source.assert_called_once_with('bash', '/rules/bash.py')
Esempio n. 18
0
def test_get_rules():
    with patch('oops.main.Path.glob') as glob, \
            patch('oops.main.load_source',
                  lambda x, _: Mock(match=x, get_new_command=x,
                                    enabled_by_default=True)):
        glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]
        assert list(main.get_rules(
            Path('~'),
            Mock(rules=conf.DEFAULT_RULES))) \
               == [Rule('bash', 'bash', 'bash'),
                   Rule('lisp', 'lisp', 'lisp'),
                   Rule('bash', 'bash', 'bash'),
                   Rule('lisp', 'lisp', 'lisp')]
        assert list(main.get_rules(
            Path('~'),
            Mock(rules=types.RulesNamesList(['bash'])))) \
               == [Rule('bash', 'bash', 'bash'),
                   Rule('bash', 'bash', 'bash')]
Esempio n. 19
0
class TestGetRules(object):
    @pytest.fixture(autouse=True)
    def glob(self, mocker):
        return mocker.patch('thefuck.main.Path.glob', return_value=[])

    def _compare_names(self, rules, names):
        return [r.name for r in rules] == names

    @pytest.mark.parametrize(
        'conf_rules, rules',
        [(conf.DEFAULT_RULES, ['bash', 'lisp', 'bash', 'lisp']),
         (types.RulesNamesList(['bash']), ['bash', 'bash'])])
    def test_get(self, monkeypatch, glob, conf_rules, rules):
        glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]
        monkeypatch.setattr('thefuck.main.load_source', lambda x, _: Rule(x))
        assert self._compare_names(
            main.get_rules(Path('~'), Mock(rules=conf_rules, priority={})),
            rules)

    @pytest.mark.parametrize(
        'priority, unordered, ordered',
        [({}, [Rule('bash', priority=100),
               Rule('python', priority=5)], ['python', 'bash']),
         ({}, [
             Rule('lisp', priority=9999),
             Rule('c', priority=conf.DEFAULT_PRIORITY)
         ], ['c', 'lisp']),
         ({
             'python': 9999
         }, [Rule('bash', priority=100),
             Rule('python', priority=5)], ['bash', 'python'])])
    def test_ordered_by_priority(self, monkeypatch, priority, unordered,
                                 ordered):
        monkeypatch.setattr('thefuck.main._get_loaded_rules',
                            lambda *_: unordered)
        assert self._compare_names(
            main.get_rules(Path('~'), Mock(priority=priority)), ordered)
Esempio n. 20
0
 def test_match(self):
     rule = Rule('', lambda x, _: x.script == 'cd ..')
     assert list(
         corrector.get_matched_rules(Command('cd ..'), [rule],
                                     Mock(no_colors=True))) == [rule]
Esempio n. 21
0
 def test_no_match(self):
     assert list(
         corrector.get_matched_rules(Command('ls'),
                                     [Rule('', lambda *_: False)],
                                     Mock(no_colors=True))) == []
Esempio n. 22
0
 def test_run_rule(self, capsys):
     main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
                   Command(), None)
     assert capsys.readouterr() == ('new-command\n', '')
Esempio n. 23
0
 def test_when_not_comfirmed(self, capsys, confirm):
     confirm.return_value = False
     main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
                   Command(), None)
     assert capsys.readouterr() == ('', '')
Esempio n. 24
0
 def load_source(self, monkeypatch):
     monkeypatch.setattr('thefuck.types.load_source',
                         lambda x, _: Rule(x))
Esempio n. 25
0
 def test_isnt_match_when_rule_failed(self, capsys):
     rule = Rule('test',
                 Mock(side_effect=OSError('Denied')),
                 requires_output=False)
     assert not rule.is_match(Command('ls'))
     assert capsys.readouterr()[1].split('\n')[0] == '[WARN] Rule test:'
Esempio n. 26
0
 def test_from_path_excluded_rule(self, mocker, settings):
     load_source = mocker.patch('thedarn.types.load_source')
     settings.update(exclude_rules=['git'])
     rule_path = os.path.join(os.sep, 'rules', 'git.py')
     assert Rule.from_path(Path(rule_path)) is None
     assert not load_source.called
Esempio n. 27
0
 def test_from_path_rule_exception(self, mocker):
     load_source = mocker.patch(
         'thedarn.types.load_source',
         side_effect=ImportError("No module named foo..."))
     assert Rule.from_path(Path('git.py')) is None
     load_source.assert_called_once_with('git', 'git.py')
Esempio n. 28
0
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'])
Esempio n. 29
0
 def test_get_corrected_commands_with_rule_returns_command(self):
     rule = Rule(get_new_command=lambda x: x.script + '!',
                 priority=100)
     assert (list(rule.get_corrected_commands(Command(script='test')))
             == [CorrectedCommand(script='test!', priority=100)])
Esempio n. 30
0
 def test_isnt_match_when_rule_failed(self, capsys):
     rule = Rule('test', Mock(side_effect=OSError('Denied')),
                 requires_output=False)
     assert not rule.is_match(Command('ls'))
     assert capsys.readouterr()[1].split('\n')[0] == '[WARN] Rule test:'
Esempio n. 31
0
 def test_isnt_match_when_rule_failed(self, capsys):
     rule = Rule("test", Mock(side_effect=OSError("Denied")), requires_output=False)
     assert not rule.is_match(Command("ls"))
     assert capsys.readouterr()[1].split("\n")[0] == "[WARN] Rule test:"
Esempio n. 32
0
 def test_get_corrected_commands_with_rule_returns_list(self):
     rule = Rule(get_new_command=lambda x: [x.script + "!", x.script + "@"], priority=100)
     assert list(rule.get_corrected_commands(Command(script="test"))) == [
         CorrectedCommand(script="test!", priority=100),
         CorrectedCommand(script="test@", priority=200),
     ]
Esempio n. 33
0
 def test_get_corrected_commands_with_rule_returns_list(self):
     rule = Rule(get_new_command=lambda x: [x.script + '!', x.script + '@'],
                 priority=100)
     assert list(rule.get_corrected_commands(Command(script='test'))) \
            == [CorrectedCommand(script='test!', priority=100),
                CorrectedCommand(script='test@', priority=200)]
Esempio n. 34
0
 def test_isnt_match(self):
     assert not Rule('', lambda _: False).is_match(Command('ls'))
Esempio n. 35
0
 def test_is_match(self):
     rule = Rule('', lambda x: x.script == 'cd ..')
     assert rule.is_match(Command('cd ..'))
Esempio n. 36
0
 def test_get_corrected_commands_with_rule_returns_command(self):
     rule = Rule(get_new_command=lambda x: x.script + '!', priority=100)
     assert list(rule.get_corrected_commands(Command(script='test'))) \
            == [CorrectedCommand(script='test!', priority=100)]
Esempio n. 37
0
 def test_is_match(self):
     rule = Rule('', lambda x: x.script == 'cd ..')
     assert rule.is_match(Command('cd ..'))
Esempio n. 38
0
 def test_is_match(self):
     rule = Rule("", lambda x: x.script == "cd ..")
     assert rule.is_match(Command("cd .."))