~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-02 04:51:33 UTC
  • mto: This revision was merged to the branch mainline in revision 1851.
  • Revision ID: john@arbash-meinel.com-20060702045133-cfda3358025568ff
Change the return value of unicode_filename, and make it testable on all platforms

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
 
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 UnicodeFilename(TestCaseWithTransport):
 
64
    """Test unicode_filename and associated helpers"""
 
65
 
 
66
    def test__accessible_unicode_filename(self):
 
67
        auf = osutils._accessible_unicode_filename
 
68
        self.assertEqual((a_circle_c, True), auf(a_circle_c))
 
69
        self.assertEqual((a_circle_c, True), auf(a_circle_d))
 
70
        self.assertEqual((a_dots_c, True), auf(a_dots_c))
 
71
        self.assertEqual((a_dots_c, True), auf(a_dots_d))
 
72
        self.assertEqual((z_umlat_c, True), auf(z_umlat_c))
 
73
        self.assertEqual((z_umlat_c, True), auf(z_umlat_d))
 
74
 
 
75
    def test__inaccessible_unicode_filename(self):
 
76
        iuf = osutils._inaccessible_unicode_filename
 
77
        self.assertEqual((a_circle_c, True), iuf(a_circle_c))
 
78
        self.assertEqual((a_circle_c, False), iuf(a_circle_d))
 
79
        self.assertEqual((a_dots_c, True), iuf(a_dots_c))
 
80
        self.assertEqual((a_dots_c, False), iuf(a_dots_d))
 
81
        self.assertEqual((z_umlat_c, True), iuf(z_umlat_c))
 
82
        self.assertEqual((z_umlat_c, False), iuf(z_umlat_d))
 
83
 
 
84
    def test_functions(self):
 
85
        if osutils.normalizes_filenames():
 
86
            self.assertEqual(osutils.unicode_filename,
 
87
                             osutils._accessible_unicode_filename)
60
88
        else:
61
 
            self.assertEqual((a_circle_d, False), unicode_filename(a_circle_d))
 
89
            self.assertEqual(osutils.unicode_filename,
 
90
                             osutils._inaccessible_unicode_filename)
62
91
 
63
92
    def test_platform(self):
64
93
        try:
74
103
        present = sorted(os.listdir(u'.'))
75
104
        self.assertEqual(expected, present)
76
105
 
77
 
    def test_access(self):
78
 
        # We should always be able to access files by the path returned
79
 
        # from unicode_filename
 
106
    def test_access_normalized(self):
 
107
        # We should always be able to access files created with normalized filenames
80
108
        files = [a_circle_c, a_dots_c, z_umlat_c]
81
109
        try:
82
110
            self.build_tree(files)
84
112
            raise TestSkipped("filesystem cannot create unicode files")
85
113
 
86
114
        for fname in files:
87
 
            path = unicode_filename(fname)[0]
88
115
            # We should get an exception if we can't open the file at
89
116
            # this location.
 
117
            path, can_access = osutils.unicode_filename(fname)
 
118
 
 
119
            self.assertEqual(path, fname)
 
120
            self.assertTrue(can_access)
 
121
 
90
122
            f = open(path, 'rb')
91
123
            f.close()
92
124
 
93
 
 
 
125
    def test_access_non_normalized(self):
 
126
        # Sometimes we can access non-normalized files by their normalized
 
127
        # path, verify that unicode_filename returns the right info
 
128
        files = [a_circle_d, a_dots_d, z_umlat_d]
 
129
 
 
130
        try:
 
131
            self.build_tree(files)
 
132
        except UnicodeError:
 
133
            raise TestSkipped("filesystem cannot create unicode files")
 
134
 
 
135
        for fname in files:
 
136
            # We should get an exception if we can't open the file at
 
137
            # this location.
 
138
            path, can_access = osutils.unicode_filename(fname)
 
139
 
 
140
            self.assertNotEqual(path, fname)
 
141
 
 
142
            # We should always be able to access them from the name
 
143
            # they were created with
 
144
            f = open(fname, 'rb')
 
145
            f.close()
 
146
 
 
147
            # And unicode_filename sholud tell us correctly if we can
 
148
            # access them by an alternate name
 
149
            if can_access:
 
150
                f = open(path, 'rb')
 
151
                f.close()
 
152
            else:
 
153
                self.assertRaises(IOError, open, path, 'rb')