34
34
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
35
35
from bzrlib.osutils import (
37
is_inside_or_parent_of_any,
43
38
from bzrlib.tests import (
151
146
self.assertTrue(is_inside('', 'foo.c'))
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'),
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))
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'),
171
self.assert_(is_inside_or_parent_of_any(dirs, fn))
166
self.assert_(osutils.is_inside_or_parent_of_any(dirs, fn))
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))
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')
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)
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)
377
375
class TestPumpFile(TestCase):
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)
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)
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)
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)
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,
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)
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)
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)
499
498
from_file = StringIO(self.test_data)
500
499
to_file = StringIO()
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)
508
507
from_file = StringIO(self.test_data)
509
508
to_file = StringIO()
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)
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())
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())
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())
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())