forked from mmueller/supybot-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
215 lines (181 loc) · 8.48 KB
/
test.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
212
213
214
215
# Copyright (c) 2011-2012, Mike Mueller
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Do whatever you want.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from supybot.test import *
from supybot import conf
from mock import Mock, patch
import git
import os
import time
SRC_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(SRC_DIR, 'test-data')
# This timeout value works for me and keeps the tests snappy. If test queries
# are not getting responses, you may need to bump this higher.
LOOP_TIMEOUT = 0.1
# Global mocks
git.Git.clone = Mock()
git.Repo = Mock()
# A pile of commits for use wherever (most recent first)
COMMITS = [Mock(), Mock(), Mock(), Mock(), Mock()]
COMMITS[0].author.name = 'nstark'
COMMITS[0].hexsha = 'abcdefabcdefabcdefabcdefabcdefabcdefabcd'
COMMITS[0].message = 'Fix bugs.'
COMMITS[1].author.name = 'tlannister'
COMMITS[1].hexsha = 'abcdefabcdefabcdefabcdefabcdefabcdefabcd'
COMMITS[1].message = 'I am more long-winded\nand may even use newlines.'
COMMITS[2].author.name = 'tlannister'
COMMITS[2].hexsha = 'abcdefabcdefabcdefabcdefabcdefabcdefabcd'
COMMITS[2].message = 'Snarks and grumpkins'
COMMITS[3].author.name = 'jsnow'
COMMITS[3].hexsha = 'abcdefabcdefabcdefabcdefabcdefabcdefabcd'
COMMITS[3].message = "Finished brooding, think I'll go brood."
COMMITS[4].author.name = 'tlannister'
COMMITS[4].hexsha = 'deadbeefcdefabcdefabcdefabcdefabcdefabcd'
COMMITS[4].message = "I'm the only one getting things done."
# Workaround Supybot 0.83.4.1 bug with Owner treating 'log' as a command
conf.registerGlobalValue(conf.supybot.commands.defaultPlugins,
'log', registry.String('Git', ''))
conf.supybot.commands.defaultPlugins.get('log').set('Git')
# Pre-test checks
GIT_API_VERSION = int(git.__version__[2])
assert GIT_API_VERSION == 3, 'Tests only run against GitPython 0.3.x+ API.'
class PluginTestCaseUtilMixin(object):
"Some additional utilities used in this plugin's tests."
def _feedMsgLoop(self, query, timeout=None, **kwargs):
"Send a message and wait for a list of responses instead of just one."
if timeout is None:
timeout = LOOP_TIMEOUT
responses = []
start = time.time()
r = self._feedMsg(query, timeout=timeout, **kwargs)
# Sleep off remaining time, then start sending empty queries until
# the replies stop coming.
remainder = timeout - (time.time() - start)
time.sleep(remainder if remainder > 0 else 0)
query = conf.supybot.reply.whenAddressedBy.chars()[0]
while r:
responses.append(r)
r = self._feedMsg(query, timeout=0, **kwargs)
return responses
def assertResponses(self, query, expectedResponses, **kwargs):
"Run a command and assert that it returns the given list of replies."
responses = self._feedMsgLoop(query, **kwargs)
responses = map(lambda m: m.args[1], responses)
self.assertEqual(responses, expectedResponses,
'\nActual:\n%s\n\nExpected:\n%s' %
('\n'.join(responses), '\n'.join(expectedResponses)))
return responses
class GitRehashTest(PluginTestCase):
plugins = ('Git',)
def setUp(self):
super(GitRehashTest, self).setUp()
conf.supybot.plugins.Git.pollPeriod.setValue(0)
def testRehashEmpty(self):
conf.supybot.plugins.Git.configFile.setValue(DATA_DIR + '/empty.ini')
self.assertResponse('rehash', 'Git reinitialized with 0 repositories.')
def testRehashOne(self):
conf.supybot.plugins.Git.configFile.setValue(DATA_DIR + '/one.ini')
self.assertResponse('rehash', 'Git reinitialized with 1 repository.')
class GitRepositoryListTest(ChannelPluginTestCase, PluginTestCaseUtilMixin):
channel = '#test'
plugins = ('Git',)
def setUp(self):
super(GitRepositoryListTest, self).setUp()
ini = os.path.join(DATA_DIR, 'multi-channel.ini')
conf.supybot.plugins.Git.pollPeriod.setValue(0)
conf.supybot.plugins.Git.configFile.setValue(ini)
self.assertResponse('rehash', 'Git reinitialized with 3 repositories.')
def testRepositoryList(self):
expected = [
'\x02test1\x02 (Test Repository 1, branch: master)',
'\x02test2\x02 (Test Repository 2, branch: feature)',
]
self.assertResponses('repositories', expected)
class GitNoAccessTest(ChannelPluginTestCase, PluginTestCaseUtilMixin):
channel = '#unused'
plugins = ('Git',)
def setUp(self):
super(GitNoAccessTest, self).setUp()
ini = os.path.join(DATA_DIR, 'multi-channel.ini')
conf.supybot.plugins.Git.configFile.setValue(ini)
self.assertResponse('rehash', 'Git reinitialized with 3 repositories.')
def testRepositoryListNoAccess(self):
expected = ['No repositories configured for this channel.']
self.assertResponses('repositories', expected)
def testLogNoAccess(self):
expected = ['Sorry, not allowed in this channel.']
self.assertResponses('log test1', expected)
class GitLogTest(ChannelPluginTestCase, PluginTestCaseUtilMixin):
channel = '#somewhere'
plugins = ('Git',)
def setUp(self):
super(GitLogTest, self).setUp()
self._metamock = patch('git.Repo')
self.Repo = self._metamock.__enter__()
self.Repo.return_value = self.Repo
self.Repo.iter_commits.return_value = COMMITS
ini = os.path.join(DATA_DIR, 'multi-channel.ini')
conf.supybot.plugins.Git.pollPeriod.setValue(0)
conf.supybot.plugins.Git.maxCommitsAtOnce.setValue(3)
conf.supybot.plugins.Git.configFile.setValue(ini)
self.assertResponse('rehash', 'Git reinitialized with 3 repositories.')
def tearDown(self):
del self.Repo
self._metamock.__exit__()
def testLogNonexistent(self):
expected = ['No configured repository named nothing.']
self.assertResponses('log nothing', expected)
def testLogNotAllowed(self):
expected = ['Sorry, not allowed in this channel.']
self.assertResponses('log test1', expected)
def testLogZero(self):
expected = ['(\x02log <short name> [count]\x02) -- Display the last ' +
'commits on the named repository. [count] defaults to 1 ' +
'if unspecified.']
self.assertResponses('log test2 0', expected)
def testLogNegative(self):
expected = ['(\x02log <short name> [count]\x02) -- Display the last ' +
'commits on the named repository. [count] defaults to 1 ' +
'if unspecified.']
self.assertResponses('log test2 -1', expected)
def testLogOne(self):
expected = ['[test2|feature|nstark] Fix bugs.']
self.assertResponses('log test2', expected)
def testLogTwo(self):
expected = [
'[test2|feature|tlannister] I am more long-winded',
'[test2|feature|nstark] Fix bugs.',
]
self.assertResponses('log test2 2', expected)
def testLogFive(self):
expected = [
'Showing latest 3 of 5 commits to Test Repository 2...',
'[test2|feature|tlannister] Snarks and grumpkins',
'[test2|feature|tlannister] I am more long-winded',
'[test2|feature|nstark] Fix bugs.',
]
self.assertResponses('log test2 5', expected)
def testSnarf(self):
self.Repo.commit.return_value = COMMITS[4]
expected = [
"[test2|feature|tlannister] I'm the only one getting things done.",
]
self.assertResponses('who wants some deadbeef?', expected,
usePrefixChar=False)
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: