~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-09-11 06:10:59 UTC
  • mfrom: (3702.1.1 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20080911061059-svzqfejar17ui4zw
(mbp) KnitVersionedFiles repr

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
23
22
import socket
 
23
import stat
24
24
import sys
25
25
import time
26
26
 
 
27
import bzrlib
27
28
from bzrlib import (
28
29
    errors,
29
 
    lazy_regex,
30
30
    osutils,
31
 
    symbol_versioning,
32
31
    tests,
33
 
    trace,
34
32
    win32utils,
35
33
    )
 
34
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
 
35
from bzrlib.osutils import (
 
36
        is_inside_any,
 
37
        is_inside_or_parent_of_any,
 
38
        pathjoin,
 
39
        pumpfile,
 
40
        pump_string_file,
 
41
        )
36
42
from bzrlib.tests import (
37
 
    features,
38
 
    file_utils,
39
 
    test__walkdirs_win32,
 
43
        adapt_tests,
 
44
        Feature,
 
45
        probe_unicode_in_user_encoding,
 
46
        split_suite_by_re,
 
47
        StringIOWrapper,
 
48
        SymlinkFeature,
 
49
        TestCase,
 
50
        TestCaseInTempDir,
 
51
        TestScenarioApplier,
 
52
        TestSkipped,
 
53
        )
 
54
from bzrlib.tests.file_utils import (
 
55
    FakeReadFile,
40
56
    )
41
 
from bzrlib.tests.scenarios import load_tests_apply_scenarios
42
 
 
43
 
 
44
 
class _UTF8DirReaderFeature(features.Feature):
 
57
from bzrlib.tests.test__walkdirs_win32 import WalkdirsWin32Feature
 
58
 
 
59
 
 
60
def load_tests(standard_tests, module, loader):
 
61
    """Parameterize readdir tests."""
 
62
    to_adapt, result = split_suite_by_re(standard_tests, "readdir")
 
63
    adapter = TestScenarioApplier()
 
64
    from bzrlib import _readdir_py
 
65
    adapter.scenarios = [('python', {'read_dir': _readdir_py.read_dir})]
 
66
    if ReadDirFeature.available():
 
67
        adapter.scenarios.append(('pyrex',
 
68
            {'read_dir': ReadDirFeature.read_dir}))
 
69
    adapt_tests(to_adapt, adapter, result)
 
70
    return result
 
71
 
 
72
 
 
73
class _ReadDirFeature(Feature):
45
74
 
46
75
    def _probe(self):
47
76
        try:
48
77
            from bzrlib import _readdir_pyx
49
 
            self.reader = _readdir_pyx.UTF8DirReader
 
78
            self.read_dir = _readdir_pyx.read_dir
50
79
            return True
51
80
        except ImportError:
52
81
            return False
54
83
    def feature_name(self):
55
84
        return 'bzrlib._readdir_pyx'
56
85
 
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):
 
86
ReadDirFeature = _ReadDirFeature()
 
87
 
 
88
 
 
89
class TestOSUtils(TestCaseInTempDir):
104
90
 
105
91
    def test_contains_whitespace(self):
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'))
 
92
        self.failUnless(osutils.contains_whitespace(u' '))
 
93
        self.failUnless(osutils.contains_whitespace(u'hello there'))
 
94
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
 
95
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
 
96
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
 
97
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
112
98
 
113
99
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
114
100
        # is whitespace, but we do not.
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)
 
101
        self.failIf(osutils.contains_whitespace(u''))
 
102
        self.failIf(osutils.contains_whitespace(u'hellothere'))
 
103
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
132
104
 
133
105
    def test_fancy_rename(self):
134
106
        # This should work everywhere
135
 
        self.create_file('a', 'something in a\n')
136
 
        self._fancy_rename('a', 'b')
137
 
        self.assertPathDoesNotExist('a')
138
 
        self.assertPathExists('b')
 
107
        def rename(a, b):
 
108
            osutils.fancy_rename(a, b,
 
109
                    rename_func=os.rename,
 
110
                    unlink_func=os.unlink)
 
111
 
 
112
        open('a', 'wb').write('something in a\n')
 
113
        rename('a', 'b')
 
114
        self.failIfExists('a')
 
115
        self.failUnlessExists('b')
139
116
        self.check_file_contents('b', 'something in a\n')
140
117
 
141
 
        self.create_file('a', 'new something in a\n')
142
 
        self._fancy_rename('b', 'a')
 
118
        open('a', 'wb').write('new something in a\n')
 
119
        rename('b', 'a')
143
120
 
144
121
        self.check_file_contents('a', 'something in a\n')
145
122
 
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
 
 
158
123
    def test_rename(self):
159
124
        # Rename should be semi-atomic on all platforms
160
 
        self.create_file('a', 'something in a\n')
 
125
        open('a', 'wb').write('something in a\n')
161
126
        osutils.rename('a', 'b')
162
 
        self.assertPathDoesNotExist('a')
163
 
        self.assertPathExists('b')
 
127
        self.failIfExists('a')
 
128
        self.failUnlessExists('b')
164
129
        self.check_file_contents('b', 'something in a\n')
165
130
 
166
 
        self.create_file('a', 'new something in a\n')
 
131
        open('a', 'wb').write('new something in a\n')
167
132
        osutils.rename('b', 'a')
168
133
 
169
134
        self.check_file_contents('a', 'something in a\n')
180
145
        shape = sorted(os.listdir('.'))
181
146
        self.assertEquals(['A', 'B'], shape)
182
147
 
183
 
 
184
 
class TestRandChars(tests.TestCase):
185
 
 
186
148
    def test_01_rand_chars_empty(self):
187
149
        result = osutils.rand_chars(0)
188
150
        self.assertEqual(result, '')
193
155
        self.assertEqual(type(result), str)
194
156
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
195
157
 
196
 
 
197
 
