/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
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
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
17
"""Tests for the osutils wrapper."""
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
18
19
import os
20
import sys
21
22
import bzrlib
1685.1.9 by John Arbash Meinel
Updated LocalTransport so that it's base is now a URL rather than a local path. This helps consistency with all other functions. To do so, I added local_abspath() which returns the local path, and local_path_to/from_url
23
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
1532 by Robert Collins
Merge in John Meinels integration branch.
24
import bzrlib.osutils as osutils
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
25
from bzrlib.tests import TestCaseInTempDir, TestCase
1532 by Robert Collins
Merge in John Meinels integration branch.
26
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
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
1553.5.5 by Martin Pool
New utility routine rand_chars
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
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
73
74
class TestSafeUnicode(TestCase):
75
76
    def test_from_ascii_string(self):
77
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
78
1534.3.2 by Robert Collins
An extra test for John.
79
    def test_from_unicode_string_ascii_contents(self):
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
80
        self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
81
1534.3.2 by Robert Collins
An extra test for John.
82
    def test_from_unicode_string_unicode_contents(self):
83
        self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
84
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
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):
1185.65.29 by Robert Collins
Implement final review suggestions.
89
        self.assertRaises(BzrBadParameterNotUnicode,
90
                          osutils.safe_unicode,
91
                          '\xbb\xbb')
1666.1.6 by Robert Collins
Make knit the default format.
92
93
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
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
1666.1.6 by Robert Collins
Make knit the default format.
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'))