add initial testing aparatus

- Add evaluater fixture to evaluate macros against the lua and macro
  defintions in the local directory.
- Add unit tests for the most basic functionality.
This commit is contained in:
Maxwell G 2023-03-27 14:06:01 -05:00
commit b7e60c0aab
Signed by: gotmax23
GPG key ID: F79E4E25E8C661F8
3 changed files with 171 additions and 0 deletions

3
tests/__init__.py Normal file
View file

@ -0,0 +1,3 @@
# Copyright (C) 2023 Maxwell G <maxwell@gtmx.me>
#
# SPDX-License-Identifier: GPL-1.0-or-later

76
tests/conftest.py Normal file
View file

@ -0,0 +1,76 @@
# Copyright (C) 2023 Maxwell G <maxwell@gtmx.me>
#
# SPDX-License-Identifier: GPL-1.0-or-later
from __future__ import annotations
import os
import subprocess
from collections.abc import Callable, Sequence
from pathlib import Path
import pytest
PARENT = Path(__file__).resolve().parent.parent
# e.g. MACRO_DIR=%{buildroot}%{_rpmmacrodir} \
# MACRO_LUA_DIR=%{buildroot}%{_rpmluadir} \
# pytest
# MACRO_DIR="" MACRO_LUA_DIR="" to only use system paths
MACRO_DIR = Path(os.environ.get("MACRO_DIR", PARENT / "rpm/macros.d"))
MACRO_LUA_DIR = Path(os.environ.get("MACRO_LUA_PATH", PARENT / "rpm/lua"))
def macros_path() -> list[str]:
if MACRO_DIR == "":
return []
path = subprocess.run(
# Don't judge. It works.
"rpm --showrc | grep 'Macro path' | awk -F ': ' '{print $2}'",
shell=True,
text=True,
check=True,
capture_output=True,
).stdout.strip()
return ["--macros", f"{path}:{MACRO_DIR}/macros.*"]
def lua_path() -> list[str]:
if MACRO_LUA_DIR == "":
return []
path = subprocess.run(
["rpm", "-E", "%{lua: print(package.path)}"],
text=True,
check=True,
capture_output=True,
).stdout.strip()
path = f"{MACRO_LUA_DIR}/?.lua;{path}"
return ["-E", "%{lua: package.path = " + repr(path) + "}"]
@pytest.fixture
def evaluater() -> Callable[..., tuple[str, str]]:
def runner(
exps: str | Sequence[str],
defines: dict[str, str] | None = None,
undefines: Sequence[str] = (),
should_fail: bool = False,
) -> tuple[str, str]:
cmd: list[str] = ["rpm", *macros_path(), *lua_path()]
defines = defines or {}
for name, value in defines.items():
cmd.extend(("--define", f"{name} {value}"))
for name in undefines:
cmd.extend(("-E", f"%undefine {name}"))
if isinstance(exps, str):
cmd.extend(("-E", exps))
else:
for exp in exps:
cmd.extend(("-E", exp))
proc = subprocess.run(cmd, text=True, capture_output=True)
if should_fail:
assert proc.returncode != 0
else:
assert proc.returncode == 0, proc.stderr
return proc.stdout.strip(), proc.stderr.strip()
return runner

92
tests/test_forge.py Normal file
View file

@ -0,0 +1,92 @@
# Copyright (C) 2023 Maxwell G <maxwell@gtmx.me>
#
# SPDX-License-Identifier: GPL-1.0-or-later
import pytest
def test_github_forgesource_simple(evaluater):
"""
Ensure that a simple Github repository works as expected
"""
forgeurl = "https://github.com/ansible/ansible"
defines = {"forgeurl": forgeurl, "version": "2.14.0"}
out = evaluater(["%forgemeta", "%forgesource"], defines)
expected = f"{forgeurl}/archive/v2.14.0/ansible-2.14.0.tar.gz"
assert out[0] == expected
@pytest.mark.parametrize(
"archiveext", [pytest.param(None), pytest.param("tar.bz2"), pytest.param("tar.gz")]
)
def test_gitlab_forgesource_archiveext(archiveext, evaluater):
"""
Ensure that a simple Gitlab repository works as expected.
Test custom %{archiveext} and ensure that tar.bz2 is the default.
"""
forgeurl = "https://gitlab.com/fdroid/fdroidclient"
defines = {"version": "1.16.2", "forgeurl": forgeurl}
if archiveext:
defines["archiveext"] = archiveext
else:
archiveext = "tar.bz2"
out = evaluater(["%forgemeta", "%forgesource"], defines)
sourceurl = f"{forgeurl}/-/archive/1.16.2/fdroidclient-1.16.2.{archiveext}"
assert out[0] == sourceurl
def test_sourcehut_v(evaluater):
"""
Ensure that a v-prefixed sourcehut repository works as expected.
Check that zero indexing works and ensure that distprefix isn't set.
"""
forgeurl = "https://git.sr.ht/~gotmax23/fedrq"
defines = {
"forgeurl": forgeurl,
"version": "0.5.0",
"tag": "v%{version}",
}
out = evaluater(
[
"%forgemeta",
# Check that zero indexing works properly
"%{forgesource} :: %{forgesource0} :: "
"%{!?distprefix:nil} :: %{!?distprefix0:nil} :: "
"%{forgesetupargs} :: %{forgesetupargs0}",
],
defines,
)
sourceurl = f"{forgeurl}/archive/v0.5.0.tar.gz#/fedrq-0.5.0.tar.gz"
expected = (
f"{sourceurl} :: {sourceurl} :: "
"nil :: nil :: "
"-n fedrq-v0.5.0 :: -n fedrq-v0.5.0"
)
assert out[0] == expected
def test_github_commit(evaluater):
forgeurl = "https://github.com/ansible/ansible"
commit = "520bb66f8ab6d53750f48f024287572962d975fa"
defines = {"forgeurl": forgeurl, "version": "2.14.0", "commit": commit}
out = evaluater(
[
"%forgemeta",
# Check that zero indexing works properly
"%{forgesource} :: %{forgesource0} :: "
"%{distprefix} :: %{distprefix0} :: "
"%{forgesetupargs} :: %{forgesetupargs0}",
],
defines,
)
sourceurl = f"{forgeurl}/archive/{commit}/ansible-{commit}.tar.gz"
expected = (
f"{sourceurl} :: {sourceurl} :: "
".git520bb66 :: .git520bb66 :: "
f"-n ansible-{commit} :: -n ansible-{commit}"
)
assert out[0] == expected