~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for the osutils wrapper."""
18
18
 
19
19
from cStringIO import StringIO
20
20
import errno
21
21
import os
 
22
import re
22
23
import socket
23
 
import stat
24
24
import sys
25
25
import time
26
26
 
27
27
from bzrlib import (
28
28
    errors,
 
29
    lazy_regex,
29
30
    osutils,
 
31
    symbol_versioning,
30
32
    tests,
 
33
    trace,
31
34
    win32utils,
32
35
    )
33
 
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
34
 
from bzrlib.osutils import (
35
 
        is_inside_any,
36
 
        is_inside_or_parent_of_any,
37
 
        pathjoin,
38
 
        pumpfile,
39
 
        pump_string_file,
40
 
        canonical_relpath,
41
 
        )
42
36
from bzrlib.tests import (
43
 
        adapt_tests,
44
 
        Feature,
45
 
        probe_unicode_in_user_encoding,
46
 
        split_suite_by_re,
47
 
        StringIOWrapper,
48
 
        SymlinkFeature,
49
 
        CaseInsCasePresFilenameFeature,
50
 
        TestCase,
51
 
        TestCaseInTempDir,
52
 
        TestScenarioApplier,
53
 
        TestSkipped,
54
 
        )
55
 
from bzrlib.tests.file_utils import (
56
 
    FakeReadFile,
 
37
    features,
 
38
    file_utils,
 
39
    test__walkdirs_win32,
57
40
    )
58
 
from bzrlib.tests.test__walkdirs_win32 import Win32ReadDirFeature
59
 
 
60
 
 
61
 
class _UTF8DirReaderFeature(Feature):
 
41
from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
42
 
 
43
 
 
44
class _UTF8DirReaderFeature(features.Feature):
62
45
 
63
46
    def _probe(self):
64
47
        try:
71
54
    def feature_name(self):
72
55
        return 'bzrlib._readdir_pyx'
73
56
 
74
 
UTF8DirReaderFeature = _UTF8DirReaderFeature()
75
 
 
76
 
 
77
 
class TestOSUtils(TestCaseInTempDir):
 
57
UTF8DirReaderFeature = features.ModuleAvailableFeature('bzrlib._readdir_pyx')
 
58
 
 
59
term_ios_feature = features.ModuleAvailableFeature('termios')
 
60
 
 
61
 
 
62
def _already_unicode(s):
 
63
    return s
 
64
 
 
65
 
 
66
def _utf8_to_unicode(s):
 
67
    return s.decode('UTF-8')
 
68
 
 
69
 
 
70
def dir_reader_scenarios():
 
71
    # For each dir reader we define:
 
72
 
 
73
    # - native_to_unicode: a function converting the native_abspath as returned
 
74
    #   by DirReader.read_dir to its unicode representation
 
75
 
 
76
    # UnicodeDirReader is the fallback, it should be tested on all platforms.
 
77
    scenarios = [('unicode',
 
78
                  dict(_dir_reader_class=osutils.UnicodeDirReader,
 
79
                       _native_to_unicode=_already_unicode))]
 
80
    # Some DirReaders are platform specific and even there they may not be
 
81
    # available.
 
82
    if UTF8DirReaderFeature.available():
 
83
        from bzrlib import _readdir_pyx
 
84
        scenarios.append(('utf8',
 
85
                          dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
 
86
                               _native_to_unicode=_utf8_to_unicode)))
 
87
 
 
88
    if test__walkdirs_win32.win32_readdir_feature.available():
 
89
        try:
 
90
            from bzrlib import _walkdirs_win32
 
91
            scenarios.append(
 
92
                ('win32',
 
93
                 dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
 
94
                      _native_to_unicode=_already_unicode)))
 
95
        except ImportError:
 
96
            pass
 
97
    return scenarios
 
98
 
 
99
 
 
100
load_tests = load_tests_apply_scenarios
 
101
 
 
102
 
 
103
class TestContainsWhitespace(tests.TestCase):
78
104
 
79
105
    def test_contains_whitespace(self):
80
 
        self.failUnless(osutils.contains_whitespace(u' '))
81
 
        self.failUnless(osutils.contains_whitespace(u'hello there'))
82
 
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
83
 
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
84
 
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
85
 
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
 
106
        self.assertTrue(osutils.contains_whitespace(u' '))
 
107
        self.assertTrue(osutils.contains_whitespace(u'hello there'))
 
108
        self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
 
109
        self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
 
110
        self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
 
111
        self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
86
112
 
87
113
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
88
114
        # is whitespace, but we do not.
89
 
        self.failIf(osutils.contains_whitespace(u''))
90
 
        self.failIf(osutils.contains_whitespace(u'hellothere'))
91
 
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
 
115
        self.assertFalse(osutils.contains_whitespace(u''))
 
116
        self.assertFalse(osutils.contains_whitespace(u'hellothere'))
 
117
        self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
 
118
 
 
119
 
 
120
class TestRename(tests.TestCaseInTempDir):
 
121
 
 
122
    def create_file(self, filename, content):
 
123
        f = open(filename, 'wb')
 
124
        try:
 
125
            f.write(content)
 
126
        finally:
 
127
            f.close()
 
128
 
 
129
    def _fancy_rename(self, a, b):
 
130
        osutils.fancy_rename(a, b, rename_func=os.rename,
 
131
                             unlink_func=os.unlink)
92
132
 
93
133
    def test_fancy_rename(self):
94
134
        # This should work everywhere
95
 
        def rename(a, b):
96
 
            osutils.fancy_rename(a, b,
97
 
                    rename_func=os.rename,
98
 
                    unlink_func=os.unlink)
99
 
 
100
 
        open('a', 'wb').write('something in a\n')
101
 
        rename('a', 'b')
102
 
        self.failIfExists('a')
103
 
        self.failUnlessExists('b')
 
135
        self.create_file('a', 'something in a\n')
 
136
        self._fancy_rename('a', 'b')
 
137
        self.assertPathDoesNotExist('a')
 
138
        self.assertPathExists('b')
104
139
        self.check_file_contents('b', 'something in a\n')
105
140
 
106
 
        open('a', 'wb').write('new something in a\n')
107
 
        rename('b', 'a')
 
141
        self.create_file('a', 'new something in a\n')
 
142
        self._fancy_rename('b', 'a')
108
143
 
109
144
        self.check_file_contents('a', 'something in a\n')
110
145
 
 
146
    def test_fancy_rename_fails_source_missing(self):
 
147
        # An exception should be raised, and the target should be left in place
 
148
        self.create_file('target', 'data in target\n')
 
149
        self.assertRaises((IOError, OSError), self._fancy_rename,
 
150
                          'missingsource', 'target')
 
151
        self.assertPathExists('target')
 
152
        self.check_file_contents('target', 'data in target\n')
 
153
 
 
154
    def test_fancy_rename_fails_if_source_and_target_missing(self):
 
155
        self.assertRaises((IOError, OSError), self._fancy_rename,
 
156
                          'missingsource', 'missingtarget')
 
157
 
111
158
    def test_rename(self):
112
159
        # Rename should be semi-atomic on all platforms
113
 
        open('a', 'wb').write('something in a\n')
 
160
        self.create_file('a', 'something in a\n')
114
161
        osutils.rename('a', 'b')
115
 
        self.failIfExists('a')
116
 
        self.failUnlessExists('b')
 
162
        self.assertPathDoesNotExist('a')
 
163
        self.assertPathExists('b')
117
164
        self.check_file_contents('b', 'something in a\n')
118
165
 
119
 
        open('a', 'wb').write('new something in a\n')
 
166
        self.create_file('a', 'new something in a\n')
120
167
        osutils.rename('b', 'a')
121
168
 
122
169
        self.check_file_contents('a', 'something in a\n')
133
180
        shape = sorted(os.listdir('.'))
134
181
        self.assertEquals(['A', 'B'], shape)
135
182
 
 
183
 
 
184
class TestRandChars(tests.TestCase):
 
185
 
136
186
    def test_01_rand_chars_empty(self):
137
187
        result = osutils.rand_chars(0)
138
188
        self.assertEqual(result, '')
143
193
        self.assertEqual(type(result), str)
144
194
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
145
195
 
 
196
 
 
197
class TestIsInside(tests.TestCase):
 
198
 
146
199
    def test_is_inside(self):
147
200
        is_inside = osutils.is_inside
148
201
        self.assertTrue(is_inside('src', 'src/foo.c'))
153
206
        self.assertTrue(is_inside('', 'foo.c'))
154
207
 
155
208
    def test_is_inside_any(self):
156
 
        SRC_FOO_C = pathjoin('src', 'foo.c')
 
209
        SRC_FOO_C = osutils.pathjoin('src', 'foo.c')
157
210
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
158
211
                         (['src'], SRC_FOO_C),
159
212
                         (['src'], 'src'),
160
213
                         ]:
161
 
            self.assert_(is_inside_any(dirs, fn))
 
214
            self.assert_(osutils.is_inside_any(dirs, fn))
162
215
        for dirs, fn in [(['src'], 'srccontrol'),
163
216
                         (['src'], 'srccontrol/foo')]:
164
 
            self.assertFalse(is_inside_any(dirs, fn))
 
217
            self.assertFalse(osutils.is_inside_any(dirs, fn))
165
218
 
166
219
    def test_is_inside_or_parent_of_any(self):
167
220
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
170
223
                         (['src/bar.c', 'bla/foo.c'], 'src'),
171
224
                         (['src'], 'src'),
172
225
                         ]:
173
 
            self.assert_(is_inside_or_parent_of_any(dirs, fn))
174
 
            
 
226
            self.assert_(osutils.is_inside_or_parent_of_any(dirs, fn))
 
227
 
175
228
        for dirs, fn in [(['src'], 'srccontrol'),
176
229
                         (['srccontrol/foo.c'], 'src'),
177
230
                         (['src'], 'srccontrol/foo')]:
178
 
            self.assertFalse(is_inside_or_parent_of_any(dirs, fn))
 
231
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
 
232
 
 
233
 
 
234
class TestLstat(tests.TestCaseInTempDir):
 
235
 
 
236
    def test_lstat_matches_fstat(self):
 
237
        # On Windows, lstat and fstat don't always agree, primarily in the
 
238
        # 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
 
239
        # custom implementation.
 
240
        if sys.platform == 'win32':
 
241
            # We only have special lstat/fstat if we have the extension.
 
242
            # Without it, we may end up re-reading content when we don't have
 
243
            # to, but otherwise it doesn't effect correctness.
 
244
            self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
245
        f = open('test-file.txt', 'wb')
 
246
        self.addCleanup(f.close)
 
247
        f.write('some content\n')
 
248
        f.flush()
 
249
        self.assertEqualStat(osutils.fstat(f.fileno()),
 
250
                             osutils.lstat('test-file.txt'))
 
251
 
 
252
 
 
253
class TestRmTree(tests.TestCaseInTempDir):
179
254
 
180
255
    def test_rmtree(self):
181
256
        # Check to remove tree with read-only files/dirs
192
267
 
193
268
        osutils.rmtree('dir')
194
269
 
195
 
        self.failIfExists('dir/file')
196
 
        self.failIfExists('dir')
 
270
        self.assertPathDoesNotExist('dir/file')
 
271
        self.assertPathDoesNotExist('dir')
 
272
 
 
273
 
 
274
class TestDeleteAny(tests.TestCaseInTempDir):
 
275
 
 
276
    def test_delete_any_readonly(self):
 
277
        # from <https://bugs.launchpad.net/bzr/+bug/218206>
 
278
        self.build_tree(['d/', 'f'])
 
279
        osutils.make_readonly('d')
 
280
        osutils.make_readonly('f')
 
281
 
 
282
        osutils.delete_any('f')
 
283
        osutils.delete_any('d')
 
284
 
 
285
 
 
286
class TestKind(tests.TestCaseInTempDir):
197
287
 
198
288
    def test_file_kind(self):
199
289
        self.build_tree(['file', 'dir/'])
202
292
        if osutils.has_symlinks():
203
293
            os.symlink('symlink', 'symlink')
204
294
            self.assertEquals('symlink', osutils.file_kind('symlink'))
205
 
        
 
295
 
206
296
        # TODO: jam 20060529 Test a block device
207
297
        try:
208
298
            os.lstat('/dev/null')
230
320
                os.remove('socket')
231
321
 
232
322
    def test_kind_marker(self):
233
 
        self.assertEqual(osutils.kind_marker('file'), '')
234
 
        self.assertEqual(osutils.kind_marker('directory'), '/')
235
 
        self.assertEqual(osutils.kind_marker('symlink'), '@')
236
 
        self.assertEqual(osutils.kind_marker('tree-reference'), '+')
 
323
        self.assertEqual("", osutils.kind_marker("file"))
 
324
        self.assertEqual("/", osutils.kind_marker('directory'))
 
325
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
 
326
        self.assertEqual("@", osutils.kind_marker("symlink"))
 
327
        self.assertEqual("+", osutils.kind_marker("tree-reference"))
 
328
        self.assertEqual("", osutils.kind_marker("fifo"))
 
329
        self.assertEqual("", osutils.kind_marker("socket"))
 
330
        self.assertEqual("", osutils.kind_marker("unknown"))
 
331
 
 
332
 
 
333
class TestUmask(tests.TestCaseInTempDir):
237
334
 
238
335
    def test_get_umask(self):
239
336
        if sys.platform == 'win32':
242
339
            return
243
340
 
244
341
        orig_umask = osutils.get_umask()
245
 
        try:
246
 
            os.umask(0222)
247
 
            self.assertEqual(0222, osutils.get_umask())
248
 
            os.umask(0022)
249
 
            self.assertEqual(0022, osutils.get_umask())
250
 
            os.umask(0002)
251
 
            self.assertEqual(0002, osutils.get_umask())
252
 
            os.umask(0027)
253
 
            self.assertEqual(0027, osutils.get_umask())
254
 
        finally:
255
 
            os.umask(orig_umask)
 
342
        self.addCleanup(os.umask, orig_umask)
 
343
        os.umask(0222)
 
344
        self.assertEqual(0222, osutils.get_umask())
 
345
        os.umask(0022)
 
346
        self.assertEqual(0022, osutils.get_umask())
 
347
        os.umask(0002)
 
348
        self.assertEqual(0002, osutils.get_umask())
 
349
        os.umask(0027)
 
350
        self.assertEqual(0027, osutils.get_umask())
 
351
 
 
352
 
 
353
class TestDateTime(tests.TestCase):
256
354
 
257
355
    def assertFormatedDelta(self, expected, seconds):
258
356
        """Assert osutils.format_delta formats as expected"""
300
398
        # Instead blackbox.test_locale should check for localized
301
399
        # dates once they do occur in output strings.
302
400
 
 
401
    def test_format_date_with_offset_in_original_timezone(self):
 
402
        self.assertEqual("Thu 1970-01-01 00:00:00 +0000",
 
403
            osutils.format_date_with_offset_in_original_timezone(0))
 
404
        self.assertEqual("Fri 1970-01-02 03:46:40 +0000",
 
405
            osutils.format_date_with_offset_in_original_timezone(100000))
 
406
        self.assertEqual("Fri 1970-01-02 05:46:40 +0200",
 
407
            osutils.format_date_with_offset_in_original_timezone(100000, 7200))
 
408
 
 
409
    def test_local_time_offset(self):
 
410
        """Test that local_time_offset() returns a sane value."""
 
411
        offset = osutils.local_time_offset()
 
412
        self.assertTrue(isinstance(offset, int))
 
413
        # Test that the offset is no more than a eighteen hours in
 
414
        # either direction.
 
415
        # Time zone handling is system specific, so it is difficult to
 
416
        # do more specific tests, but a value outside of this range is
 
417
        # probably wrong.
 
418
        eighteen_hours = 18 * 3600
 
419
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
420
 
 
421
    def test_local_time_offset_with_timestamp(self):
 
422
        """Test that local_time_offset() works with a timestamp."""
 
423
        offset = osutils.local_time_offset(1000000000.1234567)
 
424
        self.assertTrue(isinstance(offset, int))
 
425
        eighteen_hours = 18 * 3600
 
426
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
427
 
 
428
 
 
429
class TestLinks(tests.TestCaseInTempDir):
 
430
 
303
431
    def test_dereference_path(self):
304
 
        self.requireFeature(SymlinkFeature)
 
432
        self.requireFeature(features.SymlinkFeature)
305
433
        cwd = osutils.realpath('.')
306
434
        os.mkdir('bar')
307
435
        bar_path = osutils.pathjoin(cwd, 'bar')
310
438
        self.assertEqual(bar_path, osutils.realpath('./bar'))
311
439
        os.symlink('bar', 'foo')
312
440
        self.assertEqual(bar_path, osutils.realpath('./foo'))
313
 
        
 
441
 
314
442
        # Does not dereference terminal symlinks
315
443
        foo_path = osutils.pathjoin(cwd, 'foo')
316
444
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
348
476
            osutils.make_readonly('dangling')
349
477
            osutils.make_writable('dangling')
350
478
 
351
 
    def test_kind_marker(self):
352
 
        self.assertEqual("", osutils.kind_marker("file"))
353
 
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
354
 
        self.assertEqual("@", osutils.kind_marker("symlink"))
355
 
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
356
 
 
357
479
    def test_host_os_dereferences_symlinks(self):
358
480
        osutils.host_os_dereferences_symlinks()
359
481
 
360
482
 
361
 
class TestCanonicalRelPath(TestCaseInTempDir):
 
483
class TestCanonicalRelPath(tests.TestCaseInTempDir):
362
484
 
363
 
    _test_needs_features = [CaseInsCasePresFilenameFeature]
 
485
    _test_needs_features = [features.CaseInsCasePresFilenameFeature]
364
486
 
365
487
    def test_canonical_relpath_simple(self):
366
488
        f = file('MixedCaseName', 'w')
367
489
        f.close()
368
 
        self.failUnlessEqual(
369
 
            canonical_relpath(self.test_base_dir, 'mixedcasename'),
370
 
            'work/MixedCaseName')
 
490
        actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
 
491
        self.assertEqual('work/MixedCaseName', actual)
371
492
 
372
493
    def test_canonical_relpath_missing_tail(self):
373
494
        os.mkdir('MixedCaseParent')
374
 
        self.failUnlessEqual(
375
 
            canonical_relpath(self.test_base_dir, 'mixedcaseparent/nochild'),
376
 
            'work/MixedCaseParent/nochild')
377
 
 
378
 
 
379
 
class TestPumpFile(TestCase):
 
495
        actual = osutils.canonical_relpath(self.test_base_dir,
 
496
                                           'mixedcaseparent/nochild')
 
497
        self.assertEqual('work/MixedCaseParent/nochild', actual)
 
498
 
 
499
 
 
500
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
 
501
 
 
502
    def assertRelpath(self, expected, base, path):
 
503
        actual = osutils._cicp_canonical_relpath(base, path)
 
504
        self.assertEqual(expected, actual)
 
505
 
 
506
    def test_simple(self):
 
507
        self.build_tree(['MixedCaseName'])
 
508
        base = osutils.realpath(self.get_transport('.').local_abspath('.'))
 
509
        self.assertRelpath('MixedCaseName', base, 'mixedcAsename')
 
510
 
 
511
    def test_subdir_missing_tail(self):
 
512
        self.build_tree(['MixedCaseParent/', 'MixedCaseParent/a_child'])
 
513
        base = osutils.realpath(self.get_transport('.').local_abspath('.'))
 
514
        self.assertRelpath('MixedCaseParent/a_child', base,
 
515
                           'MixedCaseParent/a_child')
 
516
        self.assertRelpath('MixedCaseParent/a_child', base,
 
517
                           'MixedCaseParent/A_Child')
 
518
        self.assertRelpath('MixedCaseParent/not_child', base,
 
519
                           'MixedCaseParent/not_child')
 
520
 
 
521
    def test_at_root_slash(self):
 
522
        # We can't test this on Windows, because it has a 'MIN_ABS_PATHLENGTH'
 
523
        # check...
 
524
        if osutils.MIN_ABS_PATHLENGTH > 1:
 
525
            raise tests.TestSkipped('relpath requires %d chars'
 
526
                                    % osutils.MIN_ABS_PATHLENGTH)
 
527
        self.assertRelpath('foo', '/', '/foo')
 
528
 
 
529
    def test_at_root_drive(self):
 
530
        if sys.platform != 'win32':
 
531
            raise tests.TestNotApplicable('we can only test drive-letter relative'
 
532
                                          ' paths on Windows where we have drive'
 
533
                                          ' letters.')
 
534
        # see bug #322807
 
535
        # The specific issue is that when at the root of a drive, 'abspath'
 
536
        # returns "C:/" or just "/". However, the code assumes that abspath
 
537
        # always returns something like "C:/foo" or "/foo" (no trailing slash).
 
538
        self.assertRelpath('foo', 'C:/', 'C:/foo')
 
539
        self.assertRelpath('foo', 'X:/', 'X:/foo')
 
540
        self.assertRelpath('foo', 'X:/', 'X://foo')
 
541
 
 
542
 
 
543
class TestPumpFile(tests.TestCase):
380
544
    """Test pumpfile method."""
 
545
 
381
546
    def setUp(self):
 
547
        tests.TestCase.setUp(self)
382
548
        # create a test datablock
383
549
        self.block_size = 512
384
550
        pattern = '0123456789ABCDEF'
391
557
        # make sure test data is larger than max read size
392
558
        self.assertTrue(self.test_data_len > self.block_size)
393
559
 
394
 
        from_file = FakeReadFile(self.test_data)
 
560
        from_file = file_utils.FakeReadFile(self.test_data)
395
561
        to_file = StringIO()
396
562
 
397
563
        # read (max / 2) bytes and verify read size wasn't affected
398
564
        num_bytes_to_read = self.block_size / 2
399
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
565
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
400
566
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
401
567
        self.assertEqual(from_file.get_read_count(), 1)
402
568
 
403
569
        # read (max) bytes and verify read size wasn't affected
404
570
        num_bytes_to_read = self.block_size
405
571
        from_file.reset_read_count()
406
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
572
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
407
573
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
408
574
        self.assertEqual(from_file.get_read_count(), 1)
409
575
 
410
576
        # read (max + 1) bytes and verify read size was limited
411
577
        num_bytes_to_read = self.block_size + 1
412
578
        from_file.reset_read_count()
413
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
579
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
414
580
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
415
581
        self.assertEqual(from_file.get_read_count(), 2)
416
582
 
417
583
        # finish reading the rest of the data
418
584
        num_bytes_to_read = self.test_data_len - to_file.tell()
419
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
585
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
420
586
 
421
587
        # report error if the data wasn't equal (we only report the size due
422
588
        # to the length of the data)
432
598
        self.assertTrue(self.test_data_len > self.block_size)
433
599
 
434
600
        # retrieve data in blocks
435
 
        from_file = FakeReadFile(self.test_data)
 
601
        from_file = file_utils.FakeReadFile(self.test_data)
436
602
        to_file = StringIO()
437
 
        pumpfile(from_file, to_file, self.test_data_len, self.block_size)
 
603
        osutils.pumpfile(from_file, to_file, self.test_data_len,
 
604
                         self.block_size)
438
605
 
439
606
        # verify read size was equal to the maximum read size
440
607
        self.assertTrue(from_file.get_max_read_size() > 0)
455
622
        self.assertTrue(self.test_data_len > self.block_size)
456
623
 
457
624
        # retrieve data to EOF
458
 
        from_file = FakeReadFile(self.test_data)
 
625
        from_file = file_utils.FakeReadFile(self.test_data)
459
626
        to_file = StringIO()
460
 
        pumpfile(from_file, to_file, -1, self.block_size)
 
627
        osutils.pumpfile(from_file, to_file, -1, self.block_size)
461
628
 
462
629
        # verify read size was equal to the maximum read size
463
630
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
475
642
        test verifies that any existing usages of pumpfile will not be broken
476
643
        with this new version."""
