~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_nonascii.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-13 18:38:58 UTC
  • mfrom: (1863 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1869.
  • Revision ID: john@arbash-meinel.com-20060713183858-ebf4aa1f9ef8bb6e
[merge] bzr.dev 1863

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import sys
21
21
from unicodedata import normalize
22
22
 
23
 
from bzrlib.osutils import pathjoin, normalizes_filenames, unicode_filename
24
 
from bzrlib.tests import TestCaseWithTransport, TestSkipped
 
23
from bzrlib import osutils
 
24
from bzrlib.osutils import pathjoin
 
25
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
25
26
from bzrlib.workingtree import WorkingTree
26
27
 
27
28
 
40
41
 
41
42
 
42
43
a_circle_c = u'\xe5'
 
44
a_circle_d = u'a\u030a'
43
45
a_dots_c = u'\xe4'
44
 
a_circle_d = u'a\u030a'
45
46
a_dots_d = u'a\u0308'
46
47
z_umlat_c = u'\u017d'
47
48
z_umlat_d = u'Z\u030c'
48
49
 
49
50
 
50
 
class UnicodeFilename(TestCaseWithTransport):
51
 
    """Test that UnicodeFilename returns the expected values."""
 
51
class TestNormalization(TestCase):
 
52
    """Verify that we have our normalizations correct."""
52
53
 
53
 
    def test_a_circle(self):
 
54
    def test_normalize(self):
54
55
        self.assertEqual(a_circle_d, normalize('NFKD', a_circle_c))
55
56
        self.assertEqual(a_circle_c, normalize('NFKC', a_circle_d))
56
 
 
57
 
        self.assertEqual((a_circle_c, True), unicode_filename(a_circle_c))
58
 
        if normalizes_filenames():
59
 
            self.assertEqual((a_circle_c, True), unicode_filename(a_circle_d))
 
57
        self.assertEqual(a_dots_d, normalize('NFKD', a_dots_c))
 
58
        self.assertEqual(a_dots_c, normalize('NFKC', a_dots_d))
 
59
        self.assertEqual(z_umlat_d, normalize('NFKD', z_umlat_c))
 
60
        self.assertEqual(z_umlat_c, normalize('NFKC', z_umlat_d))
 
61
 
 
62
 
 
63
class NormalizedFilename(TestCaseWithTransport):
 
64
    """Test normalized_filename and associated helpers"""
 
65
 
 
66
    def test__accessible_normalized_filename(self):
 
67
        anf = osutils._accessible_normalized_filename
 
68
        # normalized_filename should allow plain ascii strings
 
69
        # not just unicode strings
 
70
        self.assertEqual((u'ascii', True), anf('ascii'))
 
71
        self.assertEqual((a_circle_c, True), anf(a_circle_c))
 
72
        self.assertEqual((a_circle_c, True), anf(a_circle_d))
 
73
        self.assertEqual((a_dots_c, True), anf(a_dots_c))
 
74
        self.assertEqual((a_dots_c, True), anf(a_dots_d))
 
75
        self.assertEqual((z_umlat_c, True), anf(z_umlat_c))
 
76
        self.assertEqual((z_umlat_c, True), anf(z_umlat_d))
 
77
 
 
78
    def test__inaccessible_normalized_filename(self):
 
79
        inf = osutils._inaccessible_normalized_filename
 
80
        # normalized_filename should allow plain ascii strings
 
81
        # not just unicode strings
 
82
        self.assertEqual((u'ascii', True), inf('ascii'))
 
83
        self.assertEqual((a_circle_c, True), inf(a_circle_c))
 
84
        self.assertEqual((a_circle_c, False), inf(a_circle_d))
 
85
        self.assertEqual((a_dots_c, True), inf(a_dots_c))
 
86
        self.assertEqual((a_dots_c, False), inf(a_dots_d))
 
87
        self.assertEqual((z_umlat_c, True), inf(z_umlat_c))
 
88
        self.assertEqual((z_umlat_c, False), inf(z_umlat_d))
 
89
 
 
90
    def test_functions(self):
 
91
        if osutils.normalizes_filenames():
 
92
            self.assertEqual(osutils.normalized_filename,
 
93
                             osutils._accessible_normalized_filename)
60
94
        else:
61
 
            self.assertEqual((a_circle_d, False), unicode_filename(a_circle_d))
 
95
            self.assertEqual(osutils.normalized_filename,
 
96
                             osutils._inaccessible_normalized_filename)
62
97
 
63
98
    def test_platform(self):
 
99
        # With FAT32 and certain encodings on win32
 
100
        # a_circle_c and a_dots_c actually map to the same file
 
101
        # adding a suffix kicks in the 'preserving but insensitive'
 
102
        # route, and maintains the right files
 
103
        files = [a_circle_c+'.1', a_dots_c+'.2', z_umlat_c+'.3']
64
104
        try:
65
 
            self.build_tree([a_circle_c, a_dots_c, z_umlat_c])
 
105
            self.build_tree(files)
66
106
        except UnicodeError:
67
107
            raise TestSkipped("filesystem cannot create unicode files")
68
108
 
69
109
        if sys.platform == 'darwin':
70
 
            expected = sorted([a_circle_d, a_dots_d, z_umlat_d])
 
110
            expected = sorted([a_circle_d+'.1', a_dots_d+'.2', z_umlat_d+'.3'])
71
111
        else:
72
 
            expected = sorted([a_circle_c, a_dots_c, z_umlat_c])
 
112
            expected = sorted(files)
73
113
 
74
114
        present = sorted(os.listdir(u'.'))
75
115
        self.assertEqual(expected, present)
76
116
 
77
 
    def test_access(self):
78
 
        # We should always be able to access files by the path returned
79
 
        # from unicode_filename
80
 
        files = [a_circle_c, a_dots_c, z_umlat_c]
 
117
    def test_access_normalized(self):
 
118
        # We should always be able to access files created with 
 
119
        # normalized filenames
 
120
        # With FAT32 and certain encodings on win32
 
121
        # a_circle_c and a_dots_c actually map to the same file
 
122
        # adding a suffix kicks in the 'preserving but insensitive'
 
123
        # route, and maintains the right files
 
124
        files = [a_circle_c+'.1', a_dots_c+'.2', z_umlat_c+'.3']
81
125
        try:
82
126
            self.build_tree(files)
83
127
        except UnicodeError:
84
128
            raise TestSkipped("filesystem cannot create unicode files")
85
129
 
86
130
        for fname in files:
87
 
            path = unicode_filename(fname)[0]
88
131
            # We should get an exception if we can't open the file at
89
132
            # this location.
 
133
            path, can_access = osutils.normalized_filename(fname)
 
134
 
 
135
            self.assertEqual(path, fname)
 
136
            self.assertTrue(can_access)
 
137
 
90
138
            f = open(path, 'rb')
 
139
            try:
 
140
                # Check the contents
 
141
                shouldbe = 'contents of %s%s' % (path.encode('utf8'),
 
142
                                                 os.linesep)
 
143
                actual = f.read()
 
144
            finally:
 
145
                f.close()
 
146
            self.assertEqual(shouldbe, actual, 
 
147
                             'contents of %s is incorrect: %r != %r'
 
148
                             % (path, shouldbe, actual))
 
149
 
 
150
    def test_access_non_normalized(self):
 
151
        # Sometimes we can access non-normalized files by their normalized
 
152
        # path, verify that normalized_filename returns the right info
 
153
        files = [a_circle_d+'.1', a_dots_d+'.2', z_umlat_d+'.3']
 
154
 
 
155
        try:
 
156
            self.build_tree(files)
 
157
        except UnicodeError:
 
158
            raise TestSkipped("filesystem cannot create unicode files")
 
159
 
 
160
        for fname in files:
 
161
            # We should get an exception if we can't open the file at
 
162
            # this location.
 
163
            path, can_access = osutils.normalized_filename(fname)
 
164
 
 
165
            self.assertNotEqual(path, fname)
 
166
 
 
167
            # We should always be able to access them from the name
 
168
            # they were created with
 
169
            f = open(fname, 'rb')
91
170
            f.close()
92
171
 
93
 
 
 
172
            # And normalized_filename sholud tell us correctly if we can
 
173
            # access them by an alternate name
 
174
            if can_access:
 
175
                f = open(path, 'rb')
 
176
                f.close()
 
177
            else:
 
178
                self.assertRaises(IOError, open, path, 'rb')