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

Initial commit of a pytest pacman plugin

This plugin aims to provide an easy way to create and use a pacman
localdb. For projects depending on pacman local db such as pyalpm or
syncdb's such as archweb.
parents
Pipeline #1197 failed with stage
in 25 seconds
__pycache__
image: archlinux
variables:
PYTHONPATH: "."
before_script:
- pacman -Syu --noconfirm --noprogressbar --needed
python-pytest flake8
test:
script:
- flake8 --max-line-length=120
- pytest .
# pytest-pacman - pacman db pytest fixture
This is a pytest plugin which will generate a test pacman db.
## Fixtures
The fixtures this plugin should provide:
* generate_localdb(packages)
* syncdb (/var/lib/pacman/sync/$repo.db)
* localdb (/var/lib/local/pacman/local/$pkgname-$epoch-$pkgver-$pkgrel/)
* files db (/var/lib/pacman/sync/$repo.files)
* syncdb with different compressions (zst,gz) as file
## Usage
Installing is as simple as e.g.::
pip install pytest-pacman
#!/usr/bin/python3
import os
import tarfile
from shutil import rmtree
from tempfile import mkdtemp
import pytest
def generate_desc(pkg):
'''
'''
desc = ""
for key, value in pkg.items():
# Ignore files specific keys
if key in ['backup', 'files']:
continue
if not isinstance(value, list):
value = [value]
if not value:
continue
desc += f"%{key.upper()}%\n"
for val in value:
desc += f"{val}\n\n"
return desc
def generate_files(pkg):
files = ""
if 'files' in pkg:
files += "%FILES%\n"
for entry in pkg['files']:
files += f'{entry}\n'
if 'backup' in pkg:
files += '\n'
files += "%BACKUP%\n"
for entry in pkg['backup']:
files += f'{entry}\n'
return files
def generate_syncdb(pkgs, dbfile):
dbpath = mkdtemp(dir='/tmp')
# TODO: refactor to not use chdir.
cwd = os.getcwd()
os.chdir(dbpath)
with tarfile.open(dbfile, "w") as tar:
for pkg in pkgs:
path = f"{pkg['name']}-{pkg['version']}"
os.makedirs(path)
with open(f'{path}/desc', 'w') as f:
f.write(generate_desc(pkg))
tar.add(path)
rmtree(dbpath)
os.chdir(cwd)
@pytest.fixture
def generate_localdb(tmpdir_factory):
'''Generates a localdb in provided location or when not provided pytest tmpdir
Parameters:
pkgs (list): a list of dicts containing
dbroot (string): the path to the root (normally /var/lib/pacman)
Returns:
str: path to dbroot for when dbroot is not provided
'''
def _generate_localdb(pkgs, dbroot=''):
if not dbroot:
dbroot = str(tmpdir_factory.mktemp('dbpath'))
dbloc = f"{dbroot}/local"
os.mkdir(dbloc)
for pkg in pkgs[:1]:
path = f"{dbloc}/{pkg['name']}-{pkg['version']}"
os.makedirs(path)
with open(f'{path}/desc', 'w') as f:
f.write(generate_desc(pkg))
if 'backup' in pkg or 'files' in pkg:
with open(f'{path}/files', 'w') as f:
f.write(generate_files(pkg))
return dbroot
return _generate_localdb
[flake8]
max-line-length = 120
"""Setuptools install script for pytest-pacman."""
import io
from setuptools import setup
with io.open("README.md", encoding="utf-8") as f:
long_description = f.read()
setup(
name="pytest-pacman",
description="py.test plugin to generate pacman db fixtures",
long_description=long_description,
version="0.1",
author="Jelle van der Waa",
author_email="jelle@archlinux.org",
url="http://github.com/archlinux/pytest-pacman/",
license="MIT",
packages=["pytest_pacman"],
entry_points={"pytest11": ["pacman = pytest_pacman.plugin"]},
install_requires=["pytest>=3.6.0"],
classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
"Environment :: Plugins",
"Intended Audience :: Developers",
"License :: DFSG approved",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Topic :: Software Development :: Testing",
],
)
[{
"name": "linux",
"base": "linux",
"arch": "x86_64",
"csize": "2483776",
"version": "5.5.3.arch1-1",
"builddate": "1573556456",
"desc": "The linux kernel and modules",
"url": "https://kernel.org",
"license": "GPL2",
"packager": "Arch Dev <developer@archlinux.org>",
"conflicts": [],
"replaces": [],
"depends": ["coreutils"],
"makedepends": ["bc"],
"optdepends": ["linux-firmware"]
},
{
"name": "linux-firmware",
"base": "linux-firmware",
"arch": "any",
"csize": "2483776",
"builddate": "1573556456",
"version": "20200204.b791e15-1",
"desc": "Firmware files for Linux",
"url": "https://kernel.org",
"license": "GPL2",
"packager": "Arch Dev <developer@archlinux.org>",
"conflicts": ["linux-firmware-git"],
"replaces": ["kernel26-firmware"],
"depends": [""],
"makedepends": ["git"],
"optdepends": [""]
},
{
"name": "git",
"base": "git",
"arch": "x86_64",
"csize": "2483776",
"version": "2.25.0-1",
"builddate": "1573556456",
"desc": "the fast distributed version control system",
"url": "https://git-scm.com/",
"license": "GPL2",
"packager": "Arch Git <archdev@archlinux.org>",
"conflicts": [],
"replaces": [],
"depends": [],
"makedepends": [],
"optdepends": [],
"groups": ["base-devel"]
},
{
"name": "bc",
"base": "bc",
"arch": "x86_64",
"csize": "2483776",
"version": "1.07.1-3",
"builddate": "1573556456",
"desc": "An arbitrary precision calculator languagem",
"url": "https://www.gnu.org/software/bc/",
"license": "GPL",
"packager": "Arch Git <archdev@archlinux.org>",
"conflicts": [],
"replaces": [],
"depends": [],
"makedepends": [],
"optdepends": []
},
{
"name": "linux-headers",
"base": "linux",
"arch": "x86_64",
"version": "5.5.3.arch1-1",
"builddate": "1573556456",
"desc": "Headers and scripts for building modules for the Linux kernel",
"url": "https://kernel.org",
"license": "GPL2",
"packager": "Arch Dev <developer@archlinux.org>",
"conflicts": [],
"replaces": [],
"depends": [],
"makedepends": ["bc"],
"optdepends": []
},
{
"name": "base",
"base": "base",
"arch": "any",
"version": "2-2",
"builddate": "1573556451",
"desc": "Minimal package set to define a basic Arch Linux installation",
"url": "https://archlinux.org",
"license": "GPL",
"packager": "Arch Dev <developer@archlinux.org>",
"conflicts": [],
"replaces": [],
"depends": ["linux"],
"makedepends": [],
"optdepends": []
},
{
"name": "pacman",
"base": "pacman",
"arch": "x86_64",
"version": "5.0-1",
"builddate": "1573556451",
"desc": "A library-based package manager with dependency support",
"url": "https://archlinux.org/pacman",
"license": "GPL",
"packager": "Arch Dev <developer@archlinux.org>",
"conflicts": [],
"replaces": [],
"depends": ["bash", "glibc", "libarchive"],
"makedepends": [],
"optdepends": [],
"groups": ["base-devel"],
"backup": ["etc/pacman.conf 9dd54ec11513b2838bffb04dab66c50a", "etc/makepkg.conf f613f9ecbfcafdaed423848ba7190b61"],
"files": ["etc/", "etc/pacman.conf", "var/", "var/lib/", "var/lib/pacman"]
}]
#!/usr/bin/python
import json
import os
import os.path
import pytest
pytest_plugins = "pytest_pacman.plugin"
@pytest.fixture
def core_data():
curpath = os.path.dirname(os.path.realpath(__file__))
return json.load(open(f'{curpath}/core.json'))
@pytest.fixture(scope="session")
def localdb(tmpdir_factory, core_data, generate_localdb):
dbpath = str(tmpdir_factory.mktemp('dbpath'))
return generate_localdb(core_data, dbpath)
def test_localdb(core_data, localdb):
pkg = core_data[0]
pkgpath = f"{localdb}/local/{pkg['name']}-{pkg['version']}"
assert os.path.exists(localdb)
assert os.path.exists(pkgpath)
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