コード例 #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),
     ]
コード例 #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)
     ])
コード例 #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)])
コード例 #4
0
ファイル: test_main.py プロジェクト: lardnicus/oops
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() == ('', '')
コード例 #5
0
ファイル: test_corrector.py プロジェクト: yunshan/thefuck
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;']
コード例 #6
0
ファイル: test_main.py プロジェクト: lardnicus/oops
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:'
コード例 #7
0
ファイル: test_types.py プロジェクト: vladmysiur/konspekt
 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')
コード例 #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)
コード例 #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)
コード例 #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)]
コード例 #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:'
コード例 #12
0
ファイル: test_main.py プロジェクト: youngdev/thefuck
 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)
コード例 #13
0
ファイル: test_main.py プロジェクト: lardnicus/oops
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')
コード例 #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)
コード例 #15
0
ファイル: test_main.py プロジェクト: youngdev/thefuck
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')
コード例 #16
0
ファイル: test_types.py プロジェクト: HengMrZ/thefuck
 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')
コード例 #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')
コード例 #18
0
ファイル: test_main.py プロジェクト: lardnicus/oops
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')]
コード例 #19
0
ファイル: test_main.py プロジェクト: youngdev/thefuck
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)
コード例 #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]
コード例 #21
0
 def test_no_match(self):
     assert list(
         corrector.get_matched_rules(Command('ls'),
                                     [Rule('', lambda *_: False)],
                                     Mock(no_colors=True))) == []
コード例 #22
0
ファイル: test_main.py プロジェクト: youngdev/thefuck
 def test_run_rule(self, capsys):
     main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
                   Command(), None)
     assert capsys.readouterr() == ('new-command\n', '')
コード例 #23
0
ファイル: test_main.py プロジェクト: youngdev/thefuck
 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() == ('', '')
コード例 #24
0
ファイル: test_corrector.py プロジェクト: yunshan/thefuck
 def load_source(self, monkeypatch):
     monkeypatch.setattr('thefuck.types.load_source',
                         lambda x, _: Rule(x))
コード例 #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:'
コード例 #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
コード例 #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')
コード例 #28
0
ファイル: test_types.py プロジェクト: youngdev/thefuck
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'])
コード例 #29
0
ファイル: test_types.py プロジェクト: ispiders/thefuck
 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)])
コード例 #30
0
ファイル: test_types.py プロジェクト: ispiders/thefuck
 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:'
コード例 #31
0
ファイル: test_types.py プロジェクト: chishi2612/thefuck
 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:"
コード例 #32
0
ファイル: test_types.py プロジェクト: chishi2612/thefuck
 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),
     ]
コード例 #33
0
ファイル: test_types.py プロジェクト: DannyDebevec/thefuck
 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)]
コード例 #34
0
 def test_isnt_match(self):
     assert not Rule('', lambda _: False).is_match(Command('ls'))
コード例 #35
0
 def test_is_match(self):
     rule = Rule('', lambda x: x.script == 'cd ..')
     assert rule.is_match(Command('cd ..'))
コード例 #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)]
コード例 #37
0
ファイル: test_types.py プロジェクト: ispiders/thefuck
 def test_is_match(self):
     rule = Rule('', lambda x: x.script == 'cd ..')
     assert rule.is_match(Command('cd ..'))
コード例 #38
0
ファイル: test_types.py プロジェクト: chishi2612/thefuck
 def test_is_match(self):
     rule = Rule("", lambda x: x.script == "cd ..")
     assert rule.is_match(Command("cd .."))