jw-pkg/test/unit/python/jw/pkg/lib/Uri/test.py
Jan Lindemann 0373ae3134
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 5m17s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 5m27s
CI / Packaging test (pull_request) Successful in 0s
lib.ExecApp: Rename --pkg-filter to --path-filter
The exec context's default filter selects paths, not just packages, so the
pkg spelling of the option and environment variable that set it no longer
fit. Rename both and some variables accordingly:

- ExecApp: --pkg-filter becomes --path-filter, and the fallback environment
  variable JW_DEFAULT_PKG_FILTER becomes JW_DEFAULT_PATH_FILTER
- Distro: the default_pkg_filter constructor argument and property become
  default_path_filter, matching FileContext, ExecContext, and CopyContext;
  App and KvFile pass the renamed argument
- Adapt the help test's expected output and the unit tests

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
2026-11-07 19:37:27 +01:00

119 lines
3.4 KiB
Python

from jw.pkg.lib.Uri import Uri
# Basic URL parsing
u = Uri('ssh://john.doe@host:22/path/to/repo')
assert u.protocol == 'ssh'
assert u.hostname == 'host'
assert u.port == 22
assert u.path == '/path/to/repo'
# Username and password
u = Uri('https://user:pass@example.com/repo')
assert u.username == 'user'
assert u.password == 'pass'
# Credentials in output
u = Uri('https://user:secret@example.com/repo')
assert 'secret' in u.full # full shows password
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.protocol == 'file'
assert u.full == 'file:///local/path'
# Relative paths default to file, too
u = Uri('../../local/path')
assert u.scheme == 'file'
assert u.protocol == 'file'
assert u.path == '../../local/path'
# full keeps relative paths relative: '://' would re-parse the
# first segment as a host
assert u.full == 'file:../../local/path'
assert Uri(u.full).path == '../../local/path'
# An explicit file: scheme with a relative path round-trips, too
u = Uri('file:../../local/path')
assert u.path == '../../local/path'
assert u.full == 'file:../../local/path'
# Pimp returns existing Uri unchanged
u1 = Uri('ssh://host/path')
u2 = Uri.pimp(u1)
assert u2 is u1
# New with path (add)
u = Uri('ssh://host/path')
u2 = u.new_add_path('subdir')
assert u2.to_string == 'ssh://host/path/subdir'
# New with path (replace) - to_string is updated
u3 = u.new_replace_path('/other')
assert u3.to_string == 'ssh://host/other'
# Authority includes credentials
u = Uri('ssh://user:pass@host:22/path')
assert u.authority == 'userpass@host:22'
# Origin excludes credentials
assert u.origin == 'host:22'
# ID and safe version hide password
assert u.id == 'ssh://user:<hidden>@host:22'
assert '<hidden>' in u.safe_full_with_username
# Full includes path and credentials
assert u.full == 'ssh://userpass@host:22/path'
# Simple URLs
u = Uri('https://example.com/repo.git')
assert u.hostname == 'example.com'
assert u.path == '/repo.git'
assert u.basename == 'repo.git'
# Username setter
u = Uri('ssh://host/path')
u.set_username('newuser')
assert u.username == 'newuser'
# Password setter
u = Uri('ssh://host/path')
u.set_password('newpass')
assert u.password == 'newpass'
# No port
u = Uri('ssh://host/path')
assert u.port is None
assert u.port_str is None
# to_string returns original
u = Uri('ssh://user:pass@host:22/path')
assert u.to_string == 'ssh://user:pass@host:22/path'
# String representation hides credentials
u = Uri('https://user:secret@example.com/repo.git')
assert '<hidden>' in str(u)
# The query is identity, not path: it is exposed, it survives a
# path change, and it shows in the full form
u = Uri('kvfile://host/?path-filter=url=~jw.dev')
assert u.query == 'path-filter=url=~jw.dev'
assert u.path == '/'
assert u.new_replace_path('etc/foo.conf').to_string == \
'kvfile://host/etc/foo.conf?path-filter=url=~jw.dev'
u = Uri('kvfile:///?path-filter=url=~jw.dev')
assert u.new_replace_path('etc').to_string == \
'kvfile:///etc?path-filter=url=~jw.dev'
assert 'path-filter=url=~jw.dev' in u.full
assert Uri('ssh://host/path').query == ''
# query_dict is None without a query and a key/value dict with one
assert Uri('ssh://host/path').query_dict is None
u = Uri('kvfile://host/?path-filter=url=~jw.dev')
assert u.query_dict == {'path-filter': 'url=~jw.dev'}
u = Uri('https://example.com/?a=1&b=two')
assert u.query_dict == {'a': '1', 'b': 'two'}
print('All Uri tests passed')