/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 breezy/tests/test_osutils.py

  • Committer: Jelmer Vernooij
  • Date: 2020-01-31 17:43:44 UTC
  • mto: This revision was merged to the branch mainline in revision 7478.
  • Revision ID: jelmer@jelmer.uk-20200131174344-qjhgqm7bdkuqj9sj
Default to running Python 3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2016 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Tests for the osutils wrapper."""
 
18
 
 
19
from __future__ import absolute_import, division
 
20
 
 
21
import errno
 
22
import os
 
23
import select
 
24
import socket
 
25
import sys
 
26
import tempfile
 
27
import time
 
28
 
 
29
from .. import (
 
30
    errors,
 
31
    osutils,
 
32
    tests,
 
33
    trace,
 
34
    win32utils,
 
35
    )
 
36
from ..sixish import (
 
37
    BytesIO,
 
38
    PY3,
 
39
    text_type,
 
40
    )
 
41
from . import (
 
42
    features,
 
43
    file_utils,
 
44
    test__walkdirs_win32,
 
45
    )
 
46
from .scenarios import load_tests_apply_scenarios
 
47
 
 
48
 
 
49
class _UTF8DirReaderFeature(features.ModuleAvailableFeature):
 
50
 
 
51
    def _probe(self):
 
52
        try:
 
53
            from .. import _readdir_pyx
 
54
            self._module = _readdir_pyx
 
55
            self.reader = _readdir_pyx.UTF8DirReader
 
56
            return True
 
57
        except ImportError:
 
58
            return False
 
59
 
 
60
 
 
61
UTF8DirReaderFeature = _UTF8DirReaderFeature('breezy._readdir_pyx')
 
62
 
 
63
term_ios_feature = features.ModuleAvailableFeature('termios')
 
64
 
 
65
 
 
66
def _already_unicode(s):
 
67
    return s
 
68
 
 
69
 
 
70
def _utf8_to_unicode(s):
 
71
    return s.decode('UTF-8')
 
72
 
 
73
 
 
74
def dir_reader_scenarios():
 
75
    # For each dir reader we define:
 
76
 
 
77
    # - native_to_unicode: a function converting the native_abspath as returned
 
78
    #   by DirReader.read_dir to its unicode representation
 
79
 
 
80
    # UnicodeDirReader is the fallback, it should be tested on all platforms.
 
81
    scenarios = [('unicode',
 
82
                  dict(_dir_reader_class=osutils.UnicodeDirReader,
 
83
                       _native_to_unicode=_already_unicode))]
 
84
    # Some DirReaders are platform specific and even there they may not be
 
85
    # available.
 
86
    if UTF8DirReaderFeature.available():
 
87
        from .. import _readdir_pyx
 
88
        scenarios.append(('utf8',
 
89
                          dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
 
90
                               _native_to_unicode=_utf8_to_unicode)))
 
91
 
 
92
    if test__walkdirs_win32.win32_readdir_feature.available():
 
93
        try:
 
94
            from .. import _walkdirs_win32
 
95
            scenarios.append(
 
96
                ('win32',
 
97
                 dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
 
98
                      _native_to_unicode=_already_unicode)))
 
99
        except ImportError:
 
100
            pass
 
101
    return scenarios
 
102
 
 
103
 
 
104
load_tests = load_tests_apply_scenarios
 
105
 
 
106
 
 
107
class TestContainsWhitespace(tests.TestCase):
 
108
 
 
109
    def test_contains_whitespace(self):
 
110
        self.assertTrue(osutils.contains_whitespace(u' '))
 
111
        self.assertTrue(osutils.contains_whitespace(u'hello there'))
 
112
        self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
 
113
        self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
 
114
        self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
 
115
        self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
 
116
 
 
117
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
 
118
        # is whitespace, but we do not.
 
119
        self.assertFalse(osutils.contains_whitespace(u''))
 
120
        self.assertFalse(osutils.contains_whitespace(u'hellothere'))
 
121
        self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
 
122
 
 
123
 
 
124
class TestRename(tests.TestCaseInTempDir):
 
125
 
 
126
    def create_file(self, filename, content):
 
127
        f = open(filename, 'wb')
 
128
        try:
 
129
            f.write(content)
 
130
        finally:
 
131
            f.close()
 
132
 
 
133
    def _fancy_rename(self, a, b):
 
134
        osutils.fancy_rename(a, b, rename_func=os.rename,
 
135
                             unlink_func=os.unlink)
 
136
 
 
137
    def test_fancy_rename(self):
 
138
        # This should work everywhere
 
139
        self.create_file('a', b'something in a\n')
 
140
        self._fancy_rename('a', 'b')
 
141
        self.assertPathDoesNotExist('a')
 
142
        self.assertPathExists('b')
 
143
        self.check_file_contents('b', b'something in a\n')
 
144
 
 
145
        self.create_file('a', b'new something in a\n')
 
146
        self._fancy_rename('b', 'a')
 
147
 
 
148
        self.check_file_contents('a', b'something in a\n')
 
149
 
 
150
    def test_fancy_rename_fails_source_missing(self):
 
151
        # An exception should be raised, and the target should be left in place
 
152
        self.create_file('target', b'data in target\n')
 
153
        self.assertRaises((IOError, OSError), self._fancy_rename,
 
154
                          'missingsource', 'target')
 
155
        self.assertPathExists('target')
 
156
        self.check_file_contents('target', b'data in target\n')
 
157
 
 
158
    def test_fancy_rename_fails_if_source_and_target_missing(self):
 
159
        self.assertRaises((IOError, OSError), self._fancy_rename,
 
160
                          'missingsource', 'missingtarget')
 
161
 
 
162
    def test_rename(self):
 
163
        # Rename should be semi-atomic on all platforms
 
164
        self.create_file('a', b'something in a\n')
 
165
        osutils.rename('a', 'b')
 
166
        self.assertPathDoesNotExist('a')
 
167
        self.assertPathExists('b')
 
168
        self.check_file_contents('b', b'something in a\n')
 
169
 
 
170
        self.create_file('a', b'new something in a\n')
 
171
        osutils.rename('b', 'a')
 
172
 
 
173
        self.check_file_contents('a', b'something in a\n')
 
174
 
 
175
    # TODO: test fancy_rename using a MemoryTransport
 
176
 
 
177
    def test_rename_change_case(self):
 
178
        # on Windows we should be able to change filename case by rename
 
179
        self.build_tree(['a', 'b/'])
 
180
        osutils.rename('a', 'A')
 
181
        osutils.rename('b', 'B')
 
182
        # we can't use failUnlessExists on case-insensitive filesystem
 
183
        # so try to check shape of the tree
 
184
        shape = sorted(os.listdir('.'))
 
185
        self.assertEqual(['A', 'B'], shape)
 
186
 
 
187
    def test_rename_exception(self):
 
188
        try:
 
189
            osutils.rename('nonexistent_path', 'different_nonexistent_path')
 
190
        except OSError as e:
 
191
            self.assertEqual(e.old_filename, 'nonexistent_path')
 
192
            self.assertEqual(e.new_filename, 'different_nonexistent_path')
 
193
            self.assertTrue('nonexistent_path' in e.strerror)
 
194
            self.assertTrue('different_nonexistent_path' in e.strerror)
 
195
 
 
196
 
 
197
class TestRandChars(tests.TestCase):
 
198
 
 
199
    def test_01_rand_chars_empty(self):
 
200
        result = osutils.rand_chars(0)
 
201
        self.assertEqual(result, '')
 
202
 
 
203
    def test_02_rand_chars_100(self):
 
204
        result = osutils.rand_chars(100)
 
205
        self.assertEqual(len(result), 100)
 
206
        self.assertEqual(type(result), str)
 
207
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
 
208
 
 
209
 
 
210
class TestIsInside(tests.TestCase):
 
211
 
 
212
    def test_is_inside(self):
 
213
        is_inside = osutils.is_inside
 
214
        self.assertTrue(is_inside('src', 'src/foo.c'))
 
215
        self.assertFalse(is_inside('src', 'srccontrol'))
 
216
        self.assertTrue(is_inside('src', 'src/a/a/a/foo.c'))
 
217
        self.assertTrue(is_inside('foo.c', 'foo.c'))
 
218
        self.assertFalse(is_inside('foo.c', ''))
 
219
        self.assertTrue(is_inside('', 'foo.c'))
 
220
 
 
221
    def test_is_inside_any(self):
 
222
        SRC_FOO_C = osutils.pathjoin('src', 'foo.c')
 
223
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
 
224
                         (['src'], SRC_FOO_C),
 
225
                         (['src'], 'src'),
 
226
                         ]:
 
227
            self.assertTrue(osutils.is_inside_any(dirs, fn))
 
228
        for dirs, fn in [(['src'], 'srccontrol'),
 
229
                         (['src'], 'srccontrol/foo')]:
 
230
            self.assertFalse(osutils.is_inside_any(dirs, fn))
 
231
 
 
232
    def test_is_inside_or_parent_of_any(self):
 
233
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
 
234
                         (['src'], 'src/foo.c'),
 
235
                         (['src/bar.c'], 'src'),
 
236
                         (['src/bar.c', 'bla/foo.c'], 'src'),
 
237
                         (['src'], 'src'),
 
238
                         ]:
 
239
            self.assertTrue(osutils.is_inside_or_parent_of_any(dirs, fn))
 
240
 
 
241
        for dirs, fn in [(['src'], 'srccontrol'),
 
242
                         (['srccontrol/foo.c'], 'src'),
 
243
                         (['src'], 'srccontrol/foo')]:
 
244
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
 
245
 
 
246
 
 
247
class TestLstat(tests.TestCaseInTempDir):
 
248
 
 
249
    def test_lstat_matches_fstat(self):
 
250
        # On Windows, lstat and fstat don't always agree, primarily in the
 
251
        # 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
 
252
        # custom implementation.
 
253
        if sys.platform == 'win32':
 
254
            # We only have special lstat/fstat if we have the extension.
 
255
            # Without it, we may end up re-reading content when we don't have
 
256
            # to, but otherwise it doesn't effect correctness.
 
257
            self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
258
        with open('test-file.txt', 'wb') as f:
 
259
            f.write(b'some content\n')
 
260
            f.flush()
 
261
            self.assertEqualStat(osutils.fstat(f.fileno()),
 
262
                                 osutils.lstat('test-file.txt'))
 
263
 
 
264
 
 
265
class TestRmTree(tests.TestCaseInTempDir):
 
266
 
 
267
    def test_rmtree(self):
 
268
        # Check to remove tree with read-only files/dirs
 
269
        os.mkdir('dir')
 
270
        with open('dir/file', 'w') as f:
 
271
            f.write('spam')
 
272
        # would like to also try making the directory readonly, but at the
 
273
        # moment python shutil.rmtree doesn't handle that properly - it would
 
274
        # need to chmod the directory before removing things inside it - deferred
 
275
        # for now -- mbp 20060505
 
276
        # osutils.make_readonly('dir')
 
277
        osutils.make_readonly('dir/file')
 
278
 
 
279
        osutils.rmtree('dir')
 
280
 
 
281
        self.assertPathDoesNotExist('dir/file')
 
282
        self.assertPathDoesNotExist('dir')
 
283
 
 
284
 
 
285
class TestDeleteAny(tests.TestCaseInTempDir):
 
286
 
 
287
    def test_delete_any_readonly(self):
 
288
        # from <https://bugs.launchpad.net/bzr/+bug/218206>
 
289
        self.build_tree(['d/', 'f'])
 
290
        osutils.make_readonly('d')
 
291
        osutils.make_readonly('f')
 
292
 
 
293
        osutils.delete_any('f')
 
294
        osutils.delete_any('d')
 
295
 
 
296
 
 
297
class TestKind(tests.TestCaseInTempDir):
 
298
 
 
299
    def test_file_kind(self):
 
300
        self.build_tree(['file', 'dir/'])
 
301
        self.assertEqual('file', osutils.file_kind('file'))
 
302
        self.assertEqual('directory', osutils.file_kind('dir/'))
 
303
        if osutils.has_symlinks():
 
304
            os.symlink('symlink', 'symlink')
 
305
            self.assertEqual('symlink', osutils.file_kind('symlink'))
 
306
 
 
307
        # TODO: jam 20060529 Test a block device
 
308
        try:
 
309
            os.lstat('/dev/null')
 
310
        except OSError as e:
 
311
            if e.errno not in (errno.ENOENT,):
 
312
                raise
 
313
        else:
 
314
            self.assertEqual(
 
315
                'chardev',
 
316
                osutils.file_kind(os.path.realpath('/dev/null')))
 
317
 
 
318
        mkfifo = getattr(os, 'mkfifo', None)
 
319
        if mkfifo:
 
320
            mkfifo('fifo')
 
321
            try:
 
322
                self.assertEqual('fifo', osutils.file_kind('fifo'))
 
323
            finally:
 
324
                os.remove('fifo')
 
325
 
 
326
        AF_UNIX = getattr(socket, 'AF_UNIX', None)
 
327
        if AF_UNIX:
 
328
            s = socket.socket(AF_UNIX)
 
329
            s.bind('socket')
 
330
            try:
 
331
                self.assertEqual('socket', osutils.file_kind('socket'))
 
332
            finally:
 
333
                os.remove('socket')
 
334
 
 
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"))
 
344
 
 
345
 
 
346
class TestUmask(tests.TestCaseInTempDir):
 
347
 
 
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())
 
352
            return
 
353
 
 
354
        orig_umask = osutils.get_umask()
 
355
        self.addCleanup(os.umask, orig_umask)
 
356
        os.umask(0o222)
 
357
        self.assertEqual(0o222, osutils.get_umask())
 
358
        os.umask(0o022)
 
359
        self.assertEqual(0o022, osutils.get_umask())
 
360
        os.umask(0o002)
 
361
        self.assertEqual(0o002, osutils.get_umask())
 
362
        os.umask(0o027)
 
363
        self.assertEqual(0o027, osutils.get_umask())
 
364
 
 
365
 
 
366
class TestDateTime(tests.TestCase):
 
367
 
 
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)
 
372
 
 
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)
 
397
 
 
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)
 
403
 
 
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.
 
413
 
 
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))
 
421
 
 
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
 
427
        # either direction.
 
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
 
430
        # probably wrong.
 
431
        eighteen_hours = 18 * 3600
 
432
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
433
 
 
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)
 
440
 
 
441
 
 
442
class TestFdatasync(tests.TestCaseInTempDir):
 
443
 
 
444
    def do_fdatasync(self):
 
445
        f = tempfile.NamedTemporaryFile()
 
446
        osutils.fdatasync(f.fileno())
 
447
        f.close()
 
448
 
 
449
    @staticmethod
 
450
    def raise_eopnotsupp(*args, **kwargs):
 
451
        raise IOError(errno.EOPNOTSUPP, os.strerror(errno.EOPNOTSUPP))
 
452
 
 
453
    @staticmethod
 
454
    def raise_enotsup(*args, **kwargs):
 
455
        raise IOError(errno.ENOTSUP, os.strerror(errno.ENOTSUP))
 
456
 
 
457
    def test_fdatasync_handles_system_function(self):
 
458
        self.overrideAttr(os, "fdatasync")
 
459
        self.do_fdatasync()
 
460
 
 
461
    def test_fdatasync_handles_no_fdatasync_no_fsync(self):
 
462
        self.overrideAttr(os, "fdatasync")
 
463
        self.overrideAttr(os, "fsync")
 
464
        self.do_fdatasync()
 
465
 
 
466
    def test_fdatasync_handles_no_EOPNOTSUPP(self):
 
467
        self.overrideAttr(errno, "EOPNOTSUPP")
 
468
        self.do_fdatasync()
 
469
 
 
470
    def test_fdatasync_catches_ENOTSUP(self):
 
471
        enotsup = getattr(errno, "ENOTSUP", None)
 
472
        if enotsup is None:
 
473
            raise tests.TestNotApplicable("No ENOTSUP on this platform")
 
474
        self.overrideAttr(os, "fdatasync", self.raise_enotsup)
 
475
        self.do_fdatasync()
 
476
 
 
477
    def test_fdatasync_catches_EOPNOTSUPP(self):
 
478
        enotsup = getattr(errno, "EOPNOTSUPP", None)
 
479
        if enotsup is None:
 
480
            raise tests.TestNotApplicable("No EOPNOTSUPP on this platform")
 
481
        self.overrideAttr(os, "fdatasync", self.raise_eopnotsupp)
 
482
        self.do_fdatasync()
 
483
 
 
484
 
 
485
class TestLinks(tests.TestCaseInTempDir):
 
486
 
 
487
    def test_dereference_path(self):
 
488
        self.requireFeature(features.SymlinkFeature)
 
489
        cwd = osutils.realpath('.')
 
490
        os.mkdir('bar')
 
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'))
 
497
 
 
498
        # Does not dereference terminal symlinks
 
499
        foo_path = osutils.pathjoin(cwd, 'foo')
 
500
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
 
501
 
 
502
        # Dereferences parent symlinks
 
503
        os.mkdir('bar/baz')
 
504
        baz_path = osutils.pathjoin(bar_path, 'baz')
 
505
        self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
 
506
 
 
507
        # Dereferences parent symlinks that are the first path element
 
508
        self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
 
509
 
 
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))
 
513
 
 
514
    def test_changing_access(self):
 
515
        with open('file', 'w') as f:
 
516
            f.write('monkey')
 
517
 
 
518
        # Make a file readonly
 
519
        osutils.make_readonly('file')
 
520
        mode = os.lstat('file').st_mode
 
521
        self.assertEqual(mode, mode & 0o777555)
 
522
 
 
523
        # Make a file writable
 
524
        osutils.make_writable('file')
 
525
        mode = os.lstat('file').st_mode
 
526
        self.assertEqual(mode, mode | 0o200)
 
527
 
 
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')
 
533
 
 
534
    def test_host_os_dereferences_symlinks(self):
 
535
        osutils.host_os_dereferences_symlinks()
 
536
 
 
537
 
 
538
class TestCanonicalRelPath(tests.TestCaseInTempDir):
 
539
 
 
540
    _test_needs_features = [features.CaseInsCasePresFilenameFeature]
 
541
 
 
542
    def test_canonical_relpath_simple(self):
 
543
        f = open('MixedCaseName', 'w')
 
544
        f.close()
 
545
        actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
 
546
        self.assertEqual('work/MixedCaseName', actual)
 
547
 
 
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)
 
553
 
 
554
 
 
555
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
 
556
 
 
557
    def assertRelpath(self, expected, base, path):
 
558
        actual = osutils._cicp_canonical_relpath(base, path)
 
559
        self.assertEqual(expected, actual)
 
560
 
 
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')
 
565
 
 
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')
 
575
 
 
576
    def test_at_root_slash(self):
 
577
        # We can't test this on Windows, because it has a 'MIN_ABS_PATHLENGTH'
 
578
        # check...
 
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')
 
583
 
 
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'
 
588
                                          ' letters.')
 
589
        # see bug #322807
 
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')
 
596
 
 
597
 
 
598
class TestPumpFile(tests.TestCase):
 
599
    """Test pumpfile method."""
 
600
 
 
601
    def setUp(self):
 
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)
 
608
 
 
609
    def test_bracket_block_size(self):
 
610
        """Read data in blocks with the requested read size bracketing the
 
