/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.15 by John Arbash Meinel
Merge bzr.dev 5597 to resolve NEWS, aka bzr-2.3.txt
1
# Copyright (C) 2005, 2006, 2008, 2009, 2011 Canonical Ltd
1185.16.70 by Martin Pool
- improved handling of non-ascii branch names and test
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.16.70 by Martin Pool
- improved handling of non-ascii branch names and test
16
17
"""Test that various operations work in a non-ASCII environment."""
18
19
import os
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
20
import sys
21
from unicodedata import normalize
1185.16.70 by Martin Pool
- improved handling of non-ascii branch names and test
22
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
23
from .. import osutils
24
from ..osutils import pathjoin
25
from . import TestCase, TestCaseWithTransport, TestSkipped
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
26
27
28
class NonAsciiTest(TestCaseWithTransport):
1185.16.70 by Martin Pool
- improved handling of non-ascii branch names and test
29
30
    def test_add_in_nonascii_branch(self):
31
        """Test adding in a non-ASCII branch."""
32
        br_dir = u"\u1234"
1185.16.71 by Martin Pool
- try to avoid test failure on platforms with ascii-only filesystems
33
        try:
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
34
            wt = self.make_branch_and_tree(br_dir)
1185.12.91 by Aaron Bentley
Fixed exception (No such thing as EncodingError that I can see)
35
        except UnicodeEncodeError:
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
36
            raise TestSkipped("filesystem can't accomodate nonascii names")
1185.16.71 by Martin Pool
- try to avoid test failure on platforms with ascii-only filesystems
37
            return
7143.15.2 by Jelmer Vernooij
Run autopep8.
38
        with open(pathjoin(br_dir, "a"), "w") as f:
39
            f.write("hello")
7045.4.13 by Jelmer Vernooij
Some more test fixes.
40
        wt.add(["a"], [b"a-id"])
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
41
42
43
a_circle_c = u'\xe5'
1830.3.5 by John Arbash Meinel
make_entry refuses to create non-normalized entries.
44
a_circle_d = u'a\u030a'
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
45
a_dots_c = u'\xe4'
46
a_dots_d = u'a\u0308'
47
z_umlat_c = u'\u017d'
48
z_umlat_d = u'Z\u030c'
7143.15.2 by Jelmer Vernooij
Run autopep8.
49
squared_c = u'\xbc'  # This gets mapped to '2' if we use NFK[CD]
3201.1.1 by jameinel
Fix bug #185458, switch from NFKC to NFC and add tests for filenames that would be broken under NFKC
50
squared_d = u'\xbc'
7143.15.2 by Jelmer Vernooij
Run autopep8.
51
quarter_c = u'\xb2'  # Gets mapped to u'1\u20444' (1/4) if we use NFK[CD]
3201.1.1 by jameinel
Fix bug #185458, switch from NFKC to NFC and add tests for filenames that would be broken under NFKC
52
quarter_d = u'\xb2'
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
53
54
1830.3.1 by John Arbash Meinel
Change the return value of unicode_filename, and make it testable on all platforms
55
class TestNormalization(TestCase):
56
    """Verify that we have our normalizations correct."""
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
57
1830.3.1 by John Arbash Meinel
Change the return value of unicode_filename, and make it testable on all platforms
58
    def test_normalize(self):
3201.1.1 by jameinel
Fix bug #185458, switch from NFKC to NFC and add tests for filenames that would be broken under NFKC
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))
1830.3.1 by John Arbash Meinel
Change the return value of unicode_filename, and make it testable on all platforms
69
70
1830.3.2 by John Arbash Meinel
normalized_filename is a much better name
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
1830.3.8 by John Arbash Meinel
unicodedata.normalize requires unicode strings
76
        # normalized_filename should allow plain ascii strings
77
        # not just unicode strings
78
        self.assertEqual((u'ascii', True), anf('ascii'))
1830.3.2 by John Arbash Meinel
normalized_filename is a much better name
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))
3201.1.1 by jameinel
Fix bug #185458, switch from NFKC to NFC and add tests for filenames that would be broken under NFKC
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))
1830.3.2 by John Arbash Meinel
normalized_filename is a much better name
89
90
    def test__inaccessible_normalized_filename(self):
91
        inf = osutils._inaccessible_normalized_filename
1830.3.8 by John Arbash Meinel
unicodedata.normalize requires unicode strings
92
        # normalized_filename should allow plain ascii strings
93
        # not just unicode strings
94
        self.assertEqual((u'ascii', True), inf('ascii'))
1830.3.2 by John Arbash Meinel
normalized_filename is a much better name
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))
3201.1.1 by jameinel
Fix bug #185458, switch from NFKC to NFC and add tests for filenames that would be broken under NFKC
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))
1830.3.1 by John Arbash Meinel
Change the return value of unicode_filename, and make it testable on all platforms
105
106
    def test_functions(self):
107
        if osutils.normalizes_filenames():
1830.3.2 by John Arbash Meinel
normalized_filename is a much better name
108
            self.assertEqual(osutils.normalized_filename,
109
                             osutils._accessible_normalized_filename)
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
110
        else:
1830.3.2 by John Arbash Meinel
normalized_filename is a much better name
111
            self.assertEqual(osutils.normalized_filename,
112
                             osutils._inaccessible_normalized_filename)
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
113
114
    def test_platform(self):
