14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for the osutils wrapper.
17
"""Tests for the osutils wrapper."""
24
from bzrlib.errors import BzrBadParameterNotUnicode
26
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
25
27
import bzrlib.osutils as osutils
26
from bzrlib.tests import TestCaseInTempDir, TestCase
28
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
29
31
class TestOSUtils(TestCaseInTempDir):
90
92
self.failIfExists('dir/file')
91
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'))
94
130
class TestSafeUnicode(TestCase):
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')
114
212
class TestSplitLines(TestCase):
116
214
def test_split_unicode(self):
122
220
def test_split_with_carriage_returns(self):
123
221
self.assertEqual(['foo\rbar\n'],
124
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])