477
644
        # retrieve data using default (old) pumpfile method
478
 
        from_file = FakeReadFile(self.test_data)
 
645
        from_file = file_utils.FakeReadFile(self.test_data)
479
646
        to_file = StringIO()
480
 
        pumpfile(from_file, to_file)
 
647
        osutils.pumpfile(from_file, to_file)
481
648
 
482
649
        # report error if the data wasn't equal (we only report the size due
483
650
        # to the length of the data)
492
659
            activity.append((length, direction))
493
660
        from_file = StringIO(self.test_data)
494
661
        to_file = StringIO()
495
 
        pumpfile(from_file, to_file, buff_size=500,
496
 
                 report_activity=log_activity, direction='read')
 
662
        osutils.pumpfile(from_file, to_file, buff_size=500,
 
663
                         report_activity=log_activity, direction='read')
497
664
        self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
498
665
                          (36, 'read')], activity)
499
666
 
500
667
        from_file = StringIO(self.test_data)
501
668
        to_file = StringIO()
502
669
        del activity[:]
503
 
        pumpfile(from_file, to_file, buff_size=500,
504
 
                 report_activity=log_activity, direction='write')
 
670
        osutils.pumpfile(from_file, to_file, buff_size=500,
 
671
                         report_activity=log_activity, direction='write')