611
        block size."""
 
612
        # make sure test data is larger than max read size
 
613
        self.assertTrue(self.test_data_len > self.block_size)
 
614
 
 
615
        from_file = file_utils.FakeReadFile(self.test_data)
 
616
        to_file = BytesIO()
 
617
 
 
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,
 
621
                         num_bytes_to_read, self.block_size)
 
622
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
 
623
        self.assertEqual(from_file.get_read_count(), 1)
 
624
 
 
625
        # read (max) bytes and verify read size wasn't affected
 
626
        num_bytes_to_read = self.block_size
 
627
        from_file.reset_read_count()
 
628
        osutils.pumpfile(from_file, to_file,
 
629
                         num_bytes_to_read, self.block_size)
 
630
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
 
631
        self.assertEqual(from_file.get_read_count(), 1)
 
632
 
 
633
        # read (max + 1) bytes and verify read size was limited
 
634
        num_bytes_to_read = self.block_size + 1
 
635
        from_file.reset_read_count()
 
636
        osutils.pumpfile(from_file, to_file,
 
637
                         num_bytes_to_read, self.block_size)
 
638
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
 
639
        self.assertEqual(from_file.get_read_count(), 2)
 
640
 
 
641
        # finish reading the rest of the data
 
642
        num_bytes_to_read = self.test_data_len - to_file.tell()
 
643
        osutils.pumpfile(from_file, to_file,
 
644
                         num_bytes_to_read, self.block_size)
 
645
 
 
646
        # report error if the data wasn't equal (we only report the size due
 
647
        # to the length of the data)
 
648
        response_data = to_file.getvalue()
 
649
        if response_data != self.test_data:
 
650
            message = "Data not equal.  Expected %d bytes, received %d."
 
651
            self.fail(message % (len(response_data), self.test_data_len))
 
652
 
 
653
    def test_specified_size(self):
 
654
        """Request a transfer larger than the maximum block size and verify
 
655
        that the maximum read doesn't exceed the block_size."""
 
656
        # make sure test data is larger than max read size
 
657
        self.assertTrue(self.test_data_len > self.block_size)
 
658
 
 
659
        # retrieve data in blocks
 
660
        from_file = file_utils.FakeReadFile(self.test_data)
 
661
        to_file = BytesIO()
 
662
        osutils.pumpfile(from_file, to_file, self.test_data_len,
 
663
                         self.block_size)
 
664
 
 
665
        # verify read size was equal to the maximum read size
 
666
        self.assertTrue(from_file.get_max_read_size() > 0)
 
667
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
 
668
        self.assertEqual(from_file.get_read_count(), 3)
 
669
 
 
670
        # report error if the data wasn't equal (we only report the size due
 
671
        # to the length of the data)
 
672
        response_data = to_file.getvalue()
 
673
        if response_data != self.test_data:
 
674
            message = "Data not equal.  Expected %d bytes, received %d."
 
675
            self.fail(message % (len(response_data), self.test_data_len))
 
676
 
 
677
    def test_to_eof(self):
 
678
        """Read to end-of-file and verify that the reads are not larger than
 
679
        the maximum read size."""
 
680
        # make sure test data is larger than max read size
 
681
        self.assertTrue(self.test_data_len > self.block_size)
 
682
 
 
683
        # retrieve data to EOF
 
684
        from_file = file_utils.FakeReadFile(self.test_data)
 
685
        to_file = BytesIO()
 
686
        osutils.pumpfile(from_file, to_file, -1, self.block_size)
 
687
 
 
688
        # verify read size was equal to the maximum read size
 
689
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
 
690
        self.assertEqual(from_file.get_read_count(), 4)
 
691
 
 
692
        # report error if the data wasn't equal (we only report the size due
 
693
        # to the length of the data)
 
694
        response_data = to_file.getvalue()
 
695
        if response_data != self.test_data:
 
696
            message = "Data not equal.  Expected %d bytes, received %d."
 
697
            self.fail(message % (len(response_data), self.test_data_len))
 
698
 
 
699
    def test_defaults(self):
 
700
        """Verifies that the default arguments will read to EOF -- this
 
701
        test verifies that any existing usages of pumpfile will not be broken
 
702
        with this new version."""
 
703
        # retrieve data using default (old) pumpfile method
 
704
        from_file = file_utils.FakeReadFile(self.test_data)
 
705
        to_file = BytesIO()
 
706
        osutils.pumpfile(from_file, to_file)
 
707
 
 
708
        # report error if the data wasn't equal (we only report the size due
 
709
        # to the length of the data)
 
710
        response_data = to_file.getvalue()
 
711
        if response_data != self.test_data:
 
712
            message = "Data not equal.  Expected %d bytes, received %d."
 
713
            self.fail(message % (len(response_data), self.test_data_len))
 
714
 
 
715
    def test_report_activity(self):
 
716
        activity = []
 
717
 
 
718
        def log_activity(length, direction):
 
719
            activity.append((length, direction))
 
720
        from_file = BytesIO(self.test_data)
 
721
        to_file = BytesIO()
 
722
        osutils.pumpfile(from_file, to_file, buff_size=500,
 
723
                         report_activity=log_activity, direction='read')
 
724
        self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
 
725
                          (36, 'read')], activity)
 
726
 
 
727
        from_file = BytesIO(self.test_data)
 
728
        to_file = BytesIO()
 
729
        del activity[:]
 
730
        osutils.pumpfile(from_file, to_file, buff_size=500,
 
731
                         report_activity=log_activity, direction='write')
 
732
        self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
 
733
                          (36, 'write')], activity)
 
734
 
 
735
        # And with a limited amount of data
 
736
        from_file = BytesIO(self.test_data)
 
737
        to_file = BytesIO()
 
738
        del activity[:]
 
739
        osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
 
740
                         report_activity=log_activity, direction='read')
 
741
        self.assertEqual(
 
742
            [(500, 'read'), (500, 'read'), (28, 'read')], activity)
 
743
 
 
744
 
 
745
class TestPumpStringFile(tests.TestCase):
 
746
 
 
747
    def test_empty(self):
 
748
        output = BytesIO()
 
749
        osutils.pump_string_file(b"", output)
 
750
        self.assertEqual(b"", output.getvalue())
 
751
 
 
752
    def test_more_than_segment_size(self):
 
753
        output = BytesIO()
 