class TestIsInside(tests.TestCase):
198
 
 
199
158
    def test_is_inside(self):
200
159
        is_inside = osutils.is_inside
201
160
        self.assertTrue(is_inside('src', 'src/foo.c'))
206
165
        self.assertTrue(is_inside('', 'foo.c'))
207
166
 
208
167
    def test_is_inside_any(self):
209
 
        SRC_FOO_C = osutils.pathjoin('src', 'foo.c')
 
168
        SRC_FOO_C = pathjoin('src', 'foo.c')
210
169
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
211
170
                         (['src'], SRC_FOO_C),
212
171
                         (['src'], 'src'),
213
172
                         ]:
214
 
            self.assert_(osutils.is_inside_any(dirs, fn))
 
173
            self.assert_(is_inside_any(dirs, fn))
215
174
        for dirs, fn in [(['src'], 'srccontrol'),
216
175
                         (['src'], 'srccontrol/foo')]:
217
 
            self.assertFalse(osutils.is_inside_any(dirs, fn))
 
176
            self.assertFalse(is_inside_any(dirs, fn))
218
177
 
219
178
    def test_is_inside_or_parent_of_any(self):
220
179
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
223
182
                         (['src/bar.c', 'bla/foo.c'], 'src'),
224
183
                         (['src'], 'src'),
225
184
                         ]:
226
 
            self.assert_(osutils.is_inside_or_parent_of_any(dirs, fn))
227
 
 
 
185
            self.assert_(is_inside_or_parent_of_any(dirs, fn))
 
186
            
228
187
        for dirs, fn in [(['src'], 'srccontrol'),
229
188
                         (['srccontrol/foo.c'], 'src'),
230
189
                         (['src'], 'srccontrol/foo')]:
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):
 
190
            self.assertFalse(is_inside_or_parent_of_any(dirs, fn))
254
191
 
255
192
    def test_rmtree(self):
256
193
        # Check to remove tree with read-only files/dirs
267
204
 
268
205
        osutils.rmtree('dir')
269
206
 
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):
 
207
        self.failIfExists('dir/file')
 
208
        self.failIfExists('dir')
287
209
 
288
210
    def test_file_kind(self):
289
211
        self.build_tree(['file', 'dir/'])
292
214
        if osutils.has_symlinks():
293
215
            os.symlink('symlink', 'symlink')
294
216
            self.assertEquals('symlink', osutils.file_kind('symlink'))
295
 
 
 
217
        
296
218
        # TODO: jam 20060529 Test a block device
297
219
        try:
298
220
            os.lstat('/dev/null')
320
242
                os.remove('socket')
321
243
 
322
244
    def test_kind_marker(self):
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):
 
245
        self.assertEqual(osutils.kind_marker('file'), '')
 
246
        self.assertEqual(osutils.kind_marker('directory'), '/')
 
247
        self.assertEqual(osutils.kind_marker('symlink'), '@')
 
248
        self.assertEqual(osutils.kind_marker('tree-reference'), '+')
334
249
 
335
250
    def test_get_umask(self):
336
251
        if sys.platform == 'win32':
339
254
            return
340
255
 
341
256
        orig_umask = osutils.get_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):
 
257
        try:
 
258
            os.umask(0222)
 
259
            self.assertEqual(0222, osutils.get_umask())
 
260
            os.umask(0022)
 
261
            self.assertEqual(0022, osutils.get_umask())
 
262
            os.umask(0002)
 
263
            self.assertEqual(0002, osutils.get_umask())
 
264
            os.umask(0027)
 
265
            self.assertEqual(0027, osutils.get_umask())
 
266
        finally:
 
267
            os.umask(orig_umask)
354
268
 
355
269
    def assertFormatedDelta(self, expected, seconds):
356
270
        """Assert osutils.format_delta formats as expected"""
391
305
    def test_format_date(self):
392
306
        self.assertRaises(errors.UnsupportedTimezoneFormat,
393
307
            osutils.format_date, 0, timezone='foo')
394
 
        self.assertIsInstance(osutils.format_date(0), str)
395
 
        self.assertIsInstance(osutils.format_local_date(0), unicode)
396
 
        # Testing for the actual value of the local weekday without
397
 
        # duplicating the code from format_date is difficult.
398
 
        # Instead blackbox.test_locale should check for localized
399
 
        # dates once they do occur in output strings.
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
308
 
431
309
    def test_dereference_path(self):
432
 
        self.requireFeature(features.SymlinkFeature)
 
310
        self.requireFeature(SymlinkFeature)
433
311
        cwd = osutils.realpath('.')
434
312
        os.mkdir('bar')
435
313
        bar_path = osutils.pathjoin(cwd, 'bar')
438
316
        self.assertEqual(bar_path, osutils.realpath('./bar'))
439
317
        os.symlink('bar', 'foo')
440
318
        self.assertEqual(bar_path, osutils.realpath('./foo'))
441
 
 
 
319
        
442
320
        # Does not dereference terminal symlinks
443
321
        foo_path = osutils.pathjoin(cwd, 'foo')
444
322
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
476
354
            osutils.make_readonly('dangling')
477
355
            osutils.make_writable('dangling')
478
356
 
 
357
    def test_kind_marker(self):
 
358
        self.assertEqual("", osutils.kind_marker("file"))
 
359
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
 
360
        self.assertEqual("@", osutils.kind_marker("symlink"))
 
361
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
 
362
 
479
363
    def test_host_os_dereferences_symlinks(self):
480
364
        osutils.host_os_dereferences_symlinks()
481
365
 
482
366
 
483
 
class TestCanonicalRelPath(tests.TestCaseInTempDir):
484
 
 
485
 
    _test_needs_features = [features.CaseInsCasePresFilenameFeature]
486
 
 
487
 
    def test_canonical_relpath_simple(self):
488
 
        f = file('MixedCaseName', 'w')
489
 
        f.close()
490
 
        actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
