/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

Update to bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
        is_inside_any,
34
34
        is_inside_or_parent_of_any,
35
35
        pathjoin,
 
36
        pumpfile,
36
37
        )
37
38
from bzrlib.tests import (
38
39
        probe_unicode_in_user_encoding,
42
43
        TestCaseInTempDir,
43
44
        TestSkipped,
44
45
        )
45
 
 
 
46
from bzrlib.tests.file_utils import (
 
47
    FakeReadFile,
 
48
    )
 
49
from cStringIO import StringIO
46
50
 
47
51
class TestOSUtils(TestCaseInTempDir):
48
52
 
318
322
        self.assertEqual("@", osutils.kind_marker("symlink"))
319
323
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
320
324
 
 
325
    def test_host_os_dereferences_symlinks(self):
 
326
        osutils.host_os_dereferences_symlinks()
 
327
 
 
328
 
 
329
class TestPumpFile(TestCase):
 
330
    """Test pumpfile method."""
 
331
    def setUp(self):
 
332
        # create a test datablock
 
333
        self.block_size = 512
 
334
        pattern = '0123456789ABCDEF'
 
335
        self.test_data = pattern * (3 * self.block_size / len(pattern))
 
336
        self.test_data_len = len(self.test_data)
 
337
 
 
338
    def test_bracket_block_size(self):
 
339
        """Read data in blocks with the requested read size bracketing the
 
340
        block size."""
 
341
        # make sure test data is larger than max read size
 
342
        self.assertTrue(self.test_data_len > self.block_size)
 
343
 
 
344
        from_file = FakeReadFile(self.test_data)
 
345
        to_file = StringIO()
 
346
 
 
347
        # read (max / 2) bytes and verify read size wasn't affected
 
348
        num_bytes_to_read = self.block_size / 2
 
349
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
350
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
 
351
        self.assertEqual(from_file.get_read_count(), 1)
 
352
 
 
353
        # read (max) bytes and verify read size wasn't affected
 
354
        num_bytes_to_read = self.block_size
 
355
        from_file.reset_read_count()
 
356
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
357
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
 
358
        self.assertEqual(from_file.get_read_count(), 1)
 
359
 
 
360
        # read (max + 1) bytes and verify read size was limited
 
361
        num_bytes_to_read = self.block_size + 1
 
362
        from_file.reset_read_count()
 
363
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
364
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
 
365
        self.assertEqual(from_file.get_read_count(), 2)
 
366
 
 
367
        # finish reading the rest of the data
 
368
        num_bytes_to_read = self.test_data_len - to_file.tell()
 
369
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
370
 
 
371
        # report error if the data wasn't equal (we only report the size due
 
372
        # to the length of the data)
 
373
        response_data = to_file.getvalue()
 
374
        if response_data != self.test_data:
 
375
            message = "Data not equal.  Expected %d bytes, received %d."
 
376
            self.fail(message % (len(response_data), self.test_data_len))
 
377
 
 
378
    def test_specified_size(self):
 
379
        """Request a transfer larger than the maximum block size and verify
 
380
        that the maximum read doesn't exceed the block_size."""
 
381
        # make sure test data is larger than max read size
 
382
        self.assertTrue(self.test_data_len > self.block_size)
 
383
 
 
384
        # retrieve data in blocks
 
385
        from_file = FakeReadFile(self.test_data)
 
386
        to_file = StringIO()
 
387
        pumpfile(from_file, to_file, self.test_data_len, self.block_size)
 
388
 
 
389
        # verify read size was equal to the maximum read size
 
390
        self.assertTrue(from_file.get_max_read_size() > 0)
 
391
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
 
392
        self.assertEqual(from_file.get_read_count(), 3)
 
393
 
 
394
        # report error if the data wasn't equal (we only report the size due
 
395
        # to the length of the data)
 
396
        response_data = to_file.getvalue()
 
397
        if response_data != self.test_data:
 
398
            message = "Data not equal.  Expected %d bytes, received %d."
 
399
            self.fail(message % (len(response_data), self.test_data_len))
 
400
 
 
401
    def test_to_eof(self):
 
402
        """Read to end-of-file and verify that the reads are not larger than
 
403
        the maximum read size."""
 
404
        # make sure test data is larger than max read size
 
405
        self.assertTrue(self.test_data_len > self.block_size)
 
406
 
 
407
        # retrieve data to EOF
 
408
        from_file = FakeReadFile(self.test_data)
 
409
        to_file = StringIO()
 
410
        pumpfile(from_file, to_file, -1, self.block_size)
 
411
 
 
412
        # verify read size was equal to the maximum read size
 
413
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
 
414
        self.assertEqual(from_file.get_read_count(), 4)
 
415
 
 
416
        # report error if the data wasn't equal (we only report the size due
 
417
        # to the length of the data)
 
418
        response_data = to_file.getvalue()
 
419
        if response_data != self.test_data:
 
420
            message = "Data not equal.  Expected %d bytes, received %d."
 
421
            self.fail(message % (len(response_data), self.test_data_len))
 
422
 
 
423
    def test_defaults(self):
 
424
        """Verifies that the default arguments will read to EOF -- this
 
425
        test verifies that any existing usages of pumpfile will not be broken
 
426
        with this new version."""
 
427
        # retrieve data using default (old) pumpfile method
 
428
        from_file = FakeReadFile(self.test_data)
 
429
        to_file = StringIO()
 
430
        pumpfile(from_file, to_file)
 
431
 
 
432
        # report error if the data wasn't equal (we only report the size due
 
433
        # to the length of the data)
 
434
        response_data = to_file.getvalue()
 
435
        if response_data != self.test_data:
 
436
            message = "Data not equal.  Expected %d bytes, received %d."
 
437
            self.fail(message % (len(response_data), self.test_data_len))
321
438
 
322
439
class TestSafeUnicode(TestCase):
323
440