1
# Copyright (C) 2005, 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
from bzrlib.builtins import cmd_init
21
from bzrlib.hooks import Hooks
22
from bzrlib.tests.test_ftp_transport import TestCaseWithFTPServer
23
from bzrlib.transport import (
27
from bzrlib.transport.ftp import FtpTransport
30
class InstrumentedTransport(FtpTransport):
31
"""Instrumented transport class to test use by init command"""
34
"""See FtpTransport._get_FTP.
36
This is where we can detect if the connection is reused
37
or if a new one is created. This a bit ugly, but it's the
38
easiest until transport classes are refactored.
40
instance = super(InstrumentedTransport, self)._get_FTP()
41
self.hooks.run_hook('get_FTP', self, instance)
45
class TransportHooks(Hooks):
46
"""Dict-mapping hook name to a list of callables for transport hooks"""
50
# invoked when the transport is about to create or reuse
51
# an ftp connection. The api signature is (transport, ftp_instance)
54
# FIXME: Why don't we have Hooks.run_hooks ?
55
def run_hook(self, hook_name, *args, **kwargs):
57
hooks = self[hook_name]
59
raise errors.UnknownHook(self.__class__.__name__, hook_name)
63
# FIXME: Why don't we have Hooks.uninstall_hook ?
64
def uninstall_hook(self, hook_name, a_callable):
66
self[hook_name].remove(a_callable)
68
raise errors.UnknownHook(self.__class__.__name__, hook_name)
69
# FIXME: catch ValueError and raise errors.UnknownHookValue ?
72
# install the default hooks into the Branch class.
73
InstrumentedTransport.hooks = TransportHooks()
76
class TestInit(TestCaseWithFTPServer):
79
super(TestInit, self).setUp()
80
InstrumentedTransport.hooks.install_hook('get_FTP',
81
self.get_connection_hook)
82
# Make our instrumented transport the default ftp transport
83
register_transport('ftp://', InstrumentedTransport)
86
InstrumentedTransport.hooks.uninstall_hook('get_FTP',
87
self.get_connection_hook)
88
unregister_transport('ftp://', InstrumentedTransport)
90
self.addCleanup(cleanup)
94
def get_connection_hook(self, transport, connection):
95
if connection is not None and connection not in self.connections:
96
self.connections.append(connection)
100
cmd.run(self.get_url())
101
self.assertEquals(1, len(self.connections))
103
def test_branch(self):