491
 
        self.assertEqual('work/MixedCaseName', actual)
492
 
 
493
 
    def test_canonical_relpath_missing_tail(self):
494
 
        os.mkdir('MixedCaseParent')
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):
 
367
class TestPumpFile(TestCase):
544
368
    """Test pumpfile method."""
545
 
 
546
369
    def setUp(self):
547
 
        tests.TestCase.setUp(self)
548
370
        # create a test datablock
549
371
        self.block_size = 512
550
372
        pattern = '0123456789ABCDEF'
557
379
        # make sure test data is larger than max read size
558
380
        self.assertTrue(self.test_data_len > self.block_size)
559
381
 
560
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
382
        from_file = FakeReadFile(self.test_data)
561
383
        to_file = StringIO()
562
384
 
563
385
        # read (max / 2) bytes and verify read size wasn't affected
564
386
        num_bytes_to_read = self.block_size / 2
565
 
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
387
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
566
388
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
567
389
        self.assertEqual(from_file.get_read_count(), 1)
568
390
 
569
391
        # read (max) bytes and verify read size wasn't affected
570
392
        num_bytes_to_read = self.block_size
571
393
        from_file.reset_read_count()
572
 
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
394
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
573
395
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
574
396
        self.assertEqual(from_file.get_read_count(), 1)
575
397
 
576
398
        # read (max + 1) bytes and verify read size was limited
577
399
        num_bytes_to_read = self.block_size + 1
578
400
        from_file.reset_read_count()
579
 
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
401
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
580
402
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
581
403
        self.assertEqual(from_file.get_read_count(), 2)
582
404
 
583
405
        # finish reading the rest of the data
584
406
        num_bytes_to_read = self.test_data_len - to_file.tell()
585
 
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
407
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
586
408
 
587
409
        # report error if the data wasn't equal (we only report the size due
588
410
        # to the length of the data)
598
420
        self.assertTrue(self.test_data_len > self.block_size)
599
421
 
600
422
        # retrieve data in blocks
601
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
423
        from_file = FakeReadFile(self.test_data)
602
424
        to_file = StringIO()
603
 
        osutils.pumpfile(from_file, to_file, self.test_data_len,
604
 
                         self.block_size)
 
425
        pumpfile(from_file, to_file, self.test_data_len, self.block_size)
605
426
 
606
427
        # verify read size was equal to the maximum read size
607
428
        self.assertTrue(from_file.get_max_read_size() > 0)
622
443
        self.assertTrue(self.test_data_len > self.block_size)
623
444
 
624
445
        # retrieve data to EOF
625
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
446
        from_file = FakeReadFile(self.test_data)
626
447
        to_file = StringIO()
627
 
        osutils.pumpfile(from_file, to_file, -1, self.block_size)
 
448
        pumpfile(from_file, to_file, -1, self.block_size)
628
449
 
629
450
        # verify read size was equal to the maximum read size
630
451
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
642
463
        test verifies that any existing usages of pumpfile will not be broken
