1
# Copyright (C) 2008 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
18
from bzrlib import errors, filters
19
from bzrlib.filters import (
23
filtered_output_bytes,
24
_get_filter_stack_for,
25
_get_registered_names,
26
internal_size_sha_file_byname,
27
register_filter_stack_map,
29
from bzrlib.osutils import sha_string
30
from bzrlib.tests import TestCase, TestCaseInTempDir
33
# sample filter stacks
34
def _swapcase(chunks, context=None):
35
return [s.swapcase() for s in chunks]
37
return ['junk\n'] + [s for s in chunks]
38
def _deljunk(chunks, context):
39
return [s for s in chunks[1:]]
41
ContentFilter(_swapcase, _swapcase),
44
ContentFilter(_swapcase, _swapcase),
45
ContentFilter(_addjunk, _deljunk),
49
_sample_external = ['Hello\n', 'World\n']
50
_internal_1 = ['hELLO\n', 'wORLD\n']
51
_internal_2 = ['junk\n', 'hELLO\n', 'wORLD\n']
54
class TestContentFilterContext(TestCase):
56
def test_empty_filter_context(self):
57
ctx = ContentFilterContext()
58
self.assertEqual(None, ctx.relpath())
60
def test_filter_context_with_path(self):
61
ctx = ContentFilterContext('foo/bar')
62
self.assertEquals('foo/bar', ctx.relpath())
65
class TestFilteredInput(TestCase):
67
def test_filtered_input_file(self):
68
# test an empty stack returns the same result
69
external = ''.join(_sample_external)
70
f = StringIO.StringIO(external)
71
self.assertEqual(external, filtered_input_file(f, None).read())
72
# test a single item filter stack
73
f = StringIO.StringIO(external)
74
expected = ''.join(_internal_1)
75
self.assertEqual(expected, filtered_input_file(f, _stack_1).read())
76
# test a multi item filter stack
77
f = StringIO.StringIO(external)
78
expected = ''.join(_internal_2)
79
self.assertEqual(expected, filtered_input_file(f, _stack_2).read())
82
class TestFilteredOutput(TestCase):
84
def test_filtered_output_bytes(self):
85
# test an empty stack returns the same result
86
self.assertEqual(_sample_external, list(filtered_output_bytes(
87
_sample_external, None)))
88
# test a single item filter stack
89
self.assertEqual(_sample_external, list(filtered_output_bytes(
90
_internal_1, _stack_1)))
91
# test a multi item filter stack
92
self.assertEqual(_sample_external, list(filtered_output_bytes(
93
_internal_2, _stack_2)))
96
class TestFilteredSha(TestCaseInTempDir):
98
def test_filtered_size_sha(self):
99
# check that the size and sha matches what's expected
100
text = 'Foo Bar Baz\n'
104
post_filtered_content = ''.join(_swapcase([text], None))
105
expected_len = len(post_filtered_content)
106
expected_sha = sha_string(post_filtered_content)
107
self.assertEqual((expected_len,expected_sha),
108
internal_size_sha_file_byname('a',
109
[ContentFilter(_swapcase, _swapcase)]))
112
class TestFilterStackMaps(TestCase):
114
def _register_map(self, pref, stk1, stk2):
116
return {'v1': stk1, 'v2': stk2}.get(key)
117
register_filter_stack_map(pref, stk_lookup)
119
def test_filter_stack_maps(self):
120
# Save the current registry
121
original_registry = filters._reset_registry()
124
a_stack = [ContentFilter('b', 'c')]
125
z_stack = [ContentFilter('y', 'x'), ContentFilter('w', 'v')]
126
self._register_map('foo', a_stack, z_stack)
127
self.assertEqual(['foo'], _get_registered_names())
128
self._register_map('bar', z_stack, a_stack)
129
self.assertEqual(['bar', 'foo'], _get_registered_names())
130
# Test re-registration raises an error
131
self.assertRaises(errors.BzrError, self._register_map,
134
# Restore the real registry
135
filters._reset_registry(original_registry)
137
def test_get_filter_stack_for(self):
138
# Save the current registry
139
original_registry = filters._reset_registry()
141
# Test filter stack lookup
142
a_stack = [ContentFilter('b', 'c')]
143
d_stack = [ContentFilter('d', 'D')]
144
z_stack = [ContentFilter('y', 'x'), ContentFilter('w', 'v')]
145
self._register_map('foo', a_stack, z_stack)
146
self._register_map('bar', d_stack, z_stack)
147
prefs = (('foo','v1'),)
148
self.assertEqual(a_stack, _get_filter_stack_for(prefs))
149
prefs = (('foo','v2'),)
150
self.assertEqual(z_stack, _get_filter_stack_for(prefs))
151
prefs = (('foo','v1'),('bar','v1'))
152
self.assertEqual(a_stack + d_stack, _get_filter_stack_for(prefs))
153
# Test an unknown preference
154
prefs = (('baz','v1'),)
155
self.assertEqual([], _get_filter_stack_for(prefs))
156
# Test an unknown value
157
prefs = (('foo','v3'),)
158
self.assertEqual([], _get_filter_stack_for(prefs))
160
# Restore the real registry
161
filters._reset_registry(original_registry)