~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-08 06:22:11 UTC
  • mfrom: (4241.8.9 bzr.1.14)
  • Revision ID: pqm@pqm.ubuntu.com-20090408062211-qhu50uezgnlj7ked
(tanner) merge 1.14rc1 back into bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    )
34
34
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
35
35
from bzrlib.osutils import (
36
 
        is_inside_any,
37
 
        is_inside_or_parent_of_any,
38
 
        pathjoin,
39
 
        pumpfile,
40
 
        pump_string_file,
41
36
        canonical_relpath,
42
37
        )
43
38
from bzrlib.tests import (
151
146
        self.assertTrue(is_inside('', 'foo.c'))
152
147
 
153
148
    def test_is_inside_any(self):
154
 
        SRC_FOO_C = pathjoin('src', 'foo.c')
 
149
        SRC_FOO_C = osutils.pathjoin('src', 'foo.c')
155
150
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
156
151
                         (['src'], SRC_FOO_C),
157
152
                         (['src'], 'src'),
158
153
                         ]:
159
 
            self.assert_(is_inside_any(dirs, fn))
 
154
            self.assert_(osutils.is_inside_any(dirs, fn))
160
155
        for dirs, fn in [(['src'], 'srccontrol'),
161
156
                         (['src'], 'srccontrol/foo')]:
162
 
            self.assertFalse(is_inside_any(dirs, fn))
 
157
            self.assertFalse(osutils.is_inside_any(dirs, fn))
163
158
 
164
159
    def test_is_inside_or_parent_of_any(self):
165
160
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
168
163
                         (['src/bar.c', 'bla/foo.c'], 'src'),
169
164
                         (['src'], 'src'),
170
165
                         ]:
171
 
            self.assert_(is_inside_or_parent_of_any(dirs, fn))
 
166
            self.assert_(osutils.is_inside_or_parent_of_any(dirs, fn))
172
167
 
173
168
        for dirs, fn in [(['src'], 'srccontrol'),
174
169
                         (['srccontrol/foo.c'], 'src'),
175
170
                         (['src'], 'srccontrol/foo')]:
176
 
            self.assertFalse(is_inside_or_parent_of_any(dirs, fn))
 
171
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
177
172
 
178
173
    def test_rmtree(self):
179
174
        # Check to remove tree with read-only files/dirs
363
358
    def test_canonical_relpath_simple(self):
364
359
        f = file('MixedCaseName', 'w')
365
360
        f.close()
366
 
        self.failUnlessEqual(
367
 
            canonical_relpath(self.test_base_dir, 'mixedcasename'),
368
 
            'work/MixedCaseName')
 
361
        # Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
 
362
        real_base_dir = osutils.realpath(self.test_base_dir)
 
363
        actual = osutils.canonical_relpath(real_base_dir, 'mixedcasename')
 
364
        self.failUnlessEqual('work/MixedCaseName', actual)
369
365
 
370
366
    def test_canonical_relpath_missing_tail(self):
371
367
        os.mkdir('MixedCaseParent')
372
 
        self.failUnlessEqual(
373
 
            canonical_relpath(self.test_base_dir, 'mixedcaseparent/nochild'),
374
 
            'work/MixedCaseParent/nochild')
 
368
        # Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
 
369
        real_base_dir = osutils.realpath(self.test_base_dir)
 
370
        actual = osutils.canonical_relpath(real_base_dir,
 
371
                                           'mixedcaseparent/nochild')
 
372
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
375
373
 
376
374
 
377
375
class TestPumpFile(TestCase):
395
393
 
396
394
        # read (max / 2) bytes and verify read size wasn't affected
397
395
        num_bytes_to_read = self.block_size / 2
398
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
396
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
399
397
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
400
398
        self.assertEqual(from_file.get_read_count(), 1)
401
399
 
402
400
        # read (max) bytes and verify read size wasn't affected
403
401
        num_bytes_to_read = self.block_size
404
402
        from_file.reset_read_count()
405
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
403
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
406
404
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
407
405
        self.assertEqual(from_file.get_read_count(), 1)
408
406
 
409
407
        # read (max + 1) bytes and verify read size was limited
