Skip to content

Commit

Permalink
use StdoutAssertionsMixin in the remaining useful places
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Apr 12, 2012
1 parent 9c221c7 commit 745ce33
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
27 changes: 11 additions & 16 deletions master/buildbot/test/unit/test_scripts_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,38 @@
# Copyright Buildbot Team Members

import os
import sys
import string
import cStringIO
from twisted.trial import unittest
from buildbot.scripts import base
from buildbot.test.util import dirs
from buildbot.test.util import dirs, misc

class TestIBD(dirs.DirsMixin, unittest.TestCase):
class TestIBD(dirs.DirsMixin, misc.StdoutAssertionsMixin, unittest.TestCase):

def setUp(self):
self.setUpDirs('test')
self.stdout = cStringIO.StringIO()
self.patch(sys, 'stdout', self.stdout)

def assertPrinted(self, what):
self.tearDownDirs()
self.assertEqual(self.stdout.getvalue().strip(), what)
self.setUpStdoutAssertions()

def test_isBuildmasterDir_no_dir(self):
self.assertFalse(base.isBuildmasterDir(os.path.abspath('test/nosuch')))
self.assertPrinted('no buildbot.tac')
self.assertInStdout('no buildbot.tac')

def test_isBuildmasterDir_no_file(self):
self.assertFalse(base.isBuildmasterDir(os.path.abspath('test')))
self.assertPrinted('no buildbot.tac')
self.assertInStdout('no buildbot.tac')

def test_isBuildmasterDir_no_Application(self):
with open(os.path.join('test', 'buildbot.tac'), 'w') as f:
f.write("foo\nx = Application('buildslave')\nbar")
self.assertFalse(base.isBuildmasterDir(os.path.abspath('test')))
self.assertPrinted('')
self.assertWasQuiet()

def test_isBuildmasterDir_matches(self):
with open(os.path.join('test', 'buildbot.tac'), 'w') as f:
f.write("foo\nx = Application('buildmaster')\nbar")
self.assertTrue(base.isBuildmasterDir(os.path.abspath('test')))
self.assertPrinted('')
self.assertWasQuiet()

class TestSubcommandOptions(unittest.TestCase):

Expand Down Expand Up @@ -90,13 +85,15 @@ def test_buildbotOptions_override(self):
opts = self.parse(self.ParamsAndOptions, '--volume', '7')
self.assertEqual(opts['volume'], '7')

class TestLoadOptionsFile(dirs.DirsMixin, unittest.TestCase):
class TestLoadOptionsFile(dirs.DirsMixin, misc.StdoutAssertionsMixin,
unittest.TestCase):

def setUp(self):
self.setUpDirs('test', 'home')
self.opts = base.SubcommandOptions()
self.dir = os.path.abspath('test')
self.home = os.path.abspath('home')
self.setUpStdoutAssertions()

def tearDown(self):
self.tearDownDirs()
Expand Down Expand Up @@ -156,11 +153,9 @@ def test_loadOptionsFile_subdirs_at_homedir(self):

def test_loadOptionsFile_syntax_error(self):
self.writeOptionsFile(self.dir, 'abc=abc')
stdout = cStringIO.StringIO()
self.patch(sys, 'stdout', stdout)
self.assertRaises(NameError, lambda :
self.do_loadOptionsFile(_here=self.dir, exp={}))
self.assertIn('error while reading', stdout.getvalue().strip())
self.assertInStdout('error while reading')

def test_loadOptionsFile_toomany(self):
subdir = os.path.join(self.dir, *tuple(string.lowercase))
Expand Down
4 changes: 0 additions & 4 deletions master/buildbot/test/unit/test_scripts_create_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
from __future__ import with_statement

import os
import sys
import cStringIO
import mock
from twisted.trial import unittest
from twisted.internet import defer
Expand Down Expand Up @@ -74,8 +72,6 @@ def check(_):
return d

def test_createMaster_loud(self):
self.stdout = cStringIO.StringIO()
self.patch(sys, 'stdout', self.stdout)
d = self.do_test_createMaster(mkconfig(quiet=False))
@d.addCallback
def check(_):
Expand Down
8 changes: 4 additions & 4 deletions master/buildbot/test/unit/test_scripts_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from twisted.trial import unittest
from twisted.python import usage
from buildbot.scripts import base, runner
from buildbot.test.util import misc

class OptionsMixin(object):

Expand Down Expand Up @@ -755,10 +756,11 @@ def test_no_master(self):
lambda : self.parse('-op=foo'))


class TestOptions(OptionsMixin, unittest.TestCase):
class TestOptions(OptionsMixin, misc.StdoutAssertionsMixin, unittest.TestCase):

def setUp(self):
self.setUpOptions()
self.setUpStdoutAssertions()

def parse(self, *args):
self.opts = runner.Options()
Expand All @@ -770,13 +772,11 @@ def test_defaults(self):
lambda : self.parse())

def test_version(self):
stdout = cStringIO.StringIO()
self.patch(sys, 'stdout', stdout)
try:
self.parse('--version')
except SystemExit, e:
self.assertEqual(e.args[0], 0)
self.assertIn('Buildbot version:', stdout.getvalue())
self.assertInStdout('Buildbot version:')

class TestRun(unittest.TestCase):

Expand Down
14 changes: 6 additions & 8 deletions master/buildbot/test/unit/test_scripts_sendchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
#
# Copyright Buildbot Team Members

import sys
import cStringIO
from twisted.trial import unittest
from twisted.internet import reactor, defer
from buildbot.scripts import sendchange
from buildbot.clients import sendchange as sendchange_client
from buildbot.test.util import misc

class TestSendChange(unittest.TestCase):
class TestSendChange(misc.StdoutAssertionsMixin, unittest.TestCase):

class FakeSender:
def __init__(self, testcase, master, auth, encoding=None):
Expand Down Expand Up @@ -52,8 +51,7 @@ def Sender_constr(*args, **kwargs):
# undo the effects of @in_reactor
self.patch(sendchange, 'sendchange', sendchange.sendchange._orig)

self.stdout = cStringIO.StringIO()
self.patch(sys, 'stdout', self.stdout)
self.setUpStdoutAssertions()

def test_sendchange_config(self):
d = sendchange.sendchange(dict(encoding='utf16', who='me',
Expand All @@ -64,7 +62,7 @@ def test_sendchange_config(self):
def check(rc):
self.assertEqual((self.sender.master, self.sender.auth,
self.sender.encoding, self.sender.send_kwargs,
self.stdout.getvalue(), rc),
self.getStdout(), rc),
('m', ['a','b'], 'utf16', {
'branch': 'br',
'category': 'cat',
Expand All @@ -78,15 +76,15 @@ def check(rc):
'when': 1234.0,
'who': 'me',
'vc': 'git'},
'change sent successfully\n', 0))
'change sent successfully', 0))
d.addCallback(check)
return d

def test_sendchange_fail(self):
self.fail = True
d = sendchange.sendchange({})
def check(rc):
self.assertEqual((self.stdout.getvalue().split('\n')[0], rc),
self.assertEqual((self.getStdout().split('\n')[0], rc),
('change not sent:', 1))
d.addCallback(check)
return d
2 changes: 2 additions & 0 deletions master/buildbot/test/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ def assertWasQuiet(self):
def assertInStdout(self, exp):
self.assertIn(exp, self.stdout.getvalue())

def getStdout(self):
return self.stdout.getvalue().strip()

0 comments on commit 745ce33

Please sign in to comment.