Skip to content

Commit 7a18680

Browse files
committed
Improve %changelog handling
- Handle %changelog case insensitively, allows %changeLog and variants. - Don't throw ValueError exception when %changelog isn't found. - Throw new MultipleChangelog exception on multiple changelogs. - Provide unit tests for above cases. Fixes: #126 Fixes: #127 Change-Id: Ic8fbf7e436413f9e7525f866bc0853488dbee387
1 parent 0804cae commit 7a18680

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

rdopkg/exception.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class IncompleteChangelog(RdopkgException):
4040
msg_fmt = "Description of changes is missing in %%changelog."
4141

4242

43+
class MultipleChangelog(RdopkgException):
44+
msg_fmt = "Multiple %%changelog sections found. Expected only one."
45+
46+
4347
class MultipleSpecFilesFound(RdopkgException):
4448
msg_fmt = "Multiple .spec files found. Expected only one."
4549

rdopkg/utils/specfile.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,12 @@ def get_source_fns(self):
594594
return map(os.path.basename, self.get_source_urls())
595595

596596
def get_last_changelog_entry(self, strip=False):
597-
_, changelog = self.txt.split("%changelog\n")
598-
changelog = changelog.strip()
597+
changelog = ''
598+
r = re.split("%changelog\n", self.txt, flags=re.I)
599+
if len(r) > 2:
600+
raise exception.MultipleChangelog()
601+
if len(r) == 2:
602+
changelog = r[1].strip()
599603
entries = re.split(r'\n\n+', changelog)
600604
entry = entries[0]
601605
lines = entry.split("\n")

tests/test_common.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from rdopkg.utils.cmd import git
77
from rdopkg.utils.specfile import Spec
8+
from rdopkg import exception
89

910

1011
ASSETS_DIR = 'tests/assets'
@@ -124,10 +125,14 @@ def assert_spec_version(version, release_parts, milestone):
124125

125126
def norm_changelog(count=1):
126127
spec = Spec()
127-
txt, chl = spec.txt.split('%changelog\n')
128+
r = re.compile(r"%changelog\n", flags=re.I).split(spec.txt)
129+
if len(r) > 2:
130+
raise exception.MultipleChangelog()
131+
txt, chl = r
128132
chl, n = re.subn(r'^\* .+\s(\d\S*)$',
129133
'* DATE AUTHOR \g<1>',
130-
chl, count=count,
134+
chl,
135+
count=count,
131136
flags=re.M)
132137
spec._txt = txt + '%changelog\n' + chl
133138
spec.save()

tests/test_findpkg_integration.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
# Integration tests for findpkg
33

44
set -e

tests/test_spec.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,32 @@ def test_get_magic_comment_minimal_6():
316316
def test_get_magic_comment_minimal_7():
317317
spec = specfile.Spec(txt='# foo=\n')
318318
assert '' == spec.get_magic_comment('foo')
319+
320+
321+
def test_get_last_changelog_entry_0():
322+
txt = 'Version: 1.2.3\n\n# patches_ignore=DROP-IN-RPM\n# patches_base=1.2.3\n#\nPatch0=foo.patch\n' # noqa
323+
spec = specfile.Spec(txt=txt)
324+
r = spec.get_last_changelog_entry()
325+
assert ('', []) == r
326+
327+
328+
def test_get_last_changelog_entry_1():
329+
txt = 'Version: 1.2.3\n\n# patches_ignore=DROP-IN-RPM\n# patches_base=1.2.3\n#\nPatch0=foo.patch\n' # noqa
330+
spec = specfile.Spec(txt=txt + '%changelog\nfoo')
331+
r = spec.get_last_changelog_entry()
332+
assert ('foo', []) == r
333+
334+
335+
def test_get_last_changelog_entry_1_case_insensitive():
336+
txt = 'Version: 1.2.3\n\n# patches_ignore=DROP-IN-RPM\n# patches_base=1.2.3\n#\nPatch0=foo.patch\n' # noqa
337+
spec = specfile.Spec(txt=txt + '%ChangeLog\nfoo')
338+
r = spec.get_last_changelog_entry()
339+
assert ('foo', []) == r
340+
341+
342+
def test_get_last_changelog_entry_multiple_sections():
343+
with pytest.raises(exception.MultipleChangelog):
344+
txt = 'Version: 1.2.3\n\n'
345+
spec = specfile.Spec(txt=txt + '%changelog\n* 2017-01-01\n- foo1\n\n%changelog\nbar\n') # noqa
346+
r = spec.get_last_changelog_entry()
347+
assert False, r

0 commit comments

Comments
 (0)