410
408
        num_bytes_to_read = self.block_size + 1
411
409
        from_file.reset_read_count()
412
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
410
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
413
411
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
414
412
        self.assertEqual(from_file.get_read_count(), 2)
415
413
 
416
414
        # finish reading the rest of the data
417
415
        num_bytes_to_read = self.test_data_len - to_file.tell()
418
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
416
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
419
417
 
420
418
        # report error if the data wasn't equal (we only report the size due
421
419
        # to the length of the data)
433
431
        # retrieve data in blocks
434
432
        from_file = FakeReadFile(self.test_data)
435
433
        to_file = StringIO()
436
 
        pumpfile(from_file, to_file, self.test_data_len, self.block_size)
 
434
        osutils.pumpfile(from_file, to_file, self.test_data_len,
 
435
                         self.block_size)
437
436
 
438
437
        # verify read size was equal to the maximum read size
439
438
        self.assertTrue(from_file.get_max_read_size() > 0)
456
455
        # retrieve data to EOF
457
456
        from_file = FakeReadFile(self.test_data)
458
457
        to_file = StringIO()
459
 
        pumpfile(from_file, to_file, -1, self.block_size)
 
458
        osutils.pumpfile(from_file, to_file, -1, self.block_size)
460
459
 
461
460
        # verify read size was equal to the maximum read size
462
461
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
476
475
        # retrieve data using default (old) pumpfile method
477
476
        from_file = FakeReadFile(self.test_data)
478
477
        to_file = StringIO()
479
 
        pumpfile(from_file, to_file)
 
478
        osutils.pumpfile(from_file, to_file)
480
479
 
481
480
        # report error if the data wasn't equal (we only report the size due
482
481
        # to the length of the data)
491
490
            activity.append((length, direction))
492
491
        from_file = StringIO(self.test_data)
493
492
        to_file = StringIO()
494
 
        pumpfile(from_file, to_file, buff_size=500,
495
 
                 report_activity=log_activity, direction='read')
 
493
        osutils.pumpfile(from_file, to_file, buff_size=500,
 
494
                         report_activity=log_activity, direction='read')
496
495
        self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
497
496
                          (36, 'read')], activity)
498
497
 
499
498
        from_file = StringIO(self.test_data)
500
499
        to_file = StringIO()
501
500
        del activity[:]
502
 
        pumpfile(from_file, to_file, buff_size=500,
503
 
                 report_activity=log_activity, direction='write')
 
501
        osutils.pumpfile(from_file, to_file, buff_size=500,
 
502
                         report_activity=log_activity, direction='write')
504
503
        self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
505
504
                          (36, 'write')], activity)
506
505
 
508
507
        from_file = StringIO(self.test_data)
509
508
        to_file = StringIO()
510
509
        del activity[:]
511
 
        pumpfile(from_file, to_file, buff_size=500, read_length=1028,
512
 
                 report_activity=log_activity, direction='read')
 
510
        osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
 
511
                         report_activity=log_activity, direction='read')
513
512
        self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
514
513
 
515
514
 
518
517
 
519
518
    def test_empty(self):
520
519
        output = StringIO()
521
 
        pump_string_file("", output)
 
520
        osutils.pump_string_file("", output)
522
521
        self.assertEqual("", output.getvalue())
523
522
 
524
523
    def test_more_than_segment_size(self):
525
524
        output = StringIO()
526
 
        pump_string_file("123456789", output, 2)
 
525
        osutils.pump_string_file("123456789", output, 2)
527
526
        self.assertEqual("123456789", output.getvalue())
528
527
 
529
528
    def test_segment_size(self):
530
529
        output = StringIO()
531
 
        pump_string_file("12", output, 2)
 
530
        osutils.pump_string_file("12", output, 2)
532
531
        self.assertEqual("12", output.getvalue())
533
532
 
534
533
    def test_segment_size_multiple(self):
535
534
        output = StringIO()
536
 
        pump_string_file("1234", output, 2)
 
535
        osutils.pump_string_file("1234", output, 2)
537
536
        self.assertEqual("1234", output.getvalue())
538
537
 
539
538