/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4232.1.1 by Ian Clatworthy
Integrated EOL conversion support (Ian Clatworthy)
1
# Copyright (C) 2009 Canonical Ltd
4208.4.1 by Ian Clatworthy
eol conversion support
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
4232.1.1 by Ian Clatworthy
Integrated EOL conversion support (Ian Clatworthy)
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
4208.4.1 by Ian Clatworthy
eol conversion support
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
4232.1.1 by Ian Clatworthy
Integrated EOL conversion support (Ian Clatworthy)
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
4208.4.1 by Ian Clatworthy
eol conversion support
17
"""Tests for eol conversion."""
18
19
import sys
20
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from ... import rules, status
22
from ...sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
23
    BytesIO,
24
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
25
from .. import TestSkipped
26
from . import TestCaseWithWorkingTree
27
from ...workingtree import WorkingTree
4208.4.1 by Ian Clatworthy
eol conversion support
28
29
30
# Sample files
6855.4.1 by Jelmer Vernooij
Yet more bees.
31
_sample_text         = b"""hello\nworld\r\n"""
32
_sample_text_on_win  = b"""hello\r\nworld\r\n"""
33
_sample_text_on_unix = b"""hello\nworld\n"""
34
_sample_binary       = b"""hello\nworld\r\n\x00"""
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
35
_sample_clean_lf     = _sample_text_on_unix
36
_sample_clean_crlf   = _sample_text_on_win
37
38
39
# Lists of formats for each storage policy
40
_LF_IN_REPO = ['native', 'lf', 'crlf']
41
_CRLF_IN_REPO = [ '%s-with-crlf-in-repo' % (f,) for f in _LF_IN_REPO]
4208.4.1 by Ian Clatworthy
eol conversion support
42
43
44
class TestEolConversion(TestCaseWithWorkingTree):
45
46
    def setUp(self):
47
        # formats that don't support content filtering can skip these tests
48
        fmt = self.workingtree_format
49
        f = getattr(fmt, 'supports_content_filtering')
50
        if f is None:
51
            raise TestSkipped("format %s doesn't declare whether it "
52
                "supports content filtering, assuming not" % fmt)
53
        if not f():
54
            raise TestSkipped("format %s doesn't support content filtering"
55
                % fmt)
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
56
        super(TestEolConversion, self).setUp()
4208.4.1 by Ian Clatworthy
eol conversion support
57
58
    def patch_rules_searcher(self, eol):
59
        """Patch in a custom rules searcher with a given eol setting."""
60
        if eol is None:
61
            WorkingTree._get_rules_searcher = self.real_rules_searcher
62
        else:
63
            def custom_eol_rules_searcher(tree, default_searcher):
64
                return rules._IniBasedRulesSearcher([
65
                    '[name *]\n',
66
                    'eol=%s\n' % eol,
67
                    ])
68
            WorkingTree._get_rules_searcher = custom_eol_rules_searcher
69
70
    def prepare_tree(self, content, eol=None):
71
        """Prepare a working tree and commit some content."""
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
72
        self.real_rules_searcher = self.overrideAttr(
73
            WorkingTree, '_get_rules_searcher')
4208.4.1 by Ian Clatworthy
eol conversion support
74
        self.patch_rules_searcher(eol)
75
        t = self.make_branch_and_tree('tree1')
76
        self.build_tree_contents([('tree1/file1', content)])
6855.4.1 by Jelmer Vernooij
Yet more bees.
77
        t.add('file1', b'file1-id')
4208.4.1 by Ian Clatworthy
eol conversion support
78
        t.commit("add file1")
79
        basis = t.basis_tree()
80
        basis.lock_read()
81
        self.addCleanup(basis.unlock)
82
        return t, basis
83
4208.4.5 by Ian Clatworthy
add lf-always and crlf-always
84
    def assertNewContentForSetting(self, wt, eol, expected_unix,
4393.3.3 by Ian Clatworthy
add round-trip checking to eol tests
85
        expected_win, roundtrip):
86
        """Clone a working tree and check the convenience content.
87
        
88
        If roundtrip is True, status and commit should see no changes.
89
        """
4208.4.5 by Ian Clatworthy
add lf-always and crlf-always
90
        if expected_win is None:
91
            expected_win = expected_unix
4208.4.1 by Ian Clatworthy
eol conversion support
92
        self.patch_rules_searcher(eol)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
93
        wt2 = wt.controldir.sprout('tree-%s' % eol).open_workingtree()
4208.4.1 by Ian Clatworthy
eol conversion support
94
        # To see exactly what got written to disk, we need an unfiltered read
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
95
        content = wt2.get_file('file1', filtered=False).read()