754
        osutils.pump_string_file(b"123456789", output, 2)
 
755
        self.assertEqual(b"123456789", output.getvalue())
 
756
 
 
757
    def test_segment_size(self):
 
758
        output = BytesIO()
 
759
        osutils.pump_string_file(b"12", output, 2)
 
760
        self.assertEqual(b"12", output.getvalue())
 
761
 
 
762
    def test_segment_size_multiple(self):
 
763
        output = BytesIO()
 
764
        osutils.pump_string_file(b"1234", output, 2)
 
765
        self.assertEqual(b"1234", output.getvalue())
 
766
 
 
767
 
 
768
class TestRelpath(tests.TestCase):
 
769
 
 
770
    def test_simple_relpath(self):
 
771
        cwd = osutils.getcwd()
 
772
        subdir = cwd + '/subdir'
 
773
        self.assertEqual('subdir', osutils.relpath(cwd, subdir))
 
774
 
 
775
    def test_deep_relpath(self):
 
776
        cwd = osutils.getcwd()
 
777
        subdir = cwd + '/sub/subsubdir'
 
778
        self.assertEqual('sub/subsubdir', osutils.relpath(cwd, subdir))
 
779
 
 
780
    def test_not_relative(self):
 
781
        self.assertRaises(errors.PathNotChild,
 
782
                          osutils.relpath, 'C:/path', 'H:/path')
 
783
        self.assertRaises(errors.PathNotChild,
 
784
                          osutils.relpath, 'C:/', 'H:/path')
 
785
 
 
786
 
 
787
class TestSafeUnicode(tests.TestCase):
 
788
 
 
789
    def test_from_ascii_string(self):
 
790
        self.assertEqual(u'foobar', osutils.safe_unicode(b'foobar'))
 
791
 
 
792
    def test_from_unicode_string_ascii_contents(self):
 
793
        self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
 
794
 
 
795
    def test_from_unicode_string_unicode_contents(self):
 
796
        self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
 
797
 
 
798
    def test_from_utf8_string(self):
 
799
        self.assertEqual(u'foo\xae', osutils.safe_unicode(b'foo\xc2\xae'))
 
800
 
 
801
    def test_bad_utf8_string(self):
 
802
        self.assertRaises(errors.BzrBadParameterNotUnicode,
 
803
                          osutils.safe_unicode,
 
804
                          b'\xbb\xbb')
 
805
 
 
806
 
 
807
class TestSafeUtf8(tests.TestCase):
 
808
 
 
809
    def test_from_ascii_string(self):
 
810
        f = b'foobar'
 
811
        self.assertEqual(b'foobar', osutils.safe_utf8(f))
 
812
 
 
813
    def test_from_unicode_string_ascii_contents(self):
 
814
        self.assertEqual(b'bargam', osutils.safe_utf8(u'bargam'))
 
815
 
 
816
    def test_from_unicode_string_unicode_contents(self):
 
817
        self.assertEqual(b'bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
 
818
 
 
819
    def test_from_utf8_string(self):
 
820
        self.assertEqual(b'foo\xc2\xae', osutils.safe_utf8(b'foo\xc2\xae'))
 
821
 
 
822
    def test_bad_utf8_string(self):
 
823
        self.assertRaises(errors.BzrBadParameterNotUnicode,
 
824
                          osutils.safe_utf8, b'\xbb\xbb')
 
825
 
 
826
 
 
827
class TestSafeRevisionId(tests.TestCase):
 
828
 
 
829
    def test_from_ascii_string(self):
 
830
        # this shouldn't give a warning because it's getting an ascii string
 
831
        self.assertEqual(b'foobar', osutils.safe_revision_id(b'foobar'))
 
832
 
 
833
    def test_from_unicode_string_ascii_contents(self):
 
834
        self.assertRaises(TypeError,
 
835
                          osutils.safe_revision_id, u'bargam')
 
836
 
 
837
    def test_from_unicode_string_unicode_contents(self):
 
838
        self.assertRaises(TypeError,
 
839
                          osutils.safe_revision_id, u'bargam\xae')
 
840
 
 
841
    def test_from_utf8_string(self):
 
842
        self.assertEqual(b'foo\xc2\xae',
 
843
                         osutils.safe_revision_id(b'foo\xc2\xae'))
 
844
 
 
845
    def test_none(self):
 
846
        """Currently, None is a valid revision_id"""
 
847
        self.assertEqual(None, osutils.safe_revision_id(None))
 
848
 
 
849
 
 
850
class TestSafeFileId(tests.TestCase):
 
851
 
 
852
    def test_from_ascii_string(self):
 
853
        self.assertEqual(b'foobar', osutils.safe_file_id(b'foobar'))
 
854
 
 
855
    def test_from_unicode_string_ascii_contents(self):
 
856
        self.assertRaises(TypeError, osutils.safe_file_id, u'bargam')
 
857
 
 
858
    def test_from_unicode_string_unicode_contents(self):
 
859
        self.assertRaises(TypeError,
 
860
                          osutils.safe_file_id, u'bargam\xae')
 
861
 
 
862
    def test_from_utf8_string(self):
 
863
        self.assertEqual(b'foo\xc2\xae',
 
864
                         osutils.safe_file_id(b'foo\xc2\xae'))
 
865
 
 
866
    def test_none(self):
 
867
        """Currently, None is a valid revision_id"""
 
868
        self.assertEqual(None, osutils.safe_file_id(None))
 
869
 
 
870
 
 
871
class TestSendAll(tests.TestCase):
 
872
 
 
873
    def test_send_with_disconnected_socket(self):
 
874
        class DisconnectedSocket(object):
 
875
            def __init__(self, err):
 
876
                self.err = err
 
877
 
 
878
            def send(self, content):
 
879
                raise self.err
 
880
 
 
881
            def close(self):
 
882
                pass
 
883
        # All of these should be treated as ConnectionReset
 
884
        errs = []
 
885
        for err_cls in (IOError, socket.error):
 
886
            for errnum in osutils._end_of_stream_errors:
 
887
                errs.append(err_cls(errnum))
 
888
        for err in errs:
 
889
            sock = DisconnectedSocket(err)
 
890
            self.assertRaises(errors.ConnectionReset,
 
891
                              osutils.send_all, sock, b'some more content')
 
892
 
 
893
    def test_send_with_no_progress(self):
 
894
        # See https://bugs.launchpad.net/bzr/+bug/1047309
 
895
        # It seems that paramiko can get into a state where it doesn't error,
 
896
        # but it returns 0 bytes sent for requests over and over again.
 
897
        class NoSendingSocket(object):
 
898
            def __init__(self):
 
899
                self.call_count = 0
 
900
 
 
901
            def send(self, bytes):
 
902
                self.call_count += 1
 
903
                if self.call_count > 100:
 
904
                    # Prevent the test suite from hanging
 
905
                    raise RuntimeError('too many calls')
 
906
                return 0
 
907
        sock = NoSendingSocket()
 
908
        self.assertRaises(errors.ConnectionReset,
 
909
                          osutils.send_all, sock, b'content')
 
910
        self.assertEqual(1, sock.call_count)
 
911
 
 
912
 
 
913
class TestPosixFuncs(tests.TestCase):
 
914
    """Test that the posix version of normpath returns an appropriate path
 
915
       when used with 2 leading slashes."""
 
916
 
 
917
    def test_normpath(self):
 
918
        self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
 
919
        self.assertEqual(
 
920
            '/etc/shadow', osutils._posix_normpath('//etc/shadow'))
 
921
        self.assertEqual(
 
922
            '/etc/shadow', osutils._posix_normpath('///etc/shadow'))
 
923
 
 
924
 
 
925
class TestWin32Funcs(tests.TestCase):
 
926
    """Test that _win32 versions of os utilities return appropriate paths."""
 
927
 
 
928
    def test_abspath(self):
 
929
        self.requireFeature(features.win32_feature)
 
930
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
 
931
        self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
 
932
        self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
 
933
        self.assertEqual('//HOST/path', osutils._win32_abspath('//HOST/path'))
 
934
 
 
935
    def test_realpath(self):
 
936
        self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
 
937
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
 
938
 
 
939
    def test_pathjoin(self):
 
940
        self.assertEqual('path/to/foo',
 
941
                         osutils._win32_pathjoin('path', 'to', 'foo'))
 
942
        self.assertEqual('C:/foo',
 
943
                         osutils._win32_pathjoin('path\\to', 'C:\\foo'))
 
944
        self.assertEqual('C:/foo',
 
945
                         osutils._win32_pathjoin('path/to', 'C:/foo'))
 
946
        self.assertEqual('path/to/foo',
 
947
                         osutils._win32_pathjoin('path/to/', 'foo'))
 
948
 
 
949
    def test_pathjoin_late_bugfix(self):
 
950
        if sys.version_info < (2, 7, 6):
 
951
            expected = '/foo'
 
952
        else:
 
953
            expected = 'C:/foo'
 
954
        self.assertEqual(expected,
 
955
                         osutils._win32_pathjoin('C:/path/to/', '/foo'))
 
956
        self.assertEqual(expected,
 
957
                         osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
 
958
 
 
959
    def test_normpath(self):
 
960
        self.assertEqual('path/to/foo',
 
961
                         osutils._win32_normpath(r'path\\from\..\to\.\foo'))
 
962
        self.assertEqual('path/to/foo',
 
963
                         osutils._win32_normpath('path//from/../to/./foo'))
 
964
 
 
965
    def test_getcwd(self):
 
966
        cwd = osutils._win32_getcwd()
 
967
        os_cwd = osutils._getcwd()
 
968
        self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])
 
969
        # win32 is inconsistent whether it returns lower or upper case
 
970
        # and even if it was consistent the user might type the other
 
971
        # so we force it to uppercase
 
972
        # running python.exe under cmd.exe return capital C:\\
 
973
        # running win32 python inside a cygwin shell returns lowercase
 
974
        self.assertEqual(os_cwd[0].upper(), cwd[0])
 
975
 
 
976
    def test_fixdrive(self):
 
977
        self.assertEqual('H:/foo', osutils._win32_fixdrive('h:/foo'))
 
978
        self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
 
979
        self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
 
980
 
 
981
 
 
982
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
 
983
    """Test win32 functions that create files."""
 
984
 
 
985
    def test_getcwd(self):
 
986
        self.requireFeature(features.UnicodeFilenameFeature)
 
987
        os.mkdir(u'mu-\xb5')
 
988
        os.chdir(u'mu-\xb5')
 
989
        # TODO: jam 20060427 This will probably fail on Mac OSX because
 
990
        #       it will change the normalization of B\xe5gfors
 
991
        #       Consider using a different unicode character, or make
 
992
        #       osutils.getcwd() renormalize the path.
 
993
        self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
 
994
 
 
995
    def test_minimum_path_selection(self):
 
996
        self.assertEqual(set(),
 
997
                         osutils.minimum_path_selection([]))
 
998
        self.assertEqual({'a'},
 
999
                         osutils.minimum_path_selection(['a']))
 
1000
        self.assertEqual({'a', 'b'},
 
1001
                         osutils.minimum_path_selection(['a', 'b']))
 
1002
        self.assertEqual({'a/', 'b'},
 
1003
                         osutils.minimum_path_selection(['a/', 'b']))
 
1004
        self.assertEqual({'a/', 'b'},
 
1005
                         osutils.minimum_path_selection(['a/c', 'a/', 'b']))
 
