Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
140 lines (106 sloc) 3.6 KB
from __future__ import with_statement
import sys
from import assert_raises
from mock import patch, Mock
import check
def test_captured_output(stream_mock):
"""stdout should be a StringIO in the with statement."""
# Can't assert that stdout is a file because nose may be capturing stdout.
mock = stream_mock()
with check.captured_output() as stream:
assert stream is mock
assert sys.stdout is mock
def test_which_vcs_fail(os_mock):
os_mock.path.exists.return_value = False
assert_raises(Exception, check.which_vcs)
def test_which_vcs_success(os_mock):
os_mock.path.exists.return_value = False
# Make sure getcwd is always different.
os_mock.getcwd = range(20).pop
# A chdir that flips os.path.exists after being called 3 times.
calls = [0]
def mock_chdir(path):
calls[0] += 1
if calls[0] == 3:
os_mock.path.exists.return_value = True
os_mock.chdir = mock_chdir
vcs = check.which_vcs()
# We iterate over VCS looking for matches, so which_vcs will return
# the first key when it succeeds.
assert vcs == check.VCS.keys()[0]
assert os_mock.path.exists.call_count == 1 + 3 * len(check.VCS)
def test_interesting_files(call_mock):
expected = ['foo', 'bar']
call_mock.return_value = git_status
assert check.interesting_files('git') == expected
call_mock.return_value = svn_status
assert check.interesting_files('svn') == expected
git_status = """
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
# new file: foo
# modified: bar
svn_status = """
A foo
? wtf
M bar
def test_pyflakes(call_mock):
check.pyflakes(['foo', 'bar', '', 'baz.exe'])
call_mock.assert_called_with(['pyflakes', ''])
def test_pep8(call_mock):
check.pep8(['foo', 'bar', '', 'baz.exe'])
call_mock.assert_called_with(['', '--repeat', ''])
def test_trailing_whitespace(open_mock):
m = = 'foo\nbar \n \n'
files = ['foo', '']
with check.captured_output() as output:
# Ignore the banner.
for n in 2, 3:
assert output.readline() == '%s:%s: trailing whitespace\n' % ('foo', n)
def test_main(files_mock, sys_mock, *args):
# So the checkers mock supports iteration.
mock = Mock()
Mock.__iter__ = lambda *args: iter([mock])
files = ['foo', 'bar']
files_mock.return_value = files
sys_mock.argv = [Mock()]
def test_main_with_args(sys_mock, isfile_mock, walkfiles_mock, *args):
# So the checkers mock supports iteration.
mock = Mock()
Mock.__iter__ = lambda *args: iter([mock])
sys_mock.argv = ['./check', 'foo', 'bar']
isfile_mock.return_value = True
mock.assert_called_with(['foo', 'bar'])
isfile_mock.return_value = False
walkfiles_mock.return_value = ['bar~', '']
Something went wrong with that request. Please try again.