test + lib: Fix miscellanous bugs and inconsistencies #46

Merged
Jan Lindemann merged 8 commits from jan/feature/20260704-test-fix-test-py-duplicate-module-name-error into master 2026-07-04 07:46:09 +02:00 AGit
9 changed files with 49 additions and 49 deletions

View file

@ -112,7 +112,7 @@ class CmdBuild(Cmd): # export
return ret
def add_dep_tree(cur, prereq_types, tree, all_deps):
log(DEBUG, 'Adding deps "{" ".join(prereq_types)}" of module {cur)')
log(DEBUG, f'Adding deps "{" ".join(prereq_types)}" of module {cur}')
if cur in all_deps:
log(DEBUG, 'Already handled module "{cur}"')
return 0

View file

@ -112,18 +112,6 @@ class DistroContext(FilesContext):
ec = Local()
return (await get(src_uri, content_filter = ProcFilterGpg(ec = ec))).stdout
def _matches_host_prefix(path: str) -> bool:
return re.match(r'^' + host_root_in_tar, path) is not None
def _crop_host_prefix(path: str) -> bool:
return re.sub(r'^' + host_root_in_tar, '', path) is not None
def _crop_default_prefix(path: str) -> bool:
return re.sub(default_rx, '', path) is not None
def _matches_default_prefix(path: str) -> bool:
return re.match(r'^default', path) is not None
def _is_needed_secret(path: str) -> bool:
return path in secret_paths

View file

@ -66,7 +66,7 @@ class CopyContext:
return self.__dst
async def _run(self) -> None:
await self._run()
raise NotImplementedError('CopyContext._run() must be overridden')
async def run(self) -> None:
await self._run()

View file

@ -29,10 +29,10 @@ class Distro(abc.ABC):
default_pkg_filter: PackageFilter | None = None,
) -> None:
if id is None:
raise ValueError('Tried to instaniate Distro without id')
raise ValueError('Tried to instantiate Distro without id')
if ec is None:
raise ValueError(
f'Tried to instaniate Distro "{id}" without execution context'
f'Tried to instantiate Distro "{id}" without execution context'
)
self.__exec_context = ec
self.__id: str | None = None

View file

@ -63,7 +63,7 @@ class Uri:
def scheme(self) -> str:
ret = self.__p.scheme
if not ret:
return 'file://'
return 'file'
return ret
@cached_property

0
test/__init__.py Normal file
View file

View file

@ -1,14 +1,25 @@
import os
import tempfile
from jw.pkg.lib.ProjectConf import ProjectConf
# -- Helper: create temp file and read it --
_tmpfiles: list[str] = []
def _load(text: str) -> ProjectConf:
with tempfile.NamedTemporaryFile(mode='w', suffix='.conf', delete=False) as f:
with tempfile.NamedTemporaryFile(mode = 'w', suffix = '.conf', delete = False) as f:
f.write(text)
f.flush()
_tmpfiles.append(f.name)
return ProjectConf.read(f.name)
def _cleanup():
for _p in _tmpfiles:
os.unlink(_p)
del _p
_tmpfiles.clear()
# -- Basic construction --
pc = _load('[section]\nkey = value\n')
@ -250,4 +261,5 @@ assert pc.get_str('section', 'not_a_key') == '[bracket]'
pc = _load('[section]\nlist = "a", "b", "c"\n')
assert pc.get_list('section', 'list') == ['a', 'b', 'c']
_cleanup()
print('All ProjectConf tests passed')

View file