1006
        self.assertEqual({'a-b', 'a', 'a0b'},
 
1007
                         osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
 
1008
 
 
1009
    def test_mkdtemp(self):
 
1010
        tmpdir = osutils._win32_mkdtemp(dir='.')
 
1011
        self.assertFalse('\\' in tmpdir)
 
1012
 
 
1013
    def test_rename(self):
 
1014
        with open('a', 'wb') as a:
 
1015
            a.write(b'foo\n')
 
1016
        with open('b', 'wb') as b:
 
1017
            b.write(b'baz\n')
 
1018
 
 
1019
        osutils._win32_rename('b', 'a')
 
1020
        self.assertPathExists('a')
 
1021
        self.assertPathDoesNotExist('b')
 
1022
        self.assertFileEqual(b'baz\n', 'a')
 
1023
 
 
1024
    def test_rename_missing_file(self):
 
1025
        with open('a', 'wb') as a:
 
1026
            a.write(b'foo\n')
 
1027
 
 
1028
        try:
 
1029
            osutils._win32_rename('b', 'a')
 
1030
        except (IOError, OSError) as e:
 
1031
            self.assertEqual(errno.ENOENT, e.errno)
 
1032
        self.assertFileEqual(b'foo\n', 'a')
 
1033
 
 
1034
    def test_rename_missing_dir(self):
 
1035
        os.mkdir('a')
 
1036
        try:
 
1037
            osutils._win32_rename('b', 'a')
 
1038
        except (IOError, OSError) as e:
 
1039
            self.assertEqual(errno.ENOENT, e.errno)
 
1040
 
 
1041
    def test_rename_current_dir(self):
 
1042
        os.mkdir('a')
 
1043
        os.chdir('a')
 
1044
        # You can't rename the working directory
 
1045
        # doing rename non-existant . usually
 
1046
        # just raises ENOENT, since non-existant
 
1047
        # doesn't exist.
 
1048
        try:
 
1049
            osutils._win32_rename('b', '.')
 
1050
        except (IOError, OSError) as e:
 
1051
            self.assertEqual(errno.ENOENT, e.errno)
 
1052
 
 
1053
    def test_splitpath(self):
 
1054
        def check(expected, path):
 
1055
            self.assertEqual(expected, osutils.splitpath(path))
 
1056
 
 
1057
        check(['a'], 'a')
 
1058
        check(['a', 'b'], 'a/b')
 
1059
        check(['a', 'b'], 'a/./b')
 
1060
        check(['a', '.b'], 'a/.b')
 
1061
        if os.path.sep == '\\':
 
1062
            check(['a', '.b'], 'a\\.b')
 
1063
        else:
 
1064
            check(['a\\.b'], 'a\\.b')
 
1065
 
 
1066
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
 
1067
 
 
1068
 
 
1069
class TestParentDirectories(tests.TestCaseInTempDir):
 
1070
    """Test osutils.parent_directories()"""
 
1071
 
 
1072
    def test_parent_directories(self):
 
1073
        self.assertEqual([], osutils.parent_directories('a'))
 
1074
        self.assertEqual(['a'], osutils.parent_directories('a/b'))
 
1075
        self.assertEqual(['a/b', 'a'], osutils.parent_directories('a/b/c'))
 
1076
 
 
1077
 
 
1078
class TestMacFuncsDirs(tests.TestCaseInTempDir):
 
1079
    """Test mac special functions that require directories."""
 
1080
 
 
1081
    def test_getcwd(self):
 
1082
        self.requireFeature(features.UnicodeFilenameFeature)
 
1083
        os.mkdir(u'B\xe5gfors')
 
1084
        os.chdir(u'B\xe5gfors')
 
1085
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
 
1086
 
 
1087
    def test_getcwd_nonnorm(self):
 
1088
        self.requireFeature(features.UnicodeFilenameFeature)
 
1089
        # Test that _mac_getcwd() will normalize this path
 
1090
        os.mkdir(u'Ba\u030agfors')
 
1091
        os.chdir(u'Ba\u030agfors')
 
1092
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
 
1093
 
 
1094
 
 
1095
class TestChunksToLines(tests.TestCase):
 
1096
 
 
1097
    def test_smoketest(self):
 
1098
        self.assertEqual([b'foo\n', b'bar\n', b'baz\n'],
 
1099
                         osutils.chunks_to_lines([b'foo\nbar', b'\nbaz\n']))
 
1100
        self.assertEqual([b'foo\n', b'bar\n', b'baz\n'],
 
1101
                         osutils.chunks_to_lines([b'foo\n', b'bar\n', b'baz\n']))
 
1102
 
 
1103
    def test_osutils_binding(self):
 
1104
        from . import test__chunks_to_lines
 
1105
        if test__chunks_to_lines.compiled_chunkstolines_feature.available():
 
1106
            from .._chunks_to_lines_pyx import chunks_to_lines
 
1107
        else:
 
1108
            from .._chunks_to_lines_py import chunks_to_lines
 
1109
        self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
 
1110
 
 
1111
 
 
1112
class TestSplitLines(tests.TestCase):
 
1113
 
 
1114
    def test_split_unicode(self):
 
1115
        self.assertEqual([u'foo\n', u'bar\xae'],
 
1116
                         osutils.split_lines(u'foo\nbar\xae'))
 
1117
        self.assertEqual([u'foo\n', u'bar\xae\n'],
 
1118
                         osutils.split_lines(u'foo\nbar\xae\n'))
 
1119
 
 
1120
    def test_split_with_carriage_returns(self):
 
1121
        self.assertEqual([b'foo\rbar\n'],
 
1122
                         osutils.split_lines(b'foo\rbar\n'))
 
1123
 
 
1124
 
 
1125
class TestWalkDirs(tests.TestCaseInTempDir):
 
1126
 
 
1127
    def assertExpectedBlocks(self, expected, result):
 
1128
        self.assertEqual(expected,
 
1129
                         [(dirinfo, [line[0:3] for line in block])
 
1130
                          for dirinfo, block in result])
 
1131
 
 
1132
    def test_walkdirs(self):
 
1133
        tree = [
 
1134
            '.bzr',
 
1135
            '0file',
 
1136
            '1dir/',
 
1137
            '1dir/0file',
 
1138
            '1dir/1dir/',
 
1139
            '2file'
 
1140
            ]
 
1141
        self.build_tree(tree)
 
1142
        expected_dirblocks = [
 
1143
            (('', '.'),
 
1144
             [('0file', '0file', 'file'),
 
1145
              ('1dir', '1dir', 'directory'),
 
1146
              ('2file', '2file', 'file'),
 
1147
              ]
 
1148
             ),
 
1149
            (('1dir', './1dir'),
 
1150
             [('1dir/0file', '0file', 'file'),
 
1151
              ('1dir/1dir', '1dir', 'directory'),
 
1152
              ]
 
1153
             ),
 
1154
            (('1dir/1dir', './1dir/1dir'),
 
1155
             [
 
1156
                ]
 
1157
             ),
 
1158
            ]
 
1159
        result = []
 
1160
        found_bzrdir = False
 
1161
        for dirdetail, dirblock in osutils.walkdirs('.'):
 
1162
            if len(dirblock) and dirblock[0][1] == '.bzr':
 
1163
                # this tests the filtering of selected paths
 
1164
                found_bzrdir = True
 
1165
                del dirblock[0]
 
1166
            result.append((dirdetail, dirblock))
 
1167
 
 
1168
        self.assertTrue(found_bzrdir)
 
1169
        self.assertExpectedBlocks(expected_dirblocks, result)
 
1170
        # you can search a subdir only, with a supplied prefix.
 
1171
        result = []
 
1172
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
 
1173
            result.append(dirblock)
 
1174
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
 
1175
 
 
1176
    def test_walkdirs_os_error(self):
 
1177
        # <https://bugs.launchpad.net/bzr/+bug/338653>
 
1178
        # Pyrex readdir didn't raise useful messages if it had an error
 
1179
        # reading the directory
 
1180
        if sys.platform == 'win32':
 
1181
            raise tests.TestNotApplicable(
 
1182
                "readdir IOError not tested on win32")
 
1183
        self.requireFeature(features.not_running_as_root)
 
1184
        os.mkdir("test-unreadable")
 
1185
        os.chmod("test-unreadable", 0000)
 
1186
        # must chmod it back so that it can be removed
 
1187
        self.addCleanup(os.chmod, "test-unreadable", 0o700)
 
1188
        # The error is not raised until the generator is actually evaluated.
 
1189
        # (It would be ok if it happened earlier but at the moment it
 
1190
        # doesn't.)
 
1191
        e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
 
1192
        self.assertEqual('./test-unreadable', osutils.safe_unicode(e.filename))
 
1193
        self.assertEqual(errno.EACCES, e.errno)
 
1194
        # Ensure the message contains the file name
 
1195
        self.assertContainsRe(str(e), "\\./test-unreadable")
 
1196
 
 
1197
    def test_walkdirs_encoding_error(self):
 
1198
        # <https://bugs.launchpad.net/bzr/+bug/488519>
 
1199
        # walkdirs didn't raise a useful message when the filenames
 
1200
        # are not using the filesystem's encoding
 
1201
 
 
1202
        # require a bytestring based filesystem
 
1203
        self.requireFeature(features.ByteStringNamedFilesystem)
 
1204
 
 
1205
        tree = [
 
1206
            '.bzr',
 
1207
            '0file',
 
1208
            '1dir/',
 
1209
            '1dir/0file',
 
1210
            '1dir/1dir/',
 
1211
            '1file'
 
1212
            ]
 
1213
 
 
1214
        self.build_tree(tree)
 
1215
 
 
1216
        # rename the 1file to a latin-1 filename
 
1217
        os.rename(b"./1file", b"\xe8file")
 
1218
        if b"\xe8file" not in os.listdir("."):
 
1219
            self.skipTest("Lack filesystem that preserves arbitrary bytes")
 
1220
 
 
1221
        self._save_platform_info()
 
1222
        osutils._fs_enc = 'UTF-8'
 
1223
 
 
1224
        # this should raise on error
 
1225
        def attempt():
 
1226
            for dirdetail, dirblock in osutils.walkdirs(b'.'):
 
1227
                pass
 
1228
 
 
1229
        self.assertRaises(errors.BadFilenameEncoding, attempt)
 
1230
 
 
1231
    def test__walkdirs_utf8(self):
 
1232
        tree = [
 
1233
            '.bzr',
 
1234
            '0file',
 
1235
            '1dir/',
 
1236
            '1dir/0file',
 
1237
            '1dir/1dir/',
 
1238
            '2file'
 
1239
            ]
 
1240
        self.build_tree(tree)
 
1241
        expected_dirblocks = [
 
1242
            (('', '.'),
 
1243
             [('0file', '0file', 'file'),
 
1244
              ('1dir', '1dir', 'directory'),
 
1245
              ('2file', '2file', 'file'),
 
1246
              ]
 
1247
             ),
 
1248
            (('1dir', './1dir'),
 
1249
             [('1dir/0file', '0file', 'file'),
 
1250
              ('1dir/1dir', '1dir', 'directory'),
 
1251
              ]
 
1252
             ),
 
1253
            (('1dir/1dir', './1dir/1dir'),
 
1254
             [
 
1255
                ]
 
1256
             ),
 
1257
            ]
 
1258
        result = []
 
1259
        found_bzrdir = False
 
1260
        for dirdetail, dirblock in osutils._walkdirs_utf8(b'.'):
 
1261
            if len(dirblock) and dirblock[0][1] == b'.bzr':
 
1262
                # this tests the filtering of selected paths
 
1263
                found_bzrdir = True
 
1264
                del dirblock[0]
 
1265
            dirdetail = (dirdetail[0].decode('utf-8'),
 
1266
                         osutils.safe_unicode(dirdetail[1]))
 
1267
            dirblock = [
 
1268
                (entry[0].decode('utf-8'), entry[1].decode('utf-8'), entry[2])
 
1269
                for entry in dirblock]
 
1270
            result.append((dirdetail, dirblock))
 
1271
 
 
1272
        self.assertTrue(found_bzrdir)
 
1273
        self.assertExpectedBlocks(expected_dirblocks, result)
 
1274
 
 
1275
        # you can search a subdir only, with a supplied prefix.
 
1276
        result = []
 
1277
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
 
1278
            result.append(dirblock)
 
1279
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
 
1280
 
 
1281
    def _filter_out_stat(self, result):
 
1282
        """Filter out the stat value from the walkdirs result"""
 
1283
        for dirdetail, dirblock in result:
 
1284
            new_dirblock = []
 
1285
            for info in dirblock:
 
1286
                # Ignore info[3] which is the stat
 
1287
                new_dirblock.append((info[0], info[1], info[2], info[4]))
 
1288
            dirblock[:] = new_dirblock
 
1289
 
 
1290
    def _save_platform_info(self):
 
1291
        self.overrideAttr(osutils, '_fs_enc')
 
1292
        self.overrideAttr(osutils, '_selected_dir_reader')
 
1293
 
 
1294
    def assertDirReaderIs(self, expected, top):
 
1295
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
 
1296
        # Force it to redetect
 
1297
        osutils._selected_dir_reader = None
 
1298
        # Nothing to list, but should still trigger the selection logic
 
1299
        self.assertEqual([((b'', top), [])], list(osutils._walkdirs_utf8('.')))
 
1300
        self.assertIsInstance(osutils._selected_dir_reader, expected)
 
1301
 
 
1302
    def test_force_walkdirs_utf8_fs_utf8(self):
 
1303
        self.requireFeature(UTF8DirReaderFeature)
 
1304
        self._save_platform_info()
 
1305
        osutils._fs_enc = 'utf-8'
 
1306
        self.assertDirReaderIs(UTF8DirReaderFeature.module.UTF8DirReader, b".")
 
1307
 
 
1308
    def test_force_walkdirs_utf8_fs_ascii(self):
 
1309
        self.requireFeature(UTF8DirReaderFeature)
 
1310
        self._save_platform_info()
 
1311
        osutils._fs_enc = 'ascii'
 
1312
        self.assertDirReaderIs(
 
1313
            UTF8DirReaderFeature.module.UTF8DirReader, b".")
 
1314
 
 
1315
    def test_force_walkdirs_utf8_fs_latin1(self):
 
1316
        self._save_platform_info()
 
1317
        osutils._fs_enc = 'iso-8859-1'
 
1318
        self.assertDirReaderIs(osutils.UnicodeDirReader, ".")
 
1319
 
 
1320
    def test_force_walkdirs_utf8_nt(self):
 
1321
        # Disabled because the thunk of the whole walkdirs api is disabled.
 
1322
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1323
        self._save_platform_info()
 
1324
        from .._walkdirs_win32 import Win32ReadDir
 
1325
        self.assertDirReaderIs(Win32ReadDir, ".")
 
1326
 
 
1327
    def test_unicode_walkdirs(self):
 
1328
        """Walkdirs should always return unicode paths."""
 
1329
        self.requireFeature(features.UnicodeFilenameFeature)
 
1330
        name0 = u'0file-\xb6'
 
1331
        name1 = u'1dir-\u062c\u0648'
 
1332
        name2 = u'2file-\u0633'
 
1333
        tree = [
 
1334
            name0,
 
1335
            name1 + '/',
 
1336
            name1 + '/' + name0,
 
1337
            name1 + '/' + name1 + '/',
 
1338
            name2,
 
1339
            ]
 
1340
        self.build_tree(tree)
 
1341
        expected_dirblocks = [
 
1342
            ((u'', u'.'),
 
1343
             [(name0, name0, 'file', './' + name0),
 
1344
              (name1, name1, 'directory', './' + name1),
 
1345
              (name2, name2, 'file', './' + name2),
 
1346
              ]
 
1347
             ),
 
1348
            ((name1, './' + name1),
 
1349
             [(name1 + '/' + name0, name0, 'file', './' + name1
 
1350
               + '/' + name0),
 
1351
              (name1 + '/' + name1, name1, 'directory', './' + name1
 
1352
               + '/' + name1),
 
1353
              ]
 
1354
             ),
 
1355
            ((name1 + '/' + name1, './' + name1 + '/' + name1),
 
1356
             [
 
1357
                ]
 
1358
             ),
 
1359
            ]
 
1360
        result = list(osutils.walkdirs('.'))
 
1361
        self._filter_out_stat(result)
 
1362
        self.assertEqual(expected_dirblocks, result)
 
1363
        result = list(osutils.walkdirs(u'./' + name1, name1))
 
1364
        self._filter_out_stat(result)
 
1365
        self.assertEqual(expected_dirblocks[1:], result)
 
1366
 
 
1367
    def test_unicode__walkdirs_utf8(self):
 
1368
        """Walkdirs_utf8 should always return utf8 paths.
 
1369
 
 
1370
        The abspath portion might be in unicode or utf-8
 
1371
        """
 
1372
        self.requireFeature(features.UnicodeFilenameFeature)
 
1373
        name0 = u'0file-\xb6'
 
1374
        name1 = u'1dir-\u062c\u0648'
 
1375
        name2 = u'2file-\u0633'
 
1376
        tree = [
 
1377
            name0,
 
1378
            name1 + '/',
 
1379
            name1 + '/' + name0,
 
1380
            name1 + '/' + name1 + '/',
 
1381
            name2,
 
1382
            ]
 
1383
        self.build_tree(tree)
 
1384
        name0 = name0.encode('utf8')
 
1385
        name1 = name1.encode('utf8')
 
1386
        name2 = name2.encode('utf8')
 
1387
 
 
1388
        expected_dirblocks = [
 
1389
            ((b'', b'.'),
 
1390
             [(name0, name0, 'file', b'./' + name0),
 
1391
              (name1, name1, 'directory', b'./' + name1),
 
1392
              (name2, name2, 'file', b'./' + name2),
 
1393
              ]
 
1394
             ),
 
1395
            ((name1, b'./' + name1),
 
1396
             [(name1 + b'/' + name0, name0, 'file', b'./' + name1
 
1397
               + b'/' + name0),
 
1398
              (name1 + b'/' + name1, name1, 'directory', b'./' + name1
 
1399
               + b'/' + name1),
 
1400
              ]
 
1401
             ),
 
1402
            ((name1 + b'/' + name1, b'./' + name1 + b'/' + name1),
 
1403
             [
 
1404
                ]
 
1405
             ),
 
1406
            ]
 
1407
        result = []
 
1408
        # For ease in testing, if walkdirs_utf8 returns Unicode, assert that
 
1409
        # all abspaths are Unicode, and encode them back into utf8.
 
1410
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
 
1411
            self.assertIsInstance(dirdetail[0], bytes)
 
1412
            if isinstance(dirdetail[1], text_type):
 
1413
                dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
 
1414
                dirblock = [list(info) for info in dirblock]
 
1415
                for info in dirblock:
 
1416
                    self.assertIsInstance(info[4], text_type)
 
1417
                    info[4] = info[4].encode('utf8')
 
1418
            new_dirblock = []
 
1419
            for info in dirblock:
 
1420
                self.assertIsInstance(info[0], bytes)
 
1421
                self.assertIsInstance(info[1], bytes)
 
1422
                self.assertIsInstance(info[4], bytes)
 
1423
                # Remove the stat information
 
1424
                new_dirblock.append((info[0], info[1], info[2], info[4]))
 
1425
            result.append((dirdetail, new_dirblock))
 
1426
        self.assertEqual(expected_dirblocks, result)
 
1427
 
 
1428
    def test__walkdirs_utf8_with_unicode_fs(self):
 
1429
        """UnicodeDirReader should be a safe fallback everywhere
 
1430
 
 
1431
        The abspath portion should be in unicode
 
1432
        """
 
1433
        self.requireFeature(features.UnicodeFilenameFeature)
 
1434
        # Use the unicode reader. TODO: split into driver-and-driven unit
 
1435
        # tests.
 
1436
        self._save_platform_info()
 
1437
        osutils._selected_dir_reader = osutils.UnicodeDirReader()
 
1438
        name0u = u'0file-\xb6'
 
1439
        name1u = u'1dir-\u062c\u0648'
 
1440
        name2u = u'2file-\u0633'
 
1441
        tree = [
 
1442
            name0u,
 
1443
            name1u + '/',
 
1444
            name1u + '/' + name0u,
 
1445
            name1u + '/' + name1u + '/',
 
1446
            name2u,
 
1447
            ]
 
1448
        self.build_tree(tree)
 
1449
        name0 = name0u.encode('utf8')
 
1450
        name1 = name1u.encode('utf8')
 
1451
        name2 = name2u.encode('utf8')
 
1452
 
 
1453
        # All of the abspaths should be in unicode, all of the relative paths
 
1454
        # should be in utf8
 
1455
        expected_dirblocks = [
 
1456
            ((b'', '.'),
 
1457
             [(name0, name0, 'file', './' + name0u),
 
1458
              (name1, name1, 'directory', './' + name1u),
 
1459
              (name2, name2, 'file', './' + name2u),
 
1460
              ]
 
1461
             ),
 
1462
            ((name1, './' + name1u),
 
1463
             [(name1 + b'/' + name0, name0, 'file', './' + name1u
 
1464
               + '/' + name0u),
 
1465
              (name1 + b'/' + name1, name1, 'directory', './' + name1u
 
1466
               + '/' + name1u),
 
1467
              ]
 
1468
             ),
 
1469
            ((name1 + b'/' + name1, './' + name1u + '/' + name1u),
 
1470
             [
 
1471
                ]
 
1472
             ),
 
1473
            ]
 
1474
        result = list(osutils._walkdirs_utf8('.'))
 
1475
        self._filter_out_stat(result)
 
1476
        self.assertEqual(expected_dirblocks, result)
 
1477
 
 
1478
    def test__walkdirs_utf8_win32readdir(self):
 
1479
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1480
        self.requireFeature(features.UnicodeFilenameFeature)
 
1481
        from .._walkdirs_win32 import Win32ReadDir
 
1482
        self._save_platform_info()
 
1483
        osutils._selected_dir_reader = Win32ReadDir()
 
1484
        name0u = u'0file-\xb6'
 
1485
        name1u = u'1dir-\u062c\u0648'
 
1486
        name2u = u'2file-\u0633'
 
1487
        tree = [
 
1488
            name0u,
 
1489
            name1u + '/',
 
1490
            name1u + '/' + name0u,
 
1491
            name1u + '/' + name1u + '/',
 
1492
            name2u,
 
1493
            ]
 
1494
        self.build_tree(tree)
 
1495
        name0 = name0u.encode('utf8')
 
1496
        name1 = name1u.encode('utf8')
 
1497
        name2 = name2u.encode('utf8')
 
1498
 
 
1499
        # All of the abspaths should be in unicode, all of the relative paths
 
1500
        # should be in utf8
 
1501
        expected_dirblocks = [
 
1502
            (('', '.'),
 
1503
             [(name0, name0, 'file', './' + name0u),
 
1504
              (name1, name1, 'directory', './' + name1u),
 
1505
              (name2, name2, 'file', './' + name2u),
 
1506
              ]
 
1507
             ),
 
1508
            ((name1, './' + name1u),
 
1509
             [(name1 + '/' + name0, name0, 'file', './' + name1u
 
1510
               + '/' + name0u),
 
1511
              (name1 + '/' + name1, name1, 'directory', './' + name1u
 
1512
               + '/' + name1u),
 
1513
              ]
 
1514
             ),
 
1515
            ((name1 + '/' + name1, './' + name1u + '/' + name1u),
 
1516
             [
 
1517
                ]
 
1518
             ),
 
1519
            ]
 
1520
        result = list(osutils._walkdirs_utf8(u'.'))
 
1521
        self._filter_out_stat(result)
 
1522
        self.assertEqual(expected_dirblocks, result)
 
1523
 
 
1524
    def assertStatIsCorrect(self, path, win32stat):
 
1525
        os_stat = os.stat(path)
 
1526
        self.assertEqual(os_stat.st_size, win32stat.st_size)
 
1527
        self.assertAlmostEqual(os_stat.st_mtime, win32stat.st_mtime, places=4)
 
1528
        self.assertAlmostEqual(os_stat.st_ctime, win32stat.st_ctime, places=4)
 
1529
        self.assertAlmostEqual(os_stat.st_atime, win32stat.st_atime, places=4)
 
1530
        self.assertEqual(os_stat.st_dev, win32stat.st_dev)
 
1531
        self.assertEqual(os_stat.st_ino, win32stat.st_ino)
 
1532
        self.assertEqual(os_stat.st_mode, win32stat.st_mode)
 
1533
 
 
1534
    def test__walkdirs_utf_win32_find_file_stat_file(self):
 
1535
        """make sure our Stat values are valid"""
 
1536
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1537
        self.requireFeature(features.UnicodeFilenameFeature)
 
1538
        from .._walkdirs_win32 import Win32ReadDir
 
1539
        name0u = u'0file-\xb6'
 
1540
        name0 = name0u.encode('utf8')
 
1541
        self.build_tree([name0u])
 
1542
        # I hate to sleep() here, but I'm trying to make the ctime different
 
1543
        # from the mtime
 
1544
        time.sleep(2)
 
1545
        with open(name0u, 'ab') as f:
 
1546
            f.write(b'just a small update')
 
1547
 
 
1548
        result = Win32ReadDir().read_dir('', u'.')
 
1549
        entry = result[0]
 
1550
        self.assertEqual((name0, name0, 'file'), entry[:3])
 
1551
        self.assertEqual(u'./' + name0u, entry[4])
 
1552
        self.assertStatIsCorrect(entry[4], entry[3])
 
1553
        self.assertNotEqual(entry[3].st_mtime, entry[3].st_ctime)
 
1554
 
 
1555
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
 
1556
        """make sure our Stat values are valid"""
 
1557
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1558
        self.requireFeature(features.UnicodeFilenameFeature)
 
1559
        from .._walkdirs_win32 import Win32ReadDir
 
1560
        name0u = u'0dir-\u062c\u0648'
 
1561
        name0 = name0u.encode('utf8')
 
1562
        self.build_tree([name0u + '/'])
 
1563
 
 
1564
        result = Win32ReadDir().read_dir('', u'.')
 
1565
        entry = result[0]
 
1566
        self.assertEqual((name0, name0, 'directory'), entry[:3])
 
1567
        self.assertEqual(u'./' + name0u, entry[4])
 
1568
        self.assertStatIsCorrect(entry[4], entry[3])
 
1569
 
 
1570
    def assertPathCompare(self, path_less, path_greater):
 
1571
        """check that path_less and path_greater compare correctly."""
 
1572
        self.assertEqual(0, osutils.compare_paths_prefix_order(
 
1573
            path_less, path_less))
 
1574
        self.assertEqual(0, osutils.compare_paths_prefix_order(
 
1575
            path_greater, path_greater))
 
1576
        self.assertEqual(-1, osutils.compare_paths_prefix_order(
 
1577
            path_less, path_greater))
 
1578
        self.assertEqual(1, osutils.compare_paths_prefix_order(
 
1579
            path_greater, path_less))
 
1580
 
 
1581
    def test_compare_paths_prefix_order(self):
 
1582
        # root before all else
 
1583
        self.assertPathCompare("/", "/a")
 
1584
        # alpha within a dir
 
1585
        self.assertPathCompare("/a", "/b")
 
1586
        self.assertPathCompare("/b", "/z")
 
1587
        # high dirs before lower.
 
1588
        self.assertPathCompare("/z", "/a/a")
 
1589
        # except if the deeper dir should be output first
 
1590
        self.assertPathCompare("/a/b/c", "/d/g")
 
1591
        # lexical betwen dirs of the same height
 
1592
        self.assertPathCompare("/a/z", "/z/z")
 
1593
        self.assertPathCompare("/a/c/z", "/a/d/e")
 
1594
 
 
1595
        # this should also be consistent for no leading / paths
 
1596
        # root before all else
 
1597
        self.assertPathCompare("", "a")
 
1598
        # alpha within a dir
 
1599
        self.assertPathCompare("a", "b")
 
1600
        self.assertPathCompare("b", "z")
 
1601
        # high dirs before lower.
 
1602
        self.assertPathCompare("z", "a/a")
 
1603
        # except if the deeper dir should be output first
 
1604
        self.assertPathCompare("a/b/c", "d/g")
 
1605
        # lexical betwen dirs of the same height
 
1606
        self.assertPathCompare("a/z", "z/z")
 
1607
        self.assertPathCompare("a/c/z", "a/d/e")
 
1608
 
 
1609
    def test_path_prefix_sorting(self):
 
1610
        """Doing a sort on path prefix should match our sample data."""
 
1611
        original_paths = [
 
1612
            'a',
 
1613
            'a/b',
 
1614
            'a/b/c',
 
1615
            'b',
 
1616
            'b/c',
 
1617
            'd',
 
1618
            'd/e',
 
1619
            'd/e/f',
 
1620
            'd/f',
 
1621
            'd/g',
 
1622
            'g',
 
1623
            ]
 
1624
 
 
1625
        dir_sorted_paths = [
 
1626
            'a',
 
1627
            'b',
 
1628
            'd',
 
1629
            'g',
 
1630
            'a/b',
 
1631
            'a/b/c',
 
1632
            'b/c',
 
1633
            'd/e',
 
1634
            'd/f',
 
1635
            'd/g',
 
1636
            'd/e/f',
 
1637
            ]
 
1638
 
 
1639
        self.assertEqual(
 
1640
            dir_sorted_paths,
 
1641
            sorted(original_paths, key=osutils.path_prefix_key))
 
1642
        # using the comparison routine shoudl work too:
 
1643
        self.assertEqual(
 
1644
            dir_sorted_paths,
 
1645
            sorted(original_paths, key=osutils.path_prefix_key))
 
1646
 
 
1647
 
 
1648
class TestCopyTree(tests.TestCaseInTempDir):
 
1649
 
 
1650
    def test_copy_basic_tree(self):
 
1651
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
 
1652
        osutils.copy_tree('source', 'target')
 
1653
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
 
1654
        self.assertEqual(['c'], os.listdir('target/b'))
 
1655
 
 
1656
    def test_copy_tree_target_exists(self):
 
1657
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
 
1658
                         'target/'])
 
