/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: John Arbash Meinel
  • Date: 2006-05-10 19:43:34 UTC
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060510194334-0c20aad23237d047
Working on getting normalize_url working.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests for the osutils wrapper."""
 
18
 
 
19
import os
 
20
import sys
 
21
 
 
22
import bzrlib
 
23
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
 
24
import bzrlib.osutils as osutils
 
25
from bzrlib.tests import TestCaseInTempDir, TestCase
 
26
 
 
27
 
 
28
class TestOSUtils(TestCaseInTempDir):
 
29
 
 
30
    def test_fancy_rename(self):
 
31
        # This should work everywhere
 
32
        def rename(a, b):
 
33
            osutils.fancy_rename(a, b,
 
34
                    rename_func=os.rename,
 
35
                    unlink_func=os.unlink)
 
36
 
 
37
        open('a', 'wb').write('something in a\n')
 
38
        rename('a', 'b')
 
39
        self.failIfExists('a')
 
40
        self.failUnlessExists('b')
 
41
        self.check_file_contents('b', 'something in a\n')
 
42
 
 
43
        open('a', 'wb').write('new something in a\n')
 
44
        rename('b', 'a')
 
45
 
 
46
        self.check_file_contents('a', 'something in a\n')
 
47
 
 
48
    def test_rename(self):
 
49
        # Rename should be semi-atomic on all platforms
 
50
        open('a', 'wb').write('something in a\n')
 
51
        osutils.rename('a', 'b')
 
52
        self.failIfExists('a')
 
53
        self.failUnlessExists('b')
 
54
        self.check_file_contents('b', 'something in a\n')
 
55
 
 
56
        open('a', 'wb').write('new something in a\n')
 
57
        osutils.rename('b', 'a')
 
58
 
 
59
        self.check_file_contents('a', 'something in a\n')
 
60
 
 
61
    # TODO: test fancy_rename using a MemoryTransport
 
62
 
 
63
    def test_01_rand_chars_empty(self):
 
64
        result = osutils.rand_chars(0)
 
65
        self.assertEqual(result, '')
 
66
 
 
67
    def test_02_rand_chars_100(self):
 
68
        result = osutils.rand_chars(100)
 
69
        self.assertEqual(len(result), 100)
 
70
        self.assertEqual(type(result), str)
 
71
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
 
72
 
 
73
 
 
74
class TestSafeUnicode(TestCase):
 
75
 
 
76
    def test_from_ascii_string(self):
 
77
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
 
78
 
 
79
    def test_from_unicode_string_ascii_contents(self):
 
80
        self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
 
81
 
 
82
    def test_from_unicode_string_unicode_contents(self):
 
83
        self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
 
84
 
 
85
    def test_from_utf8_string(self):
 
86
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
 
87
 
 
88
    def test_bad_utf8_string(self):
 
89
        self.assertRaises(BzrBadParameterNotUnicode,
 
90
                          osutils.safe_unicode,
 
91
                          '\xbb\xbb')
 
92
 
 
93
 
 
94
class TestWin32Funcs(TestCase):
 
95
    """Test that the _win32 versions of os utilities return appropriate paths."""
 
96
 
 
97
    def test_abspath(self):
 
98
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
 
99
        self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
 
100
 
 
101
    def test_realpath(self):
 
102
        self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
 
103
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
 
104
 
 
105
    def test_pathjoin(self):
 
106
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
 
107
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
 
108
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
 
109
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
 
110
        self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
 
111
        self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
 
112
 
 
113
    def test_normpath(self):
 
114
        self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
 
115
        self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
 
116
 
 
117
    def test_getcwd(self):
 
118
        self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
 
119
 
 
120
 
 
121
class TestWin32FuncsDirs(TestCaseInTempDir):
 
122
    """Test win32 functions that create files."""
 
123
    
 
124
    def test_getcwd(self):
 
125
        # Make sure getcwd can handle unicode filenames
 
126
        try:
 
127
            os.mkdir(u'B\xe5gfors')
 
128
        except UnicodeError:
 
129
            raise TestSkipped("Unable to create Unicode filename")
 
130
 
 
131
        os.chdir(u'B\xe5gfors')
 
132
        # TODO: jam 20060427 This will probably fail on Mac OSX because
 
133
        #       it will change the normalization of B\xe5gfors
 
134
        #       Consider using a different unicode character, or make
 
135
        #       osutils.getcwd() renormalize the path.
 
136
        self.assertTrue(osutils._win32_getcwd().endswith(u'/B\xe5gfors'))
 
137
 
 
138
    def test_mkdtemp(self):
 
139
        tmpdir = osutils._win32_mkdtemp(dir='.')
 
140
        self.assertFalse('\\' in tmpdir)
 
141
 
 
142
    def test_rename(self):
 
143
        a = open('a', 'wb')
 
144
        a.write('foo\n')
 
145
        a.close()
 
146
        b = open('b', 'wb')
 
147
        b.write('baz\n')
 
148
        b.close()
 
149
 
 
150
        osutils._win32_rename('b', 'a')
 
151
        self.failUnlessExists('a')
 
152
        self.failIfExists('b')
 
153
        self.assertFileEqual('baz\n', 'a')
 
154
 
 
155
 
 
156
class TestSplitLines(TestCase):
 
157
 
 
158
    def test_split_unicode(self):
 
159
        self.assertEqual([u'foo\n', u'bar\xae'],
 
160
                         osutils.split_lines(u'foo\nbar\xae'))
 
161
        self.assertEqual([u'foo\n', u'bar\xae\n'],
 
162
                         osutils.split_lines(u'foo\nbar\xae\n'))
 
163
 
 
164
    def test_split_with_carriage_returns(self):
 
165
        self.assertEqual(['foo\rbar\n'],
 
166
                         osutils.split_lines('foo\rbar\n'))