Merge lp:~anj/bzr-keywords/fix-505038 into lp:bzr-keywords

Proposed by Andrew Johnson
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: 18
Merged at revision: 17
Proposed branch: lp:~anj/bzr-keywords/fix-505038
Merge into: lp:bzr-keywords
Diff against target: 72 lines (+14/-6)
2 files modified
__init__.py (+12/-5)
tests/test_conversion.py (+2/-1)
To merge this branch: bzr merge lp:~anj/bzr-keywords/fix-505038
Reviewer Review Type Date Requested Status
Jelmer Vernooij (community) code Approve
Ian Clatworthy Pending
Review via email: mp+37294@code.launchpad.net

Description of the change

This branch causes the compress_keywords() function to avoid compressing keywords that are not found in the keyword registry. The expand_keywords() code already knows not to touch them. Includes a modification to the test suite.

This fixes bug #505038.

To post a comment you must log in.
lp:~anj/bzr-keywords/fix-505038 updated
18. By Andrew Johnson

Fix help text to match.

Revision history for this message
Jelmer Vernooij (jelmer) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '__init__.py'
2--- __init__.py 2009-04-06 22:40:14 +0000
3+++ __init__.py 2010-10-01 19:46:11 +0000
4@@ -31,8 +31,7 @@
5
6 When expanding, the existing text is retained if an unknown keyword is
7 found. If the keyword is already expanded but known, the value is replaced.
8-When compressing, values are always removed, whether the keyword is known
9-or not.
10+When compressing, the values of known keywords are removed.
11
12 Keyword filtering needs to be enabled for selected branches and files via
13 rules. See ``bzr help rules`` for general information on defining rules.
14@@ -262,10 +261,13 @@
15 return ""
16
17
18-def compress_keywords(s):
19+def compress_keywords(s, keyword_dicts):
20 """Replace cooked style keywords with raw style in a string.
21
22+ Note: If the keyword is not known, the text is not modified.
23+
24 :param s: the string
25+ :param keyword_dicts: an iterable of keyword dictionaries.
26 :return: the string with keywords compressed
27 """
28 _raw_style = _keyword_style_registry.get('raw')
29@@ -277,7 +279,12 @@
30 break
31 result += rest[:match.start()]
32 keyword = match.group(1)
33- result += _raw_style % {'name': keyword}
34+ expansion = _get_from_dicts(keyword_dicts, keyword)
35+ if expansion is None:
36+ # Unknown expansion - leave as is
37+ result += match.group(0)
38+ else:
39+ result += _raw_style % {'name': keyword}
40 rest = rest[match.end():]
41 return result + rest
42
43@@ -351,7 +358,7 @@
44 def _kw_compressor(chunks, context=None):
45 """Filter that replaces keywords with their compressed form."""
46 text = ''.join(chunks)
47- return [compress_keywords(text)]
48+ return [compress_keywords(text, [keyword_registry])]
49
50
51 def _kw_expander(chunks, context, encoder=None):
52
53=== modified file 'tests/test_conversion.py'
54--- tests/test_conversion.py 2008-07-27 13:04:54 +0000
55+++ tests/test_conversion.py 2010-10-01 19:46:11 +0000
56@@ -34,6 +34,7 @@
57 ('$Foo$$Bar$', '$Foo: FOO! $$Bar: bar $'),
58 ('abc $Foo$ xyz $Bar$ qwe', 'abc $Foo: FOO! $ xyz $Bar: bar $ qwe'),
59 ('$Unknown$$Bar$', '$Unknown$$Bar: bar $'),
60+ ('$Unknown: unkn $$Bar$', '$Unknown: unkn $$Bar: bar $'),
61 ('$Foo$$Unknown$', '$Foo: FOO! $$Unknown$'),
62 ('$CallMe$', '$CallMe: now! $'),
63 ]
64@@ -44,7 +45,7 @@
65 def test_compression(self):
66 # Test keyword expansion
67 for raw, cooked in _samples:
68- self.assertEqual(raw, compress_keywords(cooked))
69+ self.assertEqual(raw, compress_keywords(cooked, [_keywords]))
70
71 def test_expansion(self):
72 # Test keyword expansion

Subscribers

People subscribed via source and target branches