/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
21
from bzrlib import rules
22
from bzrlib.tests import TestSkipped
23
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
24
from bzrlib.workingtree import WorkingTree
25
26
27
# Sample files
4208.4.3 by Ian Clatworthy
rename dos to windows
28
_sample_text         = """hello\nworld\r\n"""
29
_sample_text_on_win  = """hello\r\nworld\r\n"""
30
_sample_text_on_unix = """hello\nworld\n"""
31
_sample_binary       = """hello\nworld\r\n\x00"""
4208.4.1 by Ian Clatworthy
eol conversion support
32
33
34
class TestEolConversion(TestCaseWithWorkingTree):
35
36
    def setUp(self):
37
        # formats that don't support content filtering can skip these tests
38
        fmt = self.workingtree_format
39
        f = getattr(fmt, 'supports_content_filtering')
40
        if f is None:
41
            raise TestSkipped("format %s doesn't declare whether it "
42
                "supports content filtering, assuming not" % fmt)
43
        if not f():
44
            raise TestSkipped("format %s doesn't support content filtering"
45
                % fmt)
46
        TestCaseWithWorkingTree.setUp(self)
47
48
    def patch_rules_searcher(self, eol):
49
        """Patch in a custom rules searcher with a given eol setting."""
50
        if eol is None:
51
            WorkingTree._get_rules_searcher = self.real_rules_searcher
52
        else:
53
            def custom_eol_rules_searcher(tree, default_searcher):
54
                return rules._IniBasedRulesSearcher([
55
                    '[name *]\n',
56
                    'eol=%s\n' % eol,
57
                    ])
58
            WorkingTree._get_rules_searcher = custom_eol_rules_searcher
59
60
    def prepare_tree(self, content, eol=None):
61
        """Prepare a working tree and commit some content."""
62
        def restore_real_rules_searcher():
63
            WorkingTree._get_rules_searcher = self.real_rules_searcher
64
        self.real_rules_searcher = WorkingTree._get_rules_searcher
65
        self.addCleanup(restore_real_rules_searcher)
66
        self.patch_rules_searcher(eol)
67
        t = self.make_branch_and_tree('tree1')
68
        self.build_tree_contents([('tree1/file1', content)])
69
        t.add('file1', 'file1-id')
70
        t.commit("add file1")
71
        basis = t.basis_tree()
72
        basis.lock_read()
73
        self.addCleanup(basis.unlock)
74
        return t, basis
75
4208.4.5 by Ian Clatworthy
add lf-always and crlf-always
76
    def assertNewContentForSetting(self, wt, eol, expected_unix,
77
        expected_win=None):
4208.4.1 by Ian Clatworthy
eol conversion support
78
        """Clone a working tree and check the convenience content."""
4208.4.5 by Ian Clatworthy
add lf-always and crlf-always
79
        if expected_win is None:
80
            expected_win = expected_unix
4208.4.1 by Ian Clatworthy
eol conversion support
81
        self.patch_rules_searcher(eol)
82
        wt2 = wt.bzrdir.sprout('tree-%s' % eol).open_workingtree()
83
        # To see exactly what got written to disk, we need an unfiltered read
84
        content = wt2.get_file('file1-id', filtered=False).read()
85
        if sys.platform == 'win32':
4208.4.3 by Ian Clatworthy
rename dos to windows
86
            self.assertEqual(expected_win, content)
4208.4.1 by Ian Clatworthy
eol conversion support
87
        else:
88
            self.assertEqual(expected_unix, content)
89
90
    def assertContent(self, wt, basis, expected_raw, expected_unix,
4208.4.3 by Ian Clatworthy
rename dos to windows
91
        expected_win):
4208.4.1 by Ian Clatworthy
eol conversion support
92
        """Check the committed content and content in cloned trees."""
93
        basis_content = basis.get_file('file1-id').read()
94
        self.assertEqual(expected_raw, basis_content)
4208.4.5 by Ian Clatworthy
add lf-always and crlf-always
95
        self.assertNewContentForSetting(wt, None, expected_raw)
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
96
        self.assertNewContentForSetting(wt, 'native',
97
            expected_unix, expected_win)
98
        self.assertNewContentForSetting(wt, 'lf',
99
            expected_unix, expected_unix)
100
        self.assertNewContentForSetting(wt, 'crlf',
101
            expected_win, expected_win)
102
        self.assertNewContentForSetting(wt, 'native-with-crlf-in-repo',
103
            expected_unix, expected_win)
104
        self.assertNewContentForSetting(wt, 'lf-with-crlf-in-repo',
105
            expected_unix, expected_unix)
106
        self.assertNewContentForSetting(wt, 'crlf-with-crlf-in-repo',
107
            expected_win, expected_win)