643
464
        with this new version."""
644
465
        # retrieve data using default (old) pumpfile method
645
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
466
        from_file = FakeReadFile(self.test_data)
646
467
        to_file = StringIO()
647
 
        osutils.pumpfile(from_file, to_file)
 
468
        pumpfile(from_file, to_file)
648
469
 
649
470
        # report error if the data wasn't equal (we only report the size due
650
471
        # to the length of the data)
653
474
            message = "Data not equal.  Expected %d bytes, received %d."
654
475
            self.fail(message % (len(response_data), self.test_data_len))
655
476
 
656
 
    def test_report_activity(self):
657
 
        activity = []
658
 
        def log_activity(length, direction):
659
 
            activity.append((length, direction))
660
 
        from_file = StringIO(self.test_data)
661
 
        to_file = StringIO()
662
 
        osutils.pumpfile(from_file, to_file, buff_size=500,
663
 
                         report_activity=log_activity, direction='read')
664
 
        self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
665
 
                          (36, 'read')], activity)
666
 
 
667
 
        from_file = StringIO(self.test_data)
668
 
        to_file = StringIO()
669
 
        del activity[:]
670
 
        osutils.pumpfile(from_file, to_file, buff_size=500,
671
 
                         report_activity=log_activity, direction='write')
672
 
        self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
673
 
                          (36, 'write')], activity)
674
 
 
675
 
        # And with a limited amount of data
676
 
        from_file = StringIO(self.test_data)
677
 
        to_file = StringIO()
678
 
        del activity[:]
679
 
        osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
680
 
                         report_activity=log_activity, direction='read')
681
 
        self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
682
 
 
683
 
 
684
 
 
685
 
class TestPumpStringFile(tests.TestCase):
 
477
 
 
478
class TestPumpStringFile(TestCase):
686
479
 
687
480
    def test_empty(self):
688
481
        output = StringIO()
689
 
        osutils.pump_string_file("", output)
 
482
        pump_string_file("", output)
690
483
        self.assertEqual("", output.getvalue())
691
484
 
692
485
    def test_more_than_segment_size(self):
693
486
        output = StringIO()
694
 
        osutils.pump_string_file("123456789", output, 2)
 
487
        pump_string_file("123456789", output, 2)
695
488
        self.assertEqual("123456789", output.getvalue())
696
489
 
697
490
    def test_segment_size(self):
698
491
        output = StringIO()
699
 
        osutils.pump_string_file("12", output, 2)
 
492
        pump_string_file("12", output, 2)
700
493
        self.assertEqual("12", output.getvalue())
701
494
 
702
495
    def test_segment_size_multiple(self):
703
496
        output = StringIO()
704
 
        osutils.pump_string_file("1234", output, 2)
 
497
        pump_string_file("1234", output, 2)
705
498
        self.assertEqual("1234", output.getvalue())
706
499
 
707
500
 
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):
 
501
class TestSafeUnicode(TestCase):
728
502
 
729
503
    def test_from_ascii_string(self):
730
504
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
739
513
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
740
514
 
741
515
    def test_bad_utf8_string(self):
742
 
        self.assertRaises(errors.BzrBadParameterNotUnicode,
 
516
        self.assertRaises(BzrBadParameterNotUnicode,
743
517
                          osutils.safe_unicode,
744
518
                          '\xbb\xbb')
745
519
 
746
520
 
747
 
class TestSafeUtf8(tests.TestCase):
 
521
class TestSafeUtf8(TestCase):
748
522
 
749
523
    def test_from_ascii_string(self):
750
524
        f = 'foobar'
760
534
        self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
761
535
 
762
536
    def test_bad_utf8_string(self):
763
 
        self.assertRaises(errors.BzrBadParameterNotUnicode,
 
537
        self.assertRaises(BzrBadParameterNotUnicode,
764
538
                          osutils.safe_utf8, '\xbb\xbb')
765
539
 
766
540
 
767
 
class TestSafeRevisionId(tests.TestCase):
 
541
class TestSafeRevisionId(TestCase):
768
542
 
769
543
    def test_from_ascii_string(self):
770
544
        # this shouldn't give a warning because it's getting an ascii string
792
566
        self.assertEqual(None, osutils.safe_revision_id(None))
793
567
 
794
568
 
795
 
class TestSafeFileId(tests.TestCase):
 
569
class TestSafeFileId(TestCase):
796
570
 
797
571
    def test_from_ascii_string(self):
798
572
        self.assertEqual('foobar', osutils.safe_file_id('foobar'))
818
592
        self.assertEqual(None, osutils.safe_file_id(None))
819
593
 
820
594
 
821
 
class TestWin32Funcs(tests.TestCase):
822
 
    """Test that _win32 versions of os utilities return appropriate paths."""
 
595
class TestWin32Funcs(TestCase):
 
596
    """Test that the _win32 versions of os utilities return appropriate paths."""
823
597
 
824
598
    def test_abspath(self):
825
599
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
832
606
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
833
607
 
834
608
    def test_pathjoin(self):
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'))
 
609
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
 
610
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
 
611
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
 
612
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
 
613
        self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
 
614
        self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
847
615
 
848
616
    def test_normpath(self):
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'))
 
617
        self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
 
618
        self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
853
619
 
854
620
    def test_getcwd(self):
855
621
        cwd = osutils._win32_getcwd()
876
642
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
877
643
        # relative path
878
644
        cwd = osutils.getcwd().rstrip('/')
879
 
        drive = osutils.ntpath.splitdrive(cwd)[0]
 
645
        drive = osutils._nt_splitdrive(cwd)[0]
880
646
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
881
647
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
882
648
        # unicode path
884
650
        self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
885
651
 
886
652
 
887
 
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
 
653
class TestWin32FuncsDirs(TestCaseInTempDir):
888
654
    """Test win32 functions that create files."""
 
655
    
 
656
    def test_getcwd(self):
 
657
        if win32utils.winver == 'Windows 98':
 
658
            raise TestSkipped('Windows 98 cannot handle unicode filenames')
 
659
        # Make sure getcwd can handle unicode filenames
 
660
        try:
 
661
            os.mkdir(u'mu-\xb5')
 
662
        except UnicodeError:
 
663
            raise TestSkipped("Unable to create Unicode filename")
889
664
 
890
 
    def test_getcwd(self):
891
 
        self.requireFeature(features.UnicodeFilenameFeature)
892
 
        os.mkdir(u'mu-\xb5')
893
665
        os.chdir(u'mu-\xb5')
894
666
        # TODO: jam 20060427 This will probably fail on Mac OSX because
895
667
        #       it will change the normalization of B\xe5gfors
900
672
    def test_minimum_path_selection(self):
901
673
        self.assertEqual(set(),
902
674
            osutils.minimum_path_selection([]))
903
 
        self.assertEqual(set(['a']),
904
 
            osutils.minimum_path_selection(['a']))
905
675
        self.assertEqual(set(['a', 'b']),
906
676
            osutils.minimum_path_selection(['a', 'b']))
907
677
        self.assertEqual(set(['a/', 'b']),
908
678
            osutils.minimum_path_selection(['a/', 'b']))
909
679
        self.assertEqual(set(['a/', 'b']),
910
680
            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']))
913
681
 
914
682
    def test_mkdtemp(self):
915
683
        tmpdir = osutils._win32_mkdtemp(dir='.')
924
692
        b.close()
925
693
 
926
694
        osutils._win32_rename('b', 'a')
927
 
        self.assertPathExists('a')
928
 
        self.assertPathDoesNotExist('b')
 
695
        self.failUnlessExists('a')
 
696
        self.failIfExists('b')
929
697
        self.assertFileEqual('baz\n', 'a')
930
698
 
931
699
    def test_rename_missing_file(self):
971
739
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
972
740
 
973
741
 
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):
 
742
class TestMacFuncsDirs(TestCaseInTempDir):
984
743
    """Test mac special functions that require directories."""
985
744
 
986
745
    def test_getcwd(self):
987
 
        self.requireFeature(features.UnicodeFilenameFeature)
988
 
        os.mkdir(u'B\xe5gfors')
 
746
        # On Mac, this will actually create Ba\u030agfors
 
747
        # but chdir will still work, because it accepts both paths
 
748
        try:
 
749
            os.mkdir(u'B\xe5gfors')
 
750
        except UnicodeError:
 
751
            raise TestSkipped("Unable to create Unicode filename")
 
752
 
989
753
        os.chdir(u'B\xe5gfors')
990
754
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
991
755
 
992
756
    def test_getcwd_nonnorm(self):
993
 
        self.requireFeature(features.UnicodeFilenameFeature)
994
757
        # Test that _mac_getcwd() will normalize this path
995
 
        os.mkdir(u'Ba\u030agfors')
 
758
        try:
 
759
            os.mkdir(u'Ba\u030agfors')
 
760
        except UnicodeError:
 
761
            raise TestSkipped("Unable to create Unicode filename")
 
762
 
996
763
        os.chdir(u'Ba\u030agfors')
997
764
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
998
765
 
999
766
 
1000
 
class TestChunksToLines(tests.TestCase):
1001
 
 
1002
 
    def test_smoketest(self):
1003
 
        self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
1004
 
                         osutils.chunks_to_lines(['foo\nbar', '\nbaz\n']))
1005
 
        self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
1006
 
                         osutils.chunks_to_lines(['foo\n', 'bar\n', 'baz\n']))
1007
 
 
1008
 
    def test_osutils_binding(self):
1009
 
        from bzrlib.tests import test__chunks_to_lines
1010
 
        if test__chunks_to_lines.compiled_chunkstolines_feature.available():
1011
 
            from bzrlib._chunks_to_lines_pyx import chunks_to_lines
1012
 
        else:
1013
 
            from bzrlib._chunks_to_lines_py import chunks_to_lines
1014
 
        self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
1015
 
 
1016
 
 
1017
 
class TestSplitLines(tests.TestCase):
 
767
class TestSplitLines(TestCase):
1018
768
 
1019
769
    def test_split_unicode(self):
1020
770
        self.assertEqual([u'foo\n', u'bar\xae'],
1027
777
                         osutils.split_lines('foo\rbar\n'))
1028
778
 
1029
779
 
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])
1036
 
 
 
780
class TestWalkDirs(TestCaseInTempDir):
 
781
 
 
782
    def test_readdir(self):
 
783
        tree = [
 
784
            '.bzr/',
 
785
            '0file',
 
786
            '1dir/',
 
787
            '1dir/0file',
 
788
            '1dir/1dir/',
 
789
            '2file'
 
790
            ]
 
791
        self.build_tree(tree)
 
792
        expected_names = ['.bzr', '0file', '1dir', '2file']
 
793
        # read_dir returns pairs, which form a table with either None in all
 
794
        # the first columns, or a sort key to get best on-disk-read order, 
 
795
        # and the disk path name in utf-8 encoding in the second column.
 
796
        read_result = self.read_dir('.')
 
797
        # The second column is always the names, and every name except "." and
 
798
        # ".." should be present.
 
799
        names = sorted([row[1] for row in read_result])
 
800
        self.assertEqual(expected_names, names)
 
801
        expected_sort_key = None
 
802
        if read_result[0][0] is None:
 
803
            # No sort key returned - all keys must None
 
804
            operator = self.assertEqual
 
805
        else:
 
806
            # A sort key in the first row implies sort keys in the other rows.
 
807
            operator = self.assertNotEqual
 
808
        for row in read_result:
 
809
            operator(None, row[0])
 
810
 
 
811
    def test_compiled_extension_exists(self):
 
812
        self.requireFeature(ReadDirFeature)
 
813
        
1037
814
    def test_walkdirs(self):
1038
815
        tree = [
1039
816
            '.bzr',
1071
848
            result.append((dirdetail, dirblock))
1072
849
 
1073
850
        self.assertTrue(found_bzrdir)
1074
 
        self.assertExpectedBlocks(expected_dirblocks, result)
 
851
        self.assertEqual(expected_dirblocks,
 
852
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1075
853
        # you can search a subdir only, with a supplied prefix.
1076
854
        result = []
1077
855
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
1078
856
            result.append(dirblock)
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)
 
857
        self.assertEqual(expected_dirblocks[1:],
 
858
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1137
859
 
1138
860
    def test__walkdirs_utf8(self):
1139
861
        tree = [
1172
894
            result.append((dirdetail, dirblock))
1173
895
 
1174
896
        self.assertTrue(found_bzrdir)
1175
 
        self.assertExpectedBlocks(expected_dirblocks, result)
1176
 
 
 
897
        self.assertEqual(expected_dirblocks,
 
898
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1177
899
        # you can search a subdir only, with a supplied prefix.
1178
900
        result = []
1179
901
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
1180
902
            result.append(dirblock)
1181
 
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
 
903
        self.assertEqual(expected_dirblocks[1:],
 
904
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1182
905
 
1183
906
    def _filter_out_stat(self, result):
1184
907
        """Filter out the stat value from the walkdirs result"""
1189
912
                new_dirblock.append((info[0], info[1], info[2], info[4]))
1190
913
            dirblock[:] = new_dirblock
1191
914
 
 
915
    def test__walkdirs_utf8_selection(self):
 
916
        # Just trigger the function once, to make sure it has selected a real
 
917
        # implementation.
 
918
        list(osutils._walkdirs_utf8('.'))
 
919
        if WalkdirsWin32Feature.available():
 
920
            # If the compiled form is available, make sure it is used
 
921
            from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
 
922
            self.assertIs(_walkdirs_utf8_win32_find_file,
 
923
                          osutils._real_walkdirs_utf8)
 
924
        elif sys.platform == 'win32':
 
925
            self.assertIs(osutils._walkdirs_unicode_to_utf8,
 
926
                          osutils._real_walkdirs_utf8)
 
927
        elif osutils._fs_enc.upper() in ('UTF-8', 'ASCII', 'ANSI_X3.4-1968'): # ascii
 
928
            self.assertIs(osutils._walkdirs_fs_utf8,
 
929
                          osutils._real_walkdirs_utf8)
 
930
        else:
 
931
            self.assertIs(osutils._walkdirs_unicode_to_utf8,
 
932
                          osutils._real_walkdirs_utf8)
 
933
 
1192
934
    def _save_platform_info(self):
1193
 
        self.overrideAttr(win32utils, 'winver')
1194
 
        self.overrideAttr(osutils, '_fs_enc')
1195
 
        self.overrideAttr(osutils, '_selected_dir_reader')
 
935
        cur_winver = win32utils.winver
 
936
        cur_fs_enc = osutils._fs_enc
 
937
        cur_real_walkdirs_utf8 = osutils._real_walkdirs_utf8
 
938
        def restore():
 
939
            win32utils.winver = cur_winver
 
940
            osutils._fs_enc = cur_fs_enc
 
941
            osutils._real_walkdirs_utf8 = cur_real_walkdirs_utf8
 
942
        self.addCleanup(restore)
1196
943
 
1197
 
    def assertDirReaderIs(self, expected):
 
944
    def assertWalkdirsUtf8Is(self, expected):
1198
945
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
1199
946
        # Force it to redetect
1200
 
        osutils._selected_dir_reader = None
 
947
        osutils._real_walkdirs_utf8 = None
1201
948
        # Nothing to list, but should still trigger the selection logic
1202
949
        self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
1203
 
        self.assertIsInstance(osutils._selected_dir_reader, expected)
 
950
        self.assertIs(expected, osutils._real_walkdirs_utf8)
1204
951
 
1205
952
    def test_force_walkdirs_utf8_fs_utf8(self):
1206
 
        self.requireFeature(UTF8DirReaderFeature)
1207
953
        self._save_platform_info()
1208
954
        win32utils.winver = None # Avoid the win32 detection code
1209
955
        osutils._fs_enc = 'UTF-8'
1210
 
        self.assertDirReaderIs(
1211
 
            UTF8DirReaderFeature.module.UTF8DirReader)
 
956
        self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
1212
957
 
1213
958
    def test_force_walkdirs_utf8_fs_ascii(self):
1214
 
        self.requireFeature(UTF8DirReaderFeature)
1215
959
        self._save_platform_info()
1216
960
        win32utils.winver = None # Avoid the win32 detection code
1217
961
        osutils._fs_enc = 'US-ASCII'
1218
 
        self.assertDirReaderIs(
1219
 
            UTF8DirReaderFeature.module.UTF8DirReader)
 
962
        self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
1220
963
 
1221
964
    def test_force_walkdirs_utf8_fs_ANSI(self):
1222
 
        self.requireFeature(UTF8DirReaderFeature)
1223
965
        self._save_platform_info()
1224
966
        win32utils.winver = None # Avoid the win32 detection code
1225
967
        osutils._fs_enc = 'ANSI_X3.4-1968'
1226
 
        self.assertDirReaderIs(
1227
 
            UTF8DirReaderFeature.module.UTF8DirReader)
 
968
        self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
1228
969
 
1229
970
    def test_force_walkdirs_utf8_fs_latin1(self):
1230
971
        self._save_platform_info()
1231
972
        win32utils.winver = None # Avoid the win32 detection code
1232
973
        osutils._fs_enc = 'latin1'
1233
 
        self.assertDirReaderIs(osutils.UnicodeDirReader)
 
974
        self.assertWalkdirsUtf8Is(osutils._walkdirs_unicode_to_utf8)
1234
975
 
1235
976
    def test_force_walkdirs_utf8_nt(self):
1236
 
        # Disabled because the thunk of the whole walkdirs api is disabled.
1237
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
977
        self.requireFeature(WalkdirsWin32Feature)
1238
978
        self._save_platform_info()
1239
979
        win32utils.winver = 'Windows NT'
1240
 
        from bzrlib._walkdirs_win32 import Win32ReadDir
1241
 
        self.assertDirReaderIs(Win32ReadDir)
 
980
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
 
981
        self.assertWalkdirsUtf8Is(_walkdirs_utf8_win32_find_file)
1242
982
 
1243
 
    def test_force_walkdirs_utf8_98(self):
1244
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
983
    def test_force_walkdirs_utf8_nt(self):
 
984
        self.requireFeature(WalkdirsWin32Feature)
1245
985
        self._save_platform_info()
1246
986
        win32utils.winver = 'Windows 98'
1247
 
        self.assertDirReaderIs(osutils.UnicodeDirReader)
 
987
        self.assertWalkdirsUtf8Is(osutils._walkdirs_unicode_to_utf8)
1248
988
 
1249
989
    def test_unicode_walkdirs(self):
1250
990
        """Walkdirs should always return unicode paths."""
1251
 
        self.requireFeature(features.UnicodeFilenameFeature)
1252
991
        name0 = u'0file-\xb6'
1253
992
        name1 = u'1dir-\u062c\u0648'
1254
993
        name2 = u'2file-\u0633'
1259
998
            name1 + '/' + name1 + '/',
1260
999
            name2,
1261
1000
            ]
1262
 
        self.build_tree(tree)
 
1001
        try:
 
1002
            self.build_tree(tree)
 
1003
        except UnicodeError:
 
1004
            raise TestSkipped('Could not represent Unicode chars'
 
1005
                              ' in current encoding.')
1263
1006
        expected_dirblocks = [
1264
1007
                ((u'', u'.'),
1265
1008
                 [(name0, name0, 'file', './' + name0),
1291
1034
 
1292
1035
        The abspath portion might be in unicode or utf-8
1293
1036
        """
