-
Notifications
You must be signed in to change notification settings - Fork 7
/
tests.py
139 lines (106 loc) · 3.6 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from __future__ import with_statement
import sys
from nose.tools import assert_raises
from mock import patch, Mock
import check
@patch('check.StringIO')
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
@patch('check.os')
def test_which_vcs_fail(os_mock):
os_mock.path.exists.return_value = False
assert_raises(Exception, check.which_vcs)
@patch('check.os')
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)
@patch('check.call')
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
"""
@patch('check.call')
def test_pyflakes(call_mock):
check.pyflakes(['foo', 'bar', 'bla.py', 'baz.exe'])
call_mock.assert_called_with(['pyflakes', 'bla.py'])
@patch('check.call')
def test_pep8(call_mock):
check.pep8(['foo', 'bar', 'bla.py', 'baz.exe'])
call_mock.assert_called_with(['pep8.py', '--repeat', 'bla.py'])
@patch('__builtin__.open')
def test_trailing_whitespace(open_mock):
m = open_mock.return_value.read.return_value = 'foo\nbar \n \n'
files = ['foo', 'bla.py']
with check.captured_output() as output:
check.trailing_whitespace(files)
output.reset()
# Ignore the banner.
output.readline()
for n in 2, 3:
assert output.readline() == '%s:%s: trailing whitespace\n' % ('foo', n)
@patch('check.interesting_files')
@patch('check.sys')
@patch('check.os')
@patch('check.which_vcs')
@patch('check.checkers')
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()]
check._main()
mock.assert_called_with(files)
@patch('check.sys')
@patch('check.path.isfile')
@patch('check.path.walkfiles')
@patch('check.os')
@patch('check.checkers')
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
check._main()
mock.assert_called_with(['foo', 'bar'])
mock.reset()
isfile_mock.return_value = False
walkfiles_mock.return_value = ['bar~', 'bla.py']
check._main()
mock.assert_called_with(['bla.py'])