1
# Copyright (C) 2005 by Canonical Ltd
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.
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.
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
17
"""Tests for the osutils wrapper."""
23
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
24
import bzrlib.osutils as osutils
25
from bzrlib.tests import TestCaseInTempDir, TestCase
28
class TestOSUtils(TestCaseInTempDir):
30
def test_fancy_rename(self):
31
# This should work everywhere
33
osutils.fancy_rename(a, b,
34
rename_func=os.rename,
35
unlink_func=os.unlink)
37
open('a', 'wb').write('something in a\n')
39
self.failIfExists('a')
40
self.failUnlessExists('b')
41
self.check_file_contents('b', 'something in a\n')
43
open('a', 'wb').write('new something in a\n')
46
self.check_file_contents('a', 'something in a\n')
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')
56
open('a', 'wb').write('new something in a\n')
57
osutils.rename('b', 'a')
59
self.check_file_contents('a', 'something in a\n')
61
# TODO: test fancy_rename using a MemoryTransport
63
def test_01_rand_chars_empty(self):
64
result = osutils.rand_chars(0)
65
self.assertEqual(result, '')
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}$')
74
def test_rmtree(self):
75
# Check to remove tree with read-only files/dirs
77
f = file('dir/file', 'w')
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')
89
self.failIfExists('dir/file')
90
self.failIfExists('dir')
93
class TestSafeUnicode(TestCase):
95
def test_from_ascii_string(self):
96
self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
98
def test_from_unicode_string_ascii_contents(self):
99
self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
101
def test_from_unicode_string_unicode_contents(self):
102
self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
104
def test_from_utf8_string(self):
105
self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
107
def test_bad_utf8_string(self):
108
self.assertRaises(BzrBadParameterNotUnicode,
109
osutils.safe_unicode,
113
class TestWin32Funcs(TestCase):
114
"""Test that the _win32 versions of os utilities return appropriate paths."""
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'))
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'))
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'))
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'))
136
def test_getcwd(self):
137
self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
140
class TestWin32FuncsDirs(TestCaseInTempDir):
141
"""Test win32 functions that create files."""
143
def test_getcwd(self):
144
# Make sure getcwd can handle unicode filenames
146
os.mkdir(u'B\xe5gfors')
148
raise TestSkipped("Unable to create Unicode filename")
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'))
157
def test_mkdtemp(self):
158
tmpdir = osutils._win32_mkdtemp(dir='.')
159
self.assertFalse('\\' in tmpdir)
161
def test_rename(self):
169
osutils._win32_rename('b', 'a')
170
self.failUnlessExists('a')
171
self.failIfExists('b')
172
self.assertFileEqual('baz\n', 'a')
175
class TestSplitLines(TestCase):
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'))
183
def test_split_with_carriage_returns(self):
184
self.assertEqual(['foo\rbar\n'],
185
osutils.split_lines('foo\rbar\n'))