1294
 
        self.requireFeature(features.UnicodeFilenameFeature)
1295
1037
        name0 = u'0file-\xb6'
1296
1038
        name1 = u'1dir-\u062c\u0648'
1297
1039
        name2 = u'2file-\u0633'
1302
1044
            name1 + '/' + name1 + '/',
1303
1045
            name2,
1304
1046
            ]
1305
 
        self.build_tree(tree)
 
1047
        try:
 
1048
            self.build_tree(tree)
 
1049
        except UnicodeError:
 
1050
            raise TestSkipped('Could not represent Unicode chars'
 
1051
                              ' in current encoding.')
1306
1052
        name0 = name0.encode('utf8')
1307
1053
        name1 = name1.encode('utf8')
1308
1054
        name2 = name2.encode('utf8')
1347
1093
            result.append((dirdetail, new_dirblock))
1348
1094
        self.assertEqual(expected_dirblocks, result)
1349
1095
 
1350
 
    def test__walkdirs_utf8_with_unicode_fs(self):
1351
 
        """UnicodeDirReader should be a safe fallback everywhere
 
1096
    def test_unicode__walkdirs_unicode_to_utf8(self):
 
1097
        """walkdirs_unicode_to_utf8 should be a safe fallback everywhere
1352
1098
 
1353
1099
        The abspath portion should be in unicode