4208.4.1 by Ian Clatworthy
eol conversion support
96
        if sys.platform == 'win32':
4208.4.3 by Ian Clatworthy
rename dos to windows
97
            self.assertEqual(expected_win, content)
4208.4.1 by Ian Clatworthy
eol conversion support
98
        else:
99
            self.assertEqual(expected_unix, content)
4393.3.3 by Ian Clatworthy
add round-trip checking to eol tests
100
        # Confirm that status thinks nothing has changed if the text roundtrips
101
        if roundtrip:
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
102
            status_io = BytesIO()
4393.3.3 by Ian Clatworthy
add round-trip checking to eol tests
103
            status.show_tree_status(wt2, to_file=status_io)
6973.10.1 by Jelmer Vernooij
Fix some tests.
104
            self.assertEqual(b'', status_io.getvalue())
4208.4.1 by Ian Clatworthy
eol conversion support
105
106
    def assertContent(self, wt, basis, expected_raw, expected_unix,
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
107
        expected_win, roundtrip_to=None):
108
        """Check the committed content and content in cloned trees.
109
        
110
        :param roundtrip_to: the set of formats (excluding exact) we
111
          can round-trip to or None for all
112
        """
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
113
        basis_content = basis.get_file('file1').read()
4208.4.1 by Ian Clatworthy
eol conversion support
114
        self.assertEqual(expected_raw, basis_content)
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
115
4393.3.3 by Ian Clatworthy
add round-trip checking to eol tests
116
        # No setting and exact should always roundtrip
117
        self.assertNewContentForSetting(wt, None,
118
            expected_raw, expected_raw, roundtrip=True)
119
        self.assertNewContentForSetting(wt, 'exact',
120
            expected_raw, expected_raw, roundtrip=True)
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
121
122
        # Roundtripping is otherwise dependent on whether the original
123
        # text is clean - mixed line endings will prevent it. It also
124
        # depends on whether the format in the repository is being changed.
125
        if roundtrip_to is None:
126
            roundtrip_to = _LF_IN_REPO + _CRLF_IN_REPO
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
127
        self.assertNewContentForSetting(wt, 'native',
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
128
            expected_unix, expected_win, 'native' in roundtrip_to)
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
129
        self.assertNewContentForSetting(wt, 'lf',
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
130
            expected_unix, expected_unix, 'lf' in roundtrip_to)
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
131
        self.assertNewContentForSetting(wt, 'crlf',
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
132
            expected_win, expected_win, 'crlf' in roundtrip_to)
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
133
        self.assertNewContentForSetting(wt, 'native-with-crlf-in-repo',
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
134
            expected_unix, expected_win,
135
            'native-with-crlf-in-repo' in roundtrip_to)
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
136
        self.assertNewContentForSetting(wt, 'lf-with-crlf-in-repo',
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
137
            expected_unix, expected_unix,
138
            'lf-with-crlf-in-repo' in roundtrip_to)
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
139
        self.assertNewContentForSetting(wt, 'crlf-with-crlf-in-repo',
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
140
            expected_win, expected_win,
141
            'crlf-with-crlf-in-repo' in roundtrip_to)
142
143
    # Test binary files. These always roundtrip.
144
145
    def test_eol_no_rules_binary(self):
146
        wt, basis = self.prepare_tree(_sample_binary)
147
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
148
            _sample_binary)
149
150
    def test_eol_exact_binary(self):
151
        wt, basis = self.prepare_tree(_sample_binary, eol='exact')
152
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
153
            _sample_binary)
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
154
155
    def test_eol_native_binary(self):
156
        wt, basis = self.prepare_tree(_sample_binary, eol='native')
157
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
158
            _sample_binary)
159
4208.4.4 by Ian Clatworthy
unix/window -> cr/crlf based on mailing list feedback
160
    def test_eol_lf_binary(self):
161
        wt, basis = self.prepare_tree(_sample_binary, eol='lf')
4208.4.3 by Ian Clatworthy
rename dos to windows
162
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
163
            _sample_binary)
164
4208.4.4 by Ian Clatworthy
unix/window -> cr/crlf based on mailing list feedback
165
    def test_eol_crlf_binary(self):
166
        wt, basis = self.prepare_tree(_sample_binary, eol='crlf')
4208.4.3 by Ian Clatworthy
rename dos to windows
167
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
168
            _sample_binary)
4208.4.1 by Ian Clatworthy
eol conversion support
169
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
170
    def test_eol_native_with_crlf_in_repo_binary(self):
171
        wt, basis = self.prepare_tree(_sample_binary,
172
            eol='native-with-crlf-in-repo')
