~dpb/tarmac/commit-message-newline

« back to all changes in this revision

Viewing changes to tarmac/tests/mock.py

  • Committer: Tarmac
  • Author(s): Rodney Dawes
  • Date: 2013-10-31 22:00:00 UTC
  • mfrom: (412.2.5 home-loader)
  • Revision ID: tarmac-20131031220000-ud7iqws2g5xe4acs
Fix plug-in loading to use execfile() rather than relying on imports.
Remove the mock.py that conflicts namespace with python-mock.
Add tests for the load_plugins code.
Add a plug-in for testing the load_plugins code with.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009 Paul Hummer
2
 
# This file is part of Tarmac.
3
 
#
4
 
# Tarmac is free software: you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License version 3 as
6
 
# published by
7
 
# the Free Software Foundation.
8
 
#
9
 
# Tarmac is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with Tarmac.  If not, see <http://www.gnu.org/licenses/>.
16
 
 
17
 
'''Mock objects for Tarmac!'''
18
 
from base64 import b64encode
19
 
import math
20
 
import os
21
 
 
22
 
from bzrlib.bzrdir import BzrDir
23
 
 
24
 
from tarmac.bin.commands import TarmacCommand
25
 
 
26
 
 
27
 
class MockLPProject(object):
28
 
    '''A mock LP Project.'''
29
 
 
30
 
    def __init__(self):
31
 
        self.name = b64encode(
32
 
            os.urandom(int(math.ceil(0.75 * 10))), '-_')[:10]
33
 
 
34
 
 
35
 
class MockLPBranch(object):
36
 
    '''A mock LP Branch.'''
37
 
 
38
 
    def __init__(self, tree_dir, source_branch=None):
39
 
        self.tree_dir = tree_dir
40
 
        os.makedirs(tree_dir)
41
 
        if source_branch:
42
 
            source_dir = source_branch._internal_bzr_branch.bzrdir
43
 
            bzrdir = source_dir.sprout(tree_dir)
44
 
            self._internal_tree, self._internal_bzr_branch = \
45
 
                    bzrdir.open_tree_or_branch(tree_dir)
46
 
            self.revision_count = source_branch.revision_count
47
 
        else:
48
 
            self._internal_bzr_branch = BzrDir.create_branch_convenience(
49
 
                tree_dir)
50
 
            self.revision_count = 0
51
 
        self.bzr_identity = 'lp:%s' % os.path.basename(self.tree_dir)
52
 
        self.web_link = self.bzr_identity
53
 
        self.project = MockLPProject()
54
 
 
55
 
 
56
 
class cmd_mock(TarmacCommand):
57
 
    '''A mock command.'''
58
 
 
59
 
    def run(self):
60
 
        """Just a dummy command that does nothing."""
61
 
 
62
 
 
63
 
class MockModule(object):
64
 
    """A mock module."""
65
 
 
66
 
    def __init__(self):
67
 
        self.__dict__['cmd_mock'] = cmd_mock
68
 
 
69
 
 
70
 
class Thing(dict):
71
 
    """Quickly create an object with given attributes."""
72
 
 
73
 
    def __init__(self, **names):
74
 
        super(Thing, self).__init__(self, **names)
75
 
        self.__dict__.update(names)
76
 
 
77
 
    def __iter__(self):
78
 
        for item in self.values():
79
 
            if not callable(item):
80
 
                yield item