From 94f7838c0b77b4e8ee00a3c49cab41404605fd75 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Tue, 8 Sep 2026 22:33:49 +0200 Subject: [PATCH] lib.Package: Add unit tests parse_spec_str(), parse_specs_str(), order_tags(), and __repr__ are pure text processing without test coverage, shared by the dpkg and rpm package manager backends. Add unit tests for valid and invalid spec strings, multi-line input with and without a trailing newline, tag ordering with default values, and the repr layout. Signed-off-by: Jan Lindemann Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.85.1 --- test/unit/python/jw/pkg/lib/Package/Makefile | 7 ++ test/unit/python/jw/pkg/lib/Package/test.py | 80 ++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 test/unit/python/jw/pkg/lib/Package/Makefile create mode 100644 test/unit/python/jw/pkg/lib/Package/test.py diff --git a/test/unit/python/jw/pkg/lib/Package/Makefile b/test/unit/python/jw/pkg/lib/Package/Makefile new file mode 100644 index 00000000..de693650 --- /dev/null +++ b/test/unit/python/jw/pkg/lib/Package/Makefile @@ -0,0 +1,7 @@ +TOPDIR = ../../../../../../.. + +include $(TOPDIR)/make/proj.mk +include $(JWBDIR)/make/py-run.mk + +all: +test: run diff --git a/test/unit/python/jw/pkg/lib/Package/test.py b/test/unit/python/jw/pkg/lib/Package/test.py new file mode 100644 index 00000000..09269a91 --- /dev/null +++ b/test/unit/python/jw/pkg/lib/Package/test.py @@ -0,0 +1,80 @@ +from jw.pkg.lib.Package import Package, meta_tags + +# -- parse_spec_str -- + +p = Package.parse_spec_str( + 'jw-core|jw|Jan|https://example.com/jw-core|Jan ' +) +assert p.name == 'jw-core' +assert p.vendor == 'jw' +assert p.packager == 'Jan' +assert p.url == 'https://example.com/jw-core' +assert p.maintainer == 'Jan ' + +# An empty field is preserved as an empty string +p = Package.parse_spec_str('name||||') +assert p.name == 'name' +assert p.vendor == '' +assert p.packager == '' +assert p.url == '' +assert p.maintainer == '' + +# A custom delimiter works +p = Package.parse_spec_str('a,b,c,d,e', delimiter = ',') +assert p.name == 'a' +assert p.maintainer == 'e' + +# Wrong field counts raise +for spec in ('a', 'a|b', 'a|b|c|d', 'a|b|c|d|e|f'): + try: + Package.parse_spec_str(spec) + assert False, f'Should have raised for "{spec}"' + except ValueError: + pass + +# -- parse_specs_str -- + +specs = ( + 'jw-core|jw|Jan|https://example.com/jw-core|Jan\n' + 'jw-base|jw|Jan|https://example.com/jw-base|Jan' +) +packages = Package.parse_specs_str(specs) +assert [p.name for p in packages] == ['jw-core', 'jw-base'] +# A trailing newline does not create a phantom package +assert len(packages) == 2 +# A trailing newline is not needed +packages = Package.parse_specs_str(specs + '\n') +assert [p.name for p in packages] == ['jw-core', 'jw-base'] + +# An empty string parses to no packages +assert Package.parse_specs_str('') == [] + +# -- order_tags -- + +mapping = { + 'maintainer': 'Jan', + 'name': 'jw-core', + 'url': 'https://example.com/jw-core', +} +ordered = Package.order_tags(mapping) +assert list(ordered) == meta_tags +assert ordered == { + 'name': 'jw-core', + 'vendor': '', + 'packager': '', + 'url': 'https://example.com/jw-core', + 'maintainer': 'Jan', +} + +# -- __repr__ -- + +p = Package.parse_spec_str('jw-core|jw|Jan|url|Jan') +assert repr(p) == ( + 'name : jw-core\n' + 'vendor : jw\n' + 'packager : Jan\n' + 'url : url\n' + 'maintainer : Jan' +) + +print('All Package tests passed')