Patch print function instead of sys.stdout

This commit is contained in:
Martin Domke 2016-07-26 17:27:15 +02:00
parent c782060a06
commit 9bfdc867cd

View file

@ -1,5 +1,4 @@
"""Tests for the BaseFormatter object.""" """Tests for the BaseFormatter object."""
import io
import optparse import optparse
import mock import mock
@ -81,19 +80,23 @@ def test_show_source_updates_physical_line_appropriately(line, column):
@pytest.mark.parametrize('tee', [False, True]) @pytest.mark.parametrize('tee', [False, True])
def test_write_uses_an_output_file(tee): def test_write_uses_an_output_file(tee):
"""Verify that we use the output file when it's present.""" """Verify that we use the output file when it's present."""
line = u'Something to write' line = 'Something to write'
source = u'source' source = 'source'
filemock = mock.Mock() filemock = mock.Mock()
with mock.patch('sys.stdout', new_callable=io.StringIO) as stdout: formatter = base.BaseFormatter(options(tee=tee))
formatter = base.BaseFormatter(options(tee=tee)) formatter.output_fd = filemock
formatter.output_fd = filemock
with mock.patch('flake8.formatting.base.print') as print_func:
formatter.write(line, source) formatter.write(line, source)
if tee: if tee:
output = line + formatter.newline + source + formatter.newline assert print_func.called
assert print_func.mock_calls == [
mock.call(line),
mock.call(source),
]
else: else:
output = '' assert not print_func.called
assert stdout.getvalue() == output
assert filemock.write.called is True assert filemock.write.called is True
assert filemock.write.call_count == 2 assert filemock.write.call_count == 2