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
class TestSafeUnicode(TestCase):
76
def test_from_ascii_string(self):
77
self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
79
def test_from_unicode_string_ascii_contents(self):
80
self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
82
def test_from_unicode_string_unicode_contents(self):
83
self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
85
def test_from_utf8_string(self):
86
self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
88
def test_bad_utf8_string(self):
89
self.assertRaises(BzrBadParameterNotUnicode,
94
class TestWin32Funcs(TestCase):
95
"""Test that the _win32 versions of os utilities return appropriate paths."""
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'))
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'))
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'))
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'))
117
def test_getcwd(self):
118
self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
121
class TestWin32FuncsDirs(TestCaseInTempDir):
122
"""Test win32 functions that create files."""
124
def test_getcwd(self):
125
# Make sure getcwd can handle unicode filenames
127
os.mkdir(u'B\xe5gfors')
129
raise TestSkipped("Unable to create Unicode filename")
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'))
138
def test_mkdtemp(self):
139
tmpdir = osutils._win32_mkdtemp(dir='.')
140
self.assertFalse('\\' in tmpdir)
142
def test_rename(self):
150
osutils._win32_rename('b', 'a')
151
self.failUnlessExists('a')
152
self.failIfExists('b')
153
self.assertFileEqual('baz\n', 'a')
156
class TestSplitLines(TestCase):
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'))
164
def test_split_with_carriage_returns(self):
165
self.assertEqual(['foo\rbar\n'],
166
osutils.split_lines('foo\rbar\n'))