Verified Commit 552a3761 authored by Jelle van der Waa's avatar Jelle van der Waa 🚧
Browse files

Add generate_package fixture

This fixture fakes a pacman package for example required for pyalpm to
test Handle.load_pkg.
parent 36016990
......@@ -7,6 +7,19 @@ import tarfile
import pytest
def generate_pkginfo(data):
pkginfo = ''
for key, value in data.items():
if not isinstance(value, list):
value = [value]
for val in value:
pkginfo += f'{key} = {val}\n'
return pkginfo
def generate_desc(pkg):
......@@ -114,3 +127,33 @@ def generate_localdb(tmpdir_factory):
return dbroot
return _generate_localdb
def generate_package(tmpdir_factory):
'''Generates a package in provided location or when not provided pytest tmpdir
data (object): a dict containing the fields for PKGINFO
pkgname (string): the package file name ($pkgname-$pkgver-$pkgrel.pkg.tar)
pkgpath (string): the path to save the pkg file
str: path to package
def _generate_package(data, pkgname='test.pkg.tar', pkgpath=''):
if not pkgpath:
pkgpath = str(tmpdir_factory.mktemp('pkgpath').join(pkgname))
pkgpath = os.path.join(pkgpath, pkgname)
tar =, 'w')
info = tarfile.TarInfo('.PKGINFO')
data = generate_pkginfo(data)
info.size = len(data)
tar.addfile(info, io.BytesIO(data.encode()))
return pkgpath
return _generate_package
"pkgname": "filesystem",
"pkgbase": "filesystem",
"pkgver": "1-1",
"pkgdesc": "filesystem",
"url": "",
"builddate": 1599085821,
"packager": "Test <>",
"arch": "x86_64",
"license": "GPL",
"depend": [
......@@ -39,6 +39,17 @@ def syncdb_tmpdir(tmpdir_factory, core_data, generate_syncdb):
return generate_syncdb(core_data, 'foo.db', dbpath)
def pkg_data():
curpath = os.path.dirname(os.path.realpath(__file__))
return json.load(open(f'{curpath}/package.json'))
def package(pkg_data, generate_package):
return generate_package(pkg_data)
def test_localdb(core_data, localdb):
pkg = core_data[0]
pkgpath = f"{localdb}/local/{pkg['name']}-{pkg['version']}"
......@@ -75,3 +86,9 @@ def test_syncdb_tmpdir(core_data, syncdb_tmpdir):
pkgpath = f"{pkg['name']}-{pkg['version']}"
assert == f"{pkgpath}/desc"
def test_package(pkg_data, package):
with as tar:
for index, tarinfo in enumerate(tar):
assert == '.PKGINFO'
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment