Skip to content

Commit

Permalink
add and use buildbot.test.util.dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Jan 17, 2011
1 parent 592260a commit 967c2a7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 38 deletions.
14 changes: 4 additions & 10 deletions master/buildbot/test/integration/test_upgrade.py
Expand Up @@ -14,7 +14,6 @@
# Copyright Buildbot Team Members

import os
import shutil
import cPickle
import tarfile
from twisted.python import util
Expand All @@ -23,8 +22,7 @@
import sqlalchemy as sa
import migrate.versioning.api
from buildbot.db import connector
from buildbot.test.util import db
from buildbot.test.util import change_import
from buildbot.test.util import change_import, db, dirs

class UpgradeTestMixin(object):
"""Supporting code to test upgrading from older versions by untarring a
Expand Down Expand Up @@ -101,23 +99,19 @@ def fix_pickle_encoding(self, old_encoding):
changemgr.recode_changes(old_encoding, quiet=True)
cPickle.dump(changemgr, open(changes_file, "w"))

class UpgradeTestEmpty(db.RealDatabaseMixin, DBUtilsMixin, unittest.TestCase):
class UpgradeTestEmpty(dirs.DirsMixin, db.RealDatabaseMixin, DBUtilsMixin, unittest.TestCase):

def setUp(self):
self.setUpRealDatabase()

self.basedir = os.path.abspath("basedir")
if os.path.exists(self.basedir):
shutil.rmtree(self.basedir)
os.makedirs(self.basedir)
self.setUpDirs('basedir')

self.db = connector.DBConnector(self.db_url, self.basedir)

def tearDown(self):
if os.path.exists(self.basedir):
shutil.rmtree(self.basedir)

self.tearDownRealDatabase()
self.tearDownDirs()

def test_emptydb_modelmatches(self):
d = self.db.model.upgrade()
Expand Down
21 changes: 10 additions & 11 deletions master/buildbot/test/unit/test_changes_mail.py
Expand Up @@ -14,19 +14,18 @@
# Copyright Buildbot Team Members

import os
import shutil
from twisted.trial import unittest
from buildbot.test.util import changesource
from buildbot.test.util import changesource, dirs
from buildbot.changes import mail

class TestMaildirSource(changesource.ChangeSourceMixin, unittest.TestCase):
class TestMaildirSource(changesource.ChangeSourceMixin, dirs.DirsMixin,
unittest.TestCase):

def setUp(self):
self.maildir = os.path.abspath("maildir")

d = self.setUpChangeSource()
def setup_dirs(_):
self.maildir = os.path.abspath("maildir")
if os.path.exists(self.maildir):
shutil.rmtree(self.maildir)
d.addCallback(setup_dirs)
d.addCallback(lambda _ : self.setUpDirs(self.maildir))
return d

def populateMaildir(self):
Expand All @@ -46,9 +45,9 @@ def assertMailProcessed(self):
self.assertTrue(os.path.exists(os.path.join(self.maildir, "cur", "newmsg")))

def tearDown(self):
if os.path.exists(self.maildir):
shutil.rmtree(self.maildir)
return self.tearDownChangeSource()
d = self.tearDownDirs()
d.addCallback(lambda _ : self.tearDownChangeSource())
return d

# tests

Expand Down
13 changes: 6 additions & 7 deletions master/buildbot/test/unit/test_persistent_queue.py
Expand Up @@ -15,24 +15,23 @@


import os
import shutil
from twisted.trial import unittest
from buildbot.test.util import dirs

from buildbot.status.persistent_queue import DequeMemoryQueue, DiskQueue, \
IQueue, ListMemoryQueue, MemoryQueue, PersistentQueue, WriteFile

class test_Queues(unittest.TestCase):
class test_Queues(dirs.DirsMixin, unittest.TestCase):

def setUp(self):
if os.path.isdir('fake_dir'):
shutil.rmtree('fake_dir')
self.setUpDirs('fake_dir')

def tearDown(self):
if os.path.isdir('fake_dir'):
self.assertEqual([], os.listdir('fake_dir'))
self.assertEqual([], os.listdir('fake_dir'))
self.tearDownDirs()

def testQueued(self):
# Verify behavior when starting up with queued items on disk.
os.mkdir('fake_dir')
WriteFile(os.path.join('fake_dir', '3'), 'foo3')
WriteFile(os.path.join('fake_dir', '5'), 'foo5')
WriteFile(os.path.join('fake_dir', '8'), 'foo8')
Expand Down
14 changes: 4 additions & 10 deletions master/buildbot/test/unit/test_util_maildir.py
Expand Up @@ -14,31 +14,25 @@
# Copyright Buildbot Team Members

import os
import shutil
from twisted.trial import unittest
from twisted.internet import defer
from buildbot.util import maildir
from buildbot.test.util import dirs

class TestMaildirService(unittest.TestCase):
class TestMaildirService(dirs.DirsMixin, unittest.TestCase):
def setUp(self):
self.maildir = os.path.abspath("maildir")
if os.path.exists(self.maildir):
shutil.rmtree(self.maildir)

self.newdir = os.path.join(self.maildir, "new")
os.makedirs(self.newdir)
self.curdir = os.path.join(self.maildir, "cur")
os.makedirs(self.curdir)
self.tmpdir = os.path.join(self.maildir, "tmp")
os.makedirs(self.tmpdir)
self.setUpDirs(self.maildir, self.newdir, self.curdir, self.tmpdir)

self.svc = None

def tearDown(self):
if self.svc:
self.svc.stopService()
if os.path.exists(self.maildir):
shutil.rmtree(self.maildir)
self.tearDownDirs()

# tests

Expand Down
40 changes: 40 additions & 0 deletions master/buildbot/test/util/dirs.py
@@ -0,0 +1,40 @@
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

import os
import shutil
from twisted.internet import defer

class DirsMixin(object):

_dirs = None

def setUpDirs(self, *dirs):
"""Make sure C{dirs} exist and are empty, and set them up to be deleted
in tearDown."""
self._dirs = map(os.path.abspath, dirs)
for dir in self._dirs:
if os.path.exists(dir):
shutil.rmtree(dir)
os.makedirs(dir)
# return a deferred to make chaining easier
return defer.succeed(None)

def tearDownDirs(self):
for dir in self._dirs:
if os.path.exists(dir):
shutil.rmtree(dir)
# return a deferred to make chaining easier
return defer.succeed(None)

0 comments on commit 967c2a7

Please sign in to comment.