~jkakar/commandant/trunk

« back to all changes in this revision

Viewing changes to commandant/tests/test_entry_point.py

  • Committer: Jamu Kakar
  • Date: 2009-07-12 04:11:59 UTC
  • mfrom: (20.2.4 bzrlib-command)
  • Revision ID: jkakar@kakar.ca-20090712041159-huwiud65wbr5gmnq
Merged bzrlib-command.

Commands have been refactored to subclass bzrlib.commands.Command.
CommandController uses the command hooks system in Bazaar to
register commands.  Using bzrlib.commands.Command makes it possible
to create Commandant commands with rich command-line argument and
option semantics.  The controller uses Bazaar's machinery to run
commands from command-line arguments to enforce proper command-line
argument handling.  Help topics have been refactored to work with
the new system.  Information about command arguments and options is
not currently rendered in help output.  The help system should
eventually be refactored to use functionality provided by
bzrlib.help.

Command.hook support is relatively new in bzrlib.  As a result, at
this time, this merge requires bzrlib from lp:bzr for Commandant and
the test suite to work properly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import os
20
20
import sys
21
21
 
 
22
from bzrlib.errors import BzrCommandError
 
23
from bzrlib.commands import all_command_names
 
24
 
22
25
from testresources import ResourcedTestCase
23
26
 
24
 
from commandant.errors import UsageError, UnknownCommandError
 
27
from commandant.errors import UsageError
 
28
from commandant.commands import Command
25
29
from commandant.entry_point import main
26
30
from commandant.tests.resources import TemporaryDirectoryResource
27
31
 
30
34
 
31
35
    resources = [("directory", TemporaryDirectoryResource())]
32
36
 
 
37
    def tearDown(self):
 
38
        # Reset bzrlib command hooks.
 
39
        Command.hooks["list_commands"]._callbacks = []
 
40
        Command.hooks["list_commands"]._callback_names = {}
 
41
        Command.hooks["get_command"]._callbacks = []
 
42
        Command.hooks["get_command"]._callback_names = {}
 
43
 
 
44
        super(MainTest, self).tearDown()
 
45
 
33
46
    def test_incorrect_number_of_arguments(self):
34
47
        """
35
48
        L{main} requires a minimum of 2 arguments, the name of the program,
62
75
            sys.stdout = original_stdout
63
76
 
64
77
    def test_unknown_command(self):
65
 
        self.assertRaises(UnknownCommandError, main,
 
78
        self.assertRaises(BzrCommandError, main,
66
79
                          ["commandant", self.directory.path, "unknown"])
67
80
 
68
81
    def test_controller_path(self):
78
91
class cmd_test_command(Command):
79
92
    def run(self):
80
93
        file = open('%s', 'w')
81
 
        file.write(self.commandant.path)
 
94
        file.write('Hello, world!')
82
95
        file.close()
83
96
""" % (hello_path,)
84
97
        path = os.path.join(self.directory.path, "test_command.py")
85
98
        self.directory.make_path(content=content, path=path)
86
99
        main(["commandant", self.directory.path, "test-command"])
87
 
        self.assertEquals(open(hello_path).read(), self.directory.path)
 
100
        self.assertEquals(open(hello_path).read(), "Hello, world!")
 
101
 
 
102
    def test_install_bzr_hooks(self):
 
103
        """
 
104
        L{CommandController.install_bzrlib_hooks} is called to hook the
 
105
        controller into C{bzrlib}.
 
106
        """
 
107
        original_stdout = sys.stdout
 
108
        sys.stdout = StringIO()
 
109
        try:
 
110
            main(["commandant", self.directory.path, "version"])
 
111
        finally:
 
112
            sys.stdout = original_stdout
 
113
        self.assertEquals(all_command_names(), set(["help", "version"]))