Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
KATO Kazuyoshi authored and maruel committed Oct 30, 2009
1 parent df9cf59 commit b8d2a5c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 51 deletions.
27 changes: 13 additions & 14 deletions buildbot/status/words.py
Expand Up @@ -65,12 +65,11 @@ class Contact:
'broadcast contact' (chat rooms, IRC channels as a whole).
"""

def __init__(self, channel, showBlameList = False):
def __init__(self, channel):
self.channel = channel
self.notify_events = {}
self.subscribed = 0
self.add_notification_events(channel.notify_events)
self.showBlameList = showBlameList

silly = {
"What happen ?": "Somebody set up us the bomb.",
Expand Down Expand Up @@ -336,7 +335,7 @@ def buildFinished(self, builderName, build, results):
if buildurl:
r += " Build details are at %s" % buildurl

if build.getResults() != SUCCESS and self.showBlameList:
if self.channel.showBlameList and build.getResults() != SUCCESS and len(build.changes) != 0:
r += ' blamelist: ' + ', '.join([c.who for c in build.changes])

self.send(r)
Expand Down Expand Up @@ -580,15 +579,13 @@ def handleAction(self, data, user):
class IRCContact(Contact):
# this is the IRC-specific subclass of Contact

def __init__(self, channel, dest,
noticeOnChannel = False, showBlameList = False):
Contact.__init__(self, channel, True)
def __init__(self, channel, dest):
Contact.__init__(self, channel)
# when people send us public messages ("buildbot: command"),
# self.dest is the name of the channel ("#twisted"). When they send
# us private messages (/msg buildbot command), self.dest is their
# username.
self.dest = dest
self.noticeOnChannel = noticeOnChannel

def onChannel(self):
return self.dest[0] == '#'
Expand All @@ -601,10 +598,8 @@ def describeUser(self, user):
# userJoined(self, user, channel)

def send(self, message):
if self.onChannel() and self.noticeOnChannel:
self.channel.notice(self.dest, message.encode("ascii", "replace"))
else:
self.channel.msg(self.dest, message.encode("ascii", "replace"))
self.channel.msgOrNotice(self.dest, message.encode("ascii", "replace"))

def act(self, action):
self.channel.me(self.dest, action.encode("ascii", "replace"))

Expand Down Expand Up @@ -703,15 +698,19 @@ def __init__(self, nickname, password, channels, status, categories, notify_even
self.noticeOnChannel = noticeOnChannel
self.showBlameList = showBlameList

def msgOrNotice(self, dest, message):
if self.noticeOnChannel and dest[0] == '#':
self.notice(dest, message)
else:
self.msg(dest, message)

def addContact(self, name, contact):
self.contacts[name] = contact

def getContact(self, name):
if name in self.contacts:
return self.contacts[name]
new_contact = self.contactClass(self, name,
noticeOnChannel = self.noticeOnChannel,
showBlameList = self.showBlameList)
new_contact = self.contactClass(self, name)
self.contacts[name] = new_contact
return new_contact

Expand Down
70 changes: 33 additions & 37 deletions buildbot/test/test_status.py
Expand Up @@ -1323,7 +1323,7 @@ def test_notification_success(self):
self.failUnlessEqual(irc.message, "", "No finish notification generated on exception with notify_events=['success']")

def test_notification_failed(self):
irc = MyContact(showBlameList = True)
irc = MyContact()

my_builder = MyBuilder("builder834")
my_build = MyIrcBuild(my_builder, 862, builder.FAILURE)
Expand All @@ -1338,8 +1338,10 @@ def test_notification_failed(self):
self.failUnlessEqual(irc.message, "", "No started notification with notify_events=['failed']")

irc.message = ""
irc.channel.showBlameList = True
irc.buildFinished(my_builder.getName(), my_build, None)
self.failUnlessEqual(irc.message, "build #862 of builder834 is complete: Failure [step1 step2] Build details are at http://myserver/mypath?build=765 blamelist: author1", "Finish notification generated on failure with notify_events=['failed']")
irc.channel.showBlameList = False

irc.message = ""
my_build.results = builder.SUCCESS
Expand Down Expand Up @@ -1371,9 +1373,10 @@ def test_notification_exception(self):
self.failUnlessEqual(irc.message, "build #862 of builder834 is complete: Exception [step1 step2] Build details are at http://myserver/mypath?build=765", "Finish notification generated on failure with notify_events=['exception']")

irc.message = ""
irc.showBlameList = True
irc.channel.showBlameList = True
irc.buildFinished(my_builder.getName(), my_build, None)
self.failUnlessEqual(irc.message, "build #862 of builder834 is complete: Exception [step1 step2] Build details are at http://myserver/mypath?build=765 blamelist: author1", "Finish notification generated on failure with notify_events=['exception']")
irc.channel.showBlameList = False

irc.message = ""
my_build.results = builder.SUCCESS
Expand Down Expand Up @@ -1403,12 +1406,6 @@ def do_x_to_y_notification_test(self, notify, previous_result, new_result, expec
irc.buildFinished(my_builder.getName(), my_build, None)
self.failUnlessEqual(irc.message, expected_msg, "Finish notification generated on failure with notify_events=['successToFailure']")

if expected_msg != '' and new_result != builder.SUCCESS:
irc.message = ''
irc.showBlameList = True
irc.buildFinished(my_builder.getName(), my_build, None)
self.failUnlessEqual(irc.message, expected_msg + ' blamelist: author1', "Finish notification generated on failure with notify_events=['successToFailure']")

def test_notification_successToFailure(self):
self.do_x_to_y_notification_test(notify="successToFailure", previous_result=builder.SUCCESS, new_result=builder.FAILURE,
expected_msg="build #862 of builder834 is complete: Failure [step1 step2] Build details are at http://myserver/mypath?build=765" )
Expand Down Expand Up @@ -1620,12 +1617,13 @@ class MyChannel:

def __init__(self, notify_events = {}):
self.notify_events = notify_events
self.showBlameList = False

class MyContact(words.Contact):
message = ""

def __init__(self, channel = MyChannel(), showBlameList = False):
words.Contact.__init__(self, channel, showBlameList)
def __init__(self, channel = MyChannel()):
words.Contact.__init__(self, channel)
self.message = ""

def subscribe_to_build_events(self):
Expand All @@ -1637,41 +1635,39 @@ def unsubscribe_from_build_events(self):
def send(self, msg):
self.message += msg

class MyIRCChannel(MyChannel):
def __init__(self):
self.called = []
class MyIrcStatusBot(words.IrcStatusBot):
def msg(self, dest, message):
self.message = ['msg', dest, message]

def __getattr__(self, name):
def f(*args, **kwargs):
self.called += [name]
pass
return f
def notice(self, dest, message):
self.message = ['notice', dest, message]

class IrcStatusBotTester(unittest.TestCase):
def testMsgOrNotice(self):
channel = MyIrcStatusBot('alice', 'pa55w0od', ['#here'],
builder.SUCCESS, None, {})
channel.msgOrNotice('bob', 'hello')
self.failUnlessEqual(channel.message, ['msg', 'bob', 'hello'])

channel.msgOrNotice('#here', 'hello')
self.failUnlessEqual(channel.message, ['msg', '#here', 'hello'])

channel.noticeOnChannel = True

channel.msgOrNotice('bob', 'hello')
self.failUnlessEqual(channel.message, ['msg', 'bob', 'hello'])

channel.msgOrNotice('#here', 'hello')
self.failUnlessEqual(channel.message, ['notice', '#here', 'hello'])

class IRCContactTester(unittest.TestCase):
def testOnChannel(self):
contact = words.IRCContact(MyIRCChannel(), 'alice')
contact = words.IRCContact(MyChannel(), 'alice')
self.failUnlessEqual(contact.onChannel(), False)

contact = words.IRCContact(MyIRCChannel(), '#somewhere')
contact = words.IRCContact(MyChannel(), '#somewhere')
self.failUnlessEqual(contact.onChannel(), True)

def testSendToUser(self):
ch = MyIRCChannel()
contact = words.IRCContact(ch, 'alice')
contact.send('hello')
self.failUnlessEqual(ch.called, ['msg'])

def testSendToChannel(self):
ch = MyIRCChannel()
contact = words.IRCContact(ch, '#somewhere', noticeOnChannel = True)
contact.send('hi')
self.failUnlessEqual(ch.called, ['notice'])

ch = MyIRCChannel()
contact = words.IRCContact(ch, '#somewhere')
contact.send('hi')
self.failUnlessEqual(ch.called, ['msg'])

class StepStatistics(unittest.TestCase):
def testStepStatistics(self):
status = builder.BuildStatus(builder.BuilderStatus("test"), 123)
Expand Down

0 comments on commit b8d2a5c

Please sign in to comment.