1
# Copyright (C) 2009 Canonical Ltd
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.
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.
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
17
"""Tests for eol conversion."""
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
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"""
34
class TestEolConversion(TestCaseWithWorkingTree):
37
# formats that don't support content filtering can skip these tests
38
fmt = self.workingtree_format
39
f = getattr(fmt, 'supports_content_filtering')
41
raise TestSkipped("format %s doesn't declare whether it "
42
"supports content filtering, assuming not" % fmt)
44
raise TestSkipped("format %s doesn't support content filtering"
46
TestCaseWithWorkingTree.setUp(self)
48
def patch_rules_searcher(self, eol):
49
"""Patch in a custom rules searcher with a given eol setting."""
51
WorkingTree._get_rules_searcher = self.real_rules_searcher
53
def custom_eol_rules_searcher(tree, default_searcher):
54
return rules._IniBasedRulesSearcher([
58
WorkingTree._get_rules_searcher = custom_eol_rules_searcher
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')
71
basis = t.basis_tree()
73
self.addCleanup(basis.unlock)
76
def assertNewContentForSetting(self, wt, eol, expected_unix,
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)
88
self.assertEqual(expected_unix, content)
90
def assertContent(self, wt, basis, expected_raw, expected_unix,
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)
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)
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)
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,
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)
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,
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)
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,
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)
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,
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)
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,
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)
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,
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)
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,