/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
1
# Copyright (C) 2005 by Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
17
"""Tests for the osutils wrapper.
18
"""
19
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
20
import errno
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
21
import os
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
22
import socket
23
import stat
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
24
import sys
25
26
import bzrlib
1185.65.29 by Robert Collins
Implement final review suggestions.
27
from bzrlib.errors import BzrBadParameterNotUnicode
1532 by Robert Collins
Merge in John Meinels integration branch.
28
import bzrlib.osutils as osutils
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
29
from bzrlib.tests import TestCaseInTempDir, TestCase
1532 by Robert Collins
Merge in John Meinels integration branch.
30
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
31
32
class TestOSUtils(TestCaseInTempDir):
33
34
    def test_fancy_rename(self):
35
        # This should work everywhere
36
        def rename(a, b):
37
            osutils.fancy_rename(a, b,
38
                    rename_func=os.rename,
39
                    unlink_func=os.unlink)
40
41
        open('a', 'wb').write('something in a\n')
42
        rename('a', 'b')
43
        self.failIfExists('a')
44
        self.failUnlessExists('b')
45
        self.check_file_contents('b', 'something in a\n')
46
47
        open('a', 'wb').write('new something in a\n')
48
        rename('b', 'a')
49
50
        self.check_file_contents('a', 'something in a\n')
51
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')
59
60
        open('a', 'wb').write('new something in a\n')
61
        osutils.rename('b', 'a')
62
63
        self.check_file_contents('a', 'something in a\n')
64
65
    # TODO: test fancy_rename using a MemoryTransport
66
1553.5.5 by Martin Pool
New utility routine rand_chars
67
    def test_01_rand_chars_empty(self):
68
        result = osutils.rand_chars(0)
69
        self.assertEqual(result, '')
70
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}$')
76
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
77
1692.7.6 by Martin Pool
[patch] force deletion of trees containing readonly files (alexander)
78
    def test_rmtree(self):
79
        # Check to remove tree with read-only files/dirs
80
        os.mkdir('dir')
81
        f = file('dir/file', 'w')
82
        f.write('spam')
83
        f.close()
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')
90
91
        osutils.rmtree('dir')
92
93
        self.failIfExists('dir/file')
94
        self.failIfExists('dir')
95
1732.1.10 by John Arbash Meinel
Updated version of file_kind. Rather than multiple function calls, one mask + dictionary lookup
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'))
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
103
        
104
        # TODO: jam 20060529 Test a block device
105
        try:
106
            os.lstat('/dev/null')
107
        except OSError, e:
108
            if e.errno not in (errno.ENOENT,):
109
                raise
110
        else:
111
            self.assertEquals('chardev', osutils.file_kind('/dev/null'))
112
113
        mkfifo = getattr(os, 'mkfifo', None)
114
        if mkfifo:
115
            mkfifo('fifo')
116
            try:
117
                self.assertEquals('fifo', osutils.file_kind('fifo'))
118
            finally:
119
                os.remove('fifo')
120
121
        AF_UNIX = getattr(socket, 'AF_UNIX', None)
122
        if AF_UNIX:
123
            s = socket.socket(AF_UNIX)
124
            s.bind('socket')
125
            try:
126
                self.assertEquals('socket', osutils.file_kind('socket'))
127
            finally:
128
                os.remove('socket')
1732.1.10 by John Arbash Meinel
Updated version of file_kind. Rather than multiple function calls, one mask + dictionary lookup
129
1692.7.6 by Martin Pool
[patch] force deletion of trees containing readonly files (alexander)
130
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
131
class TestSafeUnicode(TestCase):
132
133
    def test_from_ascii_string(self):
134
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
135
1534.3.2 by Robert Collins
An extra test for John.
136
    def test_from_unicode_string_ascii_contents(self):
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
137
        self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
138
1534.3.2 by Robert Collins
An extra test for John.
139
    def test_from_unicode_string_unicode_contents(self):
140
        self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
141
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
142
    def test_from_utf8_string(self):
143
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
144
145
    def test_bad_utf8_string(self):
1185.65.29 by Robert Collins
Implement final review suggestions.
146
        self.assertRaises(BzrBadParameterNotUnicode,
147
                          osutils.safe_unicode,
148
                          '\xbb\xbb')
1666.1.6 by Robert Collins
Make knit the default format.
149
150
151
class TestSplitLines(TestCase):
152
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'))
158
159
    def test_split_with_carriage_returns(self):
160
        self.assertEqual(['foo\rbar\n'],
161
                         osutils.split_lines('foo\rbar\n'))