[pypy-svn] buildbot default: partially refactor the hook to be more testable, and write a test for the fix in 12cc0caf054d
antocuni
commits-noreply at bitbucket.org
Mon Dec 20 10:36:11 CET 2010
Author: Antonio Cuni <anto.cuni at gmail.com>
Branch:
Changeset: r391:9c7bc068df88
Date: 2010-12-19 14:45 +0100
http://bitbucket.org/pypy/buildbot/changeset/9c7bc068df88/
Log: partially refactor the hook to be more testable, and write a test for the fix in 12cc0caf054d
diff --git a/bitbucket_hook/hook.py b/bitbucket_hook/hook.py
--- a/bitbucket_hook/hook.py
+++ b/bitbucket_hook/hook.py
@@ -17,28 +17,6 @@
ADDRESS = 'pypy-svn at codespeak.net'
hgexe = str(py.path.local.sysfind('hg'))
-def hg(*argv):
- from subprocess import Popen, PIPE
- argv = map(str, argv)
- proc = Popen([hgexe] + list(argv), stdout=PIPE, stderr=PIPE)
- stdout, stderr = proc.communicate()
- ret = proc.wait()
- if ret != 0:
- print >> sys.stderr, 'error: hg', ' '.join(argv)
- print >> sys.stderr, stderr
- raise Exception('error when executing hg')
- return stdout.decode('utf-8')
-
-def send(from_, to, subject, body):
- import smtplib
- from email.mime.text import MIMEText
-
- smtp = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
- msg = MIMEText(body, _charset='utf-8')
- msg['From'] = from_
- msg['To'] = to
- msg['Subject'] = subject
- smtp.sendmail(from_, [to], msg.as_string())
TEMPLATE = u"""\
Author: {author}
@@ -53,6 +31,33 @@
class BitbucketHookHandler(object):
+ def _hgexe(self, argv):
+ from subprocess import Popen, PIPE
+ proc = Popen([hgexe] + list(argv), stdout=PIPE, stderr=PIPE)
+ stdout, stderr = proc.communicate()
+ ret = proc.wait()
+ return stdout, stderr, ret
+
+ def hg(self, *argv):
+ argv = map(str, argv)
+ stdout, stderr, ret = self._hgexe(argv)
+ if ret != 0:
+ print >> sys.stderr, 'error: hg', ' '.join(argv)
+ print >> sys.stderr, stderr
+ raise Exception('error when executing hg')
+ return stdout.decode('utf-8')
+
+ def send(self, from_, to, subject, body):
+ import smtplib
+ from email.mime.text import MIMEText
+
+ smtp = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
+ msg = MIMEText(body, _charset='utf-8')
+ msg['From'] = from_
+ msg['To'] = to
+ msg['Subject'] = subject
+ smtp.sendmail(from_, [to], msg.as_string())
+
def handle(self, payload):
path = payload['repository']['absolute_url']
self.payload = payload
@@ -61,7 +66,7 @@
if not self.local_repo.check(dir=True):
print >> sys.stderr, 'Ignoring unknown repo', path
return
- hg('pull', '-R', self.local_repo)
+ self.hg('pull', '-R', self.local_repo)
self.handle_diff_email()
def handle_diff_email(self):
@@ -78,11 +83,11 @@
url = self.remote_repo + 'changeset/' + commit['node'] + '/'
template = TEMPLATE % {'url': url}
subject = '%s %s: %s' % (reponame, commit['branch'], line0)
- body = hg('-R', self.local_repo, 'log', '-r', hgid,
+ body = self.hg('-R', self.local_repo, 'log', '-r', hgid,
'--template', template)
diff = self.get_diff(hgid, commit['files'])
body = body+diff
- send(sender, ADDRESS, subject, body)
+ self.send(sender, ADDRESS, subject, body)
def get_diff(self, hgid, files):
import re
@@ -90,8 +95,8 @@
files = [item['file'] for item in files]
lines = []
for filename in files:
- out = hg('-R', self.local_repo, 'diff', '--git', '-c', hgid,
- self.local_repo.join(filename))
+ out = self.hg('-R', self.local_repo, 'diff', '--git', '-c', hgid,
+ self.local_repo.join(filename))
match = binary.search(out)
if match:
# it's a binary patch, omit the content
diff --git a/bitbucket_hook/__init__.py b/bitbucket_hook/__init__.py
new file mode 100644
diff --git a/bitbucket_hook/test/__init__.py b/bitbucket_hook/test/__init__.py
new file mode 100644
diff --git a/bitbucket_hook/test/test_hook.py b/bitbucket_hook/test/test_hook.py
new file mode 100644
--- /dev/null
+++ b/bitbucket_hook/test/test_hook.py
@@ -0,0 +1,22 @@
+# -*- encoding: utf-8 -*-
+
+from bitbucket_hook.hook import BitbucketHookHandler
+
+class BaseHandler(BitbucketHookHandler):
+
+ def __init__(self):
+ self.mails = []
+
+ def send(self, from_, to, subject, body):
+ self.mails.append((from_, to, subject, body))
+
+
+def test_non_ascii_encoding():
+ class MyHandler(BaseHandler):
+ def _hgexe(self, argv):
+ return u'späm'.encode('utf-8'), '', 0
+ #
+ handler = MyHandler()
+ stdout = handler.hg('foobar')
+ assert stdout == u'späm'
+
More information about the Pypy-commit
mailing list