~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_nonascii.py

  • Committer: Ian Clatworthy
  • Date: 2009-01-19 02:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 3944.
  • Revision ID: ian.clatworthy@canonical.com-20090119022415-mo0mcfeiexfktgwt
apply jam's log --short fix (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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
 
 
50
 
class UnicodeFilename(TestCaseWithTransport):
51
 
    """Test that UnicodeFilename returns the expected values."""
52
 
 
53
 
    def test_a_circle(self):
54
 
        self.assertEqual(a_circle_d, normalize('NFKD', a_circle_c))
55
 
        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))
 
49
squared_c = u'\xbc' # This gets mapped to '2' if we use NFK[CD]
 
50
squared_d = u'\xbc'
 
51
quarter_c = u'\xb2' # Gets mapped to u'1\u20444' (1/4) if we use NFK[CD]
 
52
quarter_d = u'\xb2'
 
53
 
 
54
 
 
55
class TestNormalization(TestCase):
 
56
    """Verify that we have our normalizations correct."""
 
57
 
 
58
    def test_normalize(self):
 
59
        self.assertEqual(a_circle_d, normalize('NFD', a_circle_c))
 
60
        self.assertEqual(a_circle_c, normalize('NFC', a_circle_d))
 
61
        self.assertEqual(a_dots_d, normalize('NFD', a_dots_c))
 
62
        self.assertEqual(a_dots_c, normalize('NFC', a_dots_d))
 
63
        self.assertEqual(z_umlat_d, normalize('NFD', z_umlat_c))
 
64
        self.assertEqual(z_umlat_c, normalize('NFC', z_umlat_d))
 
65
        self.assertEqual(squared_d, normalize('NFC', squared_c))
 
66
        self.assertEqual(squared_c, normalize('NFD', squared_d))
 
67
        self.assertEqual(quarter_d, normalize('NFC', quarter_c))
 
68
        self.assertEqual(quarter_c, normalize('NFD', quarter_d))
 
69
 
 
70
 
 
71
class NormalizedFilename(TestCaseWithTransport):
 
72
    """Test normalized_filename and associated helpers"""
 
73
 
 
74
    def test__accessible_normalized_filename(self):
 
75
        anf = osutils._accessible_normalized_filename
 
76
        # normalized_filename should allow plain ascii strings
 
77
        # not just unicode strings
 
78
        self.assertEqual((u'ascii', True), anf('ascii'))
 
79
        self.assertEqual((a_circle_c, True), anf(a_circle_c))
 
80
        self.assertEqual((a_circle_c, True), anf(a_circle_d))
 
81
        self.assertEqual((a_dots_c, True), anf(a_dots_c))
 
82
        self.assertEqual((a_dots_c, True), anf(a_dots_d))
 
83
        self.assertEqual((z_umlat_c, True), anf(z_umlat_c))
 
84
        self.assertEqual((z_umlat_c, True), anf(z_umlat_d))
 
85
        self.assertEqual((squared_c, True), anf(squared_c))
 
86
        self.assertEqual((squared_c, True), anf(squared_d))
 
87
        self.assertEqual((quarter_c, True), anf(quarter_c))
 
88
        self.assertEqual((quarter_c, True), anf(quarter_d))
 
89
 
 
90
    def test__inaccessible_normalized_filename(self):
 
91
        inf = osutils._inaccessible_normalized_filename
 
92
        # normalized_filename should allow plain ascii strings
 
93
        # not just unicode strings
 
94
        self.assertEqual((u'ascii', True), inf('ascii'))
 
95
        self.assertEqual((a_circle_c, True), inf(a_circle_c))
 
96
        self.assertEqual((a_circle_c, False), inf(a_circle_d))
 
97
        self.assertEqual((a_dots_c, True), inf(a_dots_c))
 
98
        self.assertEqual((a_dots_c, False), inf(a_dots_d))
 
99
        self.assertEqual((z_umlat_c, True), inf(z_umlat_c))
 
100
        self.assertEqual((z_umlat_c, False), inf(z_umlat_d))
 
101
        self.assertEqual((squared_c, True), inf(squared_c))
 
102
        self.assertEqual((squared_c, True), inf(squared_d))
 
103
        self.assertEqual((quarter_c, True), inf(quarter_c))
 
104
        self.assertEqual((quarter_c, True), inf(quarter_d))
 
105
 
 
106
    def test_functions(self):
 
