/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_non_ascii.py

  • Committer: John Arbash Meinel
  • Date: 2009-07-08 14:37:25 UTC
  • mfrom: (4516 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4517.
  • Revision ID: john@arbash-meinel.com-20090708143725-sc9sjy3mz4cxwxzz
Merge bzr.dev 4516

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Black-box tests for bzr handling non-ascii characters."""
18
18
 
 
19
import os
19
20
import sys
20
 
import os
21
21
 
22
 
from bzrlib import osutils, urlutils
23
 
from bzrlib.tests import (
24
 
    TestCaseWithTransport,
25
 
    TestSkipped,
26
 
    multiply_tests,
 
22
from bzrlib import (
 
23
    osutils,
 
24
    tests,
 
25
    urlutils,
27
26
    )
28
 
from bzrlib.tests.EncodingAdapter import encoding_scenarios
29
 
from bzrlib.trace import mutter, note
 
27
from bzrlib.tests import EncodingAdapter
30
28
 
31
29
 
32
30
def load_tests(standard_tests, module, loader):
33
 
    return multiply_tests(standard_tests, encoding_scenarios,
34
 
        loader.suiteClass())
35
 
 
36
 
 
37
 
class TestNonAscii(TestCaseWithTransport):
 
31
    return tests.multiply_tests(standard_tests,
 
32
                                EncodingAdapter.encoding_scenarios,
 
33
                                loader.suiteClass())
 
34
 
 
35
 
 
36
class TestNonAscii(tests.TestCaseWithTransport):
38
37
    """Test that bzr handles files/committers/etc which are non-ascii."""
39
38
 
40
39
    def setUp(self):
41
40
        super(TestNonAscii, self).setUp()
42
 
        self._orig_email = os.environ.get('BZR_EMAIL', None)
43
41
        self._orig_encoding = osutils._cached_user_encoding
44
42
 
45
43
        osutils._cached_user_encoding = self.encoding
48
46
        self.create_base()
49
47
 
50
48
    def tearDown(self):
51
 
        if self._orig_email is not None:
52
 
            os.environ['BZR_EMAIL'] = self._orig_email
53
 
        else:
54
 
            if os.environ.get('BZR_EMAIL', None) is not None:
55
 
                del os.environ['BZR_EMAIL']
56
49
        osutils._cached_user_encoding = self._orig_encoding
57
50
        super(TestNonAscii, self).tearDown()
58
51
 
59
52
    def run_bzr_decode(self, args, encoding=None, fail=False, retcode=None,
60
 
                        working_dir=None):
 
53
                       working_dir=None):
61
54
        """Run bzr and decode the output into a particular encoding.
62
55
 
63
56
        Returns a string containing the stdout output from bzr.
68
61
        if encoding is None:
69
62
            encoding = osutils.get_user_encoding()
70
63
        try:
71
 
            out = self.run_bzr(args, output_encoding=encoding, encoding=encoding,
72
 
                retcode=retcode, working_dir=working_dir)[0]
 
64
            out = self.run_bzr(args,
 
65
                               output_encoding=encoding, encoding=encoding,
 
66
                               retcode=retcode, working_dir=working_dir)[0]
73
67
            return out.decode(encoding)
74
68
        except UnicodeError, e:
75
69
            if not fail:
82
76
            if fail:
83
77
                self.fail("Expected UnicodeError not raised")
84
78
 
 
79
    def _check_OSX_can_roundtrip(self, path, fs_enc=None):
 
80
        """Stop the test if it's about to fail or errors out.
 
81
 
 
82
        Until we get proper support on OSX for accented paths (in fact, any
 
83
        path whose NFD decomposition is different than the NFC one), this is
 
84
        the best way to keep test active (as opposed to disabling them
 
85
        completely). This is a stop gap. The tests should at least be rewritten
 
86
        so that the failing ones are clearly separated from the passing ones.
 
87
        """
 
88
        if fs_enc is None:
 
89
            fs_enc = osutils._fs_enc
 
90
        if sys.platform == 'darwin':
 
91
            encoded = path.encode(fs_enc)
 
92
            import unicodedata
 
93
            normal_thing = unicodedata.normalize('NFD', path)
 
94
            mac_encoded = normal_thing.encode(fs_enc)
 
95
            if mac_encoded != encoded:
 
96
                raise tests.KnownFailure(
 
97
                    'Unable to roundtrip path %r on OSX filesystem'
 
98
                    ' using encoding "%s"'
 
99
                    % (path, fs_enc))
 
100
 
85
101
    def create_base(self):
86
 
        fs_enc = sys.getfilesystemencoding()
 
102
        fs_enc = osutils._fs_enc
87
103
        terminal_enc = osutils.get_terminal_encoding()
88
104
        fname = self.info['filename']
89
105
        dir_name = self.info['directory']
91
107
            try:
92
108
                thing.encode(fs_enc)
93
109
            except UnicodeEncodeError:
94
 
                raise TestSkipped(('Unable to represent path %r'
95
 
                                   ' in filesystem encoding "%s"')
96
 
                                    % (thing, fs_enc))
 
110
                raise tests.TestSkipped(
 
111
                    'Unable to represent path %r in filesystem encoding "%s"'
 
112
                    % (thing, fs_enc))
97
113
            try:
98
114
                thing.encode(terminal_enc)
99
115
            except UnicodeEncodeError:
100
 
                raise TestSkipped(('Unable to represent path %r'
101
 
                                   ' in terminal encoding "%s"'
102
 
                                   ' (even though it is valid in'
103
 
                                   ' filesystem encoding "%s")')
104
 
                                   % (thing, terminal_enc, fs_enc))
 
116
                raise tests.TestSkipped(
 
117
                    'Unable to represent path %r in terminal encoding "%s"'
 
118
                    ' (even though it is valid in filesystem encoding "%s")'
 
119
                    % (thing, terminal_enc, fs_enc))
105
120
 
106
121
        wt = self.make_branch_and_tree('.')
107
122
        self.build_tree_contents([('a', 'foo\n')])
117
132
        wt.add(fname)
118
133
        wt.commit(u'And a unicode file\n')
119
134
        self.wt = wt
 
135
        # FIXME: We don't check that the add went well, in fact, it doesn't on
 
136
        # OSX (when LC_ALL is set correctly) because the added path doesn't
 
137
        # match the one used on OSX. But checking here will require more
 
138
        # invasive changes than adding the _check_OSX_can_roundtrip(), so I
 
139
        # punt for now -- vila 20090702
120
140
 
121
141
    def test_status(self):
122
142
        self.build_tree_contents(
123
143
            [(self.info['filename'], 'changed something\n')])
124
144
        txt = self.run_bzr_decode('status')
 
145
        self._check_OSX_can_roundtrip(self.info['filename'])
125
146
        self.assertEqual(u'modified:\n  %s\n' % (self.info['filename'],), txt)
126
147
 
127
148
        txt = self.run_bzr_decode('status', encoding='ascii')
135
156
        txt = self.run_bzr('cat b')[0]
136
157
        self.assertEqual('non-ascii \xFF\xFF\xFC\xFB\x00 in b\n', txt)
137
158
 
 
159
        self._check_OSX_can_roundtrip(self.info['filename'])
138
160
        txt = self.run_bzr(['cat', self.info['filename']])[0]
139
161
        self.assertEqual('unicode filename\n', txt)
140
162
 
168
190
 
169
191
    def test_inventory(self):
170
192
        txt = self.run_bzr_decode('inventory')
 
193
        self._check_OSX_can_roundtrip(self.info['filename'])
171
194
        self.assertEqual(['a', 'b', self.info['filename']],
172
195
                         txt.splitlines())
173
196
 
211
234
        os.mkdir(dirname)
212
235
        self.wt.add(dirname)
213
236
        txt = self.run_bzr_decode(['mv', fname1, fname2, dirname])
 
237
        self._check_OSX_can_roundtrip(self.info['filename'])
214
238
        self.assertEqual([u'%s => %s/%s' % (fname1, dirname, fname1),
215
239
                          u'%s => %s/%s' % (fname2, dirname, fname2)]
216
240
                         , txt.splitlines())
323
347
        self.wt.add('base')
324
348
        self.wt.add('base/'+dirname)
325
349
        path = osutils.pathjoin('base', dirname, fname)
 
350
        self._check_OSX_can_roundtrip(self.info['filename'])
326
351
        self.wt.rename_one(fname, path)
327
352
        self.wt.commit('moving things around')
328
353
 
344
369
        txt = self.run_bzr_decode('ancestry')
345
370
 
346
371
    def test_diff(self):
 
372
        self._check_OSX_can_roundtrip(self.info['filename'])
347
373
        # TODO: jam 20060106 diff is a difficult one to test, because it
348
374
        #       shouldn't encode the file contents, but it needs some sort
349
375
        #       of encoding for the paths, etc which are displayed.
351
377
        txt = self.run_bzr('diff', retcode=1)[0]
352
378
 
353
379
    def test_deleted(self):
 
380
        self._check_OSX_can_roundtrip(self.info['filename'])
354
381
        fname = self.info['filename']
355
382
        os.remove(fname)
356
383
        self.wt.remove(fname)
371
398
        self.build_tree_contents([(fname, 'modified\n')])
372
399
 
373
400
        txt = self.run_bzr_decode('modified')
 
401
        self._check_OSX_can_roundtrip(self.info['filename'])
374
402
        self.assertEqual('"'+fname+'"'+'\n', txt)
375
403
 
376
404
        self.run_bzr_decode('modified', encoding='ascii', fail=True)
406
434
        self.assertNotEqual(-1, txt.find(self.info['message']))
407
435
 
408
436
        txt = self.run_bzr_decode('log --verbose')
 
437
        # FIXME: iso-8859-2 test shouldn't be skipped here --vila 20090702
 
438
        self._check_OSX_can_roundtrip(self.info['filename'])
409
439
        self.assertNotEqual(-1, txt.find(fname))
410
440
 
411
441
        # Make sure log doesn't fail even if we can't write out
416
446
    def test_touching_revisions(self):
417
447
        fname = self.info['filename']
418
448
        txt = self.run_bzr_decode(['touching-revisions', fname])
 
449
        self._check_OSX_can_roundtrip(self.info['filename'])
419
450
        self.assertEqual(u'     3 added %s\n' % (fname,), txt)
420
451
 
421
452
        fname2 = self.info['filename'] + '2'
450
481
        #       quotes paths do we really want it to?
451
482
        #       awilkins 20080521 added and modified do it now as well
452
483
        txt = self.run_bzr_decode('unknowns')
 
484
        self._check_OSX_can_roundtrip(self.info['filename'])
453
485
        self.assertEqual(u'"%s"\n' % (fname,), txt)
454
486
 
455
487
        self.run_bzr_decode('unknowns', encoding='ascii', fail=True)
461
493
        def check_unknowns(expected):
462
494
            self.assertEqual(expected, list(self.wt.unknowns()))
463
495
 
 
496
        self._check_OSX_can_roundtrip(self.info['filename'])
464
497
        check_unknowns([fname2])
465
498
 
466
499
        self.run_bzr_decode(['ignore', './' + fname2])