1354
1100
        """
1355
 
        self.requireFeature(features.UnicodeFilenameFeature)
1356
 
        # Use the unicode reader. TODO: split into driver-and-driven unit
1357
 
        # tests.
1358
 
        self._save_platform_info()
1359
 
        osutils._selected_dir_reader = osutils.UnicodeDirReader()
1360
1101
        name0u = u'0file-\xb6'
1361
1102
        name1u = u'1dir-\u062c\u0648'
1362
1103
        name2u = u'2file-\u0633'
1367
1108
            name1u + '/' + name1u + '/',
1368
1109
            name2u,
1369
1110
            ]
1370
 
        self.build_tree(tree)
 
1111
        try:
 
1112
            self.build_tree(tree)
 
1113
        except UnicodeError:
 
1114
            raise TestSkipped('Could not represent Unicode chars'
 
1115
                              ' in current encoding.')
1371
1116
        name0 = name0u.encode('utf8')
1372
1117
        name1 = name1u.encode('utf8')
1373
1118
        name2 = name2u.encode('utf8')
1393
1138
                 ]
1394
1139
                ),
1395
1140
            ]
1396
 
        result = list(osutils._walkdirs_utf8('.'))
 
1141
        result = list(osutils._walkdirs_unicode_to_utf8('.'))
1397
1142
        self._filter_out_stat(result)
1398
1143
        self.assertEqual(expected_dirblocks, result)
1399
1144
 
1400
 
    def test__walkdirs_utf8_win32readdir(self):
1401
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1402
 
        self.requireFeature(features.UnicodeFilenameFeature)
1403
 
        from bzrlib._walkdirs_win32 import Win32ReadDir
1404
 
        self._save_platform_info()
1405
 
        osutils._selected_dir_reader = Win32ReadDir()
 
1145
    def test__walkdirs_utf_win32_find_file(self):
 
1146
        self.requireFeature(WalkdirsWin32Feature)
 
1147
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1148
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
1406
1149
        name0u = u'0file-\xb6'
1407
1150
        name1u = u'1dir-\u062c\u0648'
1408
1151
        name2u = u'2file-\u0633'
1439
1182
                 ]
1440
1183
                ),
1441
1184
            ]
1442
 
        result = list(osutils._walkdirs_utf8(u'.'))
 
1185
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
1443
1186
        self._filter_out_stat(result)
1444
1187
        self.assertEqual(expected_dirblocks, result)
1445
1188
 
1455
1198
 
1456
1199
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1457
1200
        """make sure our Stat values are valid"""
1458
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1459
 
        self.requireFeature(features.UnicodeFilenameFeature)
1460
 
        from bzrlib._walkdirs_win32 import Win32ReadDir
 
1201
        self.requireFeature(WalkdirsWin32Feature)
 
1202
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1203
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
1461
1204
        name0u = u'0file-\xb6'
1462
1205
        name0 = name0u.encode('utf8')
1463
1206
        self.build_tree([name0u])
1470
1213
        finally:
1471
1214
            f.close()
1472
1215
 
1473
 
        result = Win32ReadDir().read_dir('', u'.')
1474
 
        entry = result[0]
 
1216
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
 
1217
        entry = result[0][1][0]
1475
1218
        self.assertEqual((name0, name0, 'file'), entry[:3])
1476
1219
        self.assertEqual(u'./' + name0u, entry[4])
1477
1220
        self.assertStatIsCorrect(entry[4], entry[3])
1479
1222
 
1480
1223
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1481
1224
        """make sure our Stat values are valid"""
1482
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1483
 
        self.requireFeature(features.UnicodeFilenameFeature)
1484
 
        from bzrlib._walkdirs_win32 import Win32ReadDir
 
1225
        self.requireFeature(WalkdirsWin32Feature)
 
1226
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1227
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
1485
1228
        name0u = u'0dir-\u062c\u0648'
1486
1229
        name0 = name0u.encode('utf8')
1487
1230
        self.build_tree([name0u + '/'])
1488
1231
 
1489
 
        result = Win32ReadDir().read_dir('', u'.')
1490
 
        entry = result[0]
 
1232
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
 
1233
        entry = result[0][1][0]
1491
1234
        self.assertEqual((name0, name0, 'directory'), entry[:3])
1492
1235
        self.assertEqual(u'./' + name0u, entry[4])
1493
1236
        self.assertStatIsCorrect(entry[4], entry[3])
1570
1313
            sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
1571
1314
 
1572
1315
 
1573
 
class TestCopyTree(tests.TestCaseInTempDir):
1574
 
 
 
1316
class TestCopyTree(TestCaseInTempDir):
 
1317
    
1575
1318
    def test_copy_basic_tree(self):
1576
1319
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1577
1320
        osutils.copy_tree('source', 'target')
1586
1329
        self.assertEqual(['c'], os.listdir('target/b'))
1587
1330
 
1588
1331
    def test_copy_tree_symlinks(self):
1589
 
        self.requireFeature(features.SymlinkFeature)
 
1332
        self.requireFeature(SymlinkFeature)
1590
1333
        self.build_tree(['source/'])
1591
1334
        os.symlink('a/generic/path', 'source/lnk')
1592
1335
        osutils.copy_tree('source', 'target')
1617
1360
                          ('d', 'source/b', 'target/b'),
1618
1361
                          ('f', 'source/b/c', 'target/b/c'),
1619
1362
                         ], processed_files)
1620
 
        self.assertPathDoesNotExist('target')
 
1363
        self.failIfExists('target')
1621
1364
        if osutils.has_symlinks():
1622
1365
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1623
1366
 
1624
1367
 
1625
 
class TestSetUnsetEnv(tests.TestCase):
 
1368
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
 
1369
# [bialix] 2006/12/26
 
1370
 
 
1371
 
 
1372
class TestSetUnsetEnv(TestCase):
1626
1373
    """Test updating the environment"""
1627
1374
 
1628
1375
    def setUp(self):
1634
1381
        def cleanup():
1635
1382
            if 'BZR_TEST_ENV_VAR' in os.environ:
1636
1383
                del os.environ['BZR_TEST_ENV_VAR']
 
1384
 
1637
1385
        self.addCleanup(cleanup)
1638
1386
 
1639
1387
    def test_set(self):
1651
1399
 
1652
1400
    def test_unicode(self):
1653
1401
        """Environment can only contain plain strings
