Files
cthulhu/tests/test_package_binaries.py
2026-11-07 13:14:22 -05:00

75 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Check package contents for sources before and after Remote input support."""
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
MOCK_GO = '''#!/usr/bin/env python3
import os
from pathlib import Path
import sys
args = sys.argv[1:]
assert args[0] == "build"
assert Path(args[-1]).is_dir(), "build target is absent from this source version"
if args[-1] == os.environ.get("FAIL_BUILD_TARGET"):
sys.exit(2)
target = Path(args[args.index("-o") + 1])
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text("#!/bin/sh\\nexit 0\\n")
target.chmod(0o755)
'''
class PackageBinaryTests(unittest.TestCase):
def test_build_and_install_source_supported_helpers(self):
root = Path(__file__).resolve().parents[1]
recipes = sorted((root / "distro-packages").rglob("PKGBUILD"))
self.assertTrue(recipes)
for recipe in recipes:
for has_input, fail_input in ((False, False), (True, False), (True, True)):
with self.subTest(recipe=recipe.parent.name, has_input=has_input,
fail_input=fail_input), tempfile.TemporaryDirectory() as tmp:
work = Path(tmp)
source = work / "cthulhu"
binaries = ["cthulhu", "cthulhu-braille"]
if has_input:
binaries.append("cthulhu-input-helper")
for binary in binaries:
(source / "cmd" / binary).mkdir(parents=True)
for name in ("LICENSE", "AUTHORS"):
(source / name).write_text("fixture\n")
# A stale artifact must not enter an older source's package.
(source / "build").mkdir()
(source / "build" / "cthulhu-input-helper").write_text("stale")
tools = work / "tools"
tools.mkdir()
go = tools / "go"
go.write_text(MOCK_GO)
go.chmod(0o755)
stage = work / "package"
environment = dict(os.environ, PATH=f"{tools}:{os.environ['PATH']}",
srcdir=str(work), pkgdir=str(stage))
if fail_input:
environment["FAIL_BUILD_TARGET"] = "./cmd/cthulhu-input-helper"
result = subprocess.run(
["bash", "-ec", 'source "$1"\nbuild\ncd "$srcdir"\npackage',
"package-test", str(recipe)], cwd=work, env=environment,
capture_output=True, text=True, check=False)
if fail_input:
self.assertNotEqual(result.returncode, 0)
self.assertFalse(stage.exists())
continue
self.assertEqual(result.returncode, 0, result.stderr)
installed = stage / "usr/bin"
self.assertEqual(sorted(p.name for p in installed.iterdir()), sorted(binaries))
for binary in binaries:
self.assertTrue(os.access(installed / binary, os.X_OK))
if __name__ == "__main__":
unittest.main()