~bzr-pqm/bzr/bzr.dev

907.1.48 by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass.
1
# Copyright (C) 2004, 2005 by Canonical Ltd
2
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.
7
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.
12
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
16
17
1185.11.22 by John Arbash Meinel
Major refactoring of testtransport.
18
import os
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
19
import sys
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
20
import stat
1185.11.22 by John Arbash Meinel
Major refactoring of testtransport.
21
from cStringIO import StringIO
1442.1.44 by Robert Collins
Many transport related tweaks:
22
1185.50.13 by John Arbash Meinel
Expanded the Transport test suite. Including delete, copy, move, etc. Updated SftpTransport to conform.
23
from bzrlib.errors import (NoSuchFile, FileExists,
24
                           TransportNotPossible, ConnectionError)
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
25
from bzrlib.tests import TestCase, TestCaseInTempDir
26
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
27
from bzrlib.transport import (_get_protocol_handlers,
28
                              _get_transport_modules,
29
                              register_lazy_transport,
30
                              _set_protocol_handlers,
31
                              urlescape,
32
                              )
1185.31.33 by John Arbash Meinel
A couple more path.join statements needed changing.
33
from bzrlib.osutils import pathjoin
1442.1.44 by Robert Collins
Many transport related tweaks:
34
1185.11.19 by John Arbash Meinel
Testing put and append, also testing agaist file-like objects as well as strings.
35
36
def _append(fn, txt):
37
    """Append the given text (file-like object) to the supplied filename."""
38
    f = open(fn, 'ab')
39
    f.write(txt)
40
    f.flush()
41
    f.close()
42
    del f
1185.11.1 by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet.
43
1469 by Robert Collins
Change Transport.* to work with URL's.
44
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
45
if sys.platform != 'win32':
46
    def check_mode(test, path, mode):
47
        """Check that a particular path has the correct mode."""
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
48
        actual_mode = stat.S_IMODE(os.stat(path).st_mode)
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
49
        test.assertEqual(mode, actual_mode,
50
            'mode of %r incorrect (%o != %o)' % (path, mode, actual_mode))
51
else:
52
    def check_mode(test, path, mode):
53
        """On win32 chmod doesn't have any effect, 
54
        so don't actually check anything
55
        """
56
        return
57
58
1185.58.3 by John Arbash Meinel
code cleanup
59
class TestTransport(TestCase):
60
    """Test the non transport-concrete class functionality."""
61
62
    def test_urlescape(self):
63
        self.assertEqual('%25', urlescape('%'))
64
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
65
    def test__get_set_protocol_handlers(self):
66
        handlers = _get_protocol_handlers()
67
        self.assertNotEqual({}, handlers)
68
        try:
69
            _set_protocol_handlers({})
70
            self.assertEqual({}, _get_protocol_handlers())
71
        finally:
72
            _set_protocol_handlers(handlers)
73
74
    def test_get_transport_modules(self):
75
        handlers = _get_protocol_handlers()
76
        class SampleHandler(object):
77
            """I exist, isnt that enough?"""
78
        try:
79
            my_handlers = {}
80
            _set_protocol_handlers(my_handlers)
81
            register_lazy_transport('foo', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
82
            register_lazy_transport('bar', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
83
            self.assertEqual([SampleHandler.__module__],
84
                             _get_transport_modules())
85
        finally:
86
            _set_protocol_handlers(handlers)
87
            
1469 by Robert Collins
Change Transport.* to work with URL's.
88
1185.11.22 by John Arbash Meinel
Major refactoring of testtransport.
89
class TestTransportMixIn(object):
90
    """Subclass this, and it will provide a series of tests for a Transport.
91
    It assumes that the Transport object is connected to the 
92
    current working directory.  So that whatever is done 
93
    through the transport, should show up in the working 
94
    directory, and vice-versa.
1185.11.1 by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet.
95
96
    This also tests to make sure that the functions work with both
97
    generators and lists (assuming iter(list) is effectively a generator)
98
    """
1185.11.22 by John Arbash Meinel
Major refactoring of testtransport.
99
    readonly = False
100
    def get_transport(self):
101
        """Children should override this to return the Transport object.
102
        """
103
        raise NotImplementedError
104
105
    def test_put(self):
106
        t = self.get_transport()
107
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
108
        if not self.readonly:
109
            t.put('mode644', 'test text\n', mode=0644)
110
            check_mode(self, 'mode644', 0644)
111
112
            t.put('mode666', 'test text\n', mode=0666)
113
            check_mode(self, 'mode666', 0666)
114
115
            t.put('mode600', 'test text\n', mode=0600)
116
            check_mode(self, 'mode600', 0600)
117
118
            # Yes, you can put a file such that it becomes readonly
119
            t.put('mode400', 'test text\n', mode=0400)
120
            check_mode(self, 'mode400', 0400)
121
122
            t.put_multi([('mmode644', 'text\n')], mode=0644)
123
            check_mode(self, 'mmode644', 0644)
124
125
        # TODO: jam 20051215 test put_multi with a mode. I didn't bother because
126
        #                    it seems most people don't like the _multi functions
127
128
1185.11.22 by John Arbash Meinel
Major refactoring of testtransport.
129
    def test_mkdir(self):
130
        t = self.get_transport()
131
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
132
        if not self.readonly:
133
            # Test mkdir with a mode
134
            t.mkdir('dmode755', mode=0755)
135
            check_mode(self, 'dmode755', 0755)
136
137
            t.mkdir('dmode555', mode=0555)
138
            check_mode(self, 'dmode555', 0555)
139
140
            t.mkdir('dmode777', mode=0777)
141
            check_mode(self, 'dmode777', 0777)
142
143
            t.mkdir('dmode700', mode=0700)
144
            check_mode(self, 'dmode700', 0700)
145
146
            # TODO: jam 20051215 test mkdir_multi with a mode
147
            t.mkdir_multi(['mdmode755'], mode=0755)
148
            check_mode(self, 'mdmode755', 0755)
149
1185.11.22 by John Arbash Meinel
Major refactoring of testtransport.
150
    def test_copy_to(self):
151
        import tempfile
152
        from bzrlib.transport.local import LocalTransport
153
154
        t = self.get_transport()
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
155
        for mode in (0666, 0644, 0600, 0400):
156
            dtmp_base, local_t = get_temp_local()
157
            t.copy_to(files, local_t, mode=mode)
158
            for f in files:
159
                check_mode(self, os.path.join(dtmp_base, f), mode)
1185.11.22 by John Arbash Meinel
Major refactoring of testtransport.
160
1185.50.13 by John Arbash Meinel
Expanded the Transport test suite. Including delete, copy, move, etc. Updated SftpTransport to conform.
161
1530.1.5 by Robert Collins
Reinstate Memory parameter tests.
162
163
class MemoryTransportTest(TestCase):
164
    """Memory transport specific tests."""
165
166
    def test_parameters(self):
167
        import bzrlib.transport.memory as memory
168
        transport = memory.MemoryTransport()
169
        self.assertEqual(True, transport.listable())
170
        self.assertEqual(False, transport.should_cache())
171
        self.assertEqual(False, transport.is_readonly())