1654
 
 
 
1402
        
1655
1403
        So Unicode strings must be encoded.
1656
1404
        """
1657
 
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
1405
        uni_val, env_val = probe_unicode_in_user_encoding()
1658
1406
        if uni_val is None:
1659
 
            raise tests.TestSkipped(
1660
 
                'Cannot find a unicode character that works in encoding %s'
1661
 
                % (osutils.get_user_encoding(),))
 
1407
            raise TestSkipped('Cannot find a unicode character that works in'
 
1408
                              ' encoding %s' % (bzrlib.user_encoding,))
1662
1409
 
1663
1410
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
1664
1411
        self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
1669
1416
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1670
1417
        self.assertEqual('foo', old)
1671
1418
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
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):
 
1419
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
 
1420
 
 
1421
 
 
1422
class TestLocalTimeOffset(TestCase):
 
1423
 
 
1424
    def test_local_time_offset(self):
 
1425
        """Test that local_time_offset() returns a sane value."""
 
1426
        offset = osutils.local_time_offset()
 
1427
        self.assertTrue(isinstance(offset, int))
 
1428
        # Test that the offset is no more than a eighteen hours in
 
1429
        # either direction.
 
1430
        # Time zone handling is system specific, so it is difficult to
 
1431
        # do more specific tests, but a value outside of this range is
 
1432
        # probably wrong.
 
1433
        eighteen_hours = 18 * 3600
 
1434
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
1435
 
 
1436
    def test_local_time_offset_with_timestamp(self):
 
1437
        """Test that local_time_offset() works with a timestamp."""
 
1438
        offset = osutils.local_time_offset(1000000000.1234567)
 
1439
        self.assertTrue(isinstance(offset, int))
 
1440
        eighteen_hours = 18 * 3600
 
1441
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
1442
 
 
1443
 
 
1444
class TestShaFileByName(TestCaseInTempDir):
 
1445
 
 
1446
    def test_sha_empty(self):
 
1447
        self.build_tree_contents([('foo', '')])
 
1448
        expected_sha = osutils.sha_string('')
 
1449
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1450
 
 
1451
    def test_sha_mixed_endings(self):
 
1452
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
 
1453
        self.build_tree_contents([('foo', text)])
 
1454
        expected_sha = osutils.sha_string(text)
 
1455
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1456
 
 
1457
 
 
1458
_debug_text = \
 
1459
r'''# Copyright (C) 2005, 2006 Canonical Ltd
 
1460
#
 
1461
# This program is free software; you can redistribute it and/or modify
 
1462
# it under the terms of the GNU General Public License as published by
 
1463
# the Free Software Foundation; either version 2 of the License, or
 
1464
# (at your option) any later version.
 
1465
#
 
1466
# This program is distributed in the hope that it will be useful,
 
1467
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
1468
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
1469
# GNU General Public License for more details.
 
1470
#
 
1471
# You should have received a copy of the GNU General Public License
 
1472
# along with this program; if not, write to the Free Software
 
1473
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
1474
 
 
1475
 
 
1476
# NOTE: If update these, please also update the help for global-options in
 
1477
#       bzrlib/help_topics/__init__.py
 
1478
 
 
1479
debug_flags = set()
 
1480
"""Set of flags that enable different debug behaviour.
 
