self._special_directives[key].append(linerec)
return ret
+ def _set_section(self, name, text):
+ """Update/create a complete section in spec file."""
+ if name not in self.section_identifiers:
+ raise GbpError("Not a valid section directive: '%s'" % name)
+ # Delete section, if it exists
+ if name in self._special_directives:
+ if len(self._special_directives[name]) > 1:
+ raise GbpError("Multiple %%%s sections found, don't know "
+ "which to update" % name)
+ line = self._special_directives[name][0]['line']
+ gbp.log.debug("Removing content of %s section" % name)
+ while line.next:
+ match = self.directive_re.match(str(line.next))
+ if match and match.group('name') in self.section_identifiers:
+ break
+ self._content.delete(line.next)
+ else:
+ gbp.log.debug("Adding %s section to the end of spec file" % name)
+ line = self._content.append('%%%s\n' % name)
+ linerec = {'line': line, 'id': None, 'args': None}
+ self._special_directives[name] = [linerec]
+ # Add new lines
+ gbp.log.debug("Updating content of %s section" % name)
+ for linetext in text.splitlines():
+ line = self._content.insert_after(line, linetext + '\n')
+
+ def set_changelog(self, text):
+ """Update or create the %changelog section"""
+ self._set_section('changelog', text)
+
+ def get_changelog(self):
+ """Get the %changelog section"""
+ text = ''
+ if 'changelog' in self._special_directives:
+ line = self._special_directives['changelog'][0]['line']
+ while line.next:
+ line = line.next
+ match = self.directive_re.match(str(line))
+ if match and match.group('name') in self.section_identifiers:
+ break
+ text += str(line)
+ return text
+
def update_patches(self, patches, commands):
"""Update spec with new patch tags and patch macros"""
# Remove non-ignored patches
import os
import shutil
import tempfile
-from nose.tools import assert_raises
+from nose.tools import assert_raises, eq_
from gbp.errors import GbpError
from gbp.rpm import (SrcRpmFile, SpecFile, parse_srpm, NoSpecError, guess_spec,
spec.write_spec_file()
assert filecmp.cmp(tmp_spec, reference_spec) is True
- # Test adding the VCS tag
+ # Test adding the VCS tag and adding changelog
reference_spec = os.path.join(SPEC_DIR, 'gbp-test-reference2.spec')
spec.set_tag('VCS', None, 'myvcstag')
+ spec.set_changelog("* Wed Feb 05 2014 Name <email> 1\n- New entry\n")
spec.write_spec_file()
assert filecmp.cmp(tmp_spec, reference_spec) is True
def test_update_spec2(self):
"""Another test for spec autoupdate functionality"""
- tmp_spec = os.path.join(self.tmpdir, 'gbp-test.spec')
+ tmp_spec = os.path.join(self.tmpdir, 'gbp-test2.spec')
shutil.copy2(os.path.join(SPEC_DIR, 'gbp-test2.spec'), tmp_spec)
reference_spec = os.path.join(SPEC_DIR, 'gbp-test2-reference2.spec')
spec.write_spec_file()
assert filecmp.cmp(tmp_spec, reference_spec) is True
- # Test updating patches again and removing the VCS tag
+ # Test updating patches again, removing the VCS tag and re-writing
+ # changelog
reference_spec = os.path.join(SPEC_DIR, 'gbp-test2-reference.spec')
spec.update_patches(['new.patch'], {'new.patch': {'if': '1'}})
spec.set_tag('VCS', None, '')
+ spec.set_changelog("* Wed Feb 05 2014 Name <email> 2\n- New entry\n\n")
spec.write_spec_file()
assert filecmp.cmp(tmp_spec, reference_spec) is True
spec.write_spec_file()
assert filecmp.cmp(tmp_spec, reference_spec) is True
+ def test_modifying_err(self):
+ """Test error conditions of modification methods"""
+ spec_filepath = os.path.join(SPEC_DIR, 'gbp-test2.spec')
+ spec = SpecFileTester(spec_filepath)
+
+ # Unknown/invalid section name
+ with assert_raises(GbpError):
+ spec.protected('_set_section')('patch', 'new content\n')
+
+ # Multiple sections with the same name
+ with assert_raises(GbpError):
+ spec.protected('_set_section')('files', '%{_sysconfdir}/foo\n')
+
+ def test_changelog(self):
+ """Test changelog methods"""
+ spec_filepath = os.path.join(SPEC_DIR, 'gbp-test2.spec')
+ spec = SpecFile(spec_filepath)
+
+ # Read changelog
+ eq_(spec.get_changelog(),
+ "* Tue Feb 04 2014 Name <email> 1\n- My change\n\n\n")
+
+ # Set changelog and check again
+ new_text = "* Wed Feb 05 2014 Name <email> 2\n- New entry\n\n\n"
+ spec.set_changelog(new_text)
+ eq_(spec.get_changelog(), new_text)
+
def test_quirks(self):
"""Test spec that is broken/has anomalities"""
spec_filepath = os.path.join(SPEC_DIR, 'gbp-test-quirks.spec')