505
672
        self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
506
673
                          (36, 'write')], activity)
507
674
 
509
676
        from_file = StringIO(self.test_data)
510
677
        to_file = StringIO()
511
678
        del activity[:]
512
 
        pumpfile(from_file, to_file, buff_size=500, read_length=1028,
513
 
                 report_activity=log_activity, direction='read')
 
679
        osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
 
680
                         report_activity=log_activity, direction='read')
514
681
        self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
515
682
 
516
683
 
517
684
 
518
 
class TestPumpStringFile(TestCase):
 
685
class TestPumpStringFile(tests.TestCase):
519
686
 
520
687
    def test_empty(self):
521
688
        output = StringIO()
522
 
        pump_string_file("", output)
 
689
        osutils.pump_string_file("", output)
523
690
        self.assertEqual("", output.getvalue())
524
691
 
525
692
    def test_more_than_segment_size(self):
526
693
        output = StringIO()
527
 
        pump_string_file("123456789", output, 2)
 
694
        osutils.pump_string_file("123456789", output, 2)
528
695
        self.assertEqual("123456789", output.getvalue())
529
696
 
530
697
    def test_segment_size(self):
531
698
        output = StringIO()
532
 
        pump_string_file("12", output, 2)
 
699
        osutils.pump_string_file("12", output, 2)
533
700
        self.assertEqual("12", output.getvalue())
534
701
 
535
702
    def test_segment_size_multiple(self):
536
703
        output = StringIO()
537
 
        pump_string_file("1234", output, 2)
 
704
        osutils.pump_string_file("1234", output, 2)
538
705
        self.assertEqual("1234", output.getvalue())
539
706
 
540
707
 
541
 
class TestSafeUnicode(TestCase):
 
708
class TestRelpath(tests.TestCase):
 
709
 
 
710
    def test_simple_relpath(self):
 
711
        cwd = osutils.getcwd()
 
712
        subdir = cwd + '/subdir'
 
713
        self.assertEqual('subdir', osutils.relpath(cwd, subdir))
 
714
 
 
715
    def test_deep_relpath(self):
 
716
        cwd = osutils.getcwd()
 
717
        subdir = cwd + '/sub/subsubdir'
 
718
        self.assertEqual('sub/subsubdir', osutils.relpath(cwd, subdir))
 
719
 
 
720
    def test_not_relative(self):
 
721
        self.assertRaises(errors.PathNotChild,
 
722
                          osutils.relpath, 'C:/path', 'H:/path')
 
723
        self.assertRaises(errors.PathNotChild,
 
724
                          osutils.relpath, 'C:/', 'H:/path')
 
725
 
 
726
 
 
727
class TestSafeUnicode(tests.TestCase):
542
728
 
543
729
    def test_from_ascii_string(self):
544
730
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
553
739
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
554
740
 
555
741
    def test_bad_utf8_string(self):
