mirror of
https://github.com/nvbn/thefuck.git
synced 2025-10-31 23:22:10 +00:00
Initial commit
This commit is contained in:
24
tests/rules/test_git_push.py
Normal file
24
tests/rules/test_git_push.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import pytest
|
||||
from thefuck.main import Command
|
||||
from thefuck.rules.git_push import match, get_new_command
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def stderr():
|
||||
return '''fatal: The current branch master has no upstream branch.
|
||||
To push the current branch and set the remote as upstream, use
|
||||
|
||||
git push --set-upstream origin master
|
||||
|
||||
'''
|
||||
|
||||
|
||||
def test_match(stderr):
|
||||
assert match(Command('git push master', '', stderr))
|
||||
assert not match(Command('git push master', '', ''))
|
||||
assert not match(Command('ls', '', stderr))
|
||||
|
||||
|
||||
def test_get_new_command(stderr):
|
||||
assert get_new_command(Command('', '', stderr))\
|
||||
== "git push --set-upstream origin master"
|
||||
12
tests/rules/test_sudo.py
Normal file
12
tests/rules/test_sudo.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from thefuck.main import Command
|
||||
from thefuck.rules.sudo import match, get_new_command
|
||||
|
||||
|
||||
def test_match():
|
||||
assert match(Command('', '', 'Permission denied'))
|
||||
assert match(Command('', '', 'permission denied'))
|
||||
assert not match(Command('', '', ''))
|
||||
|
||||
|
||||
def test_get_new_command():
|
||||
assert get_new_command(Command('ls', '', '')) == 'sudo ls'
|
||||
81
tests/test_main.py
Normal file
81
tests/test_main.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from unittest.mock import patch, Mock
|
||||
from subprocess import PIPE
|
||||
from pathlib import PosixPath, Path
|
||||
from thefuck import main
|
||||
|
||||
|
||||
def test_setup_user_dir():
|
||||
with patch('thefuck.main.Path.is_dir', return_value=False), \
|
||||
patch('thefuck.main.Path.mkdir') as mkdir, \
|
||||
patch('thefuck.main.Path.touch') as touch:
|
||||
main.setup_user_dir()
|
||||
assert mkdir.call_count == 2
|
||||
assert touch.call_count == 1
|
||||
with patch('thefuck.main.Path.is_dir', return_value=True), \
|
||||
patch('thefuck.main.Path.mkdir') as mkdir, \
|
||||
patch('thefuck.main.Path.touch') as touch:
|
||||
main.setup_user_dir()
|
||||
assert mkdir.call_count == 0
|
||||
assert touch.call_count == 0
|
||||
|
||||
|
||||
def test_get_settings():
|
||||
with patch('thefuck.main.load_source', return_value=Mock(rules=['bash'])):
|
||||
assert main.get_settings(Path('/')).rules == ['bash']
|
||||
with patch('thefuck.main.load_source', return_value=Mock(spec=[])):
|
||||
assert main.get_settings(Path('/')).rules is None
|
||||
|
||||
|
||||
def test_is_rule_enabled():
|
||||
assert main.is_rule_enabled(main.Settings(None), Path('bash.py'))
|
||||
assert main.is_rule_enabled(main.Settings(['bash']), Path('bash.py'))
|
||||
assert not main.is_rule_enabled(main.Settings(['bash']), Path('lisp.py'))
|
||||
|
||||
|
||||
def test_load_rule():
|
||||
match = object()
|
||||
get_new_command = object()
|
||||
with patch('thefuck.main.load_source',
|
||||
return_value=Mock(
|
||||
match=match,
|
||||
get_new_command=get_new_command)) as load_source:
|
||||
assert main.load_rule(Path('/rules/bash.py')) == main.Rule(match, get_new_command)
|
||||
load_source.assert_called_once_with('bash', '/rules/bash.py')
|
||||
|
||||
|
||||
def test_get_rules():
|
||||
with patch('thefuck.main.Path.glob') as glob, \
|
||||
patch('thefuck.main.load_source',
|
||||
lambda x, _: Mock(match=x, get_new_command=x)):
|
||||
glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]
|
||||
assert main.get_rules(
|
||||
Path('~'),
|
||||
main.Settings(None)) == [main.Rule('bash', 'bash'),
|
||||
main.Rule('lisp', 'lisp'),
|
||||
main.Rule('bash', 'bash'),
|
||||
main.Rule('lisp', 'lisp')]
|
||||
assert main.get_rules(
|
||||
Path('~'),
|
||||
main.Settings(['bash'])) == [main.Rule('bash', 'bash'),
|
||||
main.Rule('bash', 'bash')]
|
||||
|
||||
|
||||
def test_get_command():
|
||||
with patch('thefuck.main.Popen') as Popen:
|
||||
Popen.return_value.stdout.read.return_value = b'stdout'
|
||||
Popen.return_value.stderr.read.return_value = b'stderr'
|
||||
assert main.get_command(['thefuck', 'apt-get', 'search', 'vim']) \
|
||||
== main.Command('apt-get search vim', 'stdout', 'stderr')
|
||||
Popen.assert_called_once_with('apt-get search vim',
|
||||
shell=True,
|
||||
stdout=PIPE,
|
||||
stderr=PIPE)
|
||||
|
||||
|
||||
def test_get_matched_rule():
|
||||
rules = [main.Rule(lambda x: x.script == 'cd ..', None),
|
||||
main.Rule(lambda _: False, None)]
|
||||
assert main.get_matched_rule(main.Command('ls', '', ''),
|
||||
rules) is None
|
||||
assert main.get_matched_rule(main.Command('cd ..', '', ''),
|
||||
rules) == rules[0]
|
||||
Reference in New Issue
Block a user