1
# Copyright (C) 2005-2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for the osutils wrapper."""
19
from __future__ import absolute_import, division
39
from ..sixish import (
49
from .scenarios import load_tests_apply_scenarios
52
class _UTF8DirReaderFeature(features.ModuleAvailableFeature):
56
from .. import _readdir_pyx
57
self._module = _readdir_pyx
58
self.reader = _readdir_pyx.UTF8DirReader
63
UTF8DirReaderFeature = _UTF8DirReaderFeature('breezy._readdir_pyx')
65
term_ios_feature = features.ModuleAvailableFeature('termios')
68
def _already_unicode(s):
72
def _utf8_to_unicode(s):
73
return s.decode('UTF-8')
76
def dir_reader_scenarios():
77
# For each dir reader we define:
79
# - native_to_unicode: a function converting the native_abspath as returned
80
# by DirReader.read_dir to its unicode representation
82
# UnicodeDirReader is the fallback, it should be tested on all platforms.
83
scenarios = [('unicode',
84
dict(_dir_reader_class=osutils.UnicodeDirReader,
85
_native_to_unicode=_already_unicode))]
86
# Some DirReaders are platform specific and even there they may not be
88
if UTF8DirReaderFeature.available():
89
from .. import _readdir_pyx
90
scenarios.append(('utf8',
91
dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
92
_native_to_unicode=_utf8_to_unicode)))
94
if test__walkdirs_win32.win32_readdir_feature.available():
96
from .. import _walkdirs_win32
99
dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
100
_native_to_unicode=_already_unicode)))
106
load_tests = load_tests_apply_scenarios
109
class TestContainsWhitespace(tests.TestCase):
111
def test_contains_whitespace(self):
112
self.assertTrue(osutils.contains_whitespace(u' '))
113
self.assertTrue(osutils.contains_whitespace(u'hello there'))
114
self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
115
self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
116
self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
117
self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
119
# \xa0 is "Non-breaking-space" which on some python locales thinks it
120
# is whitespace, but we do not.
121
self.assertFalse(osutils.contains_whitespace(u''))
122
self.assertFalse(osutils.contains_whitespace(u'hellothere'))
123
self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
126
class TestRename(tests.TestCaseInTempDir):
128
def create_file(self, filename, content):
129
f = open(filename, 'wb')
135
def _fancy_rename(self, a, b):
136
osutils.fancy_rename(a, b, rename_func=os.rename,
137
unlink_func=os.unlink)
139
def test_fancy_rename(self):
140
# This should work everywhere
141
self.create_file('a', b'something in a\n')
142
self._fancy_rename('a', 'b')
143
self.assertPathDoesNotExist('a')
144
self.assertPathExists('b')
145
self.check_file_contents('b', b'something in a\n')
147
self.create_file('a', b'new something in a\n')
148
self._fancy_rename('b', 'a')
150
self.check_file_contents('a', b'something in a\n')
152
def test_fancy_rename_fails_source_missing(self):
153
# An exception should be raised, and the target should be left in place
154
self.create_file('target', b'data in target\n')
155
self.assertRaises((IOError, OSError), self._fancy_rename,
156
'missingsource', 'target')
157
self.assertPathExists('target')
158
self.check_file_contents('target', b'data in target\n')
160
def test_fancy_rename_fails_if_source_and_target_missing(self):
161
self.assertRaises((IOError, OSError), self._fancy_rename,
162
'missingsource', 'missingtarget')
164
def test_rename(self):
165
# Rename should be semi-atomic on all platforms
166
self.create_file('a', b'something in a\n')
167
osutils.rename('a', 'b')
168
self.assertPathDoesNotExist('a')
169
self.assertPathExists('b')
170
self.check_file_contents('b', b'something in a\n')
172
self.create_file('a', b'new something in a\n')
173
osutils.rename('b', 'a')
175
self.check_file_contents('a', b'something in a\n')
177
# TODO: test fancy_rename using a MemoryTransport
179
def test_rename_change_case(self):
180
# on Windows we should be able to change filename case by rename
181
self.build_tree(['a', 'b/'])
182
osutils.rename('a', 'A')
183
osutils.rename('b', 'B')
184
# we can't use failUnlessExists on case-insensitive filesystem
185
# so try to check shape of the tree
186
shape = sorted(os.listdir('.'))
187
self.assertEqual(['A', 'B'], shape)
189
def test_rename_exception(self):
191
osutils.rename('nonexistent_path', 'different_nonexistent_path')
193
self.assertEqual(e.old_filename, 'nonexistent_path')
194
self.assertEqual(e.new_filename, 'different_nonexistent_path')
195
self.assertTrue('nonexistent_path' in e.strerror)
196
self.assertTrue('different_nonexistent_path' in e.strerror)
199
class TestRandChars(tests.TestCase):
201
def test_01_rand_chars_empty(self):
202
result = osutils.rand_chars(0)
203
self.assertEqual(result, '')
205
def test_02_rand_chars_100(self):
206
result = osutils.rand_chars(100)
207
self.assertEqual(len(result), 100)
208
self.assertEqual(type(result), str)
209
self.assertContainsRe(result, r'^[a-z0-9]{100}$')
212
class TestIsInside(tests.TestCase):
214
def test_is_inside(self):
215
is_inside = osutils.is_inside
216
self.assertTrue(is_inside('src', 'src/foo.c'))
217
self.assertFalse(is_inside('src', 'srccontrol'))
218
self.assertTrue(is_inside('src', 'src/a/a/a/foo.c'))
219
self.assertTrue(is_inside('foo.c', 'foo.c'))
220
self.assertFalse(is_inside('foo.c', ''))
221
self.assertTrue(is_inside('', 'foo.c'))
223
def test_is_inside_any(self):
224
SRC_FOO_C = osutils.pathjoin('src', 'foo.c')
225
for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
226
(['src'], SRC_FOO_C),
229
self.assertTrue(osutils.is_inside_any(dirs, fn))
230
for dirs, fn in [(['src'], 'srccontrol'),
231
(['src'], 'srccontrol/foo')]:
232
self.assertFalse(osutils.is_inside_any(dirs, fn))
234
def test_is_inside_or_parent_of_any(self):
235
for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
236
(['src'], 'src/foo.c'),
237
(['src/bar.c'], 'src'),
238
(['src/bar.c', 'bla/foo.c'], 'src'),
241
self.assertTrue(osutils.is_inside_or_parent_of_any(dirs, fn))
243
for dirs, fn in [(['src'], 'srccontrol'),
244
(['srccontrol/foo.c'], 'src'),
245
(['src'], 'srccontrol/foo')]:
246
self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
249
class TestLstat(tests.TestCaseInTempDir):
251
def test_lstat_matches_fstat(self):
252
# On Windows, lstat and fstat don't always agree, primarily in the
253
# 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
254
# custom implementation.
255
if sys.platform == 'win32':
256
# We only have special lstat/fstat if we have the extension.
257
# Without it, we may end up re-reading content when we don't have
258
# to, but otherwise it doesn't effect correctness.
259
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
260
with open('test-file.txt', 'wb') as f:
261
f.write(b'some content\n')
263
self.assertEqualStat(osutils.fstat(f.fileno()),
264
osutils.lstat('test-file.txt'))
267
class TestRmTree(tests.TestCaseInTempDir):
269
def test_rmtree(self):
270
# Check to remove tree with read-only files/dirs
272
with open('dir/file', 'w') as f:
274
# would like to also try making the directory readonly, but at the
275
# moment python shutil.rmtree doesn't handle that properly - it would
276
# need to chmod the directory before removing things inside it - deferred
277
# for now -- mbp 20060505
278
# osutils.make_readonly('dir')
279
osutils.make_readonly('dir/file')
281
osutils.rmtree('dir')
283
self.assertPathDoesNotExist('dir/file')
284
self.assertPathDoesNotExist('dir')
287
class TestDeleteAny(tests.TestCaseInTempDir):
289
def test_delete_any_readonly(self):
290
# from <https://bugs.launchpad.net/bzr/+bug/218206>
291
self.build_tree(['d/', 'f'])
292
osutils.make_readonly('d')
293
osutils.make_readonly('f')
295
osutils.delete_any('f')
296
osutils.delete_any('d')
299
class TestKind(tests.TestCaseInTempDir):
301
def test_file_kind(self):
302
self.build_tree(['file', 'dir/'])
303
self.assertEqual('file', osutils.file_kind('file'))
304
self.assertEqual('directory', osutils.file_kind('dir/'))
305
if osutils.has_symlinks():
306
os.symlink('symlink', 'symlink')
307
self.assertEqual('symlink', osutils.file_kind('symlink'))
309
# TODO: jam 20060529 Test a block device
311
os.lstat('/dev/null')
313
if e.errno not in (errno.ENOENT,):
316
self.assertEqual('chardev', osutils.file_kind('/dev/null'))
318
mkfifo = getattr(os, 'mkfifo', None)
322
self.assertEqual('fifo', osutils.file_kind('fifo'))
326
AF_UNIX = getattr(socket, 'AF_UNIX', None)
328
s = socket.socket(AF_UNIX)
331
self.assertEqual('socket', osutils.file_kind('socket'))
335
def test_kind_marker(self):
336
self.assertEqual("", osutils.kind_marker("file"))
337
self.assertEqual("/", osutils.kind_marker('directory'))
338
self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
339
self.assertEqual("@", osutils.kind_marker("symlink"))
340
self.assertEqual("+", osutils.kind_marker("tree-reference"))
341
self.assertEqual("", osutils.kind_marker("fifo"))
342
self.assertEqual("", osutils.kind_marker("socket"))
343
self.assertEqual("", osutils.kind_marker("unknown"))
346
class TestUmask(tests.TestCaseInTempDir):
348
def test_get_umask(self):
349
if sys.platform == 'win32':
350
# umask always returns '0', no way to set it
351
self.assertEqual(0, osutils.get_umask())
354
orig_umask = osutils.get_umask()
355
self.addCleanup(os.umask, orig_umask)
357
self.assertEqual(0o222, osutils.get_umask())
359
self.assertEqual(0o022, osutils.get_umask())
361
self.assertEqual(0o002, osutils.get_umask())
363
self.assertEqual(0o027, osutils.get_umask())
366
class TestDateTime(tests.TestCase):
368
def assertFormatedDelta(self, expected, seconds):
369
"""Assert osutils.format_delta formats as expected"""
370
actual = osutils.format_delta(seconds)
371
self.assertEqual(expected, actual)
373
def test_format_delta(self):
374
self.assertFormatedDelta('0 seconds ago', 0)
375
self.assertFormatedDelta('1 second ago', 1)
376
self.assertFormatedDelta('10 seconds ago', 10)
377
self.assertFormatedDelta('59 seconds ago', 59)
378
self.assertFormatedDelta('89 seconds ago', 89)
379
self.assertFormatedDelta('1 minute, 30 seconds ago', 90)
380
self.assertFormatedDelta('3 minutes, 0 seconds ago', 180)
381
self.assertFormatedDelta('3 minutes, 1 second ago', 181)
382
self.assertFormatedDelta('10 minutes, 15 seconds ago', 615)
383
self.assertFormatedDelta('30 minutes, 59 seconds ago', 1859)
384
self.assertFormatedDelta('31 minutes, 0 seconds ago', 1860)
385
self.assertFormatedDelta('60 minutes, 0 seconds ago', 3600)
386
self.assertFormatedDelta('89 minutes, 59 seconds ago', 5399)
387
self.assertFormatedDelta('1 hour, 30 minutes ago', 5400)
388
self.assertFormatedDelta('2 hours, 30 minutes ago', 9017)
389
self.assertFormatedDelta('10 hours, 0 minutes ago', 36000)
390
self.assertFormatedDelta('24 hours, 0 minutes ago', 86400)
391
self.assertFormatedDelta('35 hours, 59 minutes ago', 129599)
392
self.assertFormatedDelta('36 hours, 0 minutes ago', 129600)
393
self.assertFormatedDelta('36 hours, 0 minutes ago', 129601)
394
self.assertFormatedDelta('36 hours, 1 minute ago', 129660)
395
self.assertFormatedDelta('36 hours, 1 minute ago', 129661)
396
self.assertFormatedDelta('84 hours, 10 minutes ago', 303002)
398
# We handle when time steps the wrong direction because computers
399
# don't have synchronized clocks.
400
self.assertFormatedDelta('84 hours, 10 minutes in the future', -303002)
401
self.assertFormatedDelta('1 second in the future', -1)
402
self.assertFormatedDelta('2 seconds in the future', -2)
404
def test_format_date(self):
405
self.assertRaises(osutils.UnsupportedTimezoneFormat,
406
osutils.format_date, 0, timezone='foo')
407
self.assertIsInstance(osutils.format_date(0), str)
408
self.assertIsInstance(osutils.format_local_date(0), text_type)
409
# Testing for the actual value of the local weekday without
410
# duplicating the code from format_date is difficult.
411
# Instead blackbox.test_locale should check for localized
412
# dates once they do occur in output strings.
414
def test_format_date_with_offset_in_original_timezone(self):
415
self.assertEqual("Thu 1970-01-01 00:00:00 +0000",
416
osutils.format_date_with_offset_in_original_timezone(0))
417
self.assertEqual("Fri 1970-01-02 03:46:40 +0000",
418
osutils.format_date_with_offset_in_original_timezone(100000))
419
self.assertEqual("Fri 1970-01-02 05:46:40 +0200",
420
osutils.format_date_with_offset_in_original_timezone(100000, 7200))
422
def test_local_time_offset(self):
423
"""Test that local_time_offset() returns a sane value."""
424
offset = osutils.local_time_offset()
425
self.assertTrue(isinstance(offset, int))
426
# Test that the offset is no more than a eighteen hours in
428
# Time zone handling is system specific, so it is difficult to
429
# do more specific tests, but a value outside of this range is
431
eighteen_hours = 18 * 3600
432
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
434
def test_local_time_offset_with_timestamp(self):
435
"""Test that local_time_offset() works with a timestamp."""
436
offset = osutils.local_time_offset(1000000000.1234567)
437
self.assertTrue(isinstance(offset, int))
438
eighteen_hours = 18 * 3600
439
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
442
class TestFdatasync(tests.TestCaseInTempDir):
444
def do_fdatasync(self):
445
f = tempfile.NamedTemporaryFile()
446
osutils.fdatasync(f.fileno())
450
def raise_eopnotsupp(*args, **kwargs):
451
raise IOError(errno.EOPNOTSUPP, os.strerror(errno.EOPNOTSUPP))
454
def raise_enotsup(*args, **kwargs):
455
raise IOError(errno.ENOTSUP, os.strerror(errno.ENOTSUP))
457
def test_fdatasync_handles_system_function(self):
458
self.overrideAttr(os, "fdatasync")
461
def test_fdatasync_handles_no_fdatasync_no_fsync(self):
462
self.overrideAttr(os, "fdatasync")
463
self.overrideAttr(os, "fsync")
466
def test_fdatasync_handles_no_EOPNOTSUPP(self):
467
self.overrideAttr(errno, "EOPNOTSUPP")
470
def test_fdatasync_catches_ENOTSUP(self):
471
enotsup = getattr(errno, "ENOTSUP", None)
473
raise tests.TestNotApplicable("No ENOTSUP on this platform")
474
self.overrideAttr(os, "fdatasync", self.raise_enotsup)
477
def test_fdatasync_catches_EOPNOTSUPP(self):
478
enotsup = getattr(errno, "EOPNOTSUPP", None)
480
raise tests.TestNotApplicable("No EOPNOTSUPP on this platform")
481
self.overrideAttr(os, "fdatasync", self.raise_eopnotsupp)
485
class TestLinks(tests.TestCaseInTempDir):
487
def test_dereference_path(self):
488
self.requireFeature(features.SymlinkFeature)
489
cwd = osutils.realpath('.')
491
bar_path = osutils.pathjoin(cwd, 'bar')
492
# Using './' to avoid bug #1213894 (first path component not
493
# dereferenced) in Python 2.4.1 and earlier
494
self.assertEqual(bar_path, osutils.realpath('./bar'))
495
os.symlink('bar', 'foo')
496
self.assertEqual(bar_path, osutils.realpath('./foo'))
498
# Does not dereference terminal symlinks
499
foo_path = osutils.pathjoin(cwd, 'foo')
500
self.assertEqual(foo_path, osutils.dereference_path('./foo'))
502
# Dereferences parent symlinks
504
baz_path = osutils.pathjoin(bar_path, 'baz')
505
self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
507
# Dereferences parent symlinks that are the first path element
508
self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
510
# Dereferences parent symlinks in absolute paths
511
foo_baz_path = osutils.pathjoin(foo_path, 'baz')
512
self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
514
def test_changing_access(self):
515
with open('file', 'w') as f:
518
# Make a file readonly
519
osutils.make_readonly('file')
520
mode = os.lstat('file').st_mode
521
self.assertEqual(mode, mode & 0o777555)
523
# Make a file writable
524
osutils.make_writable('file')
525
mode = os.lstat('file').st_mode
526
self.assertEqual(mode, mode | 0o200)
528
if osutils.has_symlinks():
529
# should not error when handed a symlink
530
os.symlink('nonexistent', 'dangling')
531
osutils.make_readonly('dangling')
532
osutils.make_writable('dangling')
534
def test_host_os_dereferences_symlinks(self):
535
osutils.host_os_dereferences_symlinks()
538
class TestCanonicalRelPath(tests.TestCaseInTempDir):
540
_test_needs_features = [features.CaseInsCasePresFilenameFeature]
542
def test_canonical_relpath_simple(self):
543
f = open('MixedCaseName', 'w')
545
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
546
self.assertEqual('work/MixedCaseName', actual)
548
def test_canonical_relpath_missing_tail(self):
549
os.mkdir('MixedCaseParent')
550
actual = osutils.canonical_relpath(self.test_base_dir,
551
'mixedcaseparent/nochild')
552
self.assertEqual('work/MixedCaseParent/nochild', actual)
555
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
557
def assertRelpath(self, expected, base, path):
558
actual = osutils._cicp_canonical_relpath(base, path)
559
self.assertEqual(expected, actual)
561
def test_simple(self):
562
self.build_tree(['MixedCaseName'])
563
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
564
self.assertRelpath('MixedCaseName', base, 'mixedcAsename')
566
def test_subdir_missing_tail(self):
567
self.build_tree(['MixedCaseParent/', 'MixedCaseParent/a_child'])
568
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
569
self.assertRelpath('MixedCaseParent/a_child', base,
570
'MixedCaseParent/a_child')
571
self.assertRelpath('MixedCaseParent/a_child', base,
572
'MixedCaseParent/A_Child')
573
self.assertRelpath('MixedCaseParent/not_child', base,
574
'MixedCaseParent/not_child')
576
def test_at_root_slash(self):
577
# We can't test this on Windows, because it has a 'MIN_ABS_PATHLENGTH'
579
if osutils.MIN_ABS_PATHLENGTH > 1:
580
raise tests.TestSkipped('relpath requires %d chars'
581
% osutils.MIN_ABS_PATHLENGTH)
582
self.assertRelpath('foo', '/', '/foo')
584
def test_at_root_drive(self):
585
if sys.platform != 'win32':
586
raise tests.TestNotApplicable('we can only test drive-letter relative'
587
' paths on Windows where we have drive'
590
# The specific issue is that when at the root of a drive, 'abspath'
591
# returns "C:/" or just "/". However, the code assumes that abspath
592
# always returns something like "C:/foo" or "/foo" (no trailing slash).
593
self.assertRelpath('foo', 'C:/', 'C:/foo')
594
self.assertRelpath('foo', 'X:/', 'X:/foo')
595
self.assertRelpath('foo', 'X:/', 'X://foo')
598
class TestPumpFile(tests.TestCase):
599
"""Test pumpfile method."""
602
super(TestPumpFile, self).setUp()
603
# create a test datablock
604
self.block_size = 512
605
pattern = b'0123456789ABCDEF'
606
self.test_data = pattern * (3 * self.block_size // len(pattern))
607
self.test_data_len = len(self.test_data)
609
def test_bracket_block_size(self):
610
"""Read data in blocks with the requested read size bracketing the
612
# make sure test data is larger than max read size
613
self.assertTrue(self.test_data_len > self.block_size)
615
from_file = file_utils.FakeReadFile(self.test_data)
618
# read (max // 2) bytes and verify read size wasn't affected
619
num_bytes_to_read = self.block_size // 2
620
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
621
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
622
self.assertEqual(from_file.get_read_count(), 1)
624
# read (max) bytes and verify read size wasn't affected
625
num_bytes_to_read = self.block_size
626
from_file.reset_read_count()
627
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
628
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
629
self.assertEqual(from_file.get_read_count(), 1)
631
# read (max + 1) bytes and verify read size was limited
632
num_bytes_to_read = self.block_size + 1
633
from_file.reset_read_count()
634
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
635
self.assertEqual(from_file.get_max_read_size(), self.block_size)
636
self.assertEqual(from_file.get_read_count(), 2)
638
# finish reading the rest of the data
639
num_bytes_to_read = self.test_data_len - to_file.tell()
640
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
642
# report error if the data wasn't equal (we only report the size due
643
# to the length of the data)
644
response_data = to_file.getvalue()
645
if response_data != self.test_data:
646
message = "Data not equal. Expected %d bytes, received %d."
647
self.fail(message % (len(response_data), self.test_data_len))
649
def test_specified_size(self):
650
"""Request a transfer larger than the maximum block size and verify
651
that the maximum read doesn't exceed the block_size."""
652
# make sure test data is larger than max read size
653
self.assertTrue(self.test_data_len > self.block_size)
655
# retrieve data in blocks
656
from_file = file_utils.FakeReadFile(self.test_data)
658
osutils.pumpfile(from_file, to_file, self.test_data_len,
661
# verify read size was equal to the maximum read size
662
self.assertTrue(from_file.get_max_read_size() > 0)
663
self.assertEqual(from_file.get_max_read_size(), self.block_size)
664
self.assertEqual(from_file.get_read_count(), 3)
666
# report error if the data wasn't equal (we only report the size due
667
# to the length of the data)
668
response_data = to_file.getvalue()
669
if response_data != self.test_data:
670
message = "Data not equal. Expected %d bytes, received %d."
671
self.fail(message % (len(response_data), self.test_data_len))
673
def test_to_eof(self):
674
"""Read to end-of-file and verify that the reads are not larger than
675
the maximum read size."""
676
# make sure test data is larger than max read size
677
self.assertTrue(self.test_data_len > self.block_size)
679
# retrieve data to EOF
680
from_file = file_utils.FakeReadFile(self.test_data)
682
osutils.pumpfile(from_file, to_file, -1, self.block_size)
684
# verify read size was equal to the maximum read size
685
self.assertEqual(from_file.get_max_read_size(), self.block_size)
686
self.assertEqual(from_file.get_read_count(), 4)
688
# report error if the data wasn't equal (we only report the size due
689
# to the length of the data)
690
response_data = to_file.getvalue()
691
if response_data != self.test_data:
692
message = "Data not equal. Expected %d bytes, received %d."
693
self.fail(message % (len(response_data), self.test_data_len))
695
def test_defaults(self):
696
"""Verifies that the default arguments will read to EOF -- this
697
test verifies that any existing usages of pumpfile will not be broken
698
with this new version."""
699
# retrieve data using default (old) pumpfile method
700
from_file = file_utils.FakeReadFile(self.test_data)
702
osutils.pumpfile(from_file, to_file)
704
# report error if the data wasn't equal (we only report the size due
705
# to the length of the data)
706
response_data = to_file.getvalue()
707
if response_data != self.test_data:
708
message = "Data not equal. Expected %d bytes, received %d."
709
self.fail(message % (len(response_data), self.test_data_len))
711
def test_report_activity(self):
713
def log_activity(length, direction):
714
activity.append((length, direction))
715
from_file = BytesIO(self.test_data)
717
osutils.pumpfile(from_file, to_file, buff_size=500,
718
report_activity=log_activity, direction='read')
719
self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
720
(36, 'read')], activity)
722
from_file = BytesIO(self.test_data)
725
osutils.pumpfile(from_file, to_file, buff_size=500,
726
report_activity=log_activity, direction='write')
727
self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
728
(36, 'write')], activity)
730
# And with a limited amount of data
731
from_file = BytesIO(self.test_data)
734
osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
735
report_activity=log_activity, direction='read')
736
self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
740
class TestPumpStringFile(tests.TestCase):
742
def test_empty(self):
744
osutils.pump_string_file(b"", output)
745
self.assertEqual(b"", output.getvalue())
747
def test_more_than_segment_size(self):
749
osutils.pump_string_file(b"123456789", output, 2)
750
self.assertEqual(b"123456789", output.getvalue())
752
def test_segment_size(self):
754
osutils.pump_string_file(b"12", output, 2)
755
self.assertEqual(b"12", output.getvalue())
757
def test_segment_size_multiple(self):
759
osutils.pump_string_file(b"1234", output, 2)
760
self.assertEqual(b"1234", output.getvalue())
763
class TestRelpath(tests.TestCase):
765
def test_simple_relpath(self):
766
cwd = osutils.getcwd()
767
subdir = cwd + '/subdir'
768
self.assertEqual('subdir', osutils.relpath(cwd, subdir))
770
def test_deep_relpath(self):
771
cwd = osutils.getcwd()
772
subdir = cwd + '/sub/subsubdir'
773
self.assertEqual('sub/subsubdir', osutils.relpath(cwd, subdir))
775
def test_not_relative(self):
776
self.assertRaises(errors.PathNotChild,
777
osutils.relpath, 'C:/path', 'H:/path')
778
self.assertRaises(errors.PathNotChild,
779
osutils.relpath, 'C:/', 'H:/path')
782
class TestSafeUnicode(tests.TestCase):
784
def test_from_ascii_string(self):
785
self.assertEqual(u'foobar', osutils.safe_unicode(b'foobar'))
787
def test_from_unicode_string_ascii_contents(self):
788
self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
790
def test_from_unicode_string_unicode_contents(self):
791
self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
793
def test_from_utf8_string(self):
794
self.assertEqual(u'foo\xae', osutils.safe_unicode(b'foo\xc2\xae'))
796
def test_bad_utf8_string(self):
797
self.assertRaises(errors.BzrBadParameterNotUnicode,
798
osutils.safe_unicode,
802
class TestSafeUtf8(tests.TestCase):
804
def test_from_ascii_string(self):
806
self.assertEqual(b'foobar', osutils.safe_utf8(f))
808
def test_from_unicode_string_ascii_contents(self):
809
self.assertEqual(b'bargam', osutils.safe_utf8(u'bargam'))
811
def test_from_unicode_string_unicode_contents(self):
812
self.assertEqual(b'bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
814
def test_from_utf8_string(self):
815
self.assertEqual(b'foo\xc2\xae', osutils.safe_utf8(b'foo\xc2\xae'))
817
def test_bad_utf8_string(self):
818
self.assertRaises(errors.BzrBadParameterNotUnicode,
819
osutils.safe_utf8, b'\xbb\xbb')
822
class TestSafeRevisionId(tests.TestCase):
824
def test_from_ascii_string(self):
825
# this shouldn't give a warning because it's getting an ascii string
826
self.assertEqual(b'foobar', osutils.safe_revision_id(b'foobar'))
828
def test_from_unicode_string_ascii_contents(self):
829
self.assertRaises(TypeError,
830
osutils.safe_revision_id, u'bargam')
832
def test_from_unicode_string_unicode_contents(self):
833
self.assertRaises(TypeError,
834
osutils.safe_revision_id, u'bargam\xae')
836
def test_from_utf8_string(self):
837
self.assertEqual(b'foo\xc2\xae',
838
osutils.safe_revision_id(b'foo\xc2\xae'))
841
"""Currently, None is a valid revision_id"""
842
self.assertEqual(None, osutils.safe_revision_id(None))
845
class TestSafeFileId(tests.TestCase):
847
def test_from_ascii_string(self):
848
self.assertEqual(b'foobar', osutils.safe_file_id(b'foobar'))
850
def test_from_unicode_string_ascii_contents(self):
851
self.assertRaises(TypeError, osutils.safe_file_id, u'bargam')
853
def test_from_unicode_string_unicode_contents(self):
854
self.assertRaises(TypeError,
855
osutils.safe_file_id, u'bargam\xae')
857
def test_from_utf8_string(self):
858
self.assertEqual(b'foo\xc2\xae',
859
osutils.safe_file_id(b'foo\xc2\xae'))
862
"""Currently, None is a valid revision_id"""
863
self.assertEqual(None, osutils.safe_file_id(None))
866
class TestSendAll(tests.TestCase):
868
def test_send_with_disconnected_socket(self):
869
class DisconnectedSocket(object):
870
def __init__(self, err):
872
def send(self, content):
876
# All of these should be treated as ConnectionReset
878
for err_cls in (IOError, socket.error):
879
for errnum in osutils._end_of_stream_errors:
880
errs.append(err_cls(errnum))
882
sock = DisconnectedSocket(err)
883
self.assertRaises(errors.ConnectionReset,
884
osutils.send_all, sock, b'some more content')
886
def test_send_with_no_progress(self):
887
# See https://bugs.launchpad.net/bzr/+bug/1047309
888
# It seems that paramiko can get into a state where it doesn't error,
889
# but it returns 0 bytes sent for requests over and over again.
890
class NoSendingSocket(object):
893
def send(self, bytes):
895
if self.call_count > 100:
896
# Prevent the test suite from hanging
897
raise RuntimeError('too many calls')
899
sock = NoSendingSocket()
900
self.assertRaises(errors.ConnectionReset,
901
osutils.send_all, sock, b'content')
902
self.assertEqual(1, sock.call_count)
905
class TestPosixFuncs(tests.TestCase):
906
"""Test that the posix version of normpath returns an appropriate path
907
when used with 2 leading slashes."""
909
def test_normpath(self):
910
self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
911
self.assertEqual('/etc/shadow', osutils._posix_normpath('//etc/shadow'))
912
self.assertEqual('/etc/shadow', osutils._posix_normpath('///etc/shadow'))
915
class TestWin32Funcs(tests.TestCase):
916
"""Test that _win32 versions of os utilities return appropriate paths."""
918
def test_abspath(self):
919
self.requireFeature(features.win32_feature)
920
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
921
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
922
self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
923
self.assertEqual('//HOST/path', osutils._win32_abspath('//HOST/path'))
925
def test_realpath(self):
926
self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
927
self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
929
def test_pathjoin(self):
930
self.assertEqual('path/to/foo',
931
osutils._win32_pathjoin('path', 'to', 'foo'))
932
self.assertEqual('C:/foo',
933
osutils._win32_pathjoin('path\\to', 'C:\\foo'))
934
self.assertEqual('C:/foo',
935
osutils._win32_pathjoin('path/to', 'C:/foo'))
936
self.assertEqual('path/to/foo',
937
osutils._win32_pathjoin('path/to/', 'foo'))
939
def test_pathjoin_late_bugfix(self):
940
if sys.version_info < (2, 7, 6):
944
self.assertEqual(expected,
945
osutils._win32_pathjoin('C:/path/to/', '/foo'))
946
self.assertEqual(expected,
947
osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
949
def test_normpath(self):
950
self.assertEqual('path/to/foo',
951
osutils._win32_normpath(r'path\\from\..\to\.\foo'))
952
self.assertEqual('path/to/foo',
953
osutils._win32_normpath('path//from/../to/./foo'))
955
def test_getcwd(self):
956
cwd = osutils._win32_getcwd()
957
os_cwd = osutils._getcwd()
958
self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])
959
# win32 is inconsistent whether it returns lower or upper case
960
# and even if it was consistent the user might type the other
961
# so we force it to uppercase
962
# running python.exe under cmd.exe return capital C:\\
963
# running win32 python inside a cygwin shell returns lowercase
964
self.assertEqual(os_cwd[0].upper(), cwd[0])
966
def test_fixdrive(self):
967
self.assertEqual('H:/foo', osutils._win32_fixdrive('h:/foo'))
968
self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
969
self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
972
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
973
"""Test win32 functions that create files."""
975
def test_getcwd(self):
976
self.requireFeature(features.UnicodeFilenameFeature)
979
# TODO: jam 20060427 This will probably fail on Mac OSX because
980
# it will change the normalization of B\xe5gfors
981
# Consider using a different unicode character, or make
982
# osutils.getcwd() renormalize the path.
983
self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
985
def test_minimum_path_selection(self):
986
self.assertEqual(set(),
987
osutils.minimum_path_selection([]))
988
self.assertEqual({'a'},
989
osutils.minimum_path_selection(['a']))
990
self.assertEqual({'a', 'b'},
991
osutils.minimum_path_selection(['a', 'b']))
992
self.assertEqual({'a/', 'b'},
993
osutils.minimum_path_selection(['a/', 'b']))
994
self.assertEqual({'a/', 'b'},
995
osutils.minimum_path_selection(['a/c', 'a/', 'b']))
996
self.assertEqual({'a-b', 'a', 'a0b'},
997
osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
999
def test_mkdtemp(self):
1000
tmpdir = osutils._win32_mkdtemp(dir='.')
1001
self.assertFalse('\\' in tmpdir)
1003
def test_rename(self):
1004
with open('a', 'wb') as a:
1006
with open('b', 'wb') as b:
1009
osutils._win32_rename('b', 'a')
1010
self.assertPathExists('a')
1011
self.assertPathDoesNotExist('b')
1012
self.assertFileEqual(b'baz\n', 'a')
1014
def test_rename_missing_file(self):
1015
with open('a', 'wb') as a:
1019
osutils._win32_rename('b', 'a')
1020
except (IOError, OSError) as e:
1021
self.assertEqual(errno.ENOENT, e.errno)
1022
self.assertFileEqual(b'foo\n', 'a')
1024
def test_rename_missing_dir(self):
1027
osutils._win32_rename('b', 'a')
1028
except (IOError, OSError) as e:
1029
self.assertEqual(errno.ENOENT, e.errno)
1031
def test_rename_current_dir(self):
1034
# You can't rename the working directory
1035
# doing rename non-existant . usually
1036
# just raises ENOENT, since non-existant
1039
osutils._win32_rename('b', '.')
1040
except (IOError, OSError) as e:
1041
self.assertEqual(errno.ENOENT, e.errno)
1043
def test_splitpath(self):
1044
def check(expected, path):
1045
self.assertEqual(expected, osutils.splitpath(path))
1048
check(['a', 'b'], 'a/b')
1049
check(['a', 'b'], 'a/./b')
1050
check(['a', '.b'], 'a/.b')
1051
check(['a', '.b'], 'a\\.b')
1053
self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
1056
class TestParentDirectories(tests.TestCaseInTempDir):
1057
"""Test osutils.parent_directories()"""
1059
def test_parent_directories(self):
1060
self.assertEqual([], osutils.parent_directories('a'))
1061
self.assertEqual(['a'], osutils.parent_directories('a/b'))
1062
self.assertEqual(['a/b', 'a'], osutils.parent_directories('a/b/c'))
1065
class TestMacFuncsDirs(tests.TestCaseInTempDir):
1066
"""Test mac special functions that require directories."""
1068
def test_getcwd(self):
1069
self.requireFeature(features.UnicodeFilenameFeature)
1070
os.mkdir(u'B\xe5gfors')
1071
os.chdir(u'B\xe5gfors')
1072
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
1074
def test_getcwd_nonnorm(self):
1075
self.requireFeature(features.UnicodeFilenameFeature)
1076
# Test that _mac_getcwd() will normalize this path
1077
os.mkdir(u'Ba\u030agfors')
1078
os.chdir(u'Ba\u030agfors')
1079
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
1082
class TestChunksToLines(tests.TestCase):
1084
def test_smoketest(self):
1085
self.assertEqual([b'foo\n', b'bar\n', b'baz\n'],
1086
osutils.chunks_to_lines([b'foo\nbar', b'\nbaz\n']))
1087
self.assertEqual([b'foo\n', b'bar\n', b'baz\n'],
1088
osutils.chunks_to_lines([b'foo\n', b'bar\n', b'baz\n']))
1090
def test_osutils_binding(self):
1091
from . import test__chunks_to_lines
1092
if test__chunks_to_lines.compiled_chunkstolines_feature.available():
1093
from .._chunks_to_lines_pyx import chunks_to_lines
1095
from .._chunks_to_lines_py import chunks_to_lines
1096
self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
1099
class TestSplitLines(tests.TestCase):
1101
def test_split_unicode(self):
1102
self.assertEqual([u'foo\n', u'bar\xae'],
1103
osutils.split_lines(u'foo\nbar\xae'))
1104
self.assertEqual([u'foo\n', u'bar\xae\n'],
1105
osutils.split_lines(u'foo\nbar\xae\n'))
1107
def test_split_with_carriage_returns(self):
1108
self.assertEqual([b'foo\rbar\n'],
1109
osutils.split_lines(b'foo\rbar\n'))
1112
class TestWalkDirs(tests.TestCaseInTempDir):
1114
def assertExpectedBlocks(self, expected, result):
1115
self.assertEqual(expected,
1116
[(dirinfo, [line[0:3] for line in block])
1117
for dirinfo, block in result])
1119
def test_walkdirs(self):
1128
self.build_tree(tree)
1129
expected_dirblocks = [
1131
[('0file', '0file', 'file'),
1132
('1dir', '1dir', 'directory'),
1133
('2file', '2file', 'file'),
1136
(('1dir', './1dir'),
1137
[('1dir/0file', '0file', 'file'),
1138
('1dir/1dir', '1dir', 'directory'),
1141
(('1dir/1dir', './1dir/1dir'),
1147
found_bzrdir = False
1148
for dirdetail, dirblock in osutils.walkdirs('.'):
1149
if len(dirblock) and dirblock[0][1] == '.bzr':
1150
# this tests the filtering of selected paths
1153
result.append((dirdetail, dirblock))
1155
self.assertTrue(found_bzrdir)
1156
self.assertExpectedBlocks(expected_dirblocks, result)
1157
# you can search a subdir only, with a supplied prefix.
1159
for dirblock in osutils.walkdirs('./1dir', '1dir'):
1160
result.append(dirblock)
1161
self.assertExpectedBlocks(expected_dirblocks[1:], result)
1163
def test_walkdirs_os_error(self):
1164
# <https://bugs.launchpad.net/bzr/+bug/338653>
1165
# Pyrex readdir didn't raise useful messages if it had an error
1166
# reading the directory
1167
if sys.platform == 'win32':
1168
raise tests.TestNotApplicable(
1169
"readdir IOError not tested on win32")
1170
self.requireFeature(features.not_running_as_root)
1171
os.mkdir("test-unreadable")
1172
os.chmod("test-unreadable", 0000)
1173
# must chmod it back so that it can be removed
1174
self.addCleanup(os.chmod, "test-unreadable", 0o700)
1175
# The error is not raised until the generator is actually evaluated.
1176
# (It would be ok if it happened earlier but at the moment it
1178
e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
1179
self.assertEqual('./test-unreadable', e.filename)
1180
self.assertEqual(errno.EACCES, e.errno)
1181
# Ensure the message contains the file name
1182
self.assertContainsRe(str(e), "\\./test-unreadable")
1185
def test_walkdirs_encoding_error(self):
1186
# <https://bugs.launchpad.net/bzr/+bug/488519>
1187
# walkdirs didn't raise a useful message when the filenames
1188
# are not using the filesystem's encoding
1190
# require a bytestring based filesystem
1191
self.requireFeature(features.ByteStringNamedFilesystem)
1202
self.build_tree(tree)
1204
# rename the 1file to a latin-1 filename
1205
os.rename("./1file", "\xe8file")
1206
if "\xe8file" not in os.listdir("."):
1207
self.skipTest("Lack filesystem that preserves arbitrary bytes")
1209
self._save_platform_info()
1210
osutils._fs_enc = 'UTF-8'
1212
# this should raise on error
1214
for dirdetail, dirblock in osutils.walkdirs('.'):
1217
self.assertRaises(errors.BadFilenameEncoding, attempt)
1219
def test__walkdirs_utf8(self):
1228
self.build_tree(tree)
1229
expected_dirblocks = [
1231
[('0file', '0file', 'file'),
1232
('1dir', '1dir', 'directory'),
1233
('2file', '2file', 'file'),
1236
(('1dir', './1dir'),
1237
[('1dir/0file', '0file', 'file'),
1238
('1dir/1dir', '1dir', 'directory'),
1241
(('1dir/1dir', './1dir/1dir'),
1247
found_bzrdir = False
1248
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1249
if len(dirblock) and dirblock[0][1] == '.bzr':
1250
# this tests the filtering of selected paths
1253
result.append((dirdetail, dirblock))
1255
self.assertTrue(found_bzrdir)
1256
self.assertExpectedBlocks(expected_dirblocks, result)
1258
# you can search a subdir only, with a supplied prefix.
1260
for dirblock in osutils.walkdirs('./1dir', '1dir'):
1261
result.append(dirblock)
1262
self.assertExpectedBlocks(expected_dirblocks[1:], result)
1264
def _filter_out_stat(self, result):
1265
"""Filter out the stat value from the walkdirs result"""
1266
for dirdetail, dirblock in result:
1268
for info in dirblock:
1269
# Ignore info[3] which is the stat
1270
new_dirblock.append((info[0], info[1], info[2], info[4]))
1271
dirblock[:] = new_dirblock
1273
def _save_platform_info(self):
1274
self.overrideAttr(osutils, '_fs_enc')
1275
self.overrideAttr(osutils, '_selected_dir_reader')
1277
def assertDirReaderIs(self, expected):
1278
"""Assert the right implementation for _walkdirs_utf8 is chosen."""
1279
# Force it to redetect
1280
osutils._selected_dir_reader = None
1281
# Nothing to list, but should still trigger the selection logic
1282
self.assertEqual([((b'', '.'), [])], list(osutils._walkdirs_utf8('.')))
1283
self.assertIsInstance(osutils._selected_dir_reader, expected)
1285
def test_force_walkdirs_utf8_fs_utf8(self):
1286
self.requireFeature(UTF8DirReaderFeature)
1287
self._save_platform_info()
1288
osutils._fs_enc = 'utf-8'
1289
self.assertDirReaderIs(
1290
UTF8DirReaderFeature.module.UTF8DirReader)
1292
def test_force_walkdirs_utf8_fs_ascii(self):
1293
self.requireFeature(UTF8DirReaderFeature)
1294
self._save_platform_info()
1295
osutils._fs_enc = 'ascii'
1296
self.assertDirReaderIs(
1297
UTF8DirReaderFeature.module.UTF8DirReader)
1299
def test_force_walkdirs_utf8_fs_latin1(self):
1300
self._save_platform_info()
1301
osutils._fs_enc = 'iso-8859-1'
1302
self.assertDirReaderIs(osutils.UnicodeDirReader)
1304
def test_force_walkdirs_utf8_nt(self):
1305
# Disabled because the thunk of the whole walkdirs api is disabled.
1306
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1307
self._save_platform_info()
1308
from .._walkdirs_win32 import Win32ReadDir
1309
self.assertDirReaderIs(Win32ReadDir)
1311
def test_unicode_walkdirs(self):
1312
"""Walkdirs should always return unicode paths."""
1313
self.requireFeature(features.UnicodeFilenameFeature)
1314
name0 = u'0file-\xb6'
1315
name1 = u'1dir-\u062c\u0648'
1316
name2 = u'2file-\u0633'
1320
name1 + '/' + name0,
1321
name1 + '/' + name1 + '/',
1324
self.build_tree(tree)
1325
expected_dirblocks = [
1327
[(name0, name0, 'file', './' + name0),
1328
(name1, name1, 'directory', './' + name1),
1329
(name2, name2, 'file', './' + name2),
1332
((name1, './' + name1),
1333
[(name1 + '/' + name0, name0, 'file', './' + name1
1335
(name1 + '/' + name1, name1, 'directory', './' + name1
1339
((name1 + '/' + name1, './' + name1 + '/' + name1),
1344
result = list(osutils.walkdirs('.'))
1345
self._filter_out_stat(result)
1346
self.assertEqual(expected_dirblocks, result)
1347
result = list(osutils.walkdirs(u'./'+name1, name1))
1348
self._filter_out_stat(result)
1349
self.assertEqual(expected_dirblocks[1:], result)
1351
def test_unicode__walkdirs_utf8(self):
1352
"""Walkdirs_utf8 should always return utf8 paths.
1354
The abspath portion might be in unicode or utf-8
1356
self.requireFeature(features.UnicodeFilenameFeature)
1357
name0 = u'0file-\xb6'
1358
name1 = u'1dir-\u062c\u0648'
1359
name2 = u'2file-\u0633'
1363
name1 + '/' + name0,
1364
name1 + '/' + name1 + '/',
1367
self.build_tree(tree)
1368
name0 = name0.encode('utf8')
1369
name1 = name1.encode('utf8')
1370
name2 = name2.encode('utf8')
1372
expected_dirblocks = [
1374
[(name0, name0, 'file', b'./' + name0),
1375
(name1, name1, 'directory', b'./' + name1),
1376
(name2, name2, 'file', b'./' + name2),
1379
((name1, b'./' + name1),
1380
[(name1 + b'/' + name0, name0, 'file', b'./' + name1
1382
(name1 + b'/' + name1, name1, 'directory', b'./' + name1
1386
((name1 + b'/' + name1, b'./' + name1 + b'/' + name1),
1392
# For ease in testing, if walkdirs_utf8 returns Unicode, assert that
1393
# all abspaths are Unicode, and encode them back into utf8.
1394
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1395
self.assertIsInstance(dirdetail[0], bytes)
1396
if isinstance(dirdetail[1], text_type):
1397
dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
1398
dirblock = [list(info) for info in dirblock]
1399
for info in dirblock:
1400
self.assertIsInstance(info[4], text_type)
1401
info[4] = info[4].encode('utf8')
1403
for info in dirblock:
1404
self.assertIsInstance(info[0], bytes)
1405
self.assertIsInstance(info[1], bytes)
1406
self.assertIsInstance(info[4], bytes)
1407
# Remove the stat information
1408
new_dirblock.append((info[0], info[1], info[2], info[4]))
1409
result.append((dirdetail, new_dirblock))
1410
self.assertEqual(expected_dirblocks, result)
1412
def test__walkdirs_utf8_with_unicode_fs(self):
1413
"""UnicodeDirReader should be a safe fallback everywhere
1415
The abspath portion should be in unicode
1417
self.requireFeature(features.UnicodeFilenameFeature)
1418
# Use the unicode reader. TODO: split into driver-and-driven unit
1420
self._save_platform_info()
1421
osutils._selected_dir_reader = osutils.UnicodeDirReader()
1422
name0u = u'0file-\xb6'
1423
name1u = u'1dir-\u062c\u0648'
1424
name2u = u'2file-\u0633'
1428
name1u + '/' + name0u,
1429
name1u + '/' + name1u + '/',
1432
self.build_tree(tree)
1433
name0 = name0u.encode('utf8')
1434
name1 = name1u.encode('utf8')
1435
name2 = name2u.encode('utf8')
1437
# All of the abspaths should be in unicode, all of the relative paths
1439
expected_dirblocks = [
1441
[(name0, name0, 'file', './' + name0u),
1442
(name1, name1, 'directory', './' + name1u),
1443
(name2, name2, 'file', './' + name2u),
1446
((name1, './' + name1u),
1447
[(name1 + b'/' + name0, name0, 'file', './' + name1u
1449
(name1 + b'/' + name1, name1, 'directory', './' + name1u
1453
((name1 + b'/' + name1, './' + name1u + '/' + name1u),
1458
result = list(osutils._walkdirs_utf8('.'))
1459
self._filter_out_stat(result)
1460
self.assertEqual(expected_dirblocks, result)
1462
def test__walkdirs_utf8_win32readdir(self):
1463
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1464
self.requireFeature(features.UnicodeFilenameFeature)
1465
from .._walkdirs_win32 import Win32ReadDir
1466
self._save_platform_info()
1467
osutils._selected_dir_reader = Win32ReadDir()
1468
name0u = u'0file-\xb6'
1469
name1u = u'1dir-\u062c\u0648'
1470
name2u = u'2file-\u0633'
1474
name1u + '/' + name0u,
1475
name1u + '/' + name1u + '/',
1478
self.build_tree(tree)
1479
name0 = name0u.encode('utf8')
1480
name1 = name1u.encode('utf8')
1481
name2 = name2u.encode('utf8')
1483
# All of the abspaths should be in unicode, all of the relative paths
1485
expected_dirblocks = [
1487
[(name0, name0, 'file', './' + name0u),
1488
(name1, name1, 'directory', './' + name1u),
1489
(name2, name2, 'file', './' + name2u),
1492
((name1, './' + name1u),
1493
[(name1 + '/' + name0, name0, 'file', './' + name1u
1495
(name1 + '/' + name1, name1, 'directory', './' + name1u
1499
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1504
result = list(osutils._walkdirs_utf8(u'.'))
1505
self._filter_out_stat(result)
1506
self.assertEqual(expected_dirblocks, result)
1508
def assertStatIsCorrect(self, path, win32stat):
1509
os_stat = os.stat(path)
1510
self.assertEqual(os_stat.st_size, win32stat.st_size)
1511
self.assertAlmostEqual(os_stat.st_mtime, win32stat.st_mtime, places=4)
1512
self.assertAlmostEqual(os_stat.st_ctime, win32stat.st_ctime, places=4)
1513
self.assertAlmostEqual(os_stat.st_atime, win32stat.st_atime, places=4)
1514
self.assertEqual(os_stat.st_dev, win32stat.st_dev)
1515
self.assertEqual(os_stat.st_ino, win32stat.st_ino)
1516
self.assertEqual(os_stat.st_mode, win32stat.st_mode)
1518
def test__walkdirs_utf_win32_find_file_stat_file(self):
1519
"""make sure our Stat values are valid"""
1520
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1521
self.requireFeature(features.UnicodeFilenameFeature)
1522
from .._walkdirs_win32 import Win32ReadDir
1523
name0u = u'0file-\xb6'
1524
name0 = name0u.encode('utf8')
1525
self.build_tree([name0u])
1526
# I hate to sleep() here, but I'm trying to make the ctime different
1529
with open(name0u, 'ab') as f:
1530
f.write(b'just a small update')
1532
result = Win32ReadDir().read_dir('', u'.')
1534
self.assertEqual((name0, name0, 'file'), entry[:3])
1535
self.assertEqual(u'./' + name0u, entry[4])
1536
self.assertStatIsCorrect(entry[4], entry[3])
1537
self.assertNotEqual(entry[3].st_mtime, entry[3].st_ctime)
1539
def test__walkdirs_utf_win32_find_file_stat_directory(self):
1540
"""make sure our Stat values are valid"""
1541
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1542
self.requireFeature(features.UnicodeFilenameFeature)
1543
from .._walkdirs_win32 import Win32ReadDir
1544
name0u = u'0dir-\u062c\u0648'
1545
name0 = name0u.encode('utf8')
1546
self.build_tree([name0u + '/'])
1548
result = Win32ReadDir().read_dir('', u'.')
1550
self.assertEqual((name0, name0, 'directory'), entry[:3])
1551
self.assertEqual(u'./' + name0u, entry[4])
1552
self.assertStatIsCorrect(entry[4], entry[3])
1554
def assertPathCompare(self, path_less, path_greater):
1555
"""check that path_less and path_greater compare correctly."""
1556
self.assertEqual(0, osutils.compare_paths_prefix_order(
1557
path_less, path_less))
1558
self.assertEqual(0, osutils.compare_paths_prefix_order(
1559
path_greater, path_greater))
1560
self.assertEqual(-1, osutils.compare_paths_prefix_order(
1561
path_less, path_greater))
1562
self.assertEqual(1, osutils.compare_paths_prefix_order(
1563
path_greater, path_less))
1565
def test_compare_paths_prefix_order(self):
1566
# root before all else
1567
self.assertPathCompare("/", "/a")
1568
# alpha within a dir
1569
self.assertPathCompare("/a", "/b")
1570
self.assertPathCompare("/b", "/z")
1571
# high dirs before lower.
1572
self.assertPathCompare("/z", "/a/a")
1573
# except if the deeper dir should be output first
1574
self.assertPathCompare("/a/b/c", "/d/g")
1575
# lexical betwen dirs of the same height
1576
self.assertPathCompare("/a/z", "/z/z")
1577
self.assertPathCompare("/a/c/z", "/a/d/e")
1579
# this should also be consistent for no leading / paths
1580
# root before all else
1581
self.assertPathCompare("", "a")
1582
# alpha within a dir
1583
self.assertPathCompare("a", "b")
1584
self.assertPathCompare("b", "z")
1585
# high dirs before lower.
1586
self.assertPathCompare("z", "a/a")
1587
# except if the deeper dir should be output first
1588
self.assertPathCompare("a/b/c", "d/g")
1589
# lexical betwen dirs of the same height
1590
self.assertPathCompare("a/z", "z/z")
1591
self.assertPathCompare("a/c/z", "a/d/e")
1593
def test_path_prefix_sorting(self):
1594
"""Doing a sort on path prefix should match our sample data."""
1609
dir_sorted_paths = [
1625
sorted(original_paths, key=osutils.path_prefix_key))
1626
# using the comparison routine shoudl work too:
1629
sorted(original_paths, key=osutils.path_prefix_key))
1632
class TestCopyTree(tests.TestCaseInTempDir):
1634
def test_copy_basic_tree(self):
1635
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1636
osutils.copy_tree('source', 'target')
1637
self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
1638
self.assertEqual(['c'], os.listdir('target/b'))
1640
def test_copy_tree_target_exists(self):
1641
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
1643
osutils.copy_tree('source', 'target')
1644
self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
1645
self.assertEqual(['c'], os.listdir('target/b'))
1647
def test_copy_tree_symlinks(self):
1648
self.requireFeature(features.SymlinkFeature)
1649
self.build_tree(['source/'])
1650
os.symlink('a/generic/path', 'source/lnk')
1651
osutils.copy_tree('source', 'target')
1652
self.assertEqual(['lnk'], os.listdir('target'))
1653
self.assertEqual('a/generic/path', os.readlink('target/lnk'))
1655
def test_copy_tree_handlers(self):
1656
processed_files = []
1657
processed_links = []
1658
def file_handler(from_path, to_path):
1659
processed_files.append(('f', from_path, to_path))
1660
def dir_handler(from_path, to_path):
1661
processed_files.append(('d', from_path, to_path))
1662
def link_handler(from_path, to_path):
1663
processed_links.append((from_path, to_path))
1664
handlers = {'file': file_handler,
1665
'directory': dir_handler,
1666
'symlink': link_handler,
1669
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1670
if osutils.has_symlinks():
1671
os.symlink('a/generic/path', 'source/lnk')
1672
osutils.copy_tree('source', 'target', handlers=handlers)
1674
self.assertEqual([('d', 'source', 'target'),
1675
('f', 'source/a', 'target/a'),
1676
('d', 'source/b', 'target/b'),
1677
('f', 'source/b/c', 'target/b/c'),
1679
self.assertPathDoesNotExist('target')
1680
if osutils.has_symlinks():
1681
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1684
class TestSetUnsetEnv(tests.TestCase):
1685
"""Test updating the environment"""
1688
super(TestSetUnsetEnv, self).setUp()
1690
self.assertEqual(None, os.environ.get('BRZ_TEST_ENV_VAR'),
1691
'Environment was not cleaned up properly.'
1692
' Variable BRZ_TEST_ENV_VAR should not exist.')
1694
if 'BRZ_TEST_ENV_VAR' in os.environ:
1695
del os.environ['BRZ_TEST_ENV_VAR']
1696
self.addCleanup(cleanup)
1699
"""Test that we can set an env variable"""
1700
old = osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'foo')
1701
self.assertEqual(None, old)
1702
self.assertEqual('foo', os.environ.get('BRZ_TEST_ENV_VAR'))
1704
def test_double_set(self):
1705
"""Test that we get the old value out"""
1706
osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'foo')
1707
old = osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'bar')
1708
self.assertEqual('foo', old)
1709
self.assertEqual('bar', os.environ.get('BRZ_TEST_ENV_VAR'))
1711
def test_unicode(self):
1712
"""Environment can only contain plain strings
1714
So Unicode strings must be encoded.
1716
uni_val, env_val = tests.probe_unicode_in_user_encoding()
1718
raise tests.TestSkipped(
1719
'Cannot find a unicode character that works in encoding %s'
1720
% (osutils.get_user_encoding(),))
1722
old = osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', uni_val)
1723
self.assertEqual(env_val, os.environ.get('BRZ_TEST_ENV_VAR'))
1725
def test_unset(self):
1726
"""Test that passing None will remove the env var"""
1727
osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'foo')
1728
old = osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', None)
1729
self.assertEqual('foo', old)
1730
self.assertEqual(None, os.environ.get('BRZ_TEST_ENV_VAR'))
1731
self.assertNotIn('BRZ_TEST_ENV_VAR', os.environ)
1734
class TestSizeShaFile(tests.TestCaseInTempDir):
1736
def test_sha_empty(self):
1737
self.build_tree_contents([('foo', b'')])
1738
expected_sha = osutils.sha_string(b'')
1740
self.addCleanup(f.close)
1741
size, sha = osutils.size_sha_file(f)
1742
self.assertEqual(0, size)
1743
self.assertEqual(expected_sha, sha)
1745
def test_sha_mixed_endings(self):
1746
text = b'test\r\nwith\nall\rpossible line endings\r\n'
1747
self.build_tree_contents([('foo', text)])
1748
expected_sha = osutils.sha_string(text)
1749
f = open('foo', 'rb')
1750
self.addCleanup(f.close)
1751
size, sha = osutils.size_sha_file(f)
1752
self.assertEqual(38, size)
1753
self.assertEqual(expected_sha, sha)
1756
class TestShaFileByName(tests.TestCaseInTempDir):
1758
def test_sha_empty(self):
1759
self.build_tree_contents([('foo', b'')])
1760
expected_sha = osutils.sha_string(b'')
1761
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1763
def test_sha_mixed_endings(self):
1764
text = b'test\r\nwith\nall\rpossible line endings\r\n'
1765
self.build_tree_contents([('foo', text)])
1766
expected_sha = osutils.sha_string(text)
1767
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1770
class TestResourceLoading(tests.TestCaseInTempDir):
1772
def test_resource_string(self):
1773
# test resource in breezy
1774
text = osutils.resource_string('breezy', 'debug.py')
1775
self.assertContainsRe(text, "debug_flags = set()")
1776
# test resource under breezy
1777
text = osutils.resource_string('breezy.ui', 'text.py')
1778
self.assertContainsRe(text, "class TextUIFactory")
1779
# test unsupported package
1780
self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
1782
# test unknown resource
1783
self.assertRaises(IOError, osutils.resource_string, 'breezy', 'yyy.xx')
1786
class TestDirReader(tests.TestCaseInTempDir):
1788
scenarios = dir_reader_scenarios()
1791
_dir_reader_class = None
1792
_native_to_unicode = None
1795
super(TestDirReader, self).setUp()
1796
self.overrideAttr(osutils,
1797
'_selected_dir_reader', self._dir_reader_class())
1799
def _get_ascii_tree(self):
1807
expected_dirblocks = [
1809
[(b'0file', b'0file', 'file'),
1810
(b'1dir', b'1dir', 'directory'),
1811
(b'2file', b'2file', 'file'),
1814
((b'1dir', './1dir'),
1815
[(b'1dir/0file', b'0file', 'file'),
1816
(b'1dir/1dir', b'1dir', 'directory'),
1819
((b'1dir/1dir', './1dir/1dir'),
1824
return tree, expected_dirblocks
1826
def test_walk_cur_dir(self):
1827
tree, expected_dirblocks = self._get_ascii_tree()
1828
self.build_tree(tree)
1829
result = list(osutils._walkdirs_utf8('.'))
1830
# Filter out stat and abspath
1831
self.assertEqual(expected_dirblocks,
1832
[(dirinfo, [line[0:3] for line in block])
1833
for dirinfo, block in result])
1835
def test_walk_sub_dir(self):
1836
tree, expected_dirblocks = self._get_ascii_tree()
1837
self.build_tree(tree)
1838
# you can search a subdir only, with a supplied prefix.
1839
result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
1840
# Filter out stat and abspath
1841
self.assertEqual(expected_dirblocks[1:],
1842
[(dirinfo, [line[0:3] for line in block])
1843
for dirinfo, block in result])
1845
def _get_unicode_tree(self):
1846
name0u = u'0file-\xb6'
1847
name1u = u'1dir-\u062c\u0648'
1848
name2u = u'2file-\u0633'
1852
name1u + '/' + name0u,
1853
name1u + '/' + name1u + '/',
1856
name0 = name0u.encode('UTF-8')
1857
name1 = name1u.encode('UTF-8')
1858
name2 = name2u.encode('UTF-8')
1859
expected_dirblocks = [
1861
[(name0, name0, 'file', './' + name0u),
1862
(name1, name1, 'directory', './' + name1u),
1863
(name2, name2, 'file', './' + name2u),
1866
((name1, './' + name1u),
1867
[(name1 + b'/' + name0, name0, 'file', './' + name1u
1869
(name1 + b'/' + name1, name1, 'directory', './' + name1u
1873
((name1 + b'/' + name1, './' + name1u + '/' + name1u),
1878
return tree, expected_dirblocks
1880
def _filter_out(self, raw_dirblocks):
1881
"""Filter out a walkdirs_utf8 result.
1883
stat field is removed, all native paths are converted to unicode
1885
filtered_dirblocks = []
1886
for dirinfo, block in raw_dirblocks:
1887
dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
1890
details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
1891
filtered_dirblocks.append((dirinfo, details))
1892
return filtered_dirblocks
1894
def test_walk_unicode_tree(self):
1895
self.requireFeature(features.UnicodeFilenameFeature)
1896
tree, expected_dirblocks = self._get_unicode_tree()
1897
self.build_tree(tree)
1898
result = list(osutils._walkdirs_utf8('.'))
1899
self.assertEqual(expected_dirblocks, self._filter_out(result))
1901
def test_symlink(self):
1902
self.requireFeature(features.SymlinkFeature)
1903
self.requireFeature(features.UnicodeFilenameFeature)
1904
target = u'target\N{Euro Sign}'
1905
link_name = u'l\N{Euro Sign}nk'
1906
os.symlink(target, link_name)
1907
target_utf8 = target.encode('UTF-8')
1908
link_name_utf8 = link_name.encode('UTF-8')
1909
expected_dirblocks = [
1911
[(link_name_utf8, link_name_utf8,
1912
'symlink', './' + link_name),],
1914
result = list(osutils._walkdirs_utf8('.'))
1915
self.assertEqual(expected_dirblocks, self._filter_out(result))
1918
class TestReadLink(tests.TestCaseInTempDir):
1919
"""Exposes os.readlink() problems and the osutils solution.
1921
The only guarantee offered by os.readlink(), starting with 2.6, is that a
1922
unicode string will be returned if a unicode string is passed.
1924
But prior python versions failed to properly encode the passed unicode
1927
_test_needs_features = [features.SymlinkFeature, features.UnicodeFilenameFeature]
1930
super(tests.TestCaseInTempDir, self).setUp()
1931
self.link = u'l\N{Euro Sign}ink'
1932
self.target = u'targe\N{Euro Sign}t'
1933
os.symlink(self.target, self.link)
1935
def test_os_readlink_link_encoding(self):
1936
self.assertEqual(self.target, os.readlink(self.link))
1938
def test_os_readlink_link_decoding(self):
1939
self.assertEqual(self.target.encode(osutils._fs_enc),
1940
os.readlink(self.link.encode(osutils._fs_enc)))
1943
class TestConcurrency(tests.TestCase):
1946
super(TestConcurrency, self).setUp()
1947
self.overrideAttr(osutils, '_cached_local_concurrency')
1949
def test_local_concurrency(self):
1950
concurrency = osutils.local_concurrency()
1951
self.assertIsInstance(concurrency, int)
1953
def test_local_concurrency_environment_variable(self):
1954
self.overrideEnv('BRZ_CONCURRENCY', '2')
1955
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1956
self.overrideEnv('BRZ_CONCURRENCY', '3')
1957
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1958
self.overrideEnv('BRZ_CONCURRENCY', 'foo')
1959
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1961
def test_option_concurrency(self):
1962
self.overrideEnv('BRZ_CONCURRENCY', '1')
1963
self.run_bzr('rocks --concurrency 42')
1964
# Command line overrides environment variable
1965
self.assertEqual('42', os.environ['BRZ_CONCURRENCY'])
1966
self.assertEqual(42, osutils.local_concurrency(use_cache=False))
1969
class TestFailedToLoadExtension(tests.TestCase):
1971
def _try_loading(self):
1973
import breezy._fictional_extension_py
1974
except ImportError as e:
1975
osutils.failed_to_load_extension(e)
1979
super(TestFailedToLoadExtension, self).setUp()
1980
self.overrideAttr(osutils, '_extension_load_failures', [])
1982
def test_failure_to_load(self):
1984
self.assertLength(1, osutils._extension_load_failures)
1986
self.assertEqual(osutils._extension_load_failures[0],
1987
"No module named 'breezy._fictional_extension_py'")
1989
self.assertEqual(osutils._extension_load_failures[0],
1990
"No module named _fictional_extension_py")
1992
def test_report_extension_load_failures_no_warning(self):
1993
self.assertTrue(self._try_loading())
1994
warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
1995
# it used to give a Python warning; it no longer does
1996
self.assertLength(0, warnings)
1998
def test_report_extension_load_failures_message(self):
2000
trace.push_log_file(log)
2001
self.assertTrue(self._try_loading())
2002
osutils.report_extension_load_failures()
2003
self.assertContainsRe(
2005
br"brz: warning: some compiled extensions could not be loaded; "
2006
b"see ``brz help missing-extensions``\n"
2010
class TestTerminalWidth(tests.TestCase):
2013
super(TestTerminalWidth, self).setUp()
2014
self._orig_terminal_size_state = osutils._terminal_size_state
2015
self._orig_first_terminal_size = osutils._first_terminal_size
2016
self.addCleanup(self.restore_osutils_globals)
2017
osutils._terminal_size_state = 'no_data'
2018
osutils._first_terminal_size = None
2020
def restore_osutils_globals(self):
2021
osutils._terminal_size_state = self._orig_terminal_size_state
2022
osutils._first_terminal_size = self._orig_first_terminal_size
2024
def replace_stdout(self, new):
2025
self.overrideAttr(sys, 'stdout', new)
2027
def replace__terminal_size(self, new):
2028
self.overrideAttr(osutils, '_terminal_size', new)
2030
def set_fake_tty(self):
2032
class I_am_a_tty(object):
2036
self.replace_stdout(I_am_a_tty())
2038
def test_default_values(self):
2039
self.assertEqual(80, osutils.default_terminal_width)
2041
def test_defaults_to_BRZ_COLUMNS(self):
2042
# BRZ_COLUMNS is set by the test framework
2043
self.assertNotEqual('12', os.environ['BRZ_COLUMNS'])
2044
self.overrideEnv('BRZ_COLUMNS', '12')
2045
self.assertEqual(12, osutils.terminal_width())
2047
def test_BRZ_COLUMNS_0_no_limit(self):
2048
self.overrideEnv('BRZ_COLUMNS', '0')
2049
self.assertEqual(None, osutils.terminal_width())
2051
def test_falls_back_to_COLUMNS(self):
2052
self.overrideEnv('BRZ_COLUMNS', None)
2053
self.assertNotEqual('42', os.environ['COLUMNS'])
2055
self.overrideEnv('COLUMNS', '42')
2056
self.assertEqual(42, osutils.terminal_width())
2058
def test_tty_default_without_columns(self):
2059
self.overrideEnv('BRZ_COLUMNS', None)
2060
self.overrideEnv('COLUMNS', None)
2062
def terminal_size(w, h):
2066
# We need to override the osutils definition as it depends on the
2067
# running environment that we can't control (PQM running without a
2068
# controlling terminal is one example).
2069
self.replace__terminal_size(terminal_size)
2070
self.assertEqual(42, osutils.terminal_width())
2072
def test_non_tty_default_without_columns(self):
2073
self.overrideEnv('BRZ_COLUMNS', None)
2074
self.overrideEnv('COLUMNS', None)
2075
self.replace_stdout(None)
2076
self.assertEqual(None, osutils.terminal_width())
2078
def test_no_TIOCGWINSZ(self):
2079
self.requireFeature(term_ios_feature)
2080
termios = term_ios_feature.module
2081
# bug 63539 is about a termios without TIOCGWINSZ attribute
2083
orig = termios.TIOCGWINSZ
2084
except AttributeError:
2085
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
2088
self.overrideAttr(termios, 'TIOCGWINSZ')
2089
del termios.TIOCGWINSZ
2090
self.overrideEnv('BRZ_COLUMNS', None)
2091
self.overrideEnv('COLUMNS', None)
2092
# Whatever the result is, if we don't raise an exception, it's ok.
2093
osutils.terminal_width()
2096
class TestCreationOps(tests.TestCaseInTempDir):
2097
_test_needs_features = [features.chown_feature]
2100
super(TestCreationOps, self).setUp()
2101
self.overrideAttr(os, 'chown', self._dummy_chown)
2103
# params set by call to _dummy_chown
2104
self.path = self.uid = self.gid = None
2106
def _dummy_chown(self, path, uid, gid):
2107
self.path, self.uid, self.gid = path, uid, gid
2109
def test_copy_ownership_from_path(self):
2110
"""copy_ownership_from_path test with specified src."""
2112
f = open('test_file', 'wt')
2113
osutils.copy_ownership_from_path('test_file', ownsrc)
2116
self.assertEqual(self.path, 'test_file')
2117
self.assertEqual(self.uid, s.st_uid)
2118
self.assertEqual(self.gid, s.st_gid)
2120
def test_copy_ownership_nonesrc(self):
2121
"""copy_ownership_from_path test with src=None."""
2122
f = open('test_file', 'wt')
2123
# should use parent dir for permissions
2124
osutils.copy_ownership_from_path('test_file')
2127
self.assertEqual(self.path, 'test_file')
2128
self.assertEqual(self.uid, s.st_uid)
2129
self.assertEqual(self.gid, s.st_gid)
2132
class TestPathFromEnviron(tests.TestCase):
2134
def test_is_unicode(self):
2135
self.overrideEnv('BRZ_TEST_PATH', './anywhere at all/')
2136
path = osutils.path_from_environ('BRZ_TEST_PATH')
2137
self.assertIsInstance(path, text_type)
2138
self.assertEqual(u'./anywhere at all/', path)
2140
def test_posix_path_env_ascii(self):
2141
self.overrideEnv('BRZ_TEST_PATH', '/tmp')
2142
home = osutils._posix_path_from_environ('BRZ_TEST_PATH')
2143
self.assertIsInstance(home, text_type)
2144
self.assertEqual(u'/tmp', home)
2146
def test_posix_path_env_unicode(self):
2147
self.requireFeature(features.ByteStringNamedFilesystem)
2148
self.overrideEnv('BRZ_TEST_PATH', '/home/\xa7test')
2149
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2150
self.assertEqual(u'/home/\xa7test',
2151
osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2152
osutils._fs_enc = "iso8859-5"
2153
self.assertEqual(u'/home/\u0407test',
2154
osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2155
osutils._fs_enc = "utf-8"
2156
self.assertRaises(errors.BadFilenameEncoding,
2157
osutils._posix_path_from_environ, 'BRZ_TEST_PATH')
2160
class TestGetHomeDir(tests.TestCase):
2162
def test_is_unicode(self):
2163
home = osutils._get_home_dir()
2164
self.assertIsInstance(home, text_type)
2166
def test_posix_homeless(self):
2167
self.overrideEnv('HOME', None)
2168
home = osutils._get_home_dir()
2169
self.assertIsInstance(home, text_type)
2171
def test_posix_home_ascii(self):
2172
self.overrideEnv('HOME', '/home/test')
2173
home = osutils._posix_get_home_dir()
2174
self.assertIsInstance(home, text_type)
2175
self.assertEqual(u'/home/test', home)
2177
def test_posix_home_unicode(self):
2178
self.requireFeature(features.ByteStringNamedFilesystem)
2179
self.overrideEnv('HOME', '/home/\xa7test')
2180
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2181
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2182
osutils._fs_enc = "iso8859-5"
2183
self.assertEqual(u'/home/\u0407test', osutils._posix_get_home_dir())
2184
osutils._fs_enc = "utf-8"
2185
self.assertRaises(errors.BadFilenameEncoding,
2186
osutils._posix_get_home_dir)
2189
class TestGetuserUnicode(tests.TestCase):
2191
def test_is_unicode(self):
2192
user = osutils.getuser_unicode()
2193
self.assertIsInstance(user, text_type)
2195
def envvar_to_override(self):
2196
if sys.platform == "win32":
2197
# Disable use of platform calls on windows so envvar is used
2198
self.overrideAttr(win32utils, 'has_ctypes', False)
2199
return 'USERNAME' # only variable used on windows
2200
return 'LOGNAME' # first variable checked by getpass.getuser()
2202
def test_ascii_user(self):
2203
self.overrideEnv(self.envvar_to_override(), 'jrandom')
2204
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2206
def test_unicode_user(self):
2207
ue = osutils.get_user_encoding()
2208
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2210
raise tests.TestSkipped(
2211
'Cannot find a unicode character that works in encoding %s'
2212
% (osutils.get_user_encoding(),))
2213
uni_username = u'jrandom' + uni_val
2214
encoded_username = uni_username.encode(ue)
2215
self.overrideEnv(self.envvar_to_override(), encoded_username)
2216
self.assertEqual(uni_username, osutils.getuser_unicode())
2219
class TestBackupNames(tests.TestCase):
2222
super(TestBackupNames, self).setUp()
2225
def backup_exists(self, name):
2226
return name in self.backups
2228
def available_backup_name(self, name):
2229
backup_name = osutils.available_backup_name(name, self.backup_exists)
2230
self.backups.append(backup_name)
2233
def assertBackupName(self, expected, name):
2234
self.assertEqual(expected, self.available_backup_name(name))
2236
def test_empty(self):
2237
self.assertBackupName('file.~1~', 'file')
2239
def test_existing(self):
2240
self.available_backup_name('file')
2241
self.available_backup_name('file')
2242
self.assertBackupName('file.~3~', 'file')
2243
# Empty slots are found, this is not a strict requirement and may be
2244
# revisited if we test against all implementations.
2245
self.backups.remove('file.~2~')
2246
self.assertBackupName('file.~2~', 'file')
2249
class TestFindExecutableInPath(tests.TestCase):
2251
def test_windows(self):
2252
if sys.platform != 'win32':
2253
raise tests.TestSkipped('test requires win32')
2254
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2256
osutils.find_executable_on_path('explorer.exe') is not None)
2258
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2260
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2261
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2263
def test_windows_app_path(self):
2264
if sys.platform != 'win32':
2265
raise tests.TestSkipped('test requires win32')
2266
# Override PATH env var so that exe can only be found on App Path
2267
self.overrideEnv('PATH', '')
2268
# Internt Explorer is always registered in the App Path
2269
self.assertTrue(osutils.find_executable_on_path('iexplore') is not None)
2271
def test_other(self):
2272
if sys.platform == 'win32':
2273
raise tests.TestSkipped('test requires non-win32')
2274
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2276
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2279
class TestEnvironmentErrors(tests.TestCase):
2280
"""Test handling of environmental errors"""
2282
def test_is_oserror(self):
2283
self.assertTrue(osutils.is_environment_error(
2284
OSError(errno.EINVAL, "Invalid parameter")))
2286
def test_is_ioerror(self):
2287
self.assertTrue(osutils.is_environment_error(
2288
IOError(errno.EINVAL, "Invalid parameter")))
2290
def test_is_socket_error(self):
2291
self.assertTrue(osutils.is_environment_error(
2292
socket.error(errno.EINVAL, "Invalid parameter")))
2294
def test_is_select_error(self):
2295
self.assertTrue(osutils.is_environment_error(
2296
select.error(errno.EINVAL, "Invalid parameter")))
2298
def test_is_pywintypes_error(self):
2299
self.requireFeature(features.pywintypes)
2301
self.assertTrue(osutils.is_environment_error(
2302
pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))