1711.7.36 by John Arbash Meinel
Use different filenames to avoid path collisions on win32 w/ FAT32
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
7143.15.2 by Jelmer Vernooij
Run autopep8.
119
        files = [a_circle_c + '.1', a_dots_c + '.2', z_umlat_c + '.3']
1685.1.74 by Wouter van Heyst
fix nonascii tests to run properly under LANG=C
120
        try:
1711.7.36 by John Arbash Meinel
Use different filenames to avoid path collisions on win32 w/ FAT32
121
            self.build_tree(files)
1685.1.74 by Wouter van Heyst
fix nonascii tests to run properly under LANG=C
122
        except UnicodeError:
123
            raise TestSkipped("filesystem cannot create unicode files")
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
124
125
        if sys.platform == 'darwin':
7143.15.2 by Jelmer Vernooij
Run autopep8.
126
            expected = sorted(
127
                [a_circle_d + '.1', a_dots_d + '.2', z_umlat_d + '.3'])
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
128
        else:
1711.7.36 by John Arbash Meinel
Use different filenames to avoid path collisions on win32 w/ FAT32
129
            expected = sorted(files)
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
130
131
        present = sorted(os.listdir(u'.'))
132
        self.assertEqual(expected, present)
133
1830.3.1 by John Arbash Meinel
Change the return value of unicode_filename, and make it testable on all platforms
134
    def test_access_normalized(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
135
        # We should always be able to access files created with
1830.3.16 by John Arbash Meinel
NEWS about fixing #43689
136
        # normalized filenames
1711.7.36 by John Arbash Meinel
Use different filenames to avoid path collisions on win32 w/ FAT32
137
        # With FAT32 and certain encodings on win32
138
        # a_circle_c and a_dots_c actually map to the same file
139
        # adding a suffix kicks in the 'preserving but insensitive'
140
        # route, and maintains the right files
7143.15.2 by Jelmer Vernooij
Run autopep8.
141
        files = [a_circle_c + '.1', a_dots_c + '.2', z_umlat_c + '.3',
142
                 squared_c + '.4', quarter_c + '.5']
1685.1.74 by Wouter van Heyst
fix nonascii tests to run properly under LANG=C
143
        try:
2204.3.1 by Alexander Belchenko
fix win32 selftest regression introduced by bzr.dev.revno2198
144
            self.build_tree(files, line_endings='native')
1685.1.74 by Wouter van Heyst
fix nonascii tests to run properly under LANG=C
145
        except UnicodeError:
146
            raise TestSkipped("filesystem cannot create unicode files")
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
147
148
        for fname in files:
149
            # We should get an exception if we can't open the file at
150
            # this location.
1830.3.2 by John Arbash Meinel
normalized_filename is a much better name
151
            path, can_access = osutils.normalized_filename(fname)
1830.3.1 by John Arbash Meinel
Change the return value of unicode_filename, and make it testable on all platforms
152
153
            self.assertEqual(path, fname)
154
            self.assertTrue(can_access)
155
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
156
            with open(path, 'rb') as f:
1711.7.36 by John Arbash Meinel
Use different filenames to avoid path collisions on win32 w/ FAT32
157
                # Check the contents
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
158
                shouldbe = b'contents of %s%s' % (path.encode('utf8'),
7065.3.6 by Jelmer Vernooij
Fix some more tests.
159
                                                  os.linesep.encode('utf-8'))
1711.7.36 by John Arbash Meinel
Use different filenames to avoid path collisions on win32 w/ FAT32
160
                actual = f.read()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
161
            self.assertEqual(shouldbe, actual,
2204.3.3 by Alexander Belchenko
Don't formats unicode strings in test error messages with '%s': it's unsafe
162
                             'contents of %r is incorrect: %r != %r'
1711.7.36 by John Arbash Meinel
Use different filenames to avoid path collisions on win32 w/ FAT32
163
                             % (path, shouldbe, actual))
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
164
1830.3.1 by John Arbash Meinel
Change the return value of unicode_filename, and make it testable on all platforms
165
    def test_access_non_normalized(self):
166
        # Sometimes we can access non-normalized files by their normalized
1830.3.2 by John Arbash Meinel
normalized_filename is a much better name
167
        # path, verify that normalized_filename returns the right info
7143.15.2 by Jelmer Vernooij
Run autopep8.
168
        files = [a_circle_d + '.1', a_dots_d + '.2', z_umlat_d + '.3']
1830.3.1 by John Arbash Meinel
Change the return value of unicode_filename, and make it testable on all platforms
169
170
        try:
171
            self.build_tree(files)
172
        except UnicodeError:
173
            raise TestSkipped("filesystem cannot create unicode files")
174
175
        for fname in files:
176
            # We should get an exception if we can't open the file at
177
            # this location.
1830.3.2 by John Arbash Meinel
normalized_filename is a much better name
178
            path, can_access = osutils.normalized_filename(fname)
1830.3.1 by John Arbash Meinel
Change the return value of unicode_filename, and make it testable on all platforms
179
180
            self.assertNotEqual(path, fname)
181
182
            # We should always be able to access them from the name
183
            # they were created with
184
            f = open(fname, 'rb')
185
            f.close()
186
1830.3.2 by John Arbash Meinel
normalized_filename is a much better name
187
            # And normalized_filename sholud tell us correctly if we can
1830.3.1 by John Arbash Meinel
Change the return value of unicode_filename, and make it testable on all platforms
188
            # access them by an alternate name
189
            if can_access:
190
                f = open(path, 'rb')
191
                f.close()
192
            else:
193
                self.assertRaises(IOError, open, path, 'rb')