1659
        osutils.copy_tree('source', 'target')
 
1660
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
 
1661
        self.assertEqual(['c'], os.listdir('target/b'))
 
1662
 
 
1663
    def test_copy_tree_symlinks(self):
 
1664
        self.requireFeature(features.SymlinkFeature)
 
1665
        self.build_tree(['source/'])
 
1666
        os.symlink('a/generic/path', 'source/lnk')
 
1667
        osutils.copy_tree('source', 'target')
 
1668
        self.assertEqual(['lnk'], os.listdir('target'))
 
1669
        self.assertEqual('a/generic/path', os.readlink('target/lnk'))
 
1670
 
 
1671
    def test_copy_tree_handlers(self):
 
1672
        processed_files = []
 
1673
        processed_links = []
 
1674
 
 
1675
        def file_handler(from_path, to_path):
 
1676
            processed_files.append(('f', from_path, to_path))
 
1677
 
 
1678
        def dir_handler(from_path, to_path):
 
1679
            processed_files.append(('d', from_path, to_path))
 
1680
 
 
1681
        def link_handler(from_path, to_path):
 
1682
            processed_links.append((from_path, to_path))
 
1683
        handlers = {'file': file_handler,
 
1684
                    'directory': dir_handler,
 
1685
                    'symlink': link_handler,
 
1686
                    }
 
