~bzr-pqm/bzr/bzr.dev

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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Copyright (C) 2004, 2005 by Canonical Ltd

# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


import os
import sys
import stat
from cStringIO import StringIO

from bzrlib.errors import (NoSuchFile, FileExists,
                           TransportNotPossible, ConnectionError)
from bzrlib.tests import TestCase, TestCaseInTempDir
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
from bzrlib.transport import (_get_protocol_handlers,
                              _get_transport_modules,
                              register_lazy_transport,
                              _set_protocol_handlers,
                              urlescape,
                              )
from bzrlib.osutils import pathjoin


def _append(fn, txt):
    """Append the given text (file-like object) to the supplied filename."""
    f = open(fn, 'ab')
    f.write(txt)
    f.flush()
    f.close()
    del f


if sys.platform != 'win32':
    def check_mode(test, path, mode):
        """Check that a particular path has the correct mode."""
        actual_mode = stat.S_IMODE(os.stat(path).st_mode)
        test.assertEqual(mode, actual_mode,
            'mode of %r incorrect (%o != %o)' % (path, mode, actual_mode))
else:
    def check_mode(test, path, mode):
        """On win32 chmod doesn't have any effect, 
        so don't actually check anything
        """
        return


class TestTransport(TestCase):
    """Test the non transport-concrete class functionality."""

    def test_urlescape(self):
        self.assertEqual('%25', urlescape('%'))

    def test__get_set_protocol_handlers(self):
        handlers = _get_protocol_handlers()
        self.assertNotEqual({}, handlers)
        try:
            _set_protocol_handlers({})
            self.assertEqual({}, _get_protocol_handlers())
        finally:
            _set_protocol_handlers(handlers)

    def test_get_transport_modules(self):
        handlers = _get_protocol_handlers()
        class SampleHandler(object):
            """I exist, isnt that enough?"""
        try:
            my_handlers = {}
            _set_protocol_handlers(my_handlers)
            register_lazy_transport('foo', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
            register_lazy_transport('bar', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
            self.assertEqual([SampleHandler.__module__],
                             _get_transport_modules())
        finally:
            _set_protocol_handlers(handlers)
            

class TestTransportMixIn(object):
    """Subclass this, and it will provide a series of tests for a Transport.
    It assumes that the Transport object is connected to the 
    current working directory.  So that whatever is done 
    through the transport, should show up in the working 
    directory, and vice-versa.

    This also tests to make sure that the functions work with both
    generators and lists (assuming iter(list) is effectively a generator)
    """
    readonly = False
    def get_transport(self):
        """Children should override this to return the Transport object.
        """
        raise NotImplementedError

    def test_put(self):
        t = self.get_transport()

        if not self.readonly:
            t.put('mode644', 'test text\n', mode=0644)
            check_mode(self, 'mode644', 0644)

            t.put('mode666', 'test text\n', mode=0666)
            check_mode(self, 'mode666', 0666)

            t.put('mode600', 'test text\n', mode=0600)
            check_mode(self, 'mode600', 0600)

            # Yes, you can put a file such that it becomes readonly
            t.put('mode400', 'test text\n', mode=0400)
            check_mode(self, 'mode400', 0400)

            t.put_multi([('mmode644', 'text\n')], mode=0644)
            check_mode(self, 'mmode644', 0644)

        # TODO: jam 20051215 test put_multi with a mode. I didn't bother because
        #                    it seems most people don't like the _multi functions


    def test_mkdir(self):
        t = self.get_transport()

        if not self.readonly:
            # Test mkdir with a mode
            t.mkdir('dmode755', mode=0755)
            check_mode(self, 'dmode755', 0755)

            t.mkdir('dmode555', mode=0555)
            check_mode(self, 'dmode555', 0555)

            t.mkdir('dmode777', mode=0777)
            check_mode(self, 'dmode777', 0777)

            t.mkdir('dmode700', mode=0700)
            check_mode(self, 'dmode700', 0700)

            # TODO: jam 20051215 test mkdir_multi with a mode
            t.mkdir_multi(['mdmode755'], mode=0755)
            check_mode(self, 'mdmode755', 0755)

    def test_copy_to(self):
        import tempfile
        from bzrlib.transport.local import LocalTransport

        t = self.get_transport()
        for mode in (0666, 0644, 0600, 0400):
            dtmp_base, local_t = get_temp_local()
            t.copy_to(files, local_t, mode=mode)
            for f in files:
                check_mode(self, os.path.join(dtmp_base, f), mode)



class MemoryTransportTest(TestCase):
    """Memory transport specific tests."""

    def test_parameters(self):
        import bzrlib.transport.memory as memory
        transport = memory.MemoryTransport()
        self.assertEqual(True, transport.listable())
        self.assertEqual(False, transport.should_cache())
        self.assertEqual(False, transport.is_readonly())