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

Add generate_syncdb fixture

This fixture can generate a pacman syncdb from a dictionary.
parent c00a8cb2
Pipeline #4705 passed with stage
in 15 seconds
#!/usr/bin/python3
import io
import os
import tarfile
from shutil import rmtree
from tempfile import mkdtemp
import pytest
......@@ -51,25 +49,36 @@ def generate_files(pkg):
return files
def generate_syncdb(pkgs, dbfile):
dbpath = mkdtemp(dir='/tmp')
# TODO: generate files db
@pytest.fixture(scope="session")
def generate_syncdb(tmpdir_factory):
'''Generates a sync database in a provided location or when not provided pytest tmpdir
# TODO: refactor to not use chdir.
cwd = os.getcwd()
os.chdir(dbpath)
Parameters:
pkgs (list): a list of dicts containing
dbname (string): sync database name (standard 'test.db')
dbloc (string): location where sync database will be created (optional)
with tarfile.open(dbfile, "w") as tar:
for pkg in pkgs:
path = f"{pkg['name']}-{pkg['version']}"
os.makedirs(path)
Returns:
str: path to dbroot for when dbroot is not provided
'''
with open(f'{path}/desc', 'w') as f:
f.write(generate_desc(pkg))
def _generate_syncdb(pkgs, dbname='test.db', dbloc=''):
if not dbloc:
dbloc = str(tmpdir_factory.mktemp('dbpath').join(dbname))
else:
dbloc = os.path.join(dbloc, dbname)
tar = tarfile.open(dbloc, "w")
for pkg in pkgs:
info = tarfile.TarInfo(f"{pkg['name']}-{pkg['version']}/desc")
data = generate_desc(pkg)
info.size = len(data)
tar.addfile(info, io.BytesIO(data.encode()))
tar.add(path)
return dbloc
rmtree(dbpath)
os.chdir(cwd)
return _generate_syncdb
@pytest.fixture(scope="session")
......
#!/usr/bin/python
import json
import tarfile
import os
import os.path
......@@ -27,6 +28,17 @@ def localdb_tmpdir(tmpdir_factory, core_data, generate_localdb):
return generate_localdb(core_data, dbpath)
@pytest.fixture
def syncdb(core_data, generate_syncdb):
return generate_syncdb(core_data)
@pytest.fixture
def syncdb_tmpdir(tmpdir_factory, core_data, generate_syncdb):
dbpath = str(tmpdir_factory.mktemp('dbpath'))
return generate_syncdb(core_data, 'foo.db', dbpath)
def test_localdb(core_data, localdb):
pkg = core_data[0]
pkgpath = f"{localdb}/local/{pkg['name']}-{pkg['version']}"
......@@ -45,3 +57,21 @@ def test_localdb_tmpdir(core_data, localdb_tmpdir):
pkgpath = f"{localdb_tmpdir}/local/{pkg['name']}-{pkg['version']}"
assert os.path.exists(localdb_tmpdir)
assert os.path.exists(pkgpath)
def test_syncdb(core_data, syncdb):
with tarfile.open(syncdb) as tar:
for index, tarinfo in enumerate(tar):
pkg = core_data[index]
pkgpath = f"{pkg['name']}-{pkg['version']}"
assert tarinfo.name == f"{pkgpath}/desc"
def test_syncdb_tmpdir(core_data, syncdb_tmpdir):
with tarfile.open(syncdb_tmpdir) as tar:
for index, tarinfo in enumerate(tar):
pkg = core_data[index]
pkgpath = f"{pkg['name']}-{pkg['version']}"
assert tarinfo.name == f"{pkgpath}/desc"
Markdown is supported
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