1687
 
 
1688
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
 
1689
        if osutils.has_symlinks():
 
1690
            os.symlink('a/generic/path', 'source/lnk')
 
1691
        osutils.copy_tree('source', 'target', handlers=handlers)
 
1692
 
 
1693
        self.assertEqual([('d', 'source', 'target'),
 
1694
                          ('f', 'source/a', 'target/a'),
 
1695
                          ('d', 'source/b', 'target/b'),
 
1696
                          ('f', 'source/b/c', 'target/b/c'),
 
1697
                          ], processed_files)
 
1698
        self.assertPathDoesNotExist('target')
 
1699
        if osutils.has_symlinks():
 
1700
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
 
1701
 
 
1702
 
 
1703
class TestSetUnsetEnv(tests.TestCase):
 
1704
    """Test updating the environment"""
 
1705
 
 
1706
    def setUp(self):
 
1707
        super(TestSetUnsetEnv, self).setUp()
 
1708
 
 
1709
        self.assertEqual(None, os.environ.get('BRZ_TEST_ENV_VAR'),
 
1710
                         'Environment was not cleaned up properly.'
 
1711
                         ' Variable BRZ_TEST_ENV_VAR should not exist.')
 
1712
 
 
1713
        def cleanup():
 
1714
            if 'BRZ_TEST_ENV_VAR' in os.environ:
 
1715
                del os.environ['BRZ_TEST_ENV_VAR']
 
1716
        self.addCleanup(cleanup)
 
1717
 
 
1718
    def test_set(self):
 
1719
        """Test that we can set an env variable"""
 
1720
        old = osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'foo')
 
1721
        self.assertEqual(None, old)
 
1722
        self.assertEqual('foo', os.environ.get('BRZ_TEST_ENV_VAR'))
 
1723
 
 
1724
    def test_double_set(self):
 
1725
        """Test that we get the old value out"""
 
1726
        osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'foo')
 
1727
        old = osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'bar')
 
1728
        self.assertEqual('foo', old)
 
1729
        self.assertEqual('bar', os.environ.get('BRZ_TEST_ENV_VAR'))
 
1730
 
 
1731
    def test_unicode(self):
 
1732
        """Environment can only contain plain strings
 
1733
 
 
1734
        So Unicode strings must be encoded.
 
1735
        """
 
1736
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
1737
        if uni_val is None:
 
1738
            raise tests.TestSkipped(
 
1739
                'Cannot find a unicode character that works in encoding %s'
 
1740
                % (osutils.get_user_encoding(),))
 
1741
 
 
1742
        osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', uni_val)
 
1743
        if PY3:
 
1744
            self.assertEqual(uni_val, os.environ.get('BRZ_TEST_ENV_VAR'))
 
1745
        else:
 
1746
            self.assertEqual(env_val, os.environ.get('BRZ_TEST_ENV_VAR'))
 
1747
 
 
1748
    def test_unset(self):
 
1749
        """Test that passing None will remove the env var"""
 
1750
        osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'foo')
 
1751
        old = osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', None)
 
1752
        self.assertEqual('foo', old)
 
1753
        self.assertEqual(None, os.environ.get('BRZ_TEST_ENV_VAR'))
 
1754
        self.assertNotIn('BRZ_TEST_ENV_VAR', os.environ)
 
1755
 
 
1756
 
 
1757
class TestSizeShaFile(tests.TestCaseInTempDir):
 
1758
 
 
1759
    def test_sha_empty(self):
 
1760
        self.build_tree_contents([('foo', b'')])
 
1761
        expected_sha = osutils.sha_string(b'')
 
1762
        f = open('foo')
 
1763
        self.addCleanup(f.close)
 
1764
        size, sha = osutils.size_sha_file(f)
 
1765
        self.assertEqual(0, size)
 
1766
        self.assertEqual(expected_sha, sha)
 
1767
 
 
1768
    def test_sha_mixed_endings(self):
 
1769
        text = b'test\r\nwith\nall\rpossible line endings\r\n'
 
1770
        self.build_tree_contents([('foo', text)])
 
1771
        expected_sha = osutils.sha_string(text)
 
1772
        f = open('foo', 'rb')
 
1773
        self.addCleanup(f.close)
 
1774
        size, sha = osutils.size_sha_file(f)
 
1775
        self.assertEqual(38, size)
 
1776
        self.assertEqual(expected_sha, sha)
 
1777
 
 
1778
 
 
1779
class TestShaFileByName(tests.TestCaseInTempDir):
 
1780
 
 
1781
    def test_sha_empty(self):
 
1782
        self.build_tree_contents([('foo', b'')])
 
1783
        expected_sha = osutils.sha_string(b'')
 
1784
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1785
 
 
1786
    def test_sha_mixed_endings(self):
 
1787
        text = b'test\r\nwith\nall\rpossible line endings\r\n'
 
1788
        self.build_tree_contents([('foo', text)])
 
1789
        expected_sha = osutils.sha_string(text)
 
1790
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1791
 
 
1792
 
 
1793
class TestResourceLoading(tests.TestCaseInTempDir):
 
1794
 
 
1795
    def test_resource_string(self):
 
1796
        # test resource in breezy
 
1797
        text = osutils.resource_string('breezy', 'debug.py')
 
1798
        self.assertContainsRe(text, "debug_flags = set()")
 
1799
        # test resource under breezy
 
1800
        text = osutils.resource_string('breezy.ui', 'text.py')
 
1801
        self.assertContainsRe(text, "class TextUIFactory")
 
1802
        # test unsupported package
 
1803
        self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
 
1804
                          'yyy.xx')
 
1805
        # test unknown resource
 
1806
        self.assertRaises(IOError, osutils.resource_string, 'breezy', 'yyy.xx')
 
1807
 
 
1808
 
 
1809
class TestDirReader(tests.TestCaseInTempDir):
 
1810
 
 
1811
    scenarios = dir_reader_scenarios()
 
1812
 
 
1813
    # Set by load_tests
 
1814
    _dir_reader_class = None
 
1815
    _native_to_unicode = None
 
1816
 
 
1817
    def setUp(self):
 
1818
        super(TestDirReader, self).setUp()
 
1819
        self.overrideAttr(osutils,
 
1820
                          '_selected_dir_reader', self._dir_reader_class())
 
1821
 
 
1822
    def _get_ascii_tree(self):
 
1823
        tree = [
 
1824
            '0file',
 
1825
            '1dir/',
 
1826
            '1dir/0file',
 
1827
            '1dir/1dir/',
 
1828
            '2file'
 
1829
            ]
 
1830
        expected_dirblocks = [
 
1831
            ((b'', '.'),
 
1832
             [(b'0file', b'0file', 'file', './0file'),
 
1833
              (b'1dir', b'1dir', 'directory', './1dir'),
 
1834
              (b'2file', b'2file', 'file', './2file'),
 
1835
              ]
 
1836
             ),
 
1837
            ((b'1dir', './1dir'),
 
1838
             [(b'1dir/0file', b'0file', 'file', './1dir/0file'),
 
1839
              (b'1dir/1dir', b'1dir', 'directory', './1dir/1dir'),
 
1840
              ]
 
1841
             ),
 
1842
            ((b'1dir/1dir', './1dir/1dir'),
 
1843
             [
 
1844
                ]
 
1845
             ),
 
1846
            ]
 
1847
        return tree, expected_dirblocks
 
1848
 
 
1849
    def test_walk_cur_dir(self):
 
1850
        tree, expected_dirblocks = self._get_ascii_tree()
 
1851
        self.build_tree(tree)
 
1852
        result = list(osutils._walkdirs_utf8('.'))
 
1853
        # Filter out stat and abspath
 
1854
        self.assertEqual(expected_dirblocks,
 
1855
                         self._filter_out(result))
 
1856
 
 
1857
    def test_walk_sub_dir(self):
 
1858
        tree, expected_dirblocks = self._get_ascii_tree()
 
1859
        self.build_tree(tree)
 
1860
        # you can search a subdir only, with a supplied prefix.
 
1861
        result = list(osutils._walkdirs_utf8(b'./1dir', b'1dir'))
 
1862
        # Filter out stat and abspath
 
1863
        self.assertEqual(expected_dirblocks[1:],
 
1864
                         self._filter_out(result))
 
1865
 
 
1866
    def _get_unicode_tree(self):
 
1867
        name0u = u'0file-\xb6'
 
