/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:26:22 UTC
  • mfrom: (7167.1.4 run-flake8)
  • Revision ID: breezy.the.bot@gmail.com-20181116182622-qw3gan3hz78a2imw
Add a flake8 test.

Merged from https://code.launchpad.net/~jelmer/brz/run-flake8/+merge/358902

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