~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 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
29
29
    win32utils,
30
30
    )
31
31
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
 
32
from bzrlib.osutils import (
 
33
        is_inside_any,
 
34
        is_inside_or_parent_of_any,
 
35
        pathjoin,
 
36
        )
32
37
from bzrlib.tests import (
 
38
        probe_unicode_in_user_encoding,
33
39
        StringIOWrapper,
34
 
        TestCase, 
35
 
        TestCaseInTempDir, 
 
40
        SymlinkFeature,
 
41
        TestCase,
 
42
        TestCaseInTempDir,
36
43
        TestSkipped,
37
44
        )
38
45
 
86
93
 
87
94
    # TODO: test fancy_rename using a MemoryTransport
88
95
 
 
96
    def test_rename_change_case(self):
 
97
        # on Windows we should be able to change filename case by rename
 
98
        self.build_tree(['a', 'b/'])
 
99
        osutils.rename('a', 'A')
 
100
        osutils.rename('b', 'B')
 
101
        # we can't use failUnlessExists on case-insensitive filesystem
 
102
        # so try to check shape of the tree
 
103
        shape = sorted(os.listdir('.'))
 
104
        self.assertEquals(['A', 'B'], shape)
 
105
 
89
106
    def test_01_rand_chars_empty(self):
90
107
        result = osutils.rand_chars(0)
91
108
        self.assertEqual(result, '')
105
122
        self.assertFalse(is_inside('foo.c', ''))
106
123
        self.assertTrue(is_inside('', 'foo.c'))
107
124
 
 
125
    def test_is_inside_any(self):
 
126
        SRC_FOO_C = pathjoin('src', 'foo.c')
 
127
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
 
128
                         (['src'], SRC_FOO_C),
 
129
                         (['src'], 'src'),
 
130
                         ]:
 
131
            self.assert_(is_inside_any(dirs, fn))
 
132
        for dirs, fn in [(['src'], 'srccontrol'),
 
133
                         (['src'], 'srccontrol/foo')]:
 
134
            self.assertFalse(is_inside_any(dirs, fn))
 
135
 
 
136
    def test_is_inside_or_parent_of_any(self):
 
137
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
 
138
                         (['src'], 'src/foo.c'),
 
139
                         (['src/bar.c'], 'src'),
 
140
                         (['src/bar.c', 'bla/foo.c'], 'src'),
 
141
                         (['src'], 'src'),
 
142
                         ]:
 
143
            self.assert_(is_inside_or_parent_of_any(dirs, fn))
 
144
            
 
145
        for dirs, fn in [(['src'], 'srccontrol'),
 
146
                         (['srccontrol/foo.c'], 'src'),
 
147
                         (['src'], 'srccontrol/foo')]:
 
148
            self.assertFalse(is_inside_or_parent_of_any(dirs, fn))
 
149
 
108
150
    def test_rmtree(self):
109
151
        # Check to remove tree with read-only files/dirs
110
152
        os.mkdir('dir')
218
260
        self.assertFormatedDelta('1 second in the future', -1)
219
261
        self.assertFormatedDelta('2 seconds in the future', -2)
220
262
 
 
263
    def test_format_date(self):
 
264
        self.assertRaises(errors.UnsupportedTimezoneFormat,
 
265
            osutils.format_date, 0, timezone='foo')
 
266
 
221
267
    def test_dereference_path(self):
222
 
        if not osutils.has_symlinks():
223
 
            raise TestSkipped('Symlinks are not supported on this platform')
 
268
        self.requireFeature(SymlinkFeature)
224
269
        cwd = osutils.realpath('.')
225
270
        os.mkdir('bar')
226
271
        bar_path = osutils.pathjoin(cwd, 'bar')
246
291
        foo_baz_path = osutils.pathjoin(foo_path, 'baz')
247
292
        self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
248
293
 
 
294
    def test_changing_access(self):
 
295
        f = file('file', 'w')
 
296
        f.write('monkey')
 
297
        f.close()
 
298
 
 
299
        # Make a file readonly
 
300
        osutils.make_readonly('file')
 
301
        mode = os.lstat('file').st_mode
 
302
        self.assertEqual(mode, mode & 0777555)
 
303
 
 
304
        # Make a file writable
 
305
        osutils.make_writable('file')
 
306
        mode = os.lstat('file').st_mode
 
307
        self.assertEqual(mode, mode | 0200)
 
308
 
 
309
        if osutils.has_symlinks():
 
310
            # should not error when handed a symlink
 
311
            os.symlink('nonexistent', 'dangling')
 
312
            osutils.make_readonly('dangling')
 
313
            osutils.make_writable('dangling')
249
314
 
250
315
    def test_kind_marker(self):
251
316
        self.assertEqual("", osutils.kind_marker("file"))
297
362
class TestSafeRevisionId(TestCase):
298
363
 
299
364
    def test_from_ascii_string(self):
 
365
        # this shouldn't give a warning because it's getting an ascii string
300
366
        self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
301
367
 
302
368
    def test_from_unicode_string_ascii_contents(self):
424
490
        #       osutils.getcwd() renormalize the path.
425
491
        self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
426
492
 
 
493
    def test_minimum_path_selection(self):
 
