/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/workingtree_implementations/test_eol_conversion.py

  • Committer: Ian Clatworthy
  • Date: 2009-04-03 00:07:49 UTC
  • mfrom: (4241 +trunk)
  • mto: (4265.1.1 bbc-merge)
  • mto: This revision was merged to the branch mainline in revision 4280.
  • Revision ID: ian.clatworthy@canonical.com-20090403000749-sevl9klctwfi8jml
merge bzr.dev r4241

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Canonical Ltd
 
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
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
 
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"""
 
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
 
 
76
    def assertNewContentForSetting(self, wt, eol, expected_unix,
 
77
        expected_win=None):
 
78
        """Clone a working tree and check the convenience content."""
 
79
        if expected_win is None:
 
80
            expected_win = expected_unix
 
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':
 
86
            self.assertEqual(expected_win, content)
 
87
        else:
 
88
            self.assertEqual(expected_unix, content)
 
89
 
 
90
    def assertContent(self, wt, basis, expected_raw, expected_unix,
 
91
        expected_win):
 
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)
 
95
        self.assertNewContentForSetting(wt, None, expected_raw)
 
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)
 
108
        self.assertNewContentForSetting(wt, 'exact', expected_raw)
 
109
 
 
110
    def test_eol_no_rules(self):
 
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
 
 
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
 
 
125
    def test_eol_lf(self):
 
126
        wt, basis = self.prepare_tree(_sample_text, eol='lf')
 
127
        self.assertContent(wt, basis, _sample_text_on_unix,
 
128
            _sample_text_on_unix, _sample_text_on_win)
 
129
 
 
130
    def test_eol_lf_binary(self):
 
131
        wt, basis = self.prepare_tree(_sample_binary, eol='lf')
 
132
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
 
133
            _sample_binary)
 
134
 
 
135
    def test_eol_crlf(self):
 
136
        wt, basis = self.prepare_tree(_sample_text, eol='crlf')
 
137
        self.assertContent(wt, basis, _sample_text_on_unix,
 
138
            _sample_text_on_unix, _sample_text_on_win)
 
139
 
 
140
    def test_eol_crlf_binary(self):
 
141
        wt, basis = self.prepare_tree(_sample_binary, eol='crlf')
 
142
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
 
143
            _sample_binary)
 
144
 
 
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')
 
174
        self.assertContent(wt, basis, _sample_binary, _sample_binary,
 
175
            _sample_binary)
 
176
 
 
177
    def test_eol_exact(self):
 
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)