556
 
        self.assertRaises(BzrBadParameterNotUnicode,
 
742
        self.assertRaises(errors.BzrBadParameterNotUnicode,
557
743
                          osutils.safe_unicode,
558
744
                          '\xbb\xbb')
559
745
 
560
746
 
561
 
class TestSafeUtf8(TestCase):
 
747
class TestSafeUtf8(tests.TestCase):
562
748
 
563
749
    def test_from_ascii_string(self):
564
750
        f = 'foobar'
574
760
        self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
575
761
 
576
762
    def test_bad_utf8_string(self):
577
 
        self.assertRaises(BzrBadParameterNotUnicode,
 
763
        self.assertRaises(errors.BzrBadParameterNotUnicode,
578
764
                          osutils.safe_utf8, '\xbb\xbb')
579
765
 
580
766
 
581
 
class TestSafeRevisionId(TestCase):
 
767
class TestSafeRevisionId(tests.TestCase):
582
768
 
583
769
    def test_from_ascii_string(self):
584
770
        # this shouldn't give a warning because it's getting an ascii string
606
792
        self.assertEqual(None, osutils.safe_revision_id(None))
607
793
 
608
794
 
609
 
class TestSafeFileId(TestCase):
 
795
class TestSafeFileId(tests.TestCase):
610
796
 
611
797
    def test_from_ascii_string(self):
612
798
        self.assertEqual('foobar', osutils.safe_file_id('foobar'))
632
818
        self.assertEqual(None, osutils.safe_file_id(None))
633
819
 
634
820
 
635
 
class TestWin32Funcs(TestCase):
636
 
    """Test that the _win32 versions of os utilities return appropriate paths."""
 
821
class TestWin32Funcs(tests.TestCase):
 
822
    """Test that _win32 versions of os utilities return appropriate paths."""
637
823
 
638
824
    def test_abspath(self):
639
825
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
646
832
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
647
833
 
648
834
    def test_pathjoin(self):
649
 
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
650
 
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
651
 
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
652
 
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
653
 
        self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
654
 
        self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
 
835
        self.assertEqual('path/to/foo',
 
836
                         osutils._win32_pathjoin('path', 'to', 'foo'))
 
837
        self.assertEqual('C:/foo',
 
838
                         osutils._win32_pathjoin('path\\to', 'C:\\foo'))
 
839
        self.assertEqual('C:/foo',
 
840
                         osutils._win32_pathjoin('path/to', 'C:/foo'))
 
841
        self.assertEqual('path/to/foo',
 
842
                         osutils._win32_pathjoin('path/to/', 'foo'))
 
843
        self.assertEqual('/foo',
 
844
                         osutils._win32_pathjoin('C:/path/to/', '/foo'))
 
845
        self.assertEqual('/foo',
 
846
                         osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
655
847
 
656
848
    def test_normpath(self):
657
 
        self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
658
 
        self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
 
849
        self.assertEqual('path/to/foo',
 
850
                         osutils._win32_normpath(r'path\\from\..\to\.\foo'))
 
851
        self.assertEqual('path/to/foo',
 
852
                         osutils._win32_normpath('path//from/../to/./foo'))
659
853
 
660
854
    def test_getcwd(self):
661
855
        cwd = osutils._win32_getcwd()
682
876
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
683
877
        # relative path
684
878
        cwd = osutils.getcwd().rstrip('/')
685
 
        drive = osutils._nt_splitdrive(cwd)[0]
 
879
        drive = osutils.ntpath.splitdrive(cwd)[0]
686
880
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
687
881
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
688
882
        # unicode path
690
884
        self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
691
885
 
692
886
 
693
 
class TestWin32FuncsDirs(TestCaseInTempDir):
 
887
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
694
888
    """Test win32 functions that create files."""
695
 
    
 
889
 
696
890
    def test_getcwd(self):
697
 
        if win32utils.winver == 'Windows 98':
698
 
            raise TestSkipped('Windows 98 cannot handle unicode filenames')
699
 
        # Make sure getcwd can handle unicode filenames
700
 
        try:
701
 
            os.mkdir(u'mu-\xb5')
702
 
        except UnicodeError:
703
 
            raise TestSkipped("Unable to create Unicode filename")
704
 
 
 
891
        self.requireFeature(features.UnicodeFilenameFeature)
 
892
        os.mkdir(u'mu-\xb5')
705
893
        os.chdir(u'mu-\xb5')
706
894
        # TODO: jam 20060427 This will probably fail on Mac OSX because
707
895
        #       it will change the normalization of B\xe5gfors
712
900
    def test_minimum_path_selection(self):
713
901
        self.assertEqual(set(),
714
902
            osutils.minimum_path_selection([]))
 
903
        self.assertEqual(set(['a']),
 
904
            osutils.minimum_path_selection(['a']))
715
905
        self.assertEqual(set(['a', 'b']),
716
906
            osutils.minimum_path_selection(['a', 'b']))
717
907
        self.assertEqual(set(['a/', 'b']),
718
908
            osutils.minimum_path_selection(['a/', 'b']))
719
909
        self.assertEqual(set(['a/', 'b']),
720
910
            osutils.minimum_path_selection(['a/c', 'a/', 'b']))
 
911
        self.assertEqual(set(['a-b', 'a', 'a0b']),
 
912
            osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
721
913
 
722
914
    def test_mkdtemp(self):
723
915
        tmpdir = osutils._win32_mkdtemp(dir='.')
732
924
        b.close()
733
925
 
734
926
        osutils._win32_rename('b', 'a')
735
 
        self.failUnlessExists('a')
736
 
        self.failIfExists('b')
 
927
        self.assertPathExists('a')
 
928
        self.assertPathDoesNotExist('b')
737
929
        self.assertFileEqual('baz\n', 'a')
738
930
 
739
931
    def test_rename_missing_file(self):
779
971
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
780
972
 
781
973
 
782
 
class TestMacFuncsDirs(TestCaseInTempDir):
 
974
class TestParentDirectories(tests.TestCaseInTempDir):
 
975
    """Test osutils.parent_directories()"""
 
976
 
 
977
    def test_parent_directories(self):
 
978
        self.assertEqual([], osutils.parent_directories('a'))
 
979
        self.assertEqual(['a'], osutils.parent_directories('a/b'))
 
980
        self.assertEqual(['a/b', 'a'], osutils.parent_directories('a/b/c'))
 
981
 
 
982
 
 
983
class TestMacFuncsDirs(tests.TestCaseInTempDir):
783
984
    """Test mac special functions that require directories."""
784
985
 
785
986
    def test_getcwd(self):
786
 
        # On Mac, this will actually create Ba\u030agfors
787
 
        # but chdir will still work, because it accepts both paths
788
 
        try:
789
 
            os.mkdir(u'B\xe5gfors')
790
 
        except UnicodeError:
791
 
            raise TestSkipped("Unable to create Unicode filename")
792
 
 
 
987
        self.requireFeature(features.UnicodeFilenameFeature)
 
988
        os.mkdir(u'B\xe5gfors')
793
989
        os.chdir(u'B\xe5gfors')
794
990
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
795
991
 
796
992
    def test_getcwd_nonnorm(self):
 
993
        self.requireFeature(features.UnicodeFilenameFeature)
797
994
        # Test that _mac_getcwd() will normalize this path
798
 
        try:
799
 
            os.mkdir(u'Ba\u030agfors')
800
 
        except UnicodeError:
801
 
            raise TestSkipped("Unable to create Unicode filename")
802
 
 
 
995
        os.mkdir(u'Ba\u030agfors')
803
996
        os.chdir(u'Ba\u030agfors')
804
997
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
805
998
 
806
999
 
807
 
class TestChunksToLines(TestCase):
 
1000
class TestChunksToLines(tests.TestCase):
808
1001
 
809
1002
    def test_smoketest(self):
810
1003
        self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
814
1007
 
815
1008
    def test_osutils_binding(self):
816
1009
        from bzrlib.tests import test__chunks_to_lines
817
 
        if test__chunks_to_lines.CompiledChunksToLinesFeature.available():
 
1010
        if test__chunks_to_lines.compiled_chunkstolines_feature.available():
818
1011
            from bzrlib._chunks_to_lines_pyx import chunks_to_lines
819
1012
        else:
820
1013
            from bzrlib._chunks_to_lines_py import chunks_to_lines
821
1014
        self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
822
1015
 
823
1016
 
824
 
class TestSplitLines(TestCase):
 
1017
class TestSplitLines(tests.TestCase):
825
1018
 
826
1019
    def test_split_unicode(self):
827
1020
        self.assertEqual([u'foo\n', u'bar\xae'],
834
1027
                         osutils.split_lines('foo\rbar\n'))
835
1028
 
836
1029
 
837
 
class TestWalkDirs(TestCaseInTempDir):
 
1030
class TestWalkDirs(tests.TestCaseInTempDir):
 
1031
 
 
1032
    def assertExpectedBlocks(self, expected, result):
 
1033
        self.assertEqual(expected,
 
1034
                         [(dirinfo, [line[0:3] for line in block])
 
1035
                          for dirinfo, block in result])
838
1036
 
839
1037
    def test_walkdirs(self):
840
1038
        tree = [
873
1071
            result.append((dirdetail, dirblock))
874
1072
 
875
1073
        self.assertTrue(found_bzrdir)
876
 
        self.assertEqual(expected_dirblocks,
877
 
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
 
1074
        self.assertExpectedBlocks(expected_dirblocks, result)
878
1075
        # you can search a subdir only, with a supplied prefix.
879
1076
        result = []
880
1077
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
881
1078
            result.append(dirblock)
882
 
        self.assertEqual(expected_dirblocks[1:],
883
 
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
 
1079
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
 
1080
 
 
1081
    def test_walkdirs_os_error(self):
 
1082
        # <https://bugs.launchpad.net/bzr/+bug/338653>
 
1083
        # Pyrex readdir didn't raise useful messages if it had an error
 
1084
        # reading the directory
 
1085
        if sys.platform == 'win32':
 
1086
            raise tests.TestNotApplicable(
 
1087
                "readdir IOError not tested on win32")
 
1088
        self.requireFeature(features.not_running_as_root)
 
1089
        os.mkdir("test-unreadable")
 
1090
        os.chmod("test-unreadable", 0000)
 
1091
        # must chmod it back so that it can be removed
 
1092
        self.addCleanup(os.chmod, "test-unreadable", 0700)
 
1093
        # The error is not raised until the generator is actually evaluated.
 
1094
        # (It would be ok if it happened earlier but at the moment it
 
1095
        # doesn't.)
 
1096
        e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
 
1097
        self.assertEquals('./test-unreadable', e.filename)
 
1098
        self.assertEquals(errno.EACCES, e.errno)
 
1099
        # Ensure the message contains the file name
 
1100
        self.assertContainsRe(str(e), "\./test-unreadable")
 
1101
 
 
1102
 
 
1103
    def test_walkdirs_encoding_error(self):
 
1104
        # <https://bugs.launchpad.net/bzr/+bug/488519>
 
1105
        # walkdirs didn't raise a useful message when the filenames
 
1106
        # are not using the filesystem's encoding
 
1107
 
 
1108
        # require a bytestring based filesystem
 
1109
        self.requireFeature(features.ByteStringNamedFilesystem)
 
1110
 
 
1111
        tree = [
 
1112
            '.bzr',
 
1113
            '0file',
 
1114
            '1dir/',
 
1115
            '1dir/0file',
 
1116
            '1dir/1dir/',
 
1117
            '1file'
 
1118
            ]
 
1119
 
 
1120
        self.build_tree(tree)
 
1121
 
 
1122
        # rename the 1file to a latin-1 filename
 
1123
        os.rename("./1file", "\xe8file")
 
1124
        if "\xe8file" not in os.listdir("."):
 
1125
            self.skip("Lack filesystem that preserves arbitrary bytes")
 
1126
 
 
1127
        self._save_platform_info()
 
1128
        win32utils.winver = None # Avoid the win32 detection code
 
1129
        osutils._fs_enc = 'UTF-8'
 
1130
 
 
1131
        # this should raise on error
 
1132
        def attempt():
 
1133
            for dirdetail, dirblock in osutils.walkdirs('.'):
 
1134
                pass
 
1135
 
 
1136
        self.assertRaises(errors.BadFilenameEncoding, attempt)
884
1137
 
885
1138
    def test__walkdirs_utf8(self):
886
1139
        tree = [
919
1172
            result.append((dirdetail, dirblock))
920
1173
 
921
1174
        self.assertTrue(found_bzrdir)
922
 
        self.assertEqual(expected_dirblocks,
923
 
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
 
1175
        self.assertExpectedBlocks(expected_dirblocks, result)
 
1176
 
924
1177
        # you can search a subdir only, with a supplied prefix.
925
1178
        result = []
926
1179
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
927
1180
            result.append(dirblock)
928
 
        self.assertEqual(expected_dirblocks[1:],
929
 
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
 
1181
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
930
1182
 
931
1183
    def _filter_out_stat(self, result):
932
1184
        """Filter out the stat value from the walkdirs result"""
938
1190
            dirblock[:] = new_dirblock
939
1191
 
940
1192
    def _save_platform_info(self):
941
 
        cur_winver = win32utils.winver
942
 
        cur_fs_enc = osutils._fs_enc
943
 
        cur_dir_reader = osutils._selected_dir_reader
944
 
        def restore():
945
 
            win32utils.winver = cur_winver
946
 
            osutils._fs_enc = cur_fs_enc
947
 
            osutils._selected_dir_reader = cur_dir_reader
948
 
        self.addCleanup(restore)
 
1193
        self.overrideAttr(win32utils, 'winver')
 
1194
        self.overrideAttr(osutils, '_fs_enc')
 
1195
        self.overrideAttr(osutils, '_selected_dir_reader')
949
1196
 
950
 
    def assertReadFSDirIs(self, expected):
 
1197
    def assertDirReaderIs(self, expected):
951
1198
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
952
1199
        # Force it to redetect
953
1200
        osutils._selected_dir_reader = None
960
1207
        self._save_platform_info()
961
1208
        win32utils.winver = None # Avoid the win32 detection code
962
1209
        osutils._fs_enc = 'UTF-8'
963
 
        self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
 
1210
        self.assertDirReaderIs(
 
1211
            UTF8DirReaderFeature.module.UTF8DirReader)
964
1212
 
965
1213
    def test_force_walkdirs_utf8_fs_ascii(self):
966
1214
        self.requireFeature(UTF8DirReaderFeature)
967
1215
        self._save_platform_info()
968
1216
        win32utils.winver = None # Avoid the win32 detection code
969
1217
        osutils._fs_enc = 'US-ASCII'
970
 
        self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
 
1218
        self.assertDirReaderIs(
 
1219
            UTF8DirReaderFeature.module.UTF8DirReader)
971
1220
 
972
1221
    def test_force_walkdirs_utf8_fs_ANSI(self):
973
1222
        self.requireFeature(UTF8DirReaderFeature)
974
1223
        self._save_platform_info()
975
1224
        win32utils.winver = None # Avoid the win32 detection code
976
1225
        osutils._fs_enc = 'ANSI_X3.4-1968'
977
 
        self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
 
1226
        self.assertDirReaderIs(
 
1227
            UTF8DirReaderFeature.module.UTF8DirReader)
978
1228
 
979
1229
    def test_force_walkdirs_utf8_fs_latin1(self):
980
1230
        self._save_platform_info()
981
1231
        win32utils.winver = None # Avoid the win32 detection code
982
1232
        osutils._fs_enc = 'latin1'
983
 
        self.assertReadFSDirIs(osutils.UnicodeDirReader)
 
1233
        self.assertDirReaderIs(osutils.UnicodeDirReader)
984
1234
 
985
1235
    def test_force_walkdirs_utf8_nt(self):
986
1236
        # Disabled because the thunk of the whole walkdirs api is disabled.
987
 
        self.requireFeature(Win32ReadDirFeature)
 
1237
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
988
1238
        self._save_platform_info()
989
1239
        win32utils.winver = 'Windows NT'
990
1240
        from bzrlib._walkdirs_win32 import Win32ReadDir
991
 
        self.assertReadFSDirIs(Win32ReadDir)
 
1241
        self.assertDirReaderIs(Win32ReadDir)
992
1242
 
993
1243
    def test_force_walkdirs_utf8_98(self):
994
 
        self.requireFeature(Win32ReadDirFeature)
 
1244
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
995
1245
        self._save_platform_info()
996
1246
        win32utils.winver = 'Windows 98'
997
 
        self.assertReadFSDirIs(osutils.UnicodeDirReader)
 
1247
        self.assertDirReaderIs(osutils.UnicodeDirReader)
998
1248
 
999
1249
    def test_unicode_walkdirs(self):
1000
1250
        """Walkdirs should always return unicode paths."""
 
1251
        self.requireFeature(features.UnicodeFilenameFeature)
1001
1252
        name0 = u'0file-\xb6'
1002
1253
        name1 = u'1dir-\u062c\u0648'
1003
1254
        name2 = u'2file-\u0633'
1008
1259
            name1 + '/' + name1 + '/',
1009
1260
            name2,
1010
1261
            ]
1011
 
        try:
1012
 
            self.build_tree(tree)
1013
 
        except UnicodeError:
1014
 
            raise TestSkipped('Could not represent Unicode chars'
1015
 
                              ' in current encoding.')
 
1262
        self.build_tree(tree)
1016
1263
        expected_dirblocks = [
1017
1264
                ((u'', u'.'),
1018
1265
                 [(name0, name0, 'file', './' + name0),
1044
1291
 
1045
1292
        The abspath portion might be in unicode or utf-8
1046
1293
        """
 
1294
        self.requireFeature(features.UnicodeFilenameFeature)
1047
1295
        name0 = u'0file-\xb6'
1048
1296
        name1 = u'1dir-\u062c\u0648'
1049
1297
        name2 = u'2file-\u0633'
1054
1302
            name1 + '/' + name1 + '/',
1055
1303
            name2,
1056
1304
            ]
1057
 
        try:
1058
 
            self.build_tree(tree)
1059
 
        except UnicodeError:
1060
 
            raise TestSkipped('Could not represent Unicode chars'
1061
 
                              ' in current encoding.')
 
1305
        self.build_tree(tree)
1062
1306
        name0 = name0.encode('utf8')
1063
1307
        name1 = name1.encode('utf8')
1064
1308
        name2 = name2.encode('utf8')
1108
1352
 
1109
1353
        The abspath portion should be in unicode
1110
1354
        """
 
1355
        self.requireFeature(features.UnicodeFilenameFeature)
1111
1356
        # Use the unicode reader. TODO: split into driver-and-driven unit
1112
1357
        # tests.
1113
1358
        self._save_platform_info()
1122
1367
            name1u + '/' + name1u + '/',
1123
1368
            name2u,
1124
1369
            ]
1125
 
        try:
1126
 
            self.build_tree(tree)
1127
 
        except UnicodeError:
1128
 
            raise TestSkipped('Could not represent Unicode chars'
1129
 
                              ' in current encoding.')
 
1370
        self.build_tree(tree)
1130
1371
        name0 = name0u.encode('utf8')
1131
1372
        name1 = name1u.encode('utf8')
1132
1373
        name2 = name2u.encode('utf8')
1157
1398
        self.assertEqual(expected_dirblocks, result)
1158
1399
 
1159
1400
    def test__walkdirs_utf8_win32readdir(self):
1160
 
        self.requireFeature(Win32ReadDirFeature)
1161
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1401
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1402
        self.requireFeature(features.UnicodeFilenameFeature)
1162
1403
        from bzrlib._walkdirs_win32 import Win32ReadDir
1163
1404
        self._save_platform_info()
1164
1405
        osutils._selected_dir_reader = Win32ReadDir()
1214
1455
 
1215
1456
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1216
1457
        """make sure our Stat values are valid"""
1217
 
        self.requireFeature(Win32ReadDirFeature)
1218
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1458
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1459
        self.requireFeature(features.UnicodeFilenameFeature)
1219
1460
        from bzrlib._walkdirs_win32 import Win32ReadDir
1220
1461
        name0u = u'0file-\xb6'
1221
1462
        name0 = name0u.encode('utf8')
1238
1479
 
1239
1480
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1240
1481
        """make sure our Stat values are valid"""
1241
 
        self.requireFeature(Win32ReadDirFeature)
1242
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1482
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1483
        self.requireFeature(features.UnicodeFilenameFeature)
1243
1484
        from bzrlib._walkdirs_win32 import Win32ReadDir
1244
1485
        name0u = u'0dir-\u062c\u0648'
1245
1486
        name0 = name0u.encode('utf8')
1329
1570
            sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
1330
1571
 
1331
1572
 
1332
 
class TestCopyTree(TestCaseInTempDir):
1333
 
    
 
1573
class TestCopyTree(tests.TestCaseInTempDir):
 
1574
 
1334
1575
    def test_copy_basic_tree(self):
1335
1576
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1336
1577
        osutils.copy_tree('source', 'target')
1345
1586
        self.assertEqual(['c'], os.listdir('target/b'))
1346
1587
 
1347
1588
    def test_copy_tree_symlinks(self):
1348
 
        self.requireFeature(SymlinkFeature)
 
1589
        self.requireFeature(features.SymlinkFeature)
1349
1590
        self.build_tree(['source/'])
1350
1591
        os.symlink('a/generic/path', 'source/lnk')
1351
1592
        osutils.copy_tree('source', 'target')
1376
1617
                          ('d', 'source/b', 'target/b'),
1377
1618
                          ('f', 'source/b/c', 'target/b/c'),
1378
1619
                         ], processed_files)
1379
 
        self.failIfExists('target')
 
1620
        self.assertPathDoesNotExist('target')
1380
1621
        if osutils.has_symlinks():
1381
1622
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1382
1623
 
1383
1624
 
1384
 
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
1385
 
# [bialix] 2006/12/26
1386
 
 
1387
 
 
1388
 
class TestSetUnsetEnv(TestCase):
 
1625
class TestSetUnsetEnv(tests.TestCase):
1389
1626
    """Test updating the environment"""
1390
1627
 
1391
1628
    def setUp(self):
1397
1634
        def cleanup():
1398
1635
            if 'BZR_TEST_ENV_VAR' in os.environ:
1399
1636
                del os.environ['BZR_TEST_ENV_VAR']
1400
 
 
1401
1637
        self.addCleanup(cleanup)
1402
1638
 
1403
1639
    def test_set(self):
1415
1651
 
1416
1652
    def test_unicode(self):
1417
1653
        """Environment can only contain plain strings
1418
 
        
 
1654
 
1419
1655
        So Unicode strings must be encoded.
1420
1656
        """
1421
 
        uni_val, env_val = probe_unicode_in_user_encoding()
 
1657
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
1422
1658
        if uni_val is None:
1423
 
            raise TestSkipped('Cannot find a unicode character that works in'
1424
 
                              ' encoding %s' % (osutils.get_user_encoding(),))
 
1659
            raise tests.TestSkipped(
 
1660
                'Cannot find a unicode character that works in encoding %s'
 
1661
                % (osutils.get_user_encoding(),))
1425
1662
 
1426
1663
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
1427
1664
        self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
1432
1669
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1433
1670
        self.assertEqual('foo', old)
1434
1671
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1435
 
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1436
 
 
1437
 
 
1438
 
class TestLocalTimeOffset(TestCase):
1439
 
 
1440
 
    def test_local_time_offset(self):
1441
 
        """Test that local_time_offset() returns a sane value."""
1442
 
        offset = osutils.local_time_offset()
1443
 
        self.assertTrue(isinstance(offset, int))
1444
 
        # Test that the offset is no more than a eighteen hours in
1445
 
        # either direction.
1446
 
        # Time zone handling is system specific, so it is difficult to
1447
 
        # do more specific tests, but a value outside of this range is
1448
 
        # probably wrong.
1449
 
        eighteen_hours = 18 * 3600
1450
 
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1451
 
 
1452
 
    def test_local_time_offset_with_timestamp(self):
1453
 
        """Test that local_time_offset() works with a timestamp."""
1454
 
        offset = osutils.local_time_offset(1000000000.1234567)
1455
 
        self.assertTrue(isinstance(offset, int))
1456
 
        eighteen_hours = 18 * 3600
1457
 
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1458
 
 
1459
 
 
1460
 
class TestShaFileByName(TestCaseInTempDir):
1461
 
 
1462
 
    def test_sha_empty(self):
1463
 
        self.build_tree_contents([('foo', '')])
1464
 
        expected_sha = osutils.sha_string('')
1465
 
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1466
 
 
1467
 
    def test_sha_mixed_endings(self):
1468
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
1469
 
        self.build_tree_contents([('foo', text)])
1470
 
        expected_sha = osutils.sha_string(text)
1471
 
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1472
 
 
1473
 
 
1474
 
class TestResourceLoading(TestCaseInTempDir):
 
1672
        self.assertFalse('BZR_TEST_ENV_VAR' in os.environ)
 
1673
 
 
1674
 
 
1675
class TestSizeShaFile(tests.TestCaseInTempDir):
 
1676
 
 
1677
    def test_sha_empty(self):
 
1678
        self.build_tree_contents([('foo', '')])
 
1679
        expected_sha = osutils.sha_string('')
 
1680
        f = open('foo')
 
1681
        self.addCleanup(f.close)
 
1682
        size, sha = osutils.size_sha_file(f)
 
1683
        self.assertEqual(0, size)
 
1684
        self.assertEqual(expected_sha, sha)
 
1685
 
 
1686
    def test_sha_mixed_endings(self):
 
1687
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
 
1688
        self.build_tree_contents([('foo', text)])
 
1689
        expected_sha = osutils.sha_string(text)
 
1690
        f = open('foo', 'rb')
 
1691
        self.addCleanup(f.close)
 
1692
        size, sha = osutils.size_sha_file(f)
 
1693
        self.assertEqual(38, size)
 
1694
        self.assertEqual(expected_sha, sha)
 
1695
 
 
1696
 
 
1697
class TestShaFileByName(tests.TestCaseInTempDir):
 
1698
 
 
1699
    def test_sha_empty(self):
 
1700
        self.build_tree_contents([('foo', '')])
 
1701
        expected_sha = osutils.sha_string('')
 
1702
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1703
 
 
1704
    def test_sha_mixed_endings(self):
 
1705
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
 
1706
        self.build_tree_contents([('foo', text)])
 
1707
        expected_sha = osutils.sha_string(text)
 
1708
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1709
 
 
1710
 
 
1711
class TestResourceLoading(tests.TestCaseInTempDir):
1475
1712
 
1476
1713
    def test_resource_string(self):
1477
1714
        # test resource in bzrlib
1485
1722
            'yyy.xx')
1486
1723
        # test unknown resource
1487
1724
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
 
1725
 
 
1726
 
 
1727
class TestReCompile(tests.TestCase):
 
1728
 
 
1729
    def _deprecated_re_compile_checked(self, *args, **kwargs):
 
1730
        return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
 
1731
            osutils.re_compile_checked, *args, **kwargs)
 
1732
 
 
1733
    def test_re_compile_checked(self):
 
1734
        r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
 
1735
        self.assertTrue(r.match('aaaa'))
 
1736
        self.assertTrue(r.match('aAaA'))
 
1737
 
 
1738
    def test_re_compile_checked_error(self):
 
1739
        # like https://bugs.launchpad.net/bzr/+bug/251352
 
1740
 
 
1741
        # Due to possible test isolation error, re.compile is not lazy at
 
1742
        # this point. We re-install lazy compile.
 
1743
        lazy_regex.install_lazy_compile()
 
1744
        err = self.assertRaises(
 
1745
            errors.BzrCommandError,
 
1746
            self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
 
1747
        self.assertEqual(
 
1748
            'Invalid regular expression in test case: '
 
1749
            '"*" nothing to repeat',
 
1750
            str(err))
 
1751
 
 
1752
 
 
1753
class TestDirReader(tests.TestCaseInTempDir):
 
1754
 
 
1755
    scenarios = dir_reader_scenarios()
 
1756
 
 
1757
    # Set by load_tests
 
1758
    _dir_reader_class = None
 
1759
    _native_to_unicode = None
 
1760
 
 
1761
    def setUp(self):
 
1762
        tests.TestCaseInTempDir.setUp(self)
 
1763
        self.overrideAttr(osutils,
 
1764
                          '_selected_dir_reader', self._dir_reader_class())
 
1765
 
 
1766
    def _get_ascii_tree(self):
 
1767
        tree = [
 
1768
            '0file',
 
1769
            '1dir/',
 
1770
            '1dir/0file',
 
1771
            '1dir/1dir/',
 
1772
            '2file'
 
1773
            ]
 
1774
        expected_dirblocks = [
 
1775
                (('', '.'),
 
1776
                 [('0file', '0file', 'file'),
 
1777
                  ('1dir', '1dir', 'directory'),
 
1778
                  ('2file', '2file', 'file'),
 
1779
                 ]
 
1780
                ),
 
1781
                (('1dir', './1dir'),
 
1782
                 [('1dir/0file', '0file', 'file'),
 
1783
                  ('1dir/1dir', '1dir', 'directory'),
 
1784
                 ]
 
1785
                ),
 
1786
                (('1dir/1dir', './1dir/1dir'),
 
1787
                 [
 
1788
                 ]
 
1789
                ),
 
1790
            ]
 
1791
        return tree, expected_dirblocks
 
1792
 
 
1793
    def test_walk_cur_dir(self):
 
1794
        tree, expected_dirblocks = self._get_ascii_tree()
 
1795
        self.build_tree(tree)
 
1796
        result = list(osutils._walkdirs_utf8('.'))
 
1797
        # Filter out stat and abspath
 
1798
        self.assertEqual(expected_dirblocks,
 
1799
                         [(dirinfo, [line[0:3] for line in block])
 
1800
                          for dirinfo, block in result])
 
1801
 
 
1802
    def test_walk_sub_dir(self):
 
1803
        tree, expected_dirblocks = self._get_ascii_tree()
 
1804
        self.build_tree(tree)
 
1805
        # you can search a subdir only, with a supplied prefix.
 
1806
        result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
 
1807
        # Filter out stat and abspath
 
1808
        self.assertEqual(expected_dirblocks[1:],
 
1809
                         [(dirinfo, [line[0:3] for line in block])
 
1810
                          for dirinfo, block in result])
 
1811
 
 
1812
    def _get_unicode_tree(self):
 
1813
        name0u = u'0file-\xb6'
 
1814
        name1u = u'1dir-\u062c\u0648'
 
1815
        name2u = u'2file-\u0633'
 
1816
        tree = [
 
1817
            name0u,
 
1818
            name1u + '/',
 
1819
            name1u + '/' + name0u,
 
1820
            name1u + '/' + name1u + '/',
 
1821
            name2u,
 
1822
            ]
 
1823
        name0 = name0u.encode('UTF-8')
 
1824
        name1 = name1u.encode('UTF-8')
 
1825
        name2 = name2u.encode('UTF-8')
 
1826
        expected_dirblocks = [
 
1827
                (('', '.'),
 
1828
                 [(name0, name0, 'file', './' + name0u),
 
1829
                  (name1, name1, 'directory', './' + name1u),
 
1830
                  (name2, name2, 'file', './' + name2u),
 
1831
                 ]
 
1832
                ),
 
1833
                ((name1, './' + name1u),
 
1834
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
 
1835
                                                        + '/' + name0u),
 
1836
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
 
1837
                                                            + '/' + name1u),
 
1838
                 ]
 
1839
                ),
 
1840
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
 
1841
                 [
 
1842
                 ]
 
1843
                ),
 
1844
            ]
 
1845
        return tree, expected_dirblocks
 
1846
 
 
1847
    def _filter_out(self, raw_dirblocks):
 
1848
        """Filter out a walkdirs_utf8 result.
 
1849
 
 
1850
        stat field is removed, all native paths are converted to unicode
 
1851
        """
 
1852
        filtered_dirblocks = []
 
1853
        for dirinfo, block in raw_dirblocks:
 
1854
            dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
 
1855
            details = []
 
1856
            for line in block:
 
1857
                details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
 
1858
            filtered_dirblocks.append((dirinfo, details))
 
1859
        return filtered_dirblocks
 
1860
 
 
1861
    def test_walk_unicode_tree(self):
 
1862
        self.requireFeature(features.UnicodeFilenameFeature)
 
1863
        tree, expected_dirblocks = self._get_unicode_tree()
 
1864
        self.build_tree(tree)
 
1865
        result = list(osutils._walkdirs_utf8('.'))
 
1866
        self.assertEqual(expected_dirblocks, self._filter_out(result))
 
1867
 
 
1868
    def test_symlink(self):
 
1869
        self.requireFeature(features.SymlinkFeature)
 
1870
        self.requireFeature(features.UnicodeFilenameFeature)
 
1871
        target = u'target\N{Euro Sign}'
 
1872
        link_name = u'l\N{Euro Sign}nk'
 
1873
        os.symlink(target, link_name)
 
1874
        target_utf8 = target.encode('UTF-8')
 
1875
        link_name_utf8 = link_name.encode('UTF-8')
 
1876
        expected_dirblocks = [
 
1877
                (('', '.'),
 
1878
                 [(link_name_utf8, link_name_utf8,
 
1879
                   'symlink', './' + link_name),],
 
1880
                 )]
 
1881
        result = list(osutils._walkdirs_utf8('.'))
 
1882
        self.assertEqual(expected_dirblocks, self._filter_out(result))
 
1883
 
 
1884
 
 
1885
class TestReadLink(tests.TestCaseInTempDir):
 
1886
    """Exposes os.readlink() problems and the osutils solution.
 
1887
 
 
1888
    The only guarantee offered by os.readlink(), starting with 2.6, is that a
 
1889
    unicode string will be returned if a unicode string is passed.
 
1890
 
 
1891
    But prior python versions failed to properly encode the passed unicode
 
1892
    string.
 
1893
    """
 
1894
    _test_needs_features = [features.SymlinkFeature, features.UnicodeFilenameFeature]
 
1895
 
 
1896
    def setUp(self):
 
1897
        super(tests.TestCaseInTempDir, self).setUp()
 
1898
        self.link = u'l\N{Euro Sign}ink'
 
1899
        self.target = u'targe\N{Euro Sign}t'
 
1900
        os.symlink(self.target, self.link)
 
1901
 
 
1902
    def test_os_readlink_link_encoding(self):
 
1903
        self.assertEquals(self.target,  os.readlink(self.link))
 
1904
 
 
1905
    def test_os_readlink_link_decoding(self):
 
1906
        self.assertEquals(self.target.encode(osutils._fs_enc),
 
1907
                          os.readlink(self.link.encode(osutils._fs_enc)))
 
1908
 
 
1909
 
 
1910
class TestConcurrency(tests.TestCase):
 
1911
 
 
1912
    def setUp(self):
 
1913
        super(TestConcurrency, self).setUp()
 
1914
        self.overrideAttr(osutils, '_cached_local_concurrency')
 
1915
 
 
1916
    def test_local_concurrency(self):
 
1917
        concurrency = osutils.local_concurrency()
 
1918
        self.assertIsInstance(concurrency, int)
 
1919
 
 
1920
    def test_local_concurrency_environment_variable(self):
 
1921
        self.overrideEnv('BZR_CONCURRENCY', '2')
 
1922
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
 
1923
        self.overrideEnv('BZR_CONCURRENCY', '3')
 
1924
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
 
1925
        self.overrideEnv('BZR_CONCURRENCY', 'foo')
 
1926
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
 
1927
 
 
1928
    def test_option_concurrency(self):
 
1929
        self.overrideEnv('BZR_CONCURRENCY', '1')
 
1930
        self.run_bzr('rocks --concurrency 42')
 
1931
        # Command line overrides environment variable
 
1932
        self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
 
1933
        self.assertEquals(42, osutils.local_concurrency(use_cache=False))
 
1934
 
 
1935
 
 
1936
class TestFailedToLoadExtension(tests.TestCase):
 
1937
 
 
1938
    def _try_loading(self):
 
1939
        try:
 
1940
            import bzrlib._fictional_extension_py
 
1941
        except ImportError, e:
 
1942
            osutils.failed_to_load_extension(e)
 
1943
            return True
 
1944
 
 
1945
    def setUp(self):
 
1946
        super(TestFailedToLoadExtension, self).setUp()
 
1947
        self.overrideAttr(osutils, '_extension_load_failures', [])
 
1948
 
 
1949
    def test_failure_to_load(self):
 
1950
        self._try_loading()
 
1951
        self.assertLength(1, osutils._extension_load_failures)
 
1952
        self.assertEquals(osutils._extension_load_failures[0],
 
1953
            "No module named _fictional_extension_py")
 
1954
 
 
1955
    def test_report_extension_load_failures_no_warning(self):
 
1956
        self.assertTrue(self._try_loading())
 
1957
        warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
 
1958
        # it used to give a Python warning; it no longer does
 
1959
        self.assertLength(0, warnings)
 
1960
 
 
1961
    def test_report_extension_load_failures_message(self):
 
1962
        log = StringIO()
 
1963
        trace.push_log_file(log)
 
1964
        self.assertTrue(self._try_loading())
 
1965
        osutils.report_extension_load_failures()
 
1966
        self.assertContainsRe(
 
1967
            log.getvalue(),
 
1968
            r"bzr: warning: some compiled extensions could not be loaded; "
 
1969
            "see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
 
1970
            )
 
1971
 
 
1972
 
 
1973
class TestTerminalWidth(tests.TestCase):
 
1974
 
 
1975
    def setUp(self):
 
1976
        tests.TestCase.setUp(self)
 
1977
        self._orig_terminal_size_state = osutils._terminal_size_state
 
1978
        self._orig_first_terminal_size = osutils._first_terminal_size
 
1979
        self.addCleanup(self.restore_osutils_globals)
 
1980
        osutils._terminal_size_state = 'no_data'
 
1981
        osutils._first_terminal_size = None
 
1982
 
 
1983
    def restore_osutils_globals(self):
 
1984
        osutils._terminal_size_state = self._orig_terminal_size_state
 
1985
        osutils._first_terminal_size = self._orig_first_terminal_size
 
1986
 
 
1987
    def replace_stdout(self, new):
 
1988
        self.overrideAttr(sys, 'stdout', new)
 
1989
 
 
1990
    def replace__terminal_size(self, new):
 
1991
        self.overrideAttr(osutils, '_terminal_size', new)
 
1992
 
 
1993
    def set_fake_tty(self):
 
1994
 
 
1995
        class I_am_a_tty(object):
 
1996
            def isatty(self):
 
1997
                return True
 
1998
 
 
1999
        self.replace_stdout(I_am_a_tty())
 
2000
 
 
2001
    def test_default_values(self):
 
2002
        self.assertEqual(80, osutils.default_terminal_width)
 
2003
 
 
2004
    def test_defaults_to_BZR_COLUMNS(self):
 
2005
        # BZR_COLUMNS is set by the test framework
 
2006
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
 
2007
        self.overrideEnv('BZR_COLUMNS', '12')
 
2008
        self.assertEqual(12, osutils.terminal_width())
 
2009
 
 
2010
    def test_BZR_COLUMNS_0_no_limit(self):
 
2011
        self.overrideEnv('BZR_COLUMNS', '0')
 
2012
        self.assertEqual(None, osutils.terminal_width())
 
2013
 
 
2014
    def test_falls_back_to_COLUMNS(self):
 
2015
        self.overrideEnv('BZR_COLUMNS', None)
 
2016
        self.assertNotEqual('42', os.environ['COLUMNS'])
 
2017
        self.set_fake_tty()
 
2018
        self.overrideEnv('COLUMNS', '42')
 
2019
        self.assertEqual(42, osutils.terminal_width())
 
2020
 
 
2021
    def test_tty_default_without_columns(self):
 
2022
        self.overrideEnv('BZR_COLUMNS', None)
 
2023
        self.overrideEnv('COLUMNS', None)
 
2024
 
 
2025
        def terminal_size(w, h):
 
2026
            return 42, 42
 
2027
 
 
2028
        self.set_fake_tty()
 
2029
        # We need to override the osutils definition as it depends on the
 
2030
        # running environment that we can't control (PQM running without a
 
2031
        # controlling terminal is one example).
 
2032
        self.replace__terminal_size(terminal_size)
 
2033
        self.assertEqual(42, osutils.terminal_width())
 
2034
 
 
2035
    def test_non_tty_default_without_columns(self):
 
2036
        self.overrideEnv('BZR_COLUMNS', None)
 
2037
        self.overrideEnv('COLUMNS', None)
 
2038
        self.replace_stdout(None)
 
2039
        self.assertEqual(None, osutils.terminal_width())
 
2040
 
 
2041
    def test_no_TIOCGWINSZ(self):
 
2042
        self.requireFeature(term_ios_feature)
 
2043
        termios = term_ios_feature.module
 
2044
        # bug 63539 is about a termios without TIOCGWINSZ attribute
 
2045
        try:
 
2046
            orig = termios.TIOCGWINSZ
 
2047
        except AttributeError:
 
2048
            # We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
 
2049
            pass
 
2050
        else:
 
2051
            self.overrideAttr(termios, 'TIOCGWINSZ')
 
2052
            del termios.TIOCGWINSZ
 
2053
        self.overrideEnv('BZR_COLUMNS', None)
 
2054
        self.overrideEnv('COLUMNS', None)
 
2055
        # Whatever the result is, if we don't raise an exception, it's ok.
 
2056
        osutils.terminal_width()
 
2057
 
 
2058
 
 
2059
class TestCreationOps(tests.TestCaseInTempDir):
 
2060
    _test_needs_features = [features.chown_feature]
 
2061
 
 
2062
    def setUp(self):
 
2063
        tests.TestCaseInTempDir.setUp(self)
 
2064
        self.overrideAttr(os, 'chown', self._dummy_chown)
 
2065
 
 
2066
        # params set by call to _dummy_chown
 
2067
        self.path = self.uid = self.gid = None
 
2068
 
 
2069
    def _dummy_chown(self, path, uid, gid):
 
2070
        self.path, self.uid, self.gid = path, uid, gid
 
2071
 
 
2072
    def test_copy_ownership_from_path(self):
 
2073
        """copy_ownership_from_path test with specified src."""
 
2074
        ownsrc = '/'
 
2075
        f = open('test_file', 'wt')
 
2076
        osutils.copy_ownership_from_path('test_file', ownsrc)
 
2077
 
 
2078
        s = os.stat(ownsrc)
 
2079
        self.assertEquals(self.path, 'test_file')
 
2080
        self.assertEquals(self.uid, s.st_uid)
 
2081
        self.assertEquals(self.gid, s.st_gid)
 
2082
 
 
2083
    def test_copy_ownership_nonesrc(self):
 
2084
        """copy_ownership_from_path test with src=None."""
 
2085
        f = open('test_file', 'wt')
 
2086
        # should use parent dir for permissions
 
2087
        osutils.copy_ownership_from_path('test_file')
 
2088
 
 
2089
        s = os.stat('..')
 
2090
        self.assertEquals(self.path, 'test_file')
 
2091
        self.assertEquals(self.uid, s.st_uid)
 
2092
        self.assertEquals(self.gid, s.st_gid)
 
2093
 
 
2094
 
 
2095
class TestGetuserUnicode(tests.TestCase):
 
2096
 
 
2097
    def test_ascii_user(self):
 
2098
        self.overrideEnv('LOGNAME', 'jrandom')
 
2099
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
 
2100
 
 
2101
    def test_unicode_user(self):
 
2102
        ue = osutils.get_user_encoding()
 
2103
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
2104
        if uni_val is None:
 
2105
            raise tests.TestSkipped(
 
2106
                'Cannot find a unicode character that works in encoding %s'
 
2107
                % (osutils.get_user_encoding(),))
 
2108
        uni_username = u'jrandom' + uni_val
 
2109
        encoded_username = uni_username.encode(ue)
 
2110
        self.overrideEnv('LOGNAME', encoded_username)
 
2111
        self.assertEqual(uni_username, osutils.getuser_unicode())
 
2112
        self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
 
2113
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
 
2114
 
 
2115
    def test_no_username_bug_660174(self):
 
2116
        self.requireFeature(features.win32_feature)
 
2117
        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
 
2118
            self.overrideEnv(name, None)
 
2119
        self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
 
2120
 
 
2121
 
 
2122
class TestBackupNames(tests.TestCase):
 
2123
 
 
2124
    def setUp(self):
 
2125
        super(TestBackupNames, self).setUp()
 
2126
        self.backups = []
 
2127
 
 
2128
    def backup_exists(self, name):
 
2129
        return name in self.backups
 
2130
 
 
2131
    def available_backup_name(self, name):
 
2132
        backup_name = osutils.available_backup_name(name, self.backup_exists)
 
2133
        self.backups.append(backup_name)
 
2134
        return backup_name
 
2135
 
 
2136
    def assertBackupName(self, expected, name):
 
2137
        self.assertEqual(expected, self.available_backup_name(name))
 
2138
 
 
2139
    def test_empty(self):
 
2140
        self.assertBackupName('file.~1~', 'file')
 
2141
 
 
2142
    def test_existing(self):
 
2143
        self.available_backup_name('file')
 
2144
        self.available_backup_name('file')
 
2145
        self.assertBackupName('file.~3~', 'file')
 
2146
        # Empty slots are found, this is not a strict requirement and may be
 
2147
        # revisited if we test against all implementations.
 
2148
        self.backups.remove('file.~2~')
 
2149
        self.assertBackupName('file.~2~', 'file')
 
2150
 
 
2151
 
 
2152
class TestFindExecutableInPath(tests.TestCase):
 
2153
 
 
2154
    def test_windows(self):
 
2155
        if sys.platform != 'win32':
 
2156
            raise tests.TestSkipped('test requires win32')
 
2157
        self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
 
2158
        self.assertTrue(
 
2159
            osutils.find_executable_on_path('explorer.exe') is not None)
 
2160
        self.assertTrue(
 
2161
            osutils.find_executable_on_path('EXPLORER.EXE') is not None)
 
2162
        self.assertTrue(
 
2163
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2164
        self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
 
2165
 
 
2166
    def test_other(self):
 
2167
        if sys.platform == 'win32':
 
2168
            raise tests.TestSkipped('test requires non-win32')
 
2169
        self.assertTrue(osutils.find_executable_on_path('sh') is not None)
 
2170
        self.assertTrue(
 
2171
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)