1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-20 09:39:01 +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 pytest
mock mock
pytest-mock

View File

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

View File

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

View File

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