173
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
174
            _sample_binary)
175
176
    def test_eol_lf_with_crlf_in_repo_binary(self):
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
177
        wt, basis = self.prepare_tree(_sample_binary,
178
            eol='lf-with-crlf-in-repo')
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
179
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
180
            _sample_binary)
181
182
    def test_eol_crlf_with_crlf_in_repo_binary(self):
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
183
        wt, basis = self.prepare_tree(_sample_binary,
184
            eol='crlf-with-crlf-in-repo')
4208.4.5 by Ian Clatworthy
add lf-always and crlf-always
185
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
186
            _sample_binary)
187
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
188
    # Test text with mixed line endings ("dirty text").
189
    # This doesn't roundtrip so status always thinks something has changed.
190
191
    def test_eol_no_rules_dirty(self):
192
        wt, basis = self.prepare_tree(_sample_text)
193
        self.assertContent(wt, basis, _sample_text,
194
            _sample_text_on_unix, _sample_text_on_win, roundtrip_to=[])
195
196
    def test_eol_exact_dirty(self):
4208.4.3 by Ian Clatworthy
rename dos to windows
197
        wt, basis = self.prepare_tree(_sample_text, eol='exact')
198
        self.assertContent(wt, basis, _sample_text,
4393.3.4 by Ian Clatworthy
add eol roundtripping tests
199
            _sample_text_on_unix, _sample_text_on_win, roundtrip_to=[])
200
201
    def test_eol_native_dirty(self):
202
        wt, basis = self.prepare_tree(_sample_text, eol='native')
203
        self.assertContent(wt, basis, _sample_text_on_unix,
204
            _sample_text_on_unix, _sample_text_on_win, roundtrip_to=[])
205
206
    def test_eol_lf_dirty(self):
207
        wt, basis = self.prepare_tree(_sample_text, eol='lf')
208
        self.assertContent(wt, basis, _sample_text_on_unix,
209
            _sample_text_on_unix, _sample_text_on_win, roundtrip_to=[])
210
211
    def test_eol_crlf_dirty(self):
212
        wt, basis = self.prepare_tree(_sample_text, eol='crlf')
213
        self.assertContent(wt, basis, _sample_text_on_unix,
214
            _sample_text_on_unix, _sample_text_on_win, roundtrip_to=[])
215
216
    def test_eol_native_with_crlf_in_repo_dirty(self):
217
        wt, basis = self.prepare_tree(_sample_text,
218
            eol='native-with-crlf-in-repo')
219
        self.assertContent(wt, basis, _sample_text_on_win,
220
            _sample_text_on_unix, _sample_text_on_win, roundtrip_to=[])
221
222
    def test_eol_lf_with_crlf_in_repo_dirty(self):
223
        wt, basis = self.prepare_tree(_sample_text,
224
            eol='lf-with-crlf-in-repo')
225
        self.assertContent(wt, basis, _sample_text_on_win,
226
            _sample_text_on_unix, _sample_text_on_win, roundtrip_to=[])
227
228
    def test_eol_crlf_with_crlf_in_repo_dirty(self):
229
        wt, basis = self.prepare_tree(_sample_text,
230
            eol='crlf-with-crlf-in-repo')
231
        self.assertContent(wt, basis, _sample_text_on_win,
232
            _sample_text_on_unix, _sample_text_on_win, roundtrip_to=[])
233
234
    # Test text with clean line endings, either always lf or always crlf.
235
    # This selectively roundtrips (based on what's stored in the repo).
236
237
    def test_eol_no_rules_clean_lf(self):
238
        wt, basis = self.prepare_tree(_sample_clean_lf)
239
        self.assertContent(wt, basis, _sample_clean_lf,
240
            _sample_text_on_unix, _sample_text_on_win,
241
            roundtrip_to=_LF_IN_REPO)
242
243
    def test_eol_no_rules_clean_crlf(self):
244
        wt, basis = self.prepare_tree(_sample_clean_crlf)
245
        self.assertContent(wt, basis, _sample_clean_crlf,
246
            _sample_text_on_unix, _sample_text_on_win,
247
            roundtrip_to=_CRLF_IN_REPO)
248
249
    def test_eol_exact_clean_lf(self):
250
        wt, basis = self.prepare_tree(_sample_clean_lf, eol='exact')
251
        self.assertContent(wt, basis, _sample_clean_lf,
252
            _sample_text_on_unix, _sample_text_on_win,
253
            roundtrip_to=_LF_IN_REPO)
254
255
    def test_eol_exact_clean_crlf(self):
256
        wt, basis = self.prepare_tree(_sample_clean_crlf, eol='exact')
