~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testtransport.py

  • Committer: Robert Collins
  • Date: 2005-10-08 00:39:04 UTC
  • mfrom: (1185.1.52)
  • Revision ID: robertc@robertcollins.net-20051008003904-aaffaea2778efe3e
merge in martins reweave, integrated to fetch, and a bugfix for commit and upgrade with executable files

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
import os
19
19
from cStringIO import StringIO
20
 
 
 
20
from bzrlib.selftest import TestCaseInTempDir
 
21
from bzrlib.selftest.HTTPTestUtil import TestCaseWithWebserver
21
22
from bzrlib.errors import NoSuchFile, FileExists, TransportNotPossible
22
 
from bzrlib.selftest import TestCase, TestCaseInTempDir
23
 
from bzrlib.selftest.HTTPTestUtil import TestCaseWithWebserver
24
 
from bzrlib.transport import memory, urlescape
25
 
 
26
23
 
27
24
def _append(fn, txt):
28
25
    """Append the given text (file-like object) to the supplied filename."""
32
29
    f.close()
33
30
    del f
34
31
 
35
 
class TestTransport(TestCase):
36
 
    """Test the non transport-concrete class functionality."""
37
 
 
38
 
    def test_urlescape(self):
39
 
        self.assertEqual('%25', urlescape('%'))