1481
 
 
1482
These are set with eg ``-Dlock`` on the bzr command line.
 
1483
 
 
1484
Options include:
 
1485
 
 
1486
 * auth - show authentication sections used
 
1487
 * error - show stack traces for all top level exceptions
 
1488
 * evil - capture call sites that do expensive or badly-scaling operations.
 
1489
 * fetch - trace history copying between repositories
 
1490
 * graph - trace graph traversal information
 
1491
 * hashcache - log every time a working file is read to determine its hash
 
1492
 * hooks - trace hook execution
 
1493
 * hpss - trace smart protocol requests and responses
 
1494
 * http - trace http connections, requests and responses
 
1495
 * index - trace major index operations
 
1496
 * knit - trace knit operations
 
1497
 * lock - trace when lockdir locks are taken or released
 
1498
 * merge - emit information for debugging merges
 
1499
 * pack - emit information about pack operations
 
1500
 
 
1501
"""
 
1502
'''
 
1503
 
 
1504
 
 
1505
class TestResourceLoading(TestCaseInTempDir):
1712
1506
 
1713
1507
    def test_resource_string(self):
1714
1508
        # test resource in bzrlib
1715
1509
        text = osutils.resource_string('bzrlib', 'debug.py')
1716
 
        self.assertContainsRe(text, "debug_flags = set()")
 
1510
        self.assertEquals(_debug_text, text)
1717
1511
        # test resource under bzrlib
1718
1512
        text = osutils.resource_string('bzrlib.ui', 'text.py')
1719
1513
        self.assertContainsRe(text, "class TextUIFactory")
1722
1516
            'yyy.xx')
1723
1517
        # test unknown resource
1724
1518
        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)