/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: Aaron Bentley
  • Date: 2006-11-17 04:06:03 UTC
  • mfrom: (2139 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2162.
  • Revision ID: aaron.bentley@utoronto.ca-20061117040603-pgebxndswvwk26tt
Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
23
23
import sys
24
24
 
25
25
import bzrlib
 
26
from bzrlib import (
 
27
    errors,
 
28
    osutils,
 
29
    )
26
30
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
27
 
import bzrlib.osutils as osutils
28
31
from bzrlib.tests import (
29
32
        StringIOWrapper,
30
33
        TestCase, 
78
81
        self.assertEqual(type(result), str)
79
82
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
80
83
 
 
84
    def test_is_inside(self):
 
85
        is_inside = osutils.is_inside
 
86
        self.assertTrue(is_inside('src', 'src/foo.c'))
 
87
        self.assertFalse(is_inside('src', 'srccontrol'))
 
88
        self.assertTrue(is_inside('src', 'src/a/a/a/foo.c'))
 
89
        self.assertTrue(is_inside('foo.c', 'foo.c'))
 
90
        self.assertFalse(is_inside('foo.c', ''))
 
91
        self.assertTrue(is_inside('', 'foo.c'))
81
92
 
82
93
    def test_rmtree(self):
83
94
        # Check to remove tree with read-only files/dirs
150
161
        finally:
151
162
            os.umask(orig_umask)
152
163
 
 
164
    def assertFormatedDelta(self, expected, seconds):
 
165
        """Assert osutils.format_delta formats as expected"""
 
166
        actual = osutils.format_delta(seconds)
 
167
        self.assertEqual(expected, actual)
 
168
 
 
169
    def test_format_delta(self):
 
170
        self.assertFormatedDelta('0 seconds ago', 0)
 
171
        self.assertFormatedDelta('1 second ago', 1)
 
172
        self.assertFormatedDelta('10 seconds ago', 10)
 
173
        self.assertFormatedDelta('59 seconds ago', 59)
 
174
        self.assertFormatedDelta('89 seconds ago', 89)
 
175
        self.assertFormatedDelta('1 minute, 30 seconds ago', 90)
 
176
        self.assertFormatedDelta('3 minutes, 0 seconds ago', 180)
 
177
        self.assertFormatedDelta('3 minutes, 1 second ago', 181)
 
178
        self.assertFormatedDelta('10 minutes, 15 seconds ago', 615)
 
179
        self.assertFormatedDelta('30 minutes, 59 seconds ago', 1859)
 
180
        self.assertFormatedDelta('31 minutes, 0 seconds ago', 1860)
 
181
        self.assertFormatedDelta('60 minutes, 0 seconds ago', 3600)
 
182
        self.assertFormatedDelta('89 minutes, 59 seconds ago', 5399)
 
183
        self.assertFormatedDelta('1 hour, 30 minutes ago', 5400)
 
184
        self.assertFormatedDelta('2 hours, 30 minutes ago', 9017)
 
185
        self.assertFormatedDelta('10 hours, 0 minutes ago', 36000)
 
186
        self.assertFormatedDelta('24 hours, 0 minutes ago', 86400)
 
187
        self.assertFormatedDelta('35 hours, 59 minutes ago', 129599)
 
188
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129600)
 
189
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129601)
 
190
        self.assertFormatedDelta('36 hours, 1 minute ago', 129660)
 
191
        self.assertFormatedDelta('36 hours, 1 minute ago', 129661)
 
192
        self.assertFormatedDelta('84 hours, 10 minutes ago', 303002)
 
193
 
 
194
        # We handle when time steps the wrong direction because computers
 
195
        # don't have synchronized clocks.
 
196
        self.assertFormatedDelta('84 hours, 10 minutes in the future', -303002)
 
197
        self.assertFormatedDelta('1 second in the future', -1)
 
198
        self.assertFormatedDelta('2 seconds in the future', -2)
 
199
 
 
200
    def test_dereference_path(self):
 
201
        if not osutils.has_symlinks():
 
202
            raise TestSkipped('Symlinks are not supported on this platform')
 
203
        cwd = osutils.realpath('.')
 
204
        os.mkdir('bar')
 
205
        bar_path = osutils.pathjoin(cwd, 'bar')
 
206
        # Using './' to avoid bug #1213894 (first path component not
 
207
        # dereferenced) in Python 2.4.1 and earlier
 
208
        self.assertEqual(bar_path, osutils.realpath('./bar'))
 
209
        os.symlink('bar', 'foo')
 
210
        self.assertEqual(bar_path, osutils.realpath('./foo'))
 
211
        
 
212
        # Does not dereference terminal symlinks
 
213
        foo_path = osutils.pathjoin(cwd, 'foo')
 
214
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
 
215
 
 
216
        # Dereferences parent symlinks
 
217
        os.mkdir('bar/baz')
 
218
        baz_path = osutils.pathjoin(bar_path, 'baz')
 
219
        self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
 
220
 
 
221
        # Dereferences parent symlinks that are the first path element
 
222
        self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
 
223
 
 
224
        # Dereferences parent symlinks in absolute paths
 
225
        foo_baz_path = osutils.pathjoin(foo_path, 'baz')
 
226
        self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
 
227
 
153
228
 
154
229
class TestSafeUnicode(TestCase):
155
230
 
275
350
        except (IOError, OSError), e:
276
351
            self.assertEqual(errno.ENOENT, e.errno)
277
352
 
 
353
    def test_splitpath(self):
 
354
        def check(expected, path):
 
355
            self.assertEqual(expected, osutils.splitpath(path))
 
356
 
 
357
        check(['a'], 'a')
 
358
        check(['a', 'b'], 'a/b')
 
359
        check(['a', 'b'], 'a/./b')
 
360
        check(['a', '.b'], 'a/.b')
 
361
        check(['a', '.b'], 'a\\.b')
 
362
 
 
363
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
 
364
 
278
365
 
279
366
class TestMacFuncsDirs(TestCaseInTempDir):
280
367
    """Test mac special functions that require directories."""
300
387
        os.chdir(u'Ba\u030agfors')
301
388
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
302
389
 
 
390
 
303
391
class TestSplitLines(TestCase):
304
392
 
305
393
    def test_split_unicode(self):
444
532
    def test_copy_basic_tree(self):
445
533
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
446
534
        osutils.copy_tree('source', 'target')
447
 
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
535
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
448
536
        self.assertEqual(['c'], os.listdir('target/b'))
449
537
 
450
538
    def test_copy_tree_target_exists(self):
451
539
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
452
540
                         'target/'])
453
541
        osutils.copy_tree('source', 'target')
454
 
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
542
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
455
543
        self.assertEqual(['c'], os.listdir('target/b'))
456
544
 
457
545
    def test_copy_tree_symlinks(self):