378
386
# which has a lookup of None
379
387
return _protocol_handlers[None](base)
389
def transport_test(tester, t):
390
"""Test a transport object. Basically, it assumes that the
391
Transport object is connected to the current working directory.
392
So that whatever is done through the transport, should show
393
up in the working directory, and vice-versa.
395
This also tests to make sure that the functions work with both
396
generators and lists (assuming iter(list) is effectively a generator)
399
from local_transport import LocalTransport
402
files = ['a', 'b', 'e', 'g']
403
tester.build_tree(files)
404
tester.assertEqual(t.has('a'), True)
405
tester.assertEqual(t.has('c'), False)
406
tester.assertEqual(t.has_multi(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']),
407
[True, True, False, False, True, False, True, False])
408
tester.assertEqual(t.has_multi(iter(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])),
409
[True, True, False, False, True, False, True, False])
412
tester.assertEqual(t.get('a').read(), open('a').read())
413
content_f = t.get_multi(files)
414
for path,f in zip(files, content_f):
415
tester.assertEqual(open(path).read(), f.read())
417
content_f = t.get_multi(iter(files))
418
for path,f in zip(files, content_f):
419
tester.assertEqual(open(path).read(), f.read())
421
tester.assertRaises(TransportError, t.get, 'c')
423
files = list(t.get_multi(['a', 'b', 'c']))
424
except TransportError:
427
tester.fail('Failed to raise TransportError for missing file in get_multi')
429
files = list(t.get_multi(iter(['a', 'b', 'c', 'e'])))
430
except TransportError:
433
tester.fail('Failed to raise TransportError for missing file in get_multi')
436
t.put('c', 'some text for c\n')
437
tester.assert_(os.path.exists('c'))
438
tester.assertEqual(open('c').read(), 'some text for c\n')
439
# Make sure 'has' is updated
440
tester.assertEqual(t.has_multi(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']),
441
[True, True, True, False, True, False, True, False])
442
# Put also replaces contents
443
tester.assertEqual(t.put_multi([('a', 'new\ncontents for\na\n'),
444
('d', 'contents\nfor d\n')]),
446
tester.assertEqual(t.has_multi(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']),
447
[True, True, True, True, True, False, True, False])
448
tester.assertEqual(open('a').read(), 'new\ncontents for\na\n')
449
tester.assertEqual(open('d').read(), 'contents\nfor d\n')
451
tester.assertEqual(t.put_multi(iter([('a', 'diff\ncontents for\na\n'),
452
('d', 'another contents\nfor d\n')])),
454
tester.assertEqual(open('a').read(), 'diff\ncontents for\na\n')
455
tester.assertEqual(open('d').read(), 'another contents\nfor d\n')
457
tester.assertRaises(TransportError, t.put, 'path/doesnt/exist/c')
461
tester.assertEqual(t.has('dir_a'), True)
462
tester.assertEqual(t.has('dir_b'), False)
465
tester.assertEqual(t.has('dir_b'), True)
466
tester.assert_(os.path.isdir('dir_b'))
468
t.mkdir_multi(['dir_c', 'dir_d'])
469
tester.assertEqual(t.has_multi(['dir_a', 'dir_b', 'dir_c', 'dir_d', 'dir_e', 'dir_b']),
470
[True, True, True, True, False, True])
471
for d in ['dir_a', 'dir_b', 'dir_c', 'dir_d']:
472
tester.assert_(os.path.isdir(d))
474
tester.assertRaises(TransportError, t.mkdir, 'path/doesnt/exist')
475
tester.assertRaises(TransportError, t.mkdir, 'dir_a') # Creating a directory again should fail
477
# This one may fail for some transports.
478
# Specifically, I know RsyncTransport doesn't check for the directory
479
# existing, before it creates it. The reason is that it seems to
480
# expensive, it does check to see if the local directory already exists,
481
# and will throw an exception for that
482
# FIXME: Make this work everywhere
484
#tester.assertRaises(TransportError, t.mkdir, 'dir_e')
486
# Test get/put in sub-directories
487
tester.assertEqual(t.put_multi([('dir_a/a', 'contents of dir_a/a'),
488
('dir_b/b', 'contents of dir_b/b')]
490
for f in ('dir_a/a', 'dir_b/b'):
491
tester.assertEqual(t.get(f).read(), open(f).read())
494
dtmp = tempfile.mkdtemp(dir='.', prefix='test-transport-')
495
dtmp_base = os.path.basename(dtmp)
496
local_t = LocalTransport(dtmp)
498
files = ['a', 'b', 'c', 'd']
499
t.copy_to(files, local_t)
501
tester.assertEquals(open(f).read(), open(os.path.join(dtmp_base, f)).read())
381
503
# Local transport should always be initialized
382
504
import local_transport