1868
        name1u = u'1dir-\u062c\u0648'
 
1869
        name2u = u'2file-\u0633'
 
1870
        tree = [
 
1871
            name0u,
 
1872
            name1u + '/',
 
1873
            name1u + '/' + name0u,
 
1874
            name1u + '/' + name1u + '/',
 
1875
            name2u,
 
1876
            ]
 
1877
        name0 = name0u.encode('UTF-8')
 
1878
        name1 = name1u.encode('UTF-8')
 
1879
        name2 = name2u.encode('UTF-8')
 
1880
        expected_dirblocks = [
 
1881
            ((b'', '.'),
 
1882
             [(name0, name0, 'file', './' + name0u),
 
1883
              (name1, name1, 'directory', './' + name1u),
 
1884
              (name2, name2, 'file', './' + name2u),
 
1885
              ]
 
1886
             ),
 
1887
            ((name1, './' + name1u),
 
1888
             [(name1 + b'/' + name0, name0, 'file', './' + name1u
 
1889
               + '/' + name0u),
 
1890
              (name1 + b'/' + name1, name1, 'directory', './' + name1u
 
1891
               + '/' + name1u),
 
1892
              ]
 
1893
             ),
 
1894
            ((name1 + b'/' + name1, './' + name1u + '/' + name1u),
 
1895
             [
 
1896
                ]
 
1897
             ),
 
1898
            ]
 
1899
        return tree, expected_dirblocks
 
1900
 
 
1901
    def _filter_out(self, raw_dirblocks):
 
1902
        """Filter out a walkdirs_utf8 result.
 
1903
 
 
1904
        stat field is removed, all native paths are converted to unicode
 
1905
        """
 
1906
        filtered_dirblocks = []
 
1907
        for dirinfo, block in raw_dirblocks:
 
1908
            dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
 
1909
            details = []
 
1910
            for line in block:
 
1911
                details.append(
 
1912
                    line[0:3] + (self._native_to_unicode(line[4]), ))
 
1913
            filtered_dirblocks.append((dirinfo, details))
 
1914
        return filtered_dirblocks
 
1915
 
 
1916
    def test_walk_unicode_tree(self):
 
1917
        self.requireFeature(features.UnicodeFilenameFeature)
 
1918
        tree, expected_dirblocks = self._get_unicode_tree()
 
1919
        self.build_tree(tree)
 
1920
        result = list(osutils._walkdirs_utf8('.'))
 
1921
        self.assertEqual(expected_dirblocks, self._filter_out(result))
 
1922
 
 
1923
    def test_symlink(self):
 
1924
        self.requireFeature(features.SymlinkFeature)
 
1925
        self.requireFeature(features.UnicodeFilenameFeature)
 
1926
        target = u'target\N{Euro Sign}'
 
1927
        link_name = u'l\N{Euro Sign}nk'
 
1928
        os.symlink(target, link_name)
 
1929
        link_name_utf8 = link_name.encode('UTF-8')
 
1930
        expected_dirblocks = [
 
1931
            ((b'', '.'),
 
1932
             [(link_name_utf8, link_name_utf8,
 
1933
               'symlink', './' + link_name), ],
 
1934
             )]
 
1935
        result = list(osutils._walkdirs_utf8('.'))
 
1936
        self.assertEqual(expected_dirblocks, self._filter_out(result))
 
1937
 
 
1938
 
 
1939
class TestReadLink(tests.TestCaseInTempDir):
 
1940
    """Exposes os.readlink() problems and the osutils solution.
 
1941
 
 
1942
    The only guarantee offered by os.readlink(), starting with 2.6, is that a
 
1943
    unicode string will be returned if a unicode string is passed.
 
1944
 
 
1945
    But prior python versions failed to properly encode the passed unicode
 
1946
    string.
 
1947
    """
 
1948
    _test_needs_features = [features.SymlinkFeature,
 
1949
                            features.UnicodeFilenameFeature]
 
1950
 
 
1951
    def setUp(self):
 
1952
        super(tests.TestCaseInTempDir, self).setUp()
 
1953
        self.link = u'l\N{Euro Sign}ink'
 
1954
        self.target = u'targe\N{Euro Sign}t'
 
1955
        os.symlink(self.target, self.link)
 
1956
 
 
1957
    def test_os_readlink_link_encoding(self):
 
1958
        self.assertEqual(self.target, os.readlink(self.link))
 
1959
 
 
1960
    def test_os_readlink_link_decoding(self):
 
1961
        self.assertEqual(self.target.encode(osutils._fs_enc),
 
1962
                         os.readlink(self.link.encode(osutils._fs_enc)))
 
1963
 
 
1964
 
 
1965
class TestConcurrency(tests.TestCase):
 
1966
 
 
1967
    def setUp(self):
 
1968
        super(TestConcurrency, self).setUp()
 
1969
        self.overrideAttr(osutils, '_cached_local_concurrency')
 
1970
 
 
1971
    def test_local_concurrency(self):
 
1972
        concurrency = osutils.local_concurrency()
 
1973
        self.assertIsInstance(concurrency, int)
 
1974
 
 
1975
    def test_local_concurrency_environment_variable(self):
 
1976
        self.overrideEnv('BRZ_CONCURRENCY', '2')
 
1977
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
 
1978
        self.overrideEnv('BRZ_CONCURRENCY', '3')
 
1979
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
 
1980
        self.overrideEnv('BRZ_CONCURRENCY', 'foo')
 
1981
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
 
1982
 
 
1983
    def test_option_concurrency(self):
 
1984
        self.overrideEnv('BRZ_CONCURRENCY', '1')
 
1985
        self.run_bzr('rocks --concurrency 42')
 
1986
        # Command line overrides environment variable
 
1987
        self.assertEqual('42', os.environ['BRZ_CONCURRENCY'])
 
1988
        self.assertEqual(42, osutils.local_concurrency(use_cache=False))
 
1989
 
 
1990
 
 
1991
class TestFailedToLoadExtension(tests.TestCase):
 
1992
 
 
1993
    def _try_loading(self):
 
1994
        try:
 
1995
            import breezy._fictional_extension_py  # noqa: F401
 
1996
        except ImportError as e:
 
1997
            osutils.failed_to_load_extension(e)
 
1998
            return True
 
1999
 
 
2000
    def setUp(self):
 
2001
        super(TestFailedToLoadExtension, self).setUp()
 
2002
        self.overrideAttr(osutils, '_extension_load_failures', [])
 
2003
 
 
2004
    def test_failure_to_load(self):
 
2005
        self._try_loading()
 
2006
        self.assertLength(1, osutils._extension_load_failures)
 
2007
        if PY3:
 
2008
            self.assertEqual(
 
2009
                osutils._extension_load_failures[0],
 
2010
                "No module named 'breezy._fictional_extension_py'")
 
2011
        else:
 
2012
            self.assertEqual(osutils._extension_load_failures[0],
 
2013
                             "No module named _fictional_extension_py")
 
2014
 
 
2015
    def test_report_extension_load_failures_no_warning(self):
 
2016
        self.assertTrue(self._try_loading())
 
2017
        warnings, result = self.callCatchWarnings(
 
2018
            osutils.report_extension_load_failures)
 
2019
        # it used to give a Python warning; it no longer does
 
2020
        self.assertLength(0, warnings)
 
2021
 
 
2022
    def test_report_extension_load_failures_message(self):
 
2023
        log = BytesIO()
 
2024
        trace.push_log_file(log)
 
2025
        self.assertTrue(self._try_loading())
 
2026
        osutils.report_extension_load_failures()
 
2027
        self.assertContainsRe(
 
2028
            log.getvalue(),
 
2029
            br"brz: warning: some compiled extensions could not be loaded; "
 
2030
            b"see ``brz help missing-extensions``\n"
 
2031
            )
 
2032
 
 
2033
 
 
2034
class TestTerminalWidth(tests.TestCase):
 
2035
 
 
2036
    def setUp(self):
 
2037
        super(TestTerminalWidth, self).setUp()
 
2038
        self._orig_terminal_size_state = osutils._terminal_size_state
 
2039
        self._orig_first_terminal_size = osutils._first_terminal_size
 
2040
        self.addCleanup(self.restore_osutils_globals)
 
2041
        osutils._terminal_size_state = 'no_data'
 
2042
        osutils._first_terminal_size = None
 
2043
 
 
2044
    def restore_osutils_globals(self):
 
2045
        osutils._terminal_size_state = self._orig_terminal_size_state
 
2046
        osutils._first_terminal_size = self._orig_first_terminal_size
 
2047
 
 
2048
    def replace_stdout(self, new):
 
2049
        self.overrideAttr(sys, 'stdout', new)
 
2050
 
 
2051
    def replace__terminal_size(self, new):
 
2052
        self.overrideAttr(osutils, '_terminal_size', new)
 
2053
 
 
2054
    def set_fake_tty(self):
 
2055
 
 
2056
        class I_am_a_tty(object):
 
2057
            def isatty(self):
 
2058
                return True
 
2059
 
 
2060
        self.replace_stdout(I_am_a_tty())
 
2061
 
 
2062
    def test_default_values(self):
 
2063
        self.assertEqual(80, osutils.default_terminal_width)
 
2064
 
 
2065
    def test_defaults_to_BRZ_COLUMNS(self):
 
2066
        # BRZ_COLUMNS is set by the test framework
 
2067
        self.assertNotEqual('12', os.environ['BRZ_COLUMNS'])
 
2068
        self.overrideEnv('BRZ_COLUMNS', '12')
 
2069
        self.assertEqual(12, osutils.terminal_width())
 
2070
 
 
2071
    def test_BRZ_COLUMNS_0_no_limit(self):
 
2072
        self.overrideEnv('BRZ_COLUMNS', '0')
 
2073
        self.assertEqual(None, osutils.terminal_width())
 
2074
 
 
2075
    def test_falls_back_to_COLUMNS(self):
 
2076
        self.overrideEnv('BRZ_COLUMNS', None)
 
2077
        self.assertNotEqual('42', os.environ['COLUMNS'])
 
2078
        self.set_fake_tty()
 
2079
        self.overrideEnv('COLUMNS', '42')
 
2080
        self.assertEqual(42, osutils.terminal_width())
 
2081
 
 
2082
    def test_tty_default_without_columns(self):
 
2083
        self.overrideEnv('BRZ_COLUMNS', None)
 
2084
        self.overrideEnv('COLUMNS', None)
 
2085
 
 
2086
        def terminal_size(w, h):
 
2087
            return 42, 42
 
2088
 
 
2089
        self.set_fake_tty()
 
2090
        # We need to override the osutils definition as it depends on the
 
2091
        # running environment that we can't control (PQM running without a
 
2092
        # controlling terminal is one example).
 
2093
        self.replace__terminal_size(terminal_size)
 
2094
        self.assertEqual(42, osutils.terminal_width())
 
2095
 
 
2096
    def test_non_tty_default_without_columns(self):
 
2097
        self.overrideEnv('BRZ_COLUMNS', None)
 
2098
        self.overrideEnv('COLUMNS', None)
 
2099
        self.replace_stdout(None)
 
2100
        self.assertEqual(None, osutils.terminal_width())
 
2101
 
 
2102
    def test_no_TIOCGWINSZ(self):
 
2103
        self.requireFeature(term_ios_feature)
 
2104
        termios = term_ios_feature.module
 
2105
        # bug 63539 is about a termios without TIOCGWINSZ attribute
 
2106
        try:
 
2107
            termios.TIOCGWINSZ
 
2108
        except AttributeError:
 
2109
            # We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
 
2110
            pass
 
2111
        else:
 
2112
            self.overrideAttr(termios, 'TIOCGWINSZ')
 
2113
            del termios.TIOCGWINSZ
 
2114
        self.overrideEnv('BRZ_COLUMNS', None)
 
2115
        self.overrideEnv('COLUMNS', None)
 
2116
        # Whatever the result is, if we don't raise an exception, it's ok.
 
2117
        osutils.terminal_width()
 
2118
 
 
2119
 
 
2120
class TestCreationOps(tests.TestCaseInTempDir):
 
2121
    _test_needs_features = [features.chown_feature]
 
2122
 
 
2123
    def setUp(self):
 
2124
        super(TestCreationOps, self).setUp()
 
2125
        self.overrideAttr(os, 'chown', self._dummy_chown)
 
2126
 
 
2127
        # params set by call to _dummy_chown
 
2128
        self.path = self.uid = self.gid = None
 
2129
 
 
2130
    def _dummy_chown(self, path, uid, gid):
 
2131
        self.path, self.uid, self.gid = path, uid, gid
 
2132
 
 
2133
    def test_copy_ownership_from_path(self):
 
2134
        """copy_ownership_from_path test with specified src."""
 
2135
        ownsrc = '/'
 
2136
        open('test_file', 'wt').close()
 
2137
        osutils.copy_ownership_from_path('test_file', ownsrc)
 
