aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/util/change_import.py
blob: e461dda6272bf6db27014ffa1fcf663246e107e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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

from __future__ import with_statement

import os
import shutil
import cPickle
from buildbot.test.util import db

from buildbot.changes.changes import Change, OldChangeMaster

class ChangeImportMixin(db.RealDatabaseMixin):
    """
    We have a number of tests that examine the results of importing particular
    flavors of Change objects.  This class uses some pickling to make this easy
    to test.

    This is a subclass of RealDatabaseMixin, so do not inherit from that class
    separately!

    >>> self.make_pickle(self.make_change(who=u'jimmy'), self.make_change(who='johnny'))
    """
    def make_pickle(self, *changes, **kwargs):
        recode_fn = kwargs.pop('recode_fn', None)
        cm = OldChangeMaster()
        cm.changes = changes
        if recode_fn:
            recode_fn(cm)
        with open(self.changes_pickle, "wb") as f:
            cPickle.dump(cm, f)

    def make_change(self, **kwargs):
        return Change(**kwargs)

    def setUpChangeImport(self):
        self.basedir = os.path.abspath("basedir")
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        os.makedirs(self.basedir)
        self.changes_pickle = os.path.join(self.basedir, "changes.pck")
        return self.setUpRealDatabase()

    def tearDownChangeImport(self):
        d = self.tearDownRealDatabase()
        def rmtree(_):
            if os.path.exists(self.basedir):
                shutil.rmtree(self.basedir)
        d.addCallback(rmtree)
        return d