1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-13 22:28:33 +00:00

Improve tests with mocker

This commit is contained in:
nvbn 2015-05-07 13:42:52 +02:00
parent 91c1fe414a
commit 2b12b4bfce
4 changed files with 28 additions and 42 deletions

View File

@ -1,2 +1,3 @@
pytest
mock
pytest-mock

View File

@ -14,10 +14,8 @@ def test_default(enabled, rules, result):
@pytest.fixture
def load_source(monkeypatch):
mock = Mock()
monkeypatch.setattr('thefuck.conf.load_source', mock)
return mock
def load_source(mocker):
return mocker.patch('thefuck.conf.load_source')
@pytest.fixture

View File

@ -6,15 +6,15 @@ from thefuck import main, conf, types
from tests.utils import Rule, Command
def test_load_rule(monkeypatch):
def test_load_rule(mocker):
match = object()
get_new_command = object()
load_source = Mock()
load_source.return_value = Mock(match=match,
get_new_command=get_new_command,
enabled_by_default=True,
priority=900)
monkeypatch.setattr('thefuck.main.load_source', load_source)
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')
@ -22,10 +22,8 @@ def test_load_rule(monkeypatch):
class TestGetRules(object):
@pytest.fixture(autouse=True)
def glob(self, monkeypatch):
mock = Mock(return_value=[])
monkeypatch.setattr('thefuck.main.Path.glob', mock)
return mock
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
@ -118,10 +116,8 @@ class TestGetMatchedRule(object):
class TestRunRule(object):
@pytest.fixture(autouse=True)
def confirm(self, monkeypatch):
mock = Mock(return_value=True)
monkeypatch.setattr('thefuck.main.confirm', mock)
return mock
def confirm(self, mocker):
return mocker.patch('thefuck.main.confirm', return_value=True)
def test_run_rule(self, capsys):
main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
@ -147,10 +143,8 @@ class TestRunRule(object):
class TestConfirm(object):
@pytest.fixture
def stdin(self, monkeypatch):
mock = Mock(return_value='\n')
monkeypatch.setattr('sys.stdin.read', mock)
return mock
def stdin(self, mocker):
return mocker.patch('sys.stdin.read', return_value='\n')
def test_when_not_required(self, capsys):
assert main.confirm('command', None, Mock(require_confirmation=False))

View File

@ -1,20 +1,15 @@
import pytest
from mock import Mock, MagicMock
from thefuck import shells
@pytest.fixture
def builtins_open(monkeypatch):
mock = MagicMock()
monkeypatch.setattr('six.moves.builtins.open', mock)
return mock
def builtins_open(mocker):
return mocker.patch('six.moves.builtins.open')
@pytest.fixture
def isfile(monkeypatch):
mock = Mock(return_value=True)
monkeypatch.setattr('os.path.isfile', mock)
return mock
def isfile(mocker):
return mocker.patch('os.path.isfile', return_value=True)
class TestGeneric(object):
@ -32,9 +27,8 @@ class TestGeneric(object):
@pytest.mark.usefixtures('isfile')
class TestBash(object):
@pytest.fixture(autouse=True)
def Popen(self, monkeypatch):
mock = Mock()
monkeypatch.setattr('thefuck.shells.Popen', mock)
def Popen(self, mocker):
mock = mocker.patch('thefuck.shells.Popen')
mock.return_value.stdout.read.return_value = (
b'alias l=\'ls -CF\'\n'
b'alias la=\'ls -A\'\n'
@ -52,16 +46,15 @@ class TestBash(object):
def test_put_to_history(self, builtins_open):
shells.Bash().put_to_history('ls')
builtins_open.return_value.__enter__.return_value.\
builtins_open.return_value.__enter__.return_value. \
write.assert_called_once_with('ls\n')
@pytest.mark.usefixtures('isfile')
class TestZsh(object):
@pytest.fixture(autouse=True)
def Popen(self, monkeypatch):
mock = Mock()
monkeypatch.setattr('thefuck.shells.Popen', mock)
def Popen(self, mocker):
mock = mocker.patch('thefuck.shells.Popen')
mock.return_value.stdout.read.return_value = (
b'l=\'ls -CF\'\n'
b'la=\'ls -A\'\n'
@ -77,9 +70,9 @@ class TestZsh(object):
def test_to_shell(self):
assert shells.Zsh().to_shell('pwd') == 'pwd'
def test_put_to_history(self, builtins_open, monkeypatch):
monkeypatch.setattr('thefuck.shells.time',
lambda: 1430707243.3517463)
def test_put_to_history(self, builtins_open, mocker):
mocker.patch('thefuck.shells.time',
return_value=1430707243.3517463)
shells.Zsh().put_to_history('ls')
builtins_open.return_value.__enter__.return_value. \
write.assert_called_once_with(': 1430707243:0;ls\n')