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."""
26
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
27
import bzrlib.osutils as osutils
28
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
31
class TestOSUtils(TestCaseInTempDir):
33
def test_fancy_rename(self):
34
# This should work everywhere
36
osutils.fancy_rename(a, b,
37
rename_func=os.rename,
38
unlink_func=os.unlink)
40
open('a', 'wb').write('something in a\n')
42
self.failIfExists('a')
43
self.failUnlessExists('b')
44
self.check_file_contents('b', 'something in a\n')
46
open('a', 'wb').write('new something in a\n')
49
self.check_file_contents('a', 'something in a\n')
51
def test_rename(self):
52
# Rename should be semi-atomic on all platforms
53
open('a', 'wb').write('something in a\n')
54
osutils.rename('a', 'b')
55
self.failIfExists('a')
56
self.failUnlessExists('b')
57
self.check_file_contents('b', 'something in a\n')
59
open('a', 'wb').write('new something in a\n')
60
osutils.rename('b', 'a')
62
self.check_file_contents('a', 'something in a\n')
64
# TODO: test fancy_rename using a MemoryTransport
66
def test_01_rand_chars_empty(self):
67
result = osutils.rand_chars(0)
68
self.assertEqual(result, '')
70
def test_02_rand_chars_100(self):
71
result = osutils.rand_chars(100)
72
self.assertEqual(len(result), 100)
73
self.assertEqual(type(result), str)
74
self.assertContainsRe(result, r'^[a-z0-9]{100}$')
77
def test_rmtree(self):
78
# Check to remove tree with read-only files/dirs
80
f = file('dir/file', 'w')
83
# would like to also try making the directory readonly, but at the
84
# moment python shutil.rmtree doesn't handle that properly - it would
85
# need to chmod the directory before removing things inside it - deferred
86
# for now -- mbp 20060505
87
# osutils.make_readonly('dir')
88
osutils.make_readonly('dir/file')
92
self.failIfExists('dir/file')
93
self.failIfExists('dir')
95
def test_file_kind(self):
96
self.build_tree(['file', 'dir/'])
97
self.assertEquals('file', osutils.file_kind('file'))
98
self.assertEquals('directory', osutils.file_kind('dir/'))
99
if osutils.has_symlinks():
100
os.symlink('symlink', 'symlink')
101
self.assertEquals('symlink', osutils.file_kind('symlink'))
103
# TODO: jam 20060529 Test a block device
105
os.lstat('/dev/null')
107
if e.errno not in (errno.ENOENT,):
110
self.assertEquals('chardev', osutils.file_kind('/dev/null'))
112
mkfifo = getattr(os, 'mkfifo', None)
116
self.assertEquals('fifo', osutils.file_kind('fifo'))
120
AF_UNIX = getattr(socket, 'AF_UNIX', None)
122
s = socket.socket(AF_UNIX)
125
self.assertEquals('socket', osutils.file_kind('socket'))
130
class TestSafeUnicode(TestCase):
132
def test_from_ascii_string(self):
133
self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
135
def test_from_unicode_string_ascii_contents(self):
136
self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
138
def test_from_unicode_string_unicode_contents(self):
139
self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
141
def test_from_utf8_string(self):
142
self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
144
def test_bad_utf8_string(self):
145
self.assertRaises(BzrBadParameterNotUnicode,
146
osutils.safe_unicode,
150
class TestWin32Funcs(TestCase):
151
"""Test that the _win32 versions of os utilities return appropriate paths."""
153
def test_abspath(self):
154
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
155
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
157
def test_realpath(self):
158
self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
159
self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
161
def test_pathjoin(self):
162
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
163
self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
164
self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
165
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
166
self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
167
self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
169
def test_normpath(self):
170
self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
171
self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
173
def test_getcwd(self):
174
self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
177
class TestWin32FuncsDirs(TestCaseInTempDir):
178
"""Test win32 functions that create files."""
180
def test_getcwd(self):
181
# Make sure getcwd can handle unicode filenames
183
os.mkdir(u'B\xe5gfors')
185
raise TestSkipped("Unable to create Unicode filename")
187
os.chdir(u'B\xe5gfors')
188
# TODO: jam 20060427 This will probably fail on Mac OSX because
189
# it will change the normalization of B\xe5gfors
190
# Consider using a different unicode character, or make
191
# osutils.getcwd() renormalize the path.
192
self.assertTrue(osutils._win32_getcwd().endswith(u'/B\xe5gfors'))
194
def test_mkdtemp(self):
195
tmpdir = osutils._win32_mkdtemp(dir='.')
196
self.assertFalse('\\' in tmpdir)
198
def test_rename(self):
206
osutils._win32_rename('b', 'a')
207
self.failUnlessExists('a')
208
self.failIfExists('b')
209
self.assertFileEqual('baz\n', 'a')
212
class TestSplitLines(TestCase):
214
def test_split_unicode(self):
215
self.assertEqual([u'foo\n', u'bar\xae'],
216
osutils.split_lines(u'foo\nbar\xae'))
217
self.assertEqual([u'foo\n', u'bar\xae\n'],
218
osutils.split_lines(u'foo\nbar\xae\n'))
220
def test_split_with_carriage_returns(self):
221
self.assertEqual(['foo\rbar\n'],
222
osutils.split_lines('foo\rbar\n'))
225
class TestWalkDirs(TestCaseInTempDir):
227
def test_walkdirs(self):
236
self.build_tree(tree)
237
expected_dirblocks = [
239
('0file', '0file', 'file'),
240
('1dir', '1dir', 'directory'),
241
('2file', '2file', 'file'),
244
('1dir/0file', '0file', 'file'),
245
('1dir/1dir', '1dir', 'directory'),
252
for dirblock in osutils.walkdirs('.'):
253
if len(dirblock) and dirblock[0][1] == '.bzr':
254
# this tests the filtering of selected paths
257
result.append(dirblock)
259
self.assertTrue(found_bzrdir)
260
self.assertEqual(expected_dirblocks,
261
[[line[0:3] for line in block] for block in result])
262
# you can search a subdir only, with a supplied prefix.
264
for dirblock in osutils.walkdirs('1dir', '1dir'):
265
result.append(dirblock)
266
self.assertEqual(expected_dirblocks[1:],
267
[[line[0:3] for line in block] for block in result])