40
 
 
41
 
 
42
32
class TestTransportMixIn(object):
43
33
    """Subclass this, and it will provide a series of tests for a Transport.
44
34
    It assumes that the Transport object is connected to the 
58
48
    def test_has(self):
59
49
        t = self.get_transport()
60
50
 
61
 
        files = ['a', 'b', 'e', 'g', '%']
 
51
        files = ['a', 'b', 'e', 'g']
62
52
        self.build_tree(files)
63
53
        self.assertEqual(t.has('a'), True)
64
54
        self.assertEqual(t.has('c'), False)
65
 
        self.assertEqual(t.has(urlescape('%')), True)
66
55
        self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])),
67
56
                [True, True, False, False, True, False, True, False])
68
57
        self.assertEqual(list(t.has_multi(iter(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']))),
442
431
                'some text for the\nthird file created\n'
443
432
                'some garbage\nto put in three\n')
444
433
 
 
434
    def test_get_partial(self):
 
435
        t = self.get_transport()
 
436
 
 
437
        contents = [
 
438
            ('f1', 
 
439
                'here is some text\nand a bit more\n'
 
440
                'adding more\ntext to two\n'),
 
441
            ('f2',
 
442
                'this is a string\nand some more stuff\n'
 
443
                'appending to\none\n'),
 
444
            ('f3',
 
445
                'some text for the\nthird file created\n'
 
446
                'some garbage\nto put in three\n')
 
447
        ]
 
448
        if self.readonly:
 
449
            for f, val in contents:
 
450
                open(f, 'wb').write(val)
 
451
        else:
 
452
            t.put_multi(contents)
 
453
 
 
454
        self.assertRaises(NoSuchFile,
 
455
                t.get_partial, 'a-missing-file', 20)
 
456
        self.assertRaises(NoSuchFile,
 
457
                t.get_partial, 'another-missing-file', 20, 30)
 
458
        f = t.get_partial('f1', 33)
 
459
        self.assertEqual(f.read(), 
 
460
                'adding more\ntext to two\n')
 
461
        f = t.get_partial('f1', 33, 10)
 
462
        self.assertEqual(f.read(10), 
 
463
                'adding mor')
 
464
 
 
465
        del f
 
466
 
 
467
        offsets = [('f2', 37), ('f3', 20, 10), ('f1', 10, 20)]
 
468
        values = ['appending to\none\n',
 
469
                  'ird file c',
 
470
                  'me text\nand a bit mo'
 
471
                 ]
 
472
        contents_f = t.get_partial_multi(offsets)
 
473
        count = 0
 
474
        for f, val in zip(contents_f, values):
 
475
            count += 1
 
476
            self.assertEqual(val, f.read(len(val)))
 
477
        # Make sure we saw all values, and no extra
 
478
        self.assertEqual(len(values), count)
 
479
        self.assertEqual(list(contents_f), [])
 
480
 
 
481
        # Do the same thing with an iterator
 
482
        offsets = iter([('f2', 34), ('f3', 18, 10), ('f1', 15, 15)])
 
483
        values = ['ff\nappending to\none\n',
 
484
                  'third file',
 
485
                  'xt\nand a bit mo'
 
486
                 ]
 
487
        contents_f = t.get_partial_multi(offsets)
 
488
        count = 0
 
489
        for f, val in zip(contents_f, values):
 
490
            count += 1
 
491
            self.assertEqual(val, f.read(len(val)))
 
492
        self.assertEqual(len(values), count)
 
493
        self.assertEqual(list(contents_f), [])
 
494
 
 
495
 
445
496
    def test_delete(self):
446
497
        # TODO: Test Transport.delete
447
498
        pass
450
501
        # TODO: Test Transport.move
451
502
        pass
452
503
 
453
 
 
454
504
class LocalTransportTest(TestCaseInTempDir, TestTransportMixIn):
455
505
    def get_transport(self):
456
506
        from bzrlib.transport.local import LocalTransport
457
507
        return LocalTransport('.')
458
508
 
459
 
 
460
509
class HttpTransportTest(TestCaseWithWebserver, TestTransportMixIn):
461
 
 
462
510
    readonly = True
463
 
 
464
511
    def get_transport(self):
465
512
        from bzrlib.transport.http import HttpTransport
466
513
        url = self.get_remote_url('.')
467
514
        return HttpTransport(url)
468
515
 
469
 
 
470
 
class TestMemoryTransport(TestCase):
471
 
 
472
 
    def test_get_transport(self):
473
 
        memory.MemoryTransport()
474
 
 
475
 
    def test_clone(self):
476
 
        transport = memory.MemoryTransport()
477
 
        self.failUnless(transport.clone() is transport)
478
 
 
479
 
    def test_abspath(self):
480
 
        transport = memory.MemoryTransport()
481
 
        self.assertEqual("in-memory:relpath", transport.abspath('relpath'))
482
 
 
483
 
    def test_relpath(self):
484
 
        transport = memory.MemoryTransport()
485
 
 
486
 
    def test_append_and_get(self):
487
 
        transport = memory.MemoryTransport()
488
 
        transport.append('path', StringIO('content'))
489
 
        self.assertEqual(transport.get('path').read(), 'content')
490
 
        transport.append('path', StringIO('content'))
491
 
        self.assertEqual(transport.get('path').read(), 'contentcontent')
492
 
 
493
 
    def test_put_and_get(self):
494
 
        transport = memory.MemoryTransport()
495
 
        transport.put('path', StringIO('content'))
496
 
        self.assertEqual(transport.get('path').read(), 'content')
497
 
        transport.put('path', StringIO('content'))
498
 
        self.assertEqual(transport.get('path').read(), 'content')
499
 
 
500
 
    def test_append_without_dir_fails(self):
501
 
        transport = memory.MemoryTransport()
502
 
        self.assertRaises(NoSuchFile,
503
 
                          transport.append, 'dir/path', StringIO('content'))
504
 
 
505
 
    def test_put_without_dir_fails(self):
506
 
        transport = memory.MemoryTransport()
507
 
        self.assertRaises(NoSuchFile,
508
 
                          transport.put, 'dir/path', StringIO('content'))
509
 
 
510
 
    def test_get_missing(self):
511
 
        transport = memory.MemoryTransport()
512
 
        self.assertRaises(NoSuchFile, transport.get, 'foo')
513
 
 
514
 
    def test_has_missing(self):
515
 
        transport = memory.MemoryTransport()
516
 
        self.assertEquals(False, transport.has('foo'))
517
 
 
518
 
    def test_has_present(self):
519
 
        transport = memory.MemoryTransport()
520
 
        transport.append('foo', StringIO('content'))
521
 
        self.assertEquals(True, transport.has('foo'))
522
 
 
523
 
    def test_mkdir(self):
524
 
        transport = memory.MemoryTransport()
525
 
        transport.mkdir('dir')
526
 
        transport.append('dir/path', StringIO('content'))
527
 
        self.assertEqual(transport.get('dir/path').read(), 'content')
528
 
 
529
 
    def test_mkdir_missing_parent(self):
530
 
        transport = memory.MemoryTransport()
531
 
        self.assertRaises(NoSuchFile,
532
 
                          transport.mkdir, 'dir/dir')
533
 
 
534
 
    def test_mkdir_twice(self):
535
 
        transport = memory.MemoryTransport()
536
 
        transport.mkdir('dir')
537
 
        self.assertRaises(FileExists, transport.mkdir, 'dir')
538
 
 
539
 
    def test_parameters(self):
540
 
        transport = memory.MemoryTransport()
541
 
        self.assertEqual(True, transport.listable())
542
 
        self.assertEqual(False, transport.should_cache())
543
 
 
544
 
    def test_iter_files_recursive(self):
545
 
        transport = memory.MemoryTransport()
546
 
        transport.mkdir('dir')
547
 
        transport.put('dir/foo', StringIO('content'))
548
 
        transport.put('dir/bar', StringIO('content'))
549
 
        transport.put('bar', StringIO('content'))
550
 
        paths = set(transport.iter_files_recursive())
551
 
        self.assertEqual(set(['dir/foo', 'dir/bar', 'bar']), paths)
552
 
 
553
 
    def test_stat(self):
554
 
        transport = memory.MemoryTransport()
555
 
        transport.put('foo', StringIO('content'))
556
 
        transport.put('bar', StringIO('phowar'))
557
 
        self.assertEqual(7, transport.stat('foo').st_size)
558
 
        self.assertEqual(6, transport.stat('bar').st_size)
559