/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Jan Balster
  • Date: 2006-08-15 12:39:42 UTC
  • mfrom: (1923 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1928.
  • Revision ID: jan@merlinux.de-20060815123942-22c388c6e9a8ac91
merge bzr.dev 1923

Show diffs side-by-side

added added

removed removed

Lines of Context:
131
131
            finally:
132
132
                os.remove('socket')
133
133
 
 
134
    def test_get_umask(self):
 
135
        if sys.platform == 'win32':
 
136
            # umask always returns '0', no way to set it
 
137
            self.assertEqual(0, osutils.get_umask())
 
138
            return
 
139
 
 
140
        orig_umask = osutils.get_umask()
 
141
        try:
 
142
            os.umask(0222)
 
143
            self.assertEqual(0222, osutils.get_umask())
 
144
            os.umask(0022)
 
145
            self.assertEqual(0022, osutils.get_umask())
 
146
            os.umask(0002)
 
147
            self.assertEqual(0002, osutils.get_umask())
 
148
            os.umask(0027)
 
149
            self.assertEqual(0027, osutils.get_umask())
 
150
        finally:
 
151
            os.umask(orig_umask)
 
152
 
134
153
 
135
154
class TestSafeUnicode(TestCase):
136
155
 
176
195
        self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
177
196
 
178
197
    def test_getcwd(self):
179
 
        self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
 
198
        cwd = osutils._win32_getcwd()
 
199
        os_cwd = os.getcwdu()
 
200
        self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])
 
201
        # win32 is inconsistent whether it returns lower or upper case
 
202
        # and even if it was consistent the user might type the other
 
203
        # so we force it to uppercase
 
204
        # running python.exe under cmd.exe return capital C:\\
 
205
        # running win32 python inside a cygwin shell returns lowercase
 
206
        self.assertEqual(os_cwd[0].upper(), cwd[0])
 
207
 
 
208
    def test_fixdrive(self):
 
209
        self.assertEqual('H:/foo', osutils._win32_fixdrive('h:/foo'))
 
210
        self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
 
211
        self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
180
212
 
181
213
 
182
214
class TestWin32FuncsDirs(TestCaseInTempDir):
185
217
    def test_getcwd(self):
186
218
        # Make sure getcwd can handle unicode filenames
187
219
        try:
188
 
            os.mkdir(u'B\xe5gfors')
 
220
            os.mkdir(u'mu-\xb5')
189
221
        except UnicodeError:
190
222
            raise TestSkipped("Unable to create Unicode filename")
191
223
 
192
 
        os.chdir(u'B\xe5gfors')
 
224
        os.chdir(u'mu-\xb5')
193
225
        # TODO: jam 20060427 This will probably fail on Mac OSX because
194
226
        #       it will change the normalization of B\xe5gfors
195
227
        #       Consider using a different unicode character, or make
196
228
        #       osutils.getcwd() renormalize the path.
197
 
        self.assertTrue(osutils._win32_getcwd().endswith(u'/B\xe5gfors'))
 
229
        self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
198
230
 
199
231
    def test_mkdtemp(self):
200
232
        tmpdir = osutils._win32_mkdtemp(dir='.')
213
245
        self.failIfExists('b')
214
246
        self.assertFileEqual('baz\n', 'a')
215
247
 
 
248
    def test_rename_missing_file(self):
 
249
        a = open('a', 'wb')
 
250
        a.write('foo\n')
 
251
        a.close()
 
252
 
 
253
        try:
 
254
            osutils._win32_rename('b', 'a')
 
255
        except (IOError, OSError), e:
 
256
            self.assertEqual(errno.ENOENT, e.errno)
 
257
        self.assertFileEqual('foo\n', 'a')
 
258
 
 
259
    def test_rename_missing_dir(self):
 
260
        os.mkdir('a')
 
261
        try:
 
262
            osutils._win32_rename('b', 'a')
 
263
        except (IOError, OSError), e:
 
264
            self.assertEqual(errno.ENOENT, e.errno)
 
265
 
 
266
    def test_rename_current_dir(self):
 
267
        os.mkdir('a')
 
268
        os.chdir('a')
 
269
        # You can't rename the working directory
 
270
        # doing rename non-existant . usually
 
271
        # just raises ENOENT, since non-existant
 
272
        # doesn't exist.
 
273
        try:
 
274
            osutils._win32_rename('b', '.')
 
275
        except (IOError, OSError), e:
 
276
            self.assertEqual(errno.ENOENT, e.errno)
 
277
 
 
278
 
 
279
class TestMacFuncsDirs(TestCaseInTempDir):
 
280
    """Test mac special functions that require directories."""
 
281
 
 
282
    def test_getcwd(self):
 
283
        # On Mac, this will actually create Ba\u030agfors
 
284
        # but chdir will still work, because it accepts both paths
 
285
        try:
 
286
            os.mkdir(u'B\xe5gfors')
 
287
        except UnicodeError:
 
288
            raise TestSkipped("Unable to create Unicode filename")
 
289
 
 
290
        os.chdir(u'B\xe5gfors')
 
291
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
 
292
 
 
293
    def test_getcwd_nonnorm(self):
 