257
        self.assertContent(wt, basis, _sample_clean_crlf,
258
            _sample_text_on_unix, _sample_text_on_win,
259
            roundtrip_to=_CRLF_IN_REPO)
260
261
    def test_eol_native_clean_lf(self):
262
        wt, basis = self.prepare_tree(_sample_clean_lf, eol='native')
263
        self.assertContent(wt, basis, _sample_text_on_unix,
264
            _sample_text_on_unix, _sample_text_on_win,
265
            roundtrip_to=_LF_IN_REPO)
266
267
    def test_eol_native_clean_crlf(self):
268
        wt, basis = self.prepare_tree(_sample_clean_crlf, eol='native')
269
        self.assertContent(wt, basis, _sample_text_on_unix,
270
            _sample_text_on_unix, _sample_text_on_win,
271
            roundtrip_to=_LF_IN_REPO)
272
273
    def test_eol_lf_clean_lf(self):
274
        wt, basis = self.prepare_tree(_sample_clean_lf, eol='lf')
275
        self.assertContent(wt, basis, _sample_text_on_unix,
276
            _sample_text_on_unix, _sample_text_on_win,
277
            roundtrip_to=_LF_IN_REPO)
278
279
    def test_eol_lf_clean_crlf(self):
280
        wt, basis = self.prepare_tree(_sample_clean_crlf, eol='lf')
281
        self.assertContent(wt, basis, _sample_text_on_unix,
282
            _sample_text_on_unix, _sample_text_on_win,
283
            roundtrip_to=_LF_IN_REPO)
284
285
    def test_eol_crlf_clean_lf(self):
286
        wt, basis = self.prepare_tree(_sample_clean_lf, eol='crlf')
287
        self.assertContent(wt, basis, _sample_text_on_unix,
288
            _sample_text_on_unix, _sample_text_on_win,
289
            roundtrip_to=_LF_IN_REPO)
290
291
    def test_eol_crlf_clean_crlf(self):
292
        wt, basis = self.prepare_tree(_sample_clean_crlf, eol='crlf')
293
        self.assertContent(wt, basis, _sample_text_on_unix,
294
            _sample_text_on_unix, _sample_text_on_win,
295
            roundtrip_to=_LF_IN_REPO)
296
297
    def test_eol_native_with_crlf_in_repo_clean_lf(self):
298
        wt, basis = self.prepare_tree(_sample_clean_lf,
299
            eol='native-with-crlf-in-repo')
300
        self.assertContent(wt, basis, _sample_text_on_win,
301
            _sample_text_on_unix, _sample_text_on_win,
302
            roundtrip_to=_CRLF_IN_REPO)
303
304
    def test_eol_native_with_crlf_in_repo_clean_crlf(self):
305
        wt, basis = self.prepare_tree(_sample_clean_crlf,
306
            eol='native-with-crlf-in-repo')
307
        self.assertContent(wt, basis, _sample_text_on_win,
308
            _sample_text_on_unix, _sample_text_on_win,
309
            roundtrip_to=_CRLF_IN_REPO)
310
311
    def test_eol_lf_with_crlf_in_repo_clean_lf(self):
312
        wt, basis = self.prepare_tree(_sample_clean_lf,
313
            eol='lf-with-crlf-in-repo')
314
        self.assertContent(wt, basis, _sample_text_on_win,
315
            _sample_text_on_unix, _sample_text_on_win,
316
            roundtrip_to=_CRLF_IN_REPO)
317
318
    def test_eol_lf_with_crlf_in_repo_clean_crlf(self):
319
        wt, basis = self.prepare_tree(_sample_clean_crlf,
320
            eol='lf-with-crlf-in-repo')
321
        self.assertContent(wt, basis, _sample_text_on_win,
322
            _sample_text_on_unix, _sample_text_on_win,
323
            roundtrip_to=_CRLF_IN_REPO)
324
325
    def test_eol_crlf_with_crlf_in_repo_clean_lf(self):
326
        wt, basis = self.prepare_tree(_sample_clean_lf,
327
            eol='crlf-with-crlf-in-repo')
328
        self.assertContent(wt, basis, _sample_text_on_win,
329
            _sample_text_on_unix, _sample_text_on_win,
330
            roundtrip_to=_CRLF_IN_REPO)
331
332
    def test_eol_crlf_with_crlf_in_repo_clean_crlf(self):
333
        wt, basis = self.prepare_tree(_sample_clean_crlf,
334
            eol='crlf-with-crlf-in-repo')
335
        self.assertContent(wt, basis, _sample_text_on_win,
336
            _sample_text_on_unix, _sample_text_on_win,
337
            roundtrip_to=_CRLF_IN_REPO)