~jkakar/commandant/trunk

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Commandant is a framework for building command-oriented tools.
# Copyright (C) 2009 Jamshed Kakar.
#
# This program 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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.

"""Test resources."""

from cStringIO import StringIO
import os
import shutil
import tempfile

from bzrlib.commands import Command

from testresources import TestResource

from commandant.controller import CommandController


class TemporaryDirectory(object):
    """A temporary directory resource/"""

    def setUp(self):
        self._counter = 1
        self.path = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.path)

    def make_dir(self):
        """Make a temporary directory.

        @return: The path to the new directory.
        """
        path = self.make_path()
        os.mkdir(path)
        return path

    def make_path(self, content=None, path=None):
        """Create a new C{path} name, optionally writing C{content} to it.

        @return: The C{path} provided or a new one if it was C{None}.
        """
        if path is None:
            self._counter += 1
            path = "%s/%03d" % (self.path, self._counter)
        if content is not None:
            file = open(path, "w")
            try:
                file.write(content)
            finally:
                file.close()
        return path


class TemporaryDirectoryResource(TestResource):
    """Resource provides and destroys temporary directories."""

    def make(self, dependency_resources):
        """Create a temporary directory."""
        directory = TemporaryDirectory()
        directory.setUp()
        return directory

    def clean(self, directory):
        """Destroy a temporary directory."""
        directory.tearDown()


class FakeCommand(Command):
    """Summary text.

    Long descriptive text that
    spans multiples lines.
    """

    def run(self, *args, **kwargs):
        self.outf.write(repr((args, kwargs)))


# This class intentionally doesn't have a docstring.
class FakeHelpTopic(Command):

    name = "fake-help-topic"

    def get_summary(self):
        """Get a short topic summary for use in a topic listing."""
        return "A fake summary!"

    def get_text(self):
        """Get topic content."""
        return "Fake descriptive help text."


class CommandFactory(object):
    """Factory creates C{bzrlib.commands.Command} and L{HelpTopic} objects."""

    def __init__(self, program_name, program_version, program_summary,
                 program_url):
        super(CommandFactory, self).__init__()
        self.controller = CommandController(program_name, program_version,
                                            program_summary, program_url)

    def create_command(self, name, command_class=FakeCommand):
        """Register a C{command_class} using C{name}.

        @return: A C{command_class} instance.
        """
        self.controller.register_command(name, command_class)
        command = self.controller.get_command(name)
        command.controller = self.controller
        command.outf = StringIO()
        return command

    def create_help_topic(self, name, help_topic_class=FakeHelpTopic):
        """Register a C{help_topic_class} using C{name}.

        @return: A C{help_topic_class} instance.
        """
        self.controller.register_help_topic(name, help_topic_class)
        help_topic = self.controller.get_help_topic(name)
        help_topic.controller = self.controller
        help_topic.outf = StringIO()
        return help_topic


class CommandFactoryResource(TestResource):
    """Resource creates L{CommandFactory}s."""

    resources = [("directory", TemporaryDirectoryResource())]

    def __init__(self, program_name=None, program_version=None,
                 program_summary=None, program_url=None):
        super(CommandFactoryResource, self).__init__()
        self.program_name = program_name
        self.program_version = program_version
        self.program_summary = program_summary
        self.program_url = program_url

    def make(self, dependency_resources):
        """Create a L{CommandFactory}."""
        return CommandFactory(self.program_name, self.program_version,
                              self.program_summary, self.program_url)