294
        # Test that _mac_getcwd() will normalize this path
 
295
        try:
 
296
            os.mkdir(u'Ba\u030agfors')
 
297
        except UnicodeError:
 
298
            raise TestSkipped("Unable to create Unicode filename")
 
299
 
 
300
        os.chdir(u'Ba\u030agfors')
 
301
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
216
302
 
217
303
class TestSplitLines(TestCase):
218
304
 
240
326
            ]
241
327
        self.build_tree(tree)
242
328
        expected_dirblocks = [
243
 
                [
244
 
                    ('0file', '0file', 'file'),
245
 
                    ('1dir', '1dir', 'directory'),
246
 
                    ('2file', '2file', 'file'),
247
 
                ],
248
 
                [
249
 
                    ('1dir/0file', '0file', 'file'),
250
 
                    ('1dir/1dir', '1dir', 'directory'),
251
 
                ],
252
 
                [
253
 
                ],
 
329
                (('', '.'),
 
330
                 [('0file', '0file', 'file'),
 
331
                  ('1dir', '1dir', 'directory'),
 
332
                  ('2file', '2file', 'file'),
 
333
                 ]
 
334
                ),
 
335
                (('1dir', './1dir'),
 
336
                 [('1dir/0file', '0file', 'file'),
 
337
                  ('1dir/1dir', '1dir', 'directory'),
 
338
                 ]
 
339
                ),
 
340
                (('1dir/1dir', './1dir/1dir'),
 
341
                 [
 
342
                 ]
 
343
                ),
254
344
            ]
255
345
        result = []
256
346
        found_bzrdir = False
257
 
        for dirblock in osutils.walkdirs('.'):
 
347
        for dirdetail, dirblock in osutils.walkdirs('.'):
258
348
            if len(dirblock) and dirblock[0][1] == '.bzr':
259
349
                # this tests the filtering of selected paths
260
350
                found_bzrdir = True
261
351
                del dirblock[0]
262
 
            result.append(dirblock)
 
352
            result.append((dirdetail, dirblock))
263
353
 
264
354
        self.assertTrue(found_bzrdir)
265
355
        self.assertEqual(expected_dirblocks,
266
 
            [[line[0:3] for line in block] for block in result])
 
356
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
267
357
        # you can search a subdir only, with a supplied prefix.
268
358
        result = []
269
 
        for dirblock in osutils.walkdirs('1dir', '1dir'):
 
359
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
270
360
            result.append(dirblock)
271
361
        self.assertEqual(expected_dirblocks[1:],
272
 
            [[line[0:3] for line in block] for block in result])
 
362
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
273
363
 
274
364
    def assertPathCompare(self, path_less, path_greater):
275
365
        """check that path_less and path_greater compare correctly."""
349
439
            sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
350
440
 
351
441
 
 
442
class TestCopyTree(TestCaseInTempDir):
 
443
    
 
444
    def test_copy_basic_tree(self):
 
445
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
 
446
        osutils.copy_tree('source', 'target')
 
447
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
448
        self.assertEqual(['c'], os.listdir('target/b'))
 
449
 
 
450
    def test_copy_tree_target_exists(self):
 
451
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
 
452
                         'target/'])
 
453
        osutils.copy_tree('source', 'target')
 
454
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
455
        self.assertEqual(['c'], os.listdir('target/b'))
 
456
 
 
457
    def test_copy_tree_symlinks(self):
 
458
        if not osutils.has_symlinks():
 
459
            return
 
460
        self.build_tree(['source/'])
 
461
        os.symlink('a/generic/path', 'source/lnk')
 
462
        osutils.copy_tree('source', 'target')
 
463
        self.assertEqual(['lnk'], os.listdir('target'))
 
464
        self.assertEqual('a/generic/path', os.readlink('target/lnk'))
 
465
 
 
466
    def test_copy_tree_handlers(self):
 
467
        processed_files = []
 
468
        processed_links = []
 
469
        def file_handler(from_path, to_path):
 
470
            processed_files.append(('f', from_path, to_path))
 
471
        def dir_handler(from_path, to_path):
 
472
            processed_files.append(('d', from_path, to_path))
 
473
        def link_handler(from_path, to_path):
 
474
            processed_links.append((from_path, to_path))
 
475
        handlers = {'file':file_handler,
 
476
                    'directory':dir_handler,
 
477
                    'symlink':link_handler,
 
478
                   }
 
479
 
 
480
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
 
481
        if osutils.has_symlinks():
 
482
            os.symlink('a/generic/path', 'source/lnk')
 
483
        osutils.copy_tree('source', 'target', handlers=handlers)
 
484
 
 
485
        self.assertEqual([('d', 'source', 'target'),
 
486
                          ('f', 'source/a', 'target/a'),
 
487
                          ('d', 'source/b', 'target/b'),
 
488
                          ('f', 'source/b/c', 'target/b/c'),
 
489
                         ], processed_files)
 
490
        self.failIfExists('target')
 
491
        if osutils.has_symlinks():
 
492
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
 
493
 
 
494
 
352
495
class TestTerminalEncoding(TestCase):
353
496
    """Test the auto-detection of proper terminal encoding."""
354
497