4208.4.5 by Ian Clatworthy
add lf-always and crlf-always
108
        self.assertNewContentForSetting(wt, 'exact', expected_raw)
4208.4.1 by Ian Clatworthy
eol conversion support
109
110
    def test_eol_no_rules(self):
4208.4.3 by Ian Clatworthy
rename dos to windows
111
        wt, basis = self.prepare_tree(_sample_text)
112
        self.assertContent(wt, basis, _sample_text,
113
            _sample_text_on_unix, _sample_text_on_win)
114
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
115
    def test_eol_native(self):
116
        wt, basis = self.prepare_tree(_sample_text, eol='native')
117
        self.assertContent(wt, basis, _sample_text_on_unix,
118
            _sample_text_on_unix, _sample_text_on_win)
119
120
    def test_eol_native_binary(self):
121
        wt, basis = self.prepare_tree(_sample_binary, eol='native')
122
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
123
            _sample_binary)
124
4208.4.4 by Ian Clatworthy
unix/window -> cr/crlf based on mailing list feedback
125
    def test_eol_lf(self):
126
        wt, basis = self.prepare_tree(_sample_text, eol='lf')
4208.4.3 by Ian Clatworthy
rename dos to windows
127
        self.assertContent(wt, basis, _sample_text_on_unix,
128
            _sample_text_on_unix, _sample_text_on_win)
129
4208.4.4 by Ian Clatworthy
unix/window -> cr/crlf based on mailing list feedback
130
    def test_eol_lf_binary(self):
131
        wt, basis = self.prepare_tree(_sample_binary, eol='lf')
4208.4.3 by Ian Clatworthy
rename dos to windows
132
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
133
            _sample_binary)
134
4208.4.4 by Ian Clatworthy
unix/window -> cr/crlf based on mailing list feedback
135
    def test_eol_crlf(self):
136
        wt, basis = self.prepare_tree(_sample_text, eol='crlf')
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
137
        self.assertContent(wt, basis, _sample_text_on_unix,
4208.4.3 by Ian Clatworthy
rename dos to windows
138
            _sample_text_on_unix, _sample_text_on_win)
139
4208.4.4 by Ian Clatworthy
unix/window -> cr/crlf based on mailing list feedback
140
    def test_eol_crlf_binary(self):
141
        wt, basis = self.prepare_tree(_sample_binary, eol='crlf')
4208.4.3 by Ian Clatworthy
rename dos to windows
142
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
143
            _sample_binary)
4208.4.1 by Ian Clatworthy
eol conversion support
144
4208.4.7 by Ian Clatworthy
tweak eol names based on mailing list discussion
145
    def test_eol_native_with_crlf_in_repo(self):
146
        wt, basis = self.prepare_tree(_sample_text,
147
            eol='native-with-crlf-in-repo')
148
        self.assertContent(wt, basis, _sample_text_on_win,
149
            _sample_text_on_unix, _sample_text_on_win)
150
151
    def test_eol_native_with_crlf_in_repo_binary(self):
152
        wt, basis = self.prepare_tree(_sample_binary,
153
            eol='native-with-crlf-in-repo')
154
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
155
            _sample_binary)
156
157
    def test_eol_lf_with_crlf_in_repo(self):
158
        wt, basis = self.prepare_tree(_sample_text, eol='lf-with-crlf-in-repo')
159
        self.assertContent(wt, basis, _sample_text_on_win,
160
            _sample_text_on_unix, _sample_text_on_win)
161
162
    def test_eol_lf_with_crlf_in_repo_binary(self):
163
        wt, basis = self.prepare_tree(_sample_binary, eol='lf-with-crlf-in-repo')
164
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
165
            _sample_binary)
166
167
    def test_eol_crlf_with_crlf_in_repo(self):
168
        wt, basis = self.prepare_tree(_sample_text, eol='crlf-with-crlf-in-repo')
169
        self.assertContent(wt, basis, _sample_text_on_win,
170
            _sample_text_on_unix, _sample_text_on_win)
171
172
    def test_eol_crlf_with_crlf_in_repo_binary(self):
173
        wt, basis = self.prepare_tree(_sample_binary, eol='crlf-with-crlf-in-repo')
4208.4.5 by Ian Clatworthy
add lf-always and crlf-always
174
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
175
            _sample_binary)
176
4208.4.1 by Ian Clatworthy
eol conversion support
177
    def test_eol_exact(self):
4208.4.3 by Ian Clatworthy
rename dos to windows
178
        wt, basis = self.prepare_tree(_sample_text, eol='exact')
179
        self.assertContent(wt, basis, _sample_text,
180
            _sample_text_on_unix, _sample_text_on_win)
181
182
    def test_eol_exact_binary(self):
183
        wt, basis = self.prepare_tree(_sample_binary, eol='exact')
184
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
185
            _sample_binary)