~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Jelmer Vernooij
  • Date: 2006-06-13 13:24:40 UTC
  • mfrom: (1767 +trunk)
  • mto: (1769.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 1770.
  • Revision ID: jelmer@samba.org-20060613132440-24e222a86f948f60
[merge] bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
16
16
 
17
 
"""Tests for the osutils wrapper.
18
 
"""
 
17
"""Tests for the osutils wrapper."""
19
18
 
 
19
import errno
20
20
import os
 
21
import socket
 
22
import stat
21
23
import sys
22
24
 
23
25
import bzrlib
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
27
29
 
28
30
 
29
31
class TestOSUtils(TestCaseInTempDir):
90
92
        self.failIfExists('dir/file')
91
93
        self.failIfExists('dir')
92
94
 
 
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'))
 
102
        
 
103
        # TODO: jam 20060529 Test a block device
 
104
        try:
 
105
            os.lstat('/dev/null')
 
106
        except OSError, e:
 
107
            if e.errno not in (errno.ENOENT,):
 
108
                raise
 
109
        else:
 
110
            self.assertEquals('chardev', osutils.file_kind('/dev/null'))
 
111
 
 
112
        mkfifo = getattr(os, 'mkfifo', None)
 
113
        if mkfifo:
 
114
            mkfifo('fifo')
 
115
            try:
 
116
                self.assertEquals('fifo', osutils.file_kind('fifo'))
 
117
            finally:
 
118
                os.remove('fifo')
 
119
 
 
120
        AF_UNIX = getattr(socket, 'AF_UNIX', None)
 
121
        if AF_UNIX:
 
122
            s = socket.socket(AF_UNIX)
 
123
            s.bind('socket')
 
124
            try:
 
125
                self.assertEquals('socket', osutils.file_kind('socket'))
 
126
            finally:
 
127
                os.remove('socket')
 
128
 
93
129
 
94
130
class TestSafeUnicode(TestCase):
95
131
 
111
147
                          '\xbb\xbb')
112
148
 
113
149
 
 
150
class TestWin32Funcs(TestCase):
 
151
    """Test that the _win32 versions of os utilities return appropriate paths."""
 
152
 
 
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'))
 
156
 
 
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'))
 
160
 
 
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'))
 
168
 
 
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'))
 
172
 
 
173
    def test_getcwd(self):
 
174
        self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
 
175
 
 
176
 
 
177
class TestWin32FuncsDirs(TestCaseInTempDir):
 
178
    """Test win32 functions that create files."""
 
179
    
 
180
    def test_getcwd(self):
 
181
        # Make sure getcwd can handle unicode filenames
 
182
        try:
 
183
            os.mkdir(u'B\xe5gfors')
 
184
        except UnicodeError:
 
185
            raise TestSkipped("Unable to create Unicode filename")
 
186
 
 
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'))
 
193
 
 
194
    def test_mkdtemp(self):
 
195
        tmpdir = osutils._win32_mkdtemp(dir='.')
 
196
        self.assertFalse('\\' in tmpdir)
 
197
 
 
198
    def test_rename(self):
 
199
        a = open('a', 'wb')
 
200
        a.write('foo\n')
 
201
        a.close()
 
202
        b = open('b', 'wb')
 
203
        b.write('baz\n')
 
204
        b.close()
 
205
 
 
206
        osutils._win32_rename('b', 'a')
 
207
        self.failUnlessExists('a')
 
208
        self.failIfExists('b')
 
209
        self.assertFileEqual('baz\n', 'a')
 
210
 
 
211
 
114
212
class TestSplitLines(TestCase):
115
213
 
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'))
 
223
 
 
224
 
 
225
class TestWalkDirs(TestCaseInTempDir):
 
226
 
 
227
    def test_walkdirs(self):
 
228
        tree = [
 
229
            '.bzr',
 
230
            '0file',
 
231
            '1dir/',
 
232
            '1dir/0file',
 
233
            '1dir/1dir/',
 
234
            '2file'
 
235
            ]
 
236
        self.build_tree(tree)
 
237
        expected_dirblocks = [
 
238
                [
 
239
                    ('0file', '0file', 'file'),
 
240
                    ('1dir', '1dir', 'directory'),
 
241
                    ('2file', '2file', 'file'),
 
242
                ],
 
243
                [
 
244
                    ('1dir/0file', '0file', 'file'),
 
245
                    ('1dir/1dir', '1dir', 'directory'),
 
246
                ],
 
247
                [
 
248
                ],
 
249
            ]
 
250
        result = []
 
251
        found_bzrdir = False
 
252
        for dirblock in osutils.walkdirs('.'):
 
253
            if len(dirblock) and dirblock[0][1] == '.bzr':
 
254
                # this tests the filtering of selected paths
 
255
                found_bzrdir = True
 
256
                del dirblock[0]
 
257
            result.append(dirblock)
 
258
 
 
259
        self.assertTrue(found_bzrdir)
 
260
        self.assertEqual(expected_dirblocks,
 
261
            [[line[0:3] for line in block] for block in result])