494
        self.assertEqual(set(),
 
495
            osutils.minimum_path_selection([]))
 
496
        self.assertEqual(set(['a', 'b']),
 
497
            osutils.minimum_path_selection(['a', 'b']))
 
498
        self.assertEqual(set(['a/', 'b']),
 
499
            osutils.minimum_path_selection(['a/', 'b']))
 
500
        self.assertEqual(set(['a/', 'b']),
 
501
            osutils.minimum_path_selection(['a/c', 'a/', 'b']))
 
502
 
427
503
    def test_mkdtemp(self):
428
504
        tmpdir = osutils._win32_mkdtemp(dir='.')
429
505
        self.assertFalse('\\' in tmpdir)
875
951
        self.assertEqual(['c'], os.listdir('target/b'))
876
952
 
877
953
    def test_copy_tree_symlinks(self):
878
 
        if not osutils.has_symlinks():
879
 
            return
 
954
        self.requireFeature(SymlinkFeature)
880
955
        self.build_tree(['source/'])
881
956
        os.symlink('a/generic/path', 'source/lnk')
882
957
        osutils.copy_tree('source', 'target')
949
1024
        
950
1025
        So Unicode strings must be encoded.
951
1026
        """
952
 
        # Try a few different characters, to see if we can get
953
 
        # one that will be valid in the user_encoding
954
 
        possible_vals = [u'm\xb5', u'\xe1', u'\u0410']
955
 
        for uni_val in possible_vals:
956
 
            try:
957
 
                env_val = uni_val.encode(bzrlib.user_encoding)
958
 
            except UnicodeEncodeError:
959
 
                # Try a different character
960
 
                pass
961
 
            else:
962
 
                break
963
 
        else:
 
1027
        uni_val, env_val = probe_unicode_in_user_encoding()
 
1028
        if uni_val is None:
964
1029
            raise TestSkipped('Cannot find a unicode character that works in'
965
1030
                              ' encoding %s' % (bzrlib.user_encoding,))
966
1031
 
996
1061
        self.assertTrue(isinstance(offset, int))
997
1062
        eighteen_hours = 18 * 3600
998
1063
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
1064
 
 
1065
 
 
1066
class TestShaFileByName(TestCaseInTempDir):
 
1067
 
 
1068
    def test_sha_empty(self):
 
1069
        self.build_tree_contents([('foo', '')])
 
1070
        expected_sha = osutils.sha_string('')
 
1071
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1072
 
 
1073
    def test_sha_mixed_endings(self):
 
1074
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
 
1075
        self.build_tree_contents([('foo', text)])
 
1076
        expected_sha = osutils.sha_string(text)
 
1077
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1078
 
 
1079
 
 
1080
_debug_text = \
 
1081
r'''# Copyright (C) 2005, 2006 Canonical Ltd
 
1082
#
 
1083
# This program is free software; you can redistribute it and/or modify
 
1084
# it under the terms of the GNU General Public License as published by
 
1085
# the Free Software Foundation; either version 2 of the License, or
 
1086
# (at your option) any later version.
 
1087
#
 
1088
# This program is distributed in the hope that it will be useful,
 
1089
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
1090
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
1091
# GNU General Public License for more details.
 
1092
#
 
1093
# You should have received a copy of the GNU General Public License
 
1094
# along with this program; if not, write to the Free Software
 
1095
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
1096
 
 
1097
 
 
1098
# NOTE: If update these, please also update the help for global-options in
 
1099
#       bzrlib/help_topics/__init__.py
 
1100
 
 
1101
debug_flags = set()
 
1102
"""Set of flags that enable different debug behaviour.
 
1103
 
 
1104
These are set with eg ``-Dlock`` on the bzr command line.
 
1105
 
 
1106
Options include:
 
1107
 
 
1108
 * auth - show authentication sections used
 
1109
 * error - show stack traces for all top level exceptions
 
1110
 * evil - capture call sites that do expensive or badly-scaling operations.
 
1111
 * fetch - trace history copying between repositories
 
1112
 * hashcache - log every time a working file is read to determine its hash
 
1113
 * hooks - trace hook execution
 
1114
 * hpss - trace smart protocol requests and responses
 
1115
 * http - trace http connections, requests and responses
 
1116
 * index - trace major index operations
 
1117
 * knit - trace knit operations
 
1118
 * lock - trace when lockdir locks are taken or released
 
1119
 * merge - emit information for debugging merges
 
1120
 * pack - emit information about pack operations
 
1121
 
 
1122
"""
 
1123
'''
 
1124
 
 
1125
 
 
1126
class TestResourceLoading(TestCaseInTempDir):
 
1127
 
 
1128
    def test_resource_string(self):
 
1129
        # test resource in bzrlib
 
1130
        text = osutils.resource_string('bzrlib', 'debug.py')
 
1131
        self.assertEquals(_debug_text, text)
 
1132
        # test resource under bzrlib
 
1133
        text = osutils.resource_string('bzrlib.ui', 'text.py')
 
1134
        self.assertContainsRe(text, "class TextUIFactory")
 
1135
        # test unsupported package
 
1136
        self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
 
1137
            'yyy.xx')
 
1138
        # test unknown resource
 
1139
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')