From 75d058f2efac8849e58c48ff3434812f21b96cf0 Mon Sep 17 00:00:00 2001 From: Preetham Vayigandla Date: Tue, 16 Jan 2024 15:21:09 -0600 Subject: [PATCH 1/2] Added Uncommon Typos --- tests/rules/test_git_not_command.py | 24 ++++++++++++++++++++++++ thefuck/rules/git_not_command.py | 14 +++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/tests/rules/test_git_not_command.py b/tests/rules/test_git_not_command.py index 8a9740c54..018e66e66 100644 --- a/tests/rules/test_git_not_command.py +++ b/tests/rules/test_git_not_command.py @@ -55,3 +55,27 @@ def test_get_new_command(git_not_command, git_not_command_one_of_this, == ['git stats', 'git stash', 'git stage']) assert (get_new_command(Command('git tags', git_not_command_closest)) == ['git tag', 'git stage']) + + +@pytest.mark.parametrize('script, output', [ + ('git lock', "git: lock is not a git command. See 'git --help'. \n\n The most similar command is \n log"), + ('git lock --help', "git: lock is not a git command. See 'git --help'. \n\n The most similar command is \n log")]) +def test_match_uncommon(output, script): + assert match(Command(script, output)) + + +@pytest.mark.parametrize('script', [ + 'git branch foo', + 'git checkout feature/test_commit', + 'git push']) +def test_not_match_uncommon(script): + assert not match(Command(script, '')) + + +@pytest.mark.parametrize('script, expected_output', [ + ('git lock', ['git log']), + ('git lock --help', ['git log --help'])]) +def test_get_new_command_uncommon(script, expected_output): + output = "git: '{}' is not a git command. See 'git --help'.".format(script.split()[1]) + command = Command(script, output) + assert get_new_command(command) == expected_output diff --git a/thefuck/rules/git_not_command.py b/thefuck/rules/git_not_command.py index fd2a1bd12..5d2fe4d61 100644 --- a/thefuck/rules/git_not_command.py +++ b/thefuck/rules/git_not_command.py @@ -3,6 +3,14 @@ from thefuck.specific.git import git_support +COMMON_TYPOS = { + 'copy': ['branch'], + 'list': ['branch'], + 'lock': ['log'], + 'update': ['fetch', 'fetch --all', 'fetch --all --tags', 'remote update'], +} + + @git_support def match(command): return (" is not a git command. See 'git --help'." in command.output @@ -14,5 +22,9 @@ def match(command): def get_new_command(command): broken_cmd = re.findall(r"git: '([^']*)' is not a git command", command.output)[0] - matched = get_all_matched_commands(command.output, ['The most similar command', 'Did you mean']) + # check if the broken_cmd exists in COMMON_TYPOS + if broken_cmd in COMMON_TYPOS: + matched = COMMON_TYPOS[broken_cmd] + else: + matched = get_all_matched_commands(command.output, ['The most similar command', 'Did you mean']) return replace_command(command, broken_cmd, matched) From 9b17f6b4ba2f95833eea4ee375419bafed4743da Mon Sep 17 00:00:00 2001 From: Preetham Vayigandla Date: Wed, 17 Jan 2024 11:17:08 -0600 Subject: [PATCH 2/2] Added test cases to existing functions --- tests/rules/test_git_not_command.py | 43 ++++++++++++----------------- thefuck/rules/git_not_command.py | 7 ++--- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/tests/rules/test_git_not_command.py b/tests/rules/test_git_not_command.py index 018e66e66..bb3ed32c0 100644 --- a/tests/rules/test_git_not_command.py +++ b/tests/rules/test_git_not_command.py @@ -35,20 +35,32 @@ def git_not_command_closest(): ''' +@pytest.fixture +def git_not_command_uncommon(): + return "git: 'lock' is not a git command. See 'git --help'.\n\nThe most similar command is" + + @pytest.fixture def git_command(): return "* master" -def test_match(git_not_command, git_command, git_not_command_one_of_this): +def test_match(git_not_command, git_command, git_not_command_one_of_this, git_not_command_closest, git_not_command_uncommon): assert match(Command('git brnch', git_not_command)) assert match(Command('git st', git_not_command_one_of_this)) assert not match(Command('ls brnch', git_not_command)) assert not match(Command('git branch', git_command)) + assert match(Command('git lock', git_not_command_uncommon)) + assert match(Command('git lock --help', git_not_command_uncommon)) + + assert not match(Command('git branch foo', '')) + assert not match(Command('git checkout feature/test_commit', '')) + assert not match(Command('git push', '')) + def test_get_new_command(git_not_command, git_not_command_one_of_this, - git_not_command_closest): + git_not_command_closest, git_not_command_uncommon): assert (get_new_command(Command('git brnch', git_not_command)) == ['git branch']) assert (get_new_command(Command('git st', git_not_command_one_of_this)) @@ -56,26 +68,7 @@ def test_get_new_command(git_not_command, git_not_command_one_of_this, assert (get_new_command(Command('git tags', git_not_command_closest)) == ['git tag', 'git stage']) - -@pytest.mark.parametrize('script, output', [ - ('git lock', "git: lock is not a git command. See 'git --help'. \n\n The most similar command is \n log"), - ('git lock --help', "git: lock is not a git command. See 'git --help'. \n\n The most similar command is \n log")]) -def test_match_uncommon(output, script): - assert match(Command(script, output)) - - -@pytest.mark.parametrize('script', [ - 'git branch foo', - 'git checkout feature/test_commit', - 'git push']) -def test_not_match_uncommon(script): - assert not match(Command(script, '')) - - -@pytest.mark.parametrize('script, expected_output', [ - ('git lock', ['git log']), - ('git lock --help', ['git log --help'])]) -def test_get_new_command_uncommon(script, expected_output): - output = "git: '{}' is not a git command. See 'git --help'.".format(script.split()[1]) - command = Command(script, output) - assert get_new_command(command) == expected_output + assert (get_new_command(Command('git lock', git_not_command_uncommon)) + == ['git log']) + assert (get_new_command(Command('git lock --help', git_not_command_uncommon)) + == ['git log --help']) diff --git a/thefuck/rules/git_not_command.py b/thefuck/rules/git_not_command.py index 5d2fe4d61..7146e7338 100644 --- a/thefuck/rules/git_not_command.py +++ b/thefuck/rules/git_not_command.py @@ -22,9 +22,6 @@ def match(command): def get_new_command(command): broken_cmd = re.findall(r"git: '([^']*)' is not a git command", command.output)[0] - # check if the broken_cmd exists in COMMON_TYPOS - if broken_cmd in COMMON_TYPOS: - matched = COMMON_TYPOS[broken_cmd] - else: - matched = get_all_matched_commands(command.output, ['The most similar command', 'Did you mean']) + matched = COMMON_TYPOS.get(broken_cmd, []) + matched.extend(get_all_matched_commands(command.output, ['The most similar command', 'Did you mean'])) return replace_command(command, broken_cmd, matched)