107
        if osutils.normalizes_filenames():
 
108
            self.assertEqual(osutils.normalized_filename,
 
109
                             osutils._accessible_normalized_filename)
60
110
        else:
61
 
            self.assertEqual((a_circle_d, False), unicode_filename(a_circle_d))
 
111
            self.assertEqual(osutils.normalized_filename,
 
112
                             osutils._inaccessible_normalized_filename)
62
113
 
63
114
    def test_platform(self):
 
115
        # With FAT32 and certain encodings on win32
 
116
        # a_circle_c and a_dots_c actually map to the same file
 
117
        # adding a suffix kicks in the 'preserving but insensitive'
 
118
        # route, and maintains the right files
 
119
        files = [a_circle_c+'.1', a_dots_c+'.2', z_umlat_c+'.3']
64
120
        try:
65
 
            self.build_tree([a_circle_c, a_dots_c, z_umlat_c])
 
121
            self.build_tree(files)
66
122
        except UnicodeError:
67
123
            raise TestSkipped("filesystem cannot create unicode files")
68
124
 
69
125
        if sys.platform == 'darwin':
70
 
            expected = sorted([a_circle_d, a_dots_d, z_umlat_d])
 
126
            expected = sorted([a_circle_d+'.1', a_dots_d+'.2', z_umlat_d+'.3'])
71
127
        else:
72
 
            expected = sorted([a_circle_c, a_dots_c, z_umlat_c])
 
128
            expected = sorted(files)
73
129
 
74
130
        present = sorted(os.listdir(u'.'))
75
131
        self.assertEqual(expected, present)
76
132
 
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]
 
133
    def test_access_normalized(self):
 
134
        # We should always be able to access files created with 
 
135
        # normalized filenames
 
136
        # With FAT32 and certain encodings on win32
 
137
        # a_circle_c and a_dots_c actually map to the same file
 
138
        # adding a suffix kicks in the 'preserving but insensitive'
 
139
        # route, and maintains the right files
 
140
        files = [a_circle_c+'.1', a_dots_c+'.2', z_umlat_c+'.3',
 
141
                 squared_c+'.4', quarter_c+'.5']
81
142
        try:
82
 
            self.build_tree(files)
 
143
            self.build_tree(files, line_endings='native')
83
144
        except UnicodeError:
84
145
            raise TestSkipped("filesystem cannot create unicode files")
85
146
 
86
147
        for fname in files:
87
 
            path = unicode_filename(fname)[0]
88
148
            # We should get an exception if we can't open the file at
89
149
            # this location.
 
150
            path, can_access = osutils.normalized_filename(fname)
 
151
 
 
152
            self.assertEqual(path, fname)
 
153
            self.assertTrue(can_access)
 
154
 
90
155
            f = open(path, 'rb')
 
156
            try:
 
157
                # Check the contents
 
158
                shouldbe = 'contents of %s%s' % (path.encode('utf8'),
 
159
                                                 os.linesep)
 
160
                actual = f.read()
 
161
            finally:
 
162
                f.close()
 
163
            self.assertEqual(shouldbe, actual, 
 
164
                             'contents of %r is incorrect: %r != %r'
 
165
                             % (path, shouldbe, actual))
 
166
 
 
167
    def test_access_non_normalized(self):
 
168
        # Sometimes we can access non-normalized files by their normalized
 
169
        # path, verify that normalized_filename returns the right info
 
170
        files = [a_circle_d+'.1', a_dots_d+'.2', z_umlat_d+'.3']
 
171
 
 
172
        try:
 
173
            self.build_tree(files)
 
174
        except UnicodeError:
 
175
            raise TestSkipped("filesystem cannot create unicode files")
 
176
 
 
177
        for fname in files:
 
178
            # We should get an exception if we can't open the file at
 
179
            # this location.
 
180
            path, can_access = osutils.normalized_filename(fname)
 
181
 
 
182
            self.assertNotEqual(path, fname)
 
183
 
 
184
            # We should always be able to access them from the name
 
185
            # they were created with
 
186
            f = open(fname, 'rb')
91
187
            f.close()
92
188
 
93
 
 
 
189
            # And normalized_filename sholud tell us correctly if we can
 
190
            # access them by an alternate name
 
191
            if can_access:
 
192
                f = open(path, 'rb')
 
193
                f.close()
 
194
            else:
 
195
                self.assertRaises(IOError, open, path, 'rb')