single opt for get, plus simple unit test

This commit is contained in:
Mike McLean 2023-03-13 16:00:23 -04:00 committed by Tomas Kopecek
parent 898af45e4f
commit 106ccbcde5
2 changed files with 52 additions and 2 deletions

View file

@ -920,12 +920,25 @@ class RawHeader(object):
else:
raise GenericError("Unknown header data type: %x" % dtype)
def get(self, key, default=None, decode=None):
def get(self, key, default=None, decode=None, single=False):
# With decode on, we will _mostly_ return the same value that rpmlib will.
# There are exceptions where rpmlib will automatically translate or update values, e.g.
# * fields that rpm treats as scalars
# * special tags like Headerimmutable
# * i18n string translations
# * the Fileclass extension tag that overlaps a concrete tag
# * auto converting PREINPROG/POSTINPROG/etc to string arrays for older rpms
entry = self.index.get(key)
if entry is None:
return default
else:
return self._getitem(*entry[1:], decode=decode)
value = self._getitem(*entry[1:], decode=decode)
if single and isinstance(value, list):
if len(value) == 1:
return value[0]
else:
raise ValueError('single value requested for array at key %s' % key)
return value
def rip_rpm_sighdr(src):

View file

@ -0,0 +1,37 @@
# coding=utf-8
from __future__ import absolute_import
import os.path
import unittest
import koji
class TestRawHeaderFields(unittest.TestCase):
RPMFILES = [
"test-deps-1-1.fc24.x86_64.rpm",
"test-files-1-1.fc27.noarch.rpm",
"test-nosrc-1-1.fc24.nosrc.rpm",
"test-deps-1-1.fc24.x86_64.rpm.signed",
"test-nopatch-1-1.fc24.nosrc.rpm",
"test-src-1-1.fc24.src.rpm",
]
def test_header_sizes(self):
for basename in self.RPMFILES:
fn = os.path.join(os.path.dirname(__file__), 'data/rpms', basename)
rh = koji.RawHeader(koji.rip_rpm_hdr(fn))
hdr = koji.get_rpm_header(fn)
for key in rh.index:
if key in (63, 1141):
continue
ours = rh.get(key, decode=True)
theirs = hdr[key]
if type(ours) != type(theirs):
if isinstance(ours, list) and len(ours) == 1 and ours[0] == theirs:
# rpm is presenting as a scalar
continue
# otherwise
self.assertEqual(ours, theirs)