2138
 
 
2139
        s = os.stat(ownsrc)
 
2140
        self.assertEqual(self.path, 'test_file')
 
2141
        self.assertEqual(self.uid, s.st_uid)
 
2142
        self.assertEqual(self.gid, s.st_gid)
 
2143
 
 
2144
    def test_copy_ownership_nonesrc(self):
 
2145
        """copy_ownership_from_path test with src=None."""
 
2146
        open('test_file', 'wt').close()
 
2147
        # should use parent dir for permissions
 
2148
        osutils.copy_ownership_from_path('test_file')
 
2149
 
 
2150
        s = os.stat('..')
 
2151
        self.assertEqual(self.path, 'test_file')
 
2152
        self.assertEqual(self.uid, s.st_uid)
 
2153
        self.assertEqual(self.gid, s.st_gid)
 
2154
 
 
2155
 
 
2156
class TestPathFromEnviron(tests.TestCase):
 
2157
 
 
2158
    def test_is_unicode(self):
 
2159
        self.overrideEnv('BRZ_TEST_PATH', './anywhere at all/')
 
2160
        path = osutils.path_from_environ('BRZ_TEST_PATH')
 
2161
        self.assertIsInstance(path, text_type)
 
2162
        self.assertEqual(u'./anywhere at all/', path)
 
2163
 
 
2164
    def test_posix_path_env_ascii(self):
 
2165
        self.overrideEnv('BRZ_TEST_PATH', '/tmp')
 
2166
        home = osutils._posix_path_from_environ('BRZ_TEST_PATH')
 
2167
        self.assertIsInstance(home, text_type)
 
2168
        self.assertEqual(u'/tmp', home)
 
2169
 
 
2170
    def test_posix_path_env_unicode(self):
 
2171
        self.requireFeature(features.ByteStringNamedFilesystem)
 
2172
        self.overrideEnv('BRZ_TEST_PATH', '/home/\xa7test')
 
2173
        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
 
2174
        self.assertEqual(u'/home/\xa7test',
 
2175
                         osutils._posix_path_from_environ('BRZ_TEST_PATH'))
 
2176
        osutils._fs_enc = "iso8859-5"
 
2177
        if PY3:
 
2178
            # In Python 3, os.environ returns unicode.
 
2179
            self.assertEqual(u'/home/\xa7test',
 
2180
                             osutils._posix_path_from_environ('BRZ_TEST_PATH'))
 
2181
        else:
 
2182
            self.assertEqual(u'/home/\u0407test',
 
2183
                             osutils._posix_path_from_environ('BRZ_TEST_PATH'))
 
2184
            osutils._fs_enc = "utf-8"
 
2185
            self.assertRaises(
 
2186
                errors.BadFilenameEncoding,
 
2187
                osutils._posix_path_from_environ, 'BRZ_TEST_PATH')
 
2188
 
 
2189
 
 
2190
class TestGetHomeDir(tests.TestCase):
 
2191
 
 
2192
    def test_is_unicode(self):
 
2193
        home = osutils._get_home_dir()
 
2194
        self.assertIsInstance(home, text_type)
 
2195
 
 
2196
    def test_posix_homeless(self):
 
2197
        self.overrideEnv('HOME', None)
 
2198
        home = osutils._get_home_dir()
 
2199
        self.assertIsInstance(home, text_type)
 
2200
 
 
2201
    def test_posix_home_ascii(self):
 
2202
        self.overrideEnv('HOME', '/home/test')
 
2203
        home = osutils._posix_get_home_dir()
 
2204
        self.assertIsInstance(home, text_type)
 
2205
        self.assertEqual(u'/home/test', home)
 
2206
 
 
2207
    def test_posix_home_unicode(self):
 
2208
        self.requireFeature(features.ByteStringNamedFilesystem)
 
2209
        self.overrideEnv('HOME', '/home/\xa7test')
 
2210
        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
 
2211
        self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
 
2212
        osutils._fs_enc = "iso8859-5"
 
2213
        if PY3:
 
2214
            # In python 3, os.environ returns unicode
 
2215
            self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
 
2216
        else:
 
2217
            self.assertEqual(u'/home/\u0407test',
 
2218
                             osutils._posix_get_home_dir())
 
2219
            osutils._fs_enc = "utf-8"
 
2220
            self.assertRaises(errors.BadFilenameEncoding,
 
2221
                              osutils._posix_get_home_dir)
 
2222
 
 
2223
 
 
2224
class TestGetuserUnicode(tests.TestCase):
 
2225
 
 
2226
    def test_is_unicode(self):
 
2227
        user = osutils.getuser_unicode()
 
2228
        self.assertIsInstance(user, text_type)
 
2229
 
 
2230
    def envvar_to_override(self):
 
2231
        if sys.platform == "win32":
 
2232
            # Disable use of platform calls on windows so envvar is used
 
2233
            self.overrideAttr(win32utils, 'has_ctypes', False)
 
2234
            return 'USERNAME'  # only variable used on windows
 
2235
        return 'LOGNAME'  # first variable checked by getpass.getuser()
 
2236
 
 
2237
    def test_ascii_user(self):
 
2238
        self.overrideEnv(self.envvar_to_override(), 'jrandom')
 
2239
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
 
2240
 
 
2241
    def test_unicode_user(self):
 
2242
        ue = osutils.get_user_encoding()
 
2243
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
2244
        if uni_val is None:
 
2245
            raise tests.TestSkipped(
 
2246
                'Cannot find a unicode character that works in encoding %s'
 
2247
                % (osutils.get_user_encoding(),))
 
2248
        uni_username = u'jrandom' + uni_val
 
2249
        encoded_username = uni_username.encode(ue)
 
2250
        if PY3:
 
2251
            self.overrideEnv(self.envvar_to_override(), uni_username)
 
2252
        else:
 
2253
            self.overrideEnv(self.envvar_to_override(), encoded_username)
 
2254
        self.assertEqual(uni_username, osutils.getuser_unicode())
 
2255
 
 
2256
 
 
2257
class TestBackupNames(tests.TestCase):
 
2258
 
 
2259
    def setUp(self):
 
2260
        super(TestBackupNames, self).setUp()
 
2261
        self.backups = []
 
2262
 
 
2263
    def backup_exists(self, name):
 
2264
        return name in self.backups
 
2265
 
 
2266
    def available_backup_name(self, name):
 
2267
        backup_name = osutils.available_backup_name(name, self.backup_exists)
 
2268
        self.backups.append(backup_name)
 
2269
        return backup_name
 
2270
 
 
2271
    def assertBackupName(self, expected, name):
 
2272
        self.assertEqual(expected, self.available_backup_name(name))
 
2273
 
 
2274
    def test_empty(self):
 
2275
        self.assertBackupName('file.~1~', 'file')
 
2276
 
 
2277
    def test_existing(self):
 
2278
        self.available_backup_name('file')
 
2279
        self.available_backup_name('file')
 
2280
        self.assertBackupName('file.~3~', 'file')
 
2281
        # Empty slots are found, this is not a strict requirement and may be
 
2282
        # revisited if we test against all implementations.
 
2283
        self.backups.remove('file.~2~')
 
2284
        self.assertBackupName('file.~2~', 'file')
 
2285
 
 
2286
 
 
2287
class TestFindExecutableInPath(tests.TestCase):
 
2288
 
 
2289
    def test_windows(self):
 
2290
        if sys.platform != 'win32':
 
2291
            raise tests.TestSkipped('test requires win32')
 
2292
        self.assertTrue(osutils.find_executable_on_path(
 
2293
            'explorer') is not None)
 
2294
        self.assertTrue(
 
2295
            osutils.find_executable_on_path('explorer.exe') is not None)
 
2296
        self.assertTrue(
 
2297
            osutils.find_executable_on_path('EXPLORER.EXE') is not None)
 
2298
        self.assertTrue(
 
2299
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2300
        self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
 
2301
 
 
2302
    def test_windows_app_path(self):
 
2303
        if sys.platform != 'win32':
 
2304
            raise tests.TestSkipped('test requires win32')
 
2305
        # Override PATH env var so that exe can only be found on App Path
 
2306
        self.overrideEnv('PATH', '')
 
2307
        # Internt Explorer is always registered in the App Path
 
2308
        self.assertTrue(osutils.find_executable_on_path(
 
2309
            'iexplore') is not None)
 
2310
 
 
2311
    def test_other(self):
 
2312
        if sys.platform == 'win32':
 
2313
            raise tests.TestSkipped('test requires non-win32')
 
2314
        self.assertTrue(osutils.find_executable_on_path('sh') is not None)
 
2315
        self.assertTrue(
 
2316
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2317
 
 
2318
 
 
2319
class TestEnvironmentErrors(tests.TestCase):
 
2320
    """Test handling of environmental errors"""
 
2321
 
 
2322
    def test_is_oserror(self):
 
2323
        self.assertTrue(osutils.is_environment_error(
 
2324
            OSError(errno.EINVAL, "Invalid parameter")))
 
2325
 
 
2326
    def test_is_ioerror(self):
 
2327
        self.assertTrue(osutils.is_environment_error(
 
2328
            IOError(errno.EINVAL, "Invalid parameter")))
 
2329
 
 
2330
    def test_is_socket_error(self):
 
2331
        self.assertTrue(osutils.is_environment_error(
 
2332
            socket.error(errno.EINVAL, "Invalid parameter")))
 
2333
 
 
2334
    def test_is_select_error(self):
 
2335
        self.assertTrue(osutils.is_environment_error(
 
2336
            select.error(errno.EINVAL, "Invalid parameter")))
 
2337
 
 
2338
    def test_is_pywintypes_error(self):
 
2339
        self.requireFeature(features.pywintypes)
 
2340
        import pywintypes
 
2341
        self.assertTrue(osutils.is_environment_error(
 
2342
            pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))
 
2343
 
 
2344
 
 
2345
class SupportsExecutableTests(tests.TestCaseInTempDir):
 
2346
 
 
2347
    def test_returns_bool(self):
 
2348
        self.assertIsInstance(osutils.supports_executable(self.test_dir), bool)
 
2349
 
 
2350
 
 
2351
class SupportsSymlinksTests(tests.TestCaseInTempDir):
 
2352
 
 
2353
    def test_returns_bool(self):
 
2354
        self.assertIsInstance(osutils.supports_symlinks(self.test_dir), bool)
 
2355
 
 
2356
 
 
2357
class MtabReader(tests.TestCaseInTempDir):
 
2358
 
 
2359
    def test_read_mtab(self):
 
2360
        self.build_tree_contents([('mtab', """\
 
2361
/dev/mapper/blah--vg-root / ext4 rw,relatime,errors=remount-ro 0 0
 
2362
/dev/mapper/blah--vg-home /home vfat rw,relatime 0 0
 
2363
# comment
 
2364
 
 
2365
iminvalid
 
2366
""")])
 
2367
        self.assertEqual(
 
2368
            list(osutils.read_mtab('mtab')),
 
2369
            [(b'/', 'ext4'),
 
2370
             (b'/home', 'vfat')])
 
2371
 
 
2372
 
 
2373
class GetFsTypeTests(tests.TestCaseInTempDir):
 
2374
 
 
2375
    def test_returns_string_or_none(self):
 
2376
        ret = osutils.get_fs_type(self.test_dir)
 
2377
        self.assertTrue(isinstance(ret, text_type) or ret is None)
 
2378
 
 
2379
    def test_returns_most_specific(self):
 
2380
        self.overrideAttr(
 
2381
            osutils, '_FILESYSTEM_FINDER',
 
2382
            osutils.FilesystemFinder(
 
2383
                [(b'/', 'ext4'), (b'/home', 'vfat'),
 
2384
                 (b'/home/jelmer', 'ext2')]))
 
2385
        self.assertEqual(osutils.get_fs_type(b'/home/jelmer/blah'), 'ext2')
 
2386
        self.assertEqual(osutils.get_fs_type('/home/jelmer/blah'), 'ext2')
 
2387
        self.assertEqual(osutils.get_fs_type(b'/home/jelmer'), 'ext2')
 
2388
        self.assertEqual(osutils.get_fs_type(b'/home/martin'), 'vfat')
 
2389
        self.assertEqual(osutils.get_fs_type(b'/home'), 'vfat')
 
2390
        self.assertEqual(osutils.get_fs_type(b'/other'), 'ext4')
 
2391
 
 
2392
    def test_returns_none(self):
 
2393
        self.overrideAttr(
 
2394
            osutils, '_FILESYSTEM_FINDER',
 
2395
            osutils.FilesystemFinder([]))
 
2396
        self.assertIs(osutils.get_fs_type('/home/jelmer/blah'), None)
 
2397
        self.assertIs(osutils.get_fs_type(b'/home/jelmer/blah'), None)
 
2398
        self.assertIs(osutils.get_fs_type('/home/jelmer'), None)