-
Notifications
You must be signed in to change notification settings - Fork 49
/
tests.py
211 lines (157 loc) · 6.66 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# pylint: disable=missing-docstring
import os
import shutil
import subprocess
import tempfile
import unittest
from git_pylint_commit_hook import commit_hook
class TestException(Exception):
pass
class TestHook(unittest.TestCase):
# pylint: disable=protected-access,too-many-public-methods,invalid-name
def setUp(self):
# Create temporary directory
self.tmp_dir = tempfile.mkdtemp(prefix='pylint_hook_test_')
# Set current working directory to the temporary directory for
# all the commands run in the test
os.chdir(self.tmp_dir)
# Initialize temporary git repository
self.cmd('git init')
def tearDown(self):
shutil.rmtree(self.tmp_dir)
def write_file(self, filename, contents):
with open(os.path.join(self.tmp_dir, filename), 'w') as wfile:
wfile.write(contents)
return filename
def cmd(self, args):
return subprocess.check_output(args.split(), cwd=self.tmp_dir)
def test_current_commit(self):
"""Test commit_hook._current_commit"""
# Test empty tree
empty_hash = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
self.assertEquals(commit_hook._current_commit(), empty_hash)
# Test after commit
self.cmd('git commit --allow-empty -m msg')
self.assertEquals(commit_hook._current_commit(), 'HEAD')
def test_current_stash(self):
"""Test commit_hook._current_stash"""
# Test empty stash
empty_hash = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
self.assertEquals(commit_hook._current_stash(), empty_hash)
# Need an initial commit to stash
self.cmd('git commit --allow-empty -m msg')
# Test after stash
self.write_file('a', 'foo')
self.cmd('git stash --include-untracked')
stash = commit_hook._current_stash()
self.assertNotEquals(stash, empty_hash)
# Test the next stash doesn't look like the last
self.write_file('b', 'bar')
self.cmd('git stash --include-untracked')
self.assertNotEquals(commit_hook._current_stash(), stash)
def test_list_of_committed_files(self):
"""Test commit_hook._get_list_of_committed_files"""
# Test empty tree
self.assertEquals(commit_hook._get_list_of_committed_files(), [])
# Create file 'a'
a = self.write_file('a', 'foo')
self.assertEquals(commit_hook._get_list_of_committed_files(), [])
# Add 'a'
self.cmd('git add ' + a)
self.assertEquals(commit_hook._get_list_of_committed_files(), [a])
# Commit 'a'
self.cmd('git commit -m msg')
self.assertEquals(commit_hook._get_list_of_committed_files(), [])
# Edit 'a'
self.write_file('a', 'bar')
self.assertEquals(commit_hook._get_list_of_committed_files(), [])
# Add 'a'
self.cmd('git add ' + a)
self.assertEquals(commit_hook._get_list_of_committed_files(), [a])
def test_is_python_file(self):
"""Test commit_hook._is_python_file"""
# Extension
a = self.write_file('a.py', '')
self.assertTrue(commit_hook._is_python_file(a))
# Empty
a = self.write_file('b', '')
self.assertFalse(commit_hook._is_python_file(a))
# Shebang
self.write_file('b', '#!/usr/bin/env python')
self.assertTrue(commit_hook._is_python_file(a))
def test_parse_score(self):
"""Test commit_hook._parse_score"""
text = 'Your code has been rated at 8.51/10'
self.assertEquals(commit_hook._parse_score(text), 8.51)
text = 'Your code has been rated at 8.51'
self.assertEquals(commit_hook._parse_score(text), 0.0)
def test_stash_unstaged(self):
"""Test commit_hook._stash_unstaged"""
# git stash doesn't work without an initial commit :(
self.cmd('git commit --allow-empty -m msg')
# Write a file with a style error
a = self.write_file('a', 'style error!')
# Add it to the repository
self.cmd('git add ' + a)
# Fix the style error but don't add it
self.write_file('a', 'much better :)')
# Stash any unstaged changes; check the unstaged changes disappear
with commit_hook._stash_unstaged():
with open(a) as f:
self.assertEquals(f.read(), 'style error!')
# Check the unstaged changes return
with open(a) as f:
self.assertEquals(f.read(), 'much better :)')
# Stash changes then pretend we crashed
with self.assertRaises(TestException):
with commit_hook._stash_unstaged():
raise TestException
# Check the unstaged changes return
with open(a) as f:
self.assertEquals(f.read(), 'much better :)')
def test_stash_unstaged_untracked(self):
"""Test commit_hook._stash_unstaged leaves untracked files alone"""
# git stash doesn't work without an initial commit :(
self.cmd('git commit --allow-empty -m msg')
# Write a file but don't add it
a = self.write_file('a', 'untracked')
# Stash changes; check nothing happened
with commit_hook._stash_unstaged():
with open(a) as f:
self.assertEqual(f.read(), 'untracked')
# Check the file is still unmodified
with open(a) as f:
self.assertEquals(f.read(), 'untracked')
def test_stash_unstaged_no_initial(self):
"""Test commit_hook._stash_unstaged handles no initial commit"""
# Write a file with a style error
a = self.write_file('a', 'style error!')
# Add it to the repository
self.cmd('git add ' + a)
# Fix the style error but don't add it
self.write_file('a', 'much better :)')
# A warning should be printed to screen saying nothing is stashed
with commit_hook._stash_unstaged():
with open(a) as f:
self.assertEquals(f.read(), 'much better :)')
# Check the file is still unmodified
with open(a) as f:
self.assertEquals(f.read(), 'much better :)')
def test_check_ignore(self):
"""Test commit_hook._check_ignore"""
text = 'I: 1, 0: Ignoring entire file (file-ignored)'
self.assertTrue(commit_hook._check_ignore(text))
text = 'Your code has been rated at 8.51'
self.assertFalse(commit_hook._check_ignore(text))
text = '''Report
======
0 statements analysed.
Statistics by type
------------------'''
self.assertTrue(commit_hook._check_ignore(text))
text = '''Report
======
100 statements analysed.
Statistics by type
------------------'''
self.assertFalse(commit_hook._check_ignore(text))