25
23
from bzrlib.tests import TestCase, TestCaseInTempDir
26
24
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
27
25
from bzrlib.transport import memory, urlescape
28
from bzrlib.osutils import pathjoin
31
28
def _append(fn, txt):
40
if sys.platform != 'win32':
41
def check_mode(test, path, mode):
42
"""Check that a particular path has the correct mode."""
43
actual_mode = stat.S_IMODE(os.stat(path).st_mode)
44
test.assertEqual(mode, actual_mode,
45
'mode of %r incorrect (%o != %o)' % (path, mode, actual_mode))
47
def check_mode(test, path, mode):
48
"""On win32 chmod doesn't have any effect,
49
so don't actually check anything
54
36
class TestTransport(TestCase):
55
37
"""Test the non transport-concrete class functionality."""
113
95
self.assertEqual(open('a', 'rb').read(), t.get('a').read())
114
96
content_f = t.get_multi(files)
115
97
for path,f in zip(files, content_f):
116
self.assertEqual(f.read(), open(path, 'rb').read())
98
self.assertEqual(open(path).read(), f.read())
118
100
content_f = t.get_multi(iter(files))
119
101
for path,f in zip(files, content_f):
120
self.assertEqual(f.read(), open(path, 'rb').read())
102
self.assertEqual(f.read(), open(path).read())
122
104
self.assertRaises(NoSuchFile, t.get, 'c')
123
105
self.assertListRaises(NoSuchFile, t.get_multi, ['a', 'b', 'c'])
126
108
def test_put(self):
127
109
t = self.get_transport()
129
# TODO: jam 20051215 No need to do anything if the test is readonly
130
# origininally it was thought that it would give
131
# more of a workout to readonly tests. By now the
132
# suite is probably thorough enough without testing
133
# readonly protocols in write sections
134
# The only thing that needs to be tested is that the
135
# right error is raised
137
111
if self.readonly:
138
112
self.assertRaises(TransportNotPossible,
139
113
t.put, 'a', 'some text for a\n')
184
158
self.assertRaises(NoSuchFile,
185
159
t.put, 'path/doesnt/exist/c', 'contents')
187
if not self.readonly:
188
t.put('mode644', 'test text\n', mode=0644)
189
check_mode(self, 'mode644', 0644)
191
t.put('mode666', 'test text\n', mode=0666)
192
check_mode(self, 'mode666', 0666)
194
t.put('mode600', 'test text\n', mode=0600)
195
check_mode(self, 'mode600', 0600)
197
# Yes, you can put a file such that it becomes readonly
198
t.put('mode400', 'test text\n', mode=0400)
199
check_mode(self, 'mode400', 0400)
201
t.put_multi([('mmode644', 'text\n')], mode=0644)
202
check_mode(self, 'mmode644', 0644)
204
# TODO: jam 20051215 test put_multi with a mode. I didn't bother because
205
# it seems most people don't like the _multi functions
207
161
def test_put_file(self):
208
162
t = self.get_transport()
258
212
self.check_file_contents('f5', 'here is some text\nand a bit more\n')
259
213
self.check_file_contents('f6', 'some text for the\nthird file created\n')
261
if not self.readonly:
262
sio = StringIO('test text\n')
263
t.put('mode644', sio, mode=0644)
264
check_mode(self, 'mode644', 0644)
266
a = open('mode644', 'rb')
267
t.put('mode666', a, mode=0666)
268
check_mode(self, 'mode666', 0666)
270
a = open('mode644', 'rb')
271
t.put('mode600', a, mode=0600)
272
check_mode(self, 'mode600', 0600)
274
# Yes, you can put a file such that it becomes readonly
275
a = open('mode644', 'rb')
276
t.put('mode400', a, mode=0400)
277
check_mode(self, 'mode400', 0400)
279
215
def test_mkdir(self):
280
216
t = self.get_transport()
342
278
('dir_b/b', 'contents of dir_b/b')])
344
280
for f in ('dir_a/a', 'dir_b/b'):
345
self.assertEqual(t.get(f).read(), open(f, 'rb').read())
347
if not self.readonly:
348
# Test mkdir with a mode
349
t.mkdir('dmode755', mode=0755)
350
check_mode(self, 'dmode755', 0755)
352
t.mkdir('dmode555', mode=0555)
353
check_mode(self, 'dmode555', 0555)
355
t.mkdir('dmode777', mode=0777)
356
check_mode(self, 'dmode777', 0777)
358
t.mkdir('dmode700', mode=0700)
359
check_mode(self, 'dmode700', 0700)
361
# TODO: jam 20051215 test mkdir_multi with a mode
362
t.mkdir_multi(['mdmode755'], mode=0755)
363
check_mode(self, 'mdmode755', 0755)
281
self.assertEqual(t.get(f).read(), open(f).read())
366
283
def test_copy_to(self):
372
289
files = ['a', 'b', 'c', 'd']
373
290
self.build_tree(files)
375
def get_temp_local():
376
dtmp = tempfile.mkdtemp(dir=u'.', prefix='test-transport-')
377
dtmp_base = os.path.basename(dtmp)
378
return dtmp_base, LocalTransport(dtmp)
379
dtmp_base, local_t = get_temp_local()
292
dtmp = tempfile.mkdtemp(dir=u'.', prefix='test-transport-')
293
dtmp_base = os.path.basename(dtmp)
294
local_t = LocalTransport(dtmp)
381
296
t.copy_to(files, local_t)
383
self.assertEquals(open(f, 'rb').read(),
384
open(pathjoin(dtmp_base, f), 'rb').read())
298
self.assertEquals(open(f).read(),
299
open(os.path.join(dtmp_base, f)).read())
386
301
# Test that copying into a missing directory raises
389
304
open('e/f', 'wb').write('contents of e')
390
305
self.assertRaises(NoSuchFile, t.copy_to, ['e/f'], local_t)
392
os.mkdir(pathjoin(dtmp_base, 'e'))
307
os.mkdir(os.path.join(dtmp_base, 'e'))
393
308
t.copy_to(['e/f'], local_t)
395
del dtmp_base, local_t
310
del dtmp, dtmp_base, local_t
397
dtmp_base, local_t = get_temp_local()
312
dtmp = tempfile.mkdtemp(dir=u'.', prefix='test-transport-')
313
dtmp_base = os.path.basename(dtmp)
314
local_t = LocalTransport(dtmp)
399
316
files = ['a', 'b', 'c', 'd']
400
317
t.copy_to(iter(files), local_t)
402
self.assertEquals(open(f, 'rb').read(),
403
open(pathjoin(dtmp_base, f), 'rb').read())
405
del dtmp_base, local_t
407
for mode in (0666, 0644, 0600, 0400):
408
dtmp_base, local_t = get_temp_local()
409
t.copy_to(files, local_t, mode=mode)
411
check_mode(self, os.path.join(dtmp_base, f), mode)
319
self.assertEquals(open(f).read(),
320
open(os.path.join(dtmp_base, f)).read())
322
del dtmp, dtmp_base, local_t
413
324
def test_append(self):
414
325
t = self.get_transport()