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.
27
from bzrlib.errors import BzrBadParameterNotUnicode
28
import bzrlib.osutils as osutils
29
from bzrlib.tests import TestCaseInTempDir, TestCase
32
class TestOSUtils(TestCaseInTempDir):
34
def test_fancy_rename(self):
35
# This should work everywhere
37
osutils.fancy_rename(a, b,
38
rename_func=os.rename,
39
unlink_func=os.unlink)
41
open('a', 'wb').write('something in a\n')
43
self.failIfExists('a')
44
self.failUnlessExists('b')
45
self.check_file_contents('b', 'something in a\n')
47
open('a', 'wb').write('new something in a\n')
50
self.check_file_contents('a', 'something in a\n')
52
def test_rename(self):
53
# Rename should be semi-atomic on all platforms
54
open('a', 'wb').write('something in a\n')
55
osutils.rename('a', 'b')
56
self.failIfExists('a')
57
self.failUnlessExists('b')
58
self.check_file_contents('b', 'something in a\n')
60
open('a', 'wb').write('new something in a\n')
61
osutils.rename('b', 'a')
63
self.check_file_contents('a', 'something in a\n')
65
# TODO: test fancy_rename using a MemoryTransport
67
def test_01_rand_chars_empty(self):
68
result = osutils.rand_chars(0)
69
self.assertEqual(result, '')
71
def test_02_rand_chars_100(self):
72
result = osutils.rand_chars(100)
73
self.assertEqual(len(result), 100)
74
self.assertEqual(type(result), str)
75
self.assertContainsRe(result, r'^[a-z0-9]{100}$')
78
def test_rmtree(self):
79
# Check to remove tree with read-only files/dirs
81
f = file('dir/file', 'w')
84
# would like to also try making the directory readonly, but at the
85
# moment python shutil.rmtree doesn't handle that properly - it would
86
# need to chmod the directory before removing things inside it - deferred
87
# for now -- mbp 20060505
88
# osutils.make_readonly('dir')
89
osutils.make_readonly('dir/file')
93
self.failIfExists('dir/file')
94
self.failIfExists('dir')
96
def test_file_kind(self):
97
self.build_tree(['file', 'dir/'])
98
self.assertEquals('file', osutils.file_kind('file'))
99
self.assertEquals('directory', osutils.file_kind('dir/'))
100
if osutils.has_symlinks():
101
os.symlink('symlink', 'symlink')
102
self.assertEquals('symlink', osutils.file_kind('symlink'))
104
# TODO: jam 20060529 Test a block device
106
os.lstat('/dev/null')
108
if e.errno not in (errno.ENOENT,):
111
self.assertEquals('chardev', osutils.file_kind('/dev/null'))
113
mkfifo = getattr(os, 'mkfifo', None)
117
self.assertEquals('fifo', osutils.file_kind('fifo'))
121
AF_UNIX = getattr(socket, 'AF_UNIX', None)
123
s = socket.socket(AF_UNIX)
126
self.assertEquals('socket', osutils.file_kind('socket'))
131
class TestSafeUnicode(TestCase):
133
def test_from_ascii_string(self):
134
self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
136
def test_from_unicode_string_ascii_contents(self):
137
self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
139
def test_from_unicode_string_unicode_contents(self):
140
self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
142
def test_from_utf8_string(self):
143
self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
145
def test_bad_utf8_string(self):
146
self.assertRaises(BzrBadParameterNotUnicode,
147
osutils.safe_unicode,
151
class TestSplitLines(TestCase):
153
def test_split_unicode(self):
154
self.assertEqual([u'foo\n', u'bar\xae'],
155
osutils.split_lines(u'foo\nbar\xae'))
156
self.assertEqual([u'foo\n', u'bar\xae\n'],
157
osutils.split_lines(u'foo\nbar\xae\n'))
159
def test_split_with_carriage_returns(self):
160
self.assertEqual(['foo\rbar\n'],
161
osutils.split_lines('foo\rbar\n'))