@ -2,34 +2,34 @@ from jw.pkg.lib.Result import Result
# -- Basic construction --
r = Result(stdout=b'hello', stderr=b'world', status=0)
r = Result(stdout = b'hello', stderr = b'world', status = 0)
assert r.status == 0
assert r.stdout == b'hello'
assert r.stderr == b'world'
# -- stdout property --
r = Result(stdout=b'test', status=0)
r = Result(stdout = b'test', status = 0)
assert r.stdout == b'test'
# stdout_or_none returns raw value
r = Result(stdout=b'test', status=0)
r = Result(stdout = b'test', status = 0)
assert r.stdout_or_none == b'test'
# stdout_str_or_none returns decoded string
r = Result(stdout=b'test', status=0)
r = Result(stdout = b'test', status = 0)
assert r.stdout_str_or_none == 'test'
# stdout_str returns decoded string
r = Result(stdout=b'test', status=0)
r = Result(stdout = b'test', status = 0)
assert r.stdout_str == 'test'
# stdout returns b'' when stdout is None and status == 0
r = Result(stdout=None, status=0)
r = Result(stdout = None, status = 0)
assert r.stdout == b''
# stdout raises when stdout is None and status != 0
r = Result(stdout=None, status=1)
r = Result(stdout = None, status = 1)
try:
_ = r.stdout
assert False, 'Should have raised'
@ -38,27 +38,27 @@ except Exception:
# -- stderr property --
r = Result(stderr=b'error', status=1)
r = Result(stderr = b'error', status = 1)
assert r.stderr == b'error'
# stderr_or_none returns raw value
r = Result(stderr=b'err', status=1)
r = Result(stderr = b'err', status = 1)
assert r.stderr_or_none == b'err'
# stderr_str_or_none returns decoded string
r = Result(stderr=b'err', status=1)
r = Result(stderr = b'err', status = 1)
assert r.stderr_str_or_none == 'err'
# stderr_str returns decoded string
r = Result(stderr=b'err', status=1)
r = Result(stderr = b'err', status = 1)
assert r.stderr_str == 'err'
# stderr returns b'' when stderr is None and status != 0 (error case)
r = Result(stderr=None, status=1)
r = Result(stderr = None, status = 1)
assert r.stderr == b''
# stderr raises when stderr is None and status == 0 (success case)
r = Result(stderr=None, status=0)
r = Result(stderr = None, status = 0)
try:
_ = r.stderr
assert False, 'Should have raised'
@ -67,7 +67,7 @@ except Exception:
# -- None outputs --
r = Result(stdout=None, stderr=None, status=1)
r = Result(stdout = None, stderr = None, status = 1)
assert r.stdout_or_none is None
assert r.stderr_or_none is None
assert r.stdout_str_or_none is None
@ -75,28 +75,28 @@ assert r.stderr_str_or_none is None
# -- encoding --
r = Result(stdout=b'caf\xc3\xa9', encoding='UTF-8')
r = Result(stdout = b'caf\xc3\xa9', encoding = 'UTF-8')
assert r.encoding == 'UTF-8'
assert r.stdout_str == 'café'
r = Result(stdout=b'test', encoding='UTF-8')
r = Result(stdout = b'test', encoding = 'UTF-8')
r.encoding = 'ASCII'
assert r.encoding == 'ASCII'
# -- strip_output --
r = Result(stdout=b' spaces ', status=0)
r = Result(stdout = b' spaces ', status = 0)
assert r.stdout_str_or_none == ' spaces '
r = Result(stdout=b' spaces ', status=0, strip_output=True)
r = Result(stdout = b' spaces ', status = 0, strip_output = True)
assert r.stdout_str_or_none == 'spaces'
r = Result(stdout=b' lines \n', status=0, strip_output=True)
r = Result(stdout = b' lines \n', status = 0, strip_output = True)
assert r.stdout_str_or_none == 'lines'
# -- cmd and wd setters --
r = Result(cmd=['echo', 'hello'], wd='/tmp')
r = Result(cmd = ['echo', 'hello'], wd = '/tmp')
assert r.cmd == ['echo', 'hello']
assert r.wd == '/tmp'
@ -107,49 +107,49 @@ assert r.wd == '/home'
# -- summary --
r = Result(stdout=b'output', status=0)
r = Result(stdout = b'output', status = 0)
assert 'status 0' in r.summary
r = Result(stderr=b'error', status=1)
r = Result(stderr = b'error', status = 1)
assert 'status 1' in r.summary
assert 'stderr' in r.summary
# -- matches_error --
r = Result(stderr=b'fatal error: missing file', status=128)
r = Result(stderr = b'fatal error: missing file', status = 128)
assert r.matches_error('fatal error')
r = Result(stderr=b'fatal error: missing file', status=128)
r = Result(stderr = b'fatal error: missing file', status = 128)
assert not r.matches_error('success')
r = Result(stdout=b'ok', stderr=b'', status=0)
r = Result(stdout = b'ok', stderr = b'', status = 0)
assert not r.matches_error('.*')
r = Result(stdout=b'ok', stderr=None, status=0)
r = Result(stdout = b'ok', stderr = None, status = 0)
assert not r.matches_error('.*')
# -- summarize with custom cmd/wd --
r = Result(stdout=b'out', status=0, cmd=['echo', 'test'], wd='/tmp')
r = Result(stdout = b'out', status = 0, cmd = ['echo', 'test'], wd = '/tmp')
assert 'echo' in r.summary or 'status 0' in r.summary
# -- __repr__ --
r = Result(stdout=b'repr test', status=0)
r = Result(stdout = b'repr test', status = 0)
s = repr(r)
assert '0' in s # verbose=False uses numeric status
assert 'repr test' in s
# -- encoding setter --
r = Result(stdout=b'test', status=0, encoding='UTF-8')
r = Result(stdout = b'test', status = 0, encoding = 'UTF-8')
assert r.encoding == 'UTF-8'
r.encoding = 'ASCII'
assert r.encoding == 'ASCII'
# -- strip_output setter --
r = Result(stdout=b' x ', status=0, strip_output=False)
r = Result(stdout = b' x ', status = 0, strip_output = False)
assert r.stdout_str_or_none == ' x '
r.strip_output = True
# strip_output is a property setter, the value is stored

View file

@ -19,7 +19,7 @@ assert '<hidden>' in u.safe_full_with_username # safe version hides password
# Scheme defaults to file
u = Uri('/local/path')
assert u.scheme == 'file://'
assert u.scheme == 'file'
assert u.protocol == 'file'
# Pimp returns existing Uri unchanged