changeset 541:2479e2a75afa

`BittenNotifyEmail` objects have state, so the same instance cannot be shared
author mgood
date Mon, 23 Mar 2009 01:15:52 +0000
parents 2cc5457e6610
children cacf018eb190
files bitten/notify.py bitten/tests/notify.py
diffstat 2 files changed, 21 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/bitten/notify.py
+++ b/bitten/notify.py
@@ -32,21 +32,27 @@
 
     def __init__(self):
         self.log.debug('Initializing BittenNotify plugin')
-        self.email = BittenNotifyEmail(self.env)
 
-    def notify(self, build = None):
+    def notify(self, build=None):
         self.log.info('BittenNotify invoked for build %r' % build)
         self.log.debug('build status: %s' % build.status)
-        if self._should_notify(build):
-            self.log.info('Sending notification for build %r' % build)
-            build_info = BuildInfo(self.env, build)
-            self.email.notify(build_info)
+        if not self._should_notify(build):
+            return
+        self.log.info('Sending notification for build %r' % build)
+        try:
+            email = BittenNotifyEmail(self.env)
+            email.notify(BuildInfo(self.env, build))
+        except Exception, e:
+            self.log.exception("Failure sending notification for build "
+                               "%s: %s", build.id, e)
 
     def _should_notify(self, build):
-        build_is_failure = (build.status == Build.FAILURE)
-        build_is_success = (build.status == Build.SUCCESS)
-        return (build_is_failure and self.notify_on_failure) or \
-                (build_is_success and self.notify_on_success)
+        if build.status == Build.FAILURE:
+            return self.notify_on_failure
+        elif build.status == Build.SUCCESS:
+            return self.notify_on_success
+        else:
+            return False
 
     # IBuildListener methods
 
--- a/bitten/tests/notify.py
+++ b/bitten/tests/notify.py
@@ -41,36 +41,30 @@
     """unit tests for BittenNotify dispatcher class"""
     def setUp(self):
         BittenNotifyBaseTest.setUp(self)
-        self.notify_was_called = False
-        def notify(build_info):
-            self.notify_was_called = True
-        self.dispatcher = Mock(BittenNotify, self.env,
-                               email=Mock(notify=notify))
+        self.dispatcher = BittenNotify(self.env)
         self.failed_build = Build(self.env, status=Build.FAILURE)
         self.successful_build = Build(self.env, status=Build.SUCCESS)
 
     def test_do_notify_on_failed_build(self):
         self.env.config.set(CONFIG_SECTION, NOTIFY_ON_FAILURE, 'true')
-        self.dispatcher.notify(self.failed_build)
-        self.assertTrue(self.notify_was_called,
+        self.assertTrue(self.dispatcher._should_notify(self.failed_build),
                 'notifier should be called for failed builds.')
 
     def test_do_not_notify_on_failed_build(self):
         self.env.config.set(CONFIG_SECTION, NOTIFY_ON_FAILURE, 'false')
-        self.dispatcher.notify(self.failed_build)
-        self.assertFalse(self.notify_was_called,
+        self.assertFalse(self.dispatcher._should_notify(self.failed_build),
                 'notifier should not be called for failed build.')
 
     def test_do_notify_on_successful_build(self):
         self.env.config.set(CONFIG_SECTION, NOTIFY_ON_SUCCESS, 'true')
         self.dispatcher.notify(self.successful_build)
-        self.assertTrue(self.notify_was_called,
+        self.assertTrue(self.dispatcher._should_notify(self.successful_build),
                 'notifier should be called for successful builds when configured.')
 
     def test_do_not_notify_on_successful_build(self):
         self.env.config.set(CONFIG_SECTION, NOTIFY_ON_SUCCESS, 'false')
         self.dispatcher.notify(self.successful_build)
-        self.assertFalse(self.notify_was_called,
+        self.assertFalse(self.dispatcher._should_notify(self.successful_build),
                 'notifier shouldn\'t be called for successful build.')
 
 
Copyright (C) 2012-2017 Edgewall Software