/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
1692.7.6 by Martin Pool
[patch] force deletion of trees containing readonly files (alexander)
74
    def test_rmtree(self):
75
        # Check to remove tree with read-only files/dirs
76
        os.mkdir('dir')
77
        f = file('dir/file', 'w')
78
        f.write('spam')
79
        f.close()
80
        # would like to also try making the directory readonly, but at the
81
        # moment python shutil.rmtree doesn't handle that properly - it would
82
        # need to chmod the directory before removing things inside it - deferred
83
        # for now -- mbp 20060505
84
        # osutils.make_readonly('dir')
85
        osutils.make_readonly('dir/file')
86
87
        osutils.rmtree('dir')
88
89
        self.failIfExists('dir/file')
90
        self.failIfExists('dir')
91
92
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
93
class TestSafeUnicode(TestCase):
94
95
    def test_from_ascii_string(self):
96
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
97
1534.3.2 by Robert Collins
An extra test for John.
98
    def test_from_unicode_string_ascii_contents(self):
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
99
        self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
100
1534.3.2 by Robert Collins
An extra test for John.
101
    def test_from_unicode_string_unicode_contents(self):
102
        self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
103
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
104
    def test_from_utf8_string(self):
105
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
106
107
    def test_bad_utf8_string(self):
1185.65.29 by Robert Collins
Implement final review suggestions.
108
        self.assertRaises(BzrBadParameterNotUnicode,
109
                          osutils.safe_unicode,
110
                          '\xbb\xbb')
1666.1.6 by Robert Collins
Make knit the default format.
111
112
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
113
class TestWin32Funcs(TestCase):
114
    """Test that the _win32 versions of os utilities return appropriate paths."""
115
116
    def test_abspath(self):
117
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
118
        self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
119
120
    def test_realpath(self):
121
        self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
122
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
123
124
    def test_pathjoin(self):
125
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
126
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
127
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
128
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
129
        self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
130
        self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
131
132
    def test_normpath(self):
133
        self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
134
        self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
135
136
    def test_getcwd(self):
137
        self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
138
139
140
class TestWin32FuncsDirs(TestCaseInTempDir):
141
    """Test win32 functions that create files."""
142
    
143
    def test_getcwd(self):
144
        # Make sure getcwd can handle unicode filenames
145
        try:
146
            os.mkdir(u'B\xe5gfors')
147
        except UnicodeError:
148
            raise TestSkipped("Unable to create Unicode filename")
149
150
        os.chdir(u'B\xe5gfors')
151
        # TODO: jam 20060427 This will probably fail on Mac OSX because
152
        #       it will change the normalization of B\xe5gfors
153
        #       Consider using a different unicode character, or make
154
        #       osutils.getcwd() renormalize the path.
155
        self.assertTrue(osutils._win32_getcwd().endswith(u'/B\xe5gfors'))
156
157
    def test_mkdtemp(self):
158
        tmpdir = osutils._win32_mkdtemp(dir='.')
159
        self.assertFalse('\\' in tmpdir)
160
161
    def test_rename(self):
162
        a = open('a', 'wb')
163
        a.write('foo\n')
164
        a.close()
165
        b = open('b', 'wb')
166
        b.write('baz\n')
167
        b.close()
168
169
        osutils._win32_rename('b', 'a')
170
        self.failUnlessExists('a')
171
        self.failIfExists('b')
172
        self.assertFileEqual('baz\n', 'a')
173
174
1666.1.6 by Robert Collins
Make knit the default format.
175
class TestSplitLines(TestCase):
176
177
    def test_split_unicode(self):
178
        self.assertEqual([u'foo\n', u'bar\xae'],
179
                         osutils.split_lines(u'foo\nbar\xae'))
180
        self.assertEqual([u'foo\n', u'bar\xae\n'],
181
                         osutils.split_lines(u'foo\nbar\xae\n'))
182
183
    def test_split_with_carriage_returns(self):
184
        self.assertEqual(['foo\rbar\n'],
185
                         osutils.split_lines('foo\rbar\n'))