jw.pkg.lib.Result: Add unit test #40
7 changed files with 176 additions and 1 deletions
4
test/unit/python/jw/Makefile
Normal file
4
test/unit/python/jw/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/dirs.mk
|
||||
4
test/unit/python/jw/pkg/Makefile
Normal file
4
test/unit/python/jw/pkg/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/dirs.mk
|
||||
4
test/unit/python/jw/pkg/lib/Makefile
Normal file
4
test/unit/python/jw/pkg/lib/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/dirs.mk
|
||||
8
test/unit/python/jw/pkg/lib/Result/Makefile
Normal file
8
test/unit/python/jw/pkg/lib/Result/Makefile
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
TOPDIR = ../../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/py-run.mk
|
||||
|
||||
all:
|
||||
|
||||
test: run
|
||||
159
test/unit/python/jw/pkg/lib/Result/test.py
Normal file
159
test/unit/python/jw/pkg/lib/Result/test.py
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
from jw.pkg.lib.Result import Result
|
||||
|
||||
# -- Basic construction --
|
||||
|
||||
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)
|
||||
assert r.stdout == b'test'
|
||||
|
||||
# stdout_or_none returns raw value
|
||||
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)
|
||||
assert r.stdout_str_or_none == 'test'
|
||||
|
||||
# stdout_str returns decoded string
|
||||
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)
|
||||
assert r.stdout == b''
|
||||
|
||||
# stdout raises when stdout is None and status != 0
|
||||
r = Result(stdout=None, status=1)
|
||||
try:
|
||||
_ = r.stdout
|
||||
assert False, 'Should have raised'
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# -- stderr property --
|
||||
|
||||
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)
|
||||
assert r.stderr_or_none == b'err'
|
||||
|
||||
# stderr_str_or_none returns decoded string
|
||||
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)
|
||||
assert r.stderr_str == 'err'
|
||||
|
||||
# stderr returns b'' when stderr is None and status != 0 (error case)
|
||||
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)
|
||||
try:
|
||||
_ = r.stderr
|
||||
assert False, 'Should have raised'
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# -- None outputs --
|
||||
|
||||
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
|
||||
assert r.stderr_str_or_none is None
|
||||
|
||||
# -- encoding --
|
||||
|
||||
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.encoding = 'ASCII'
|
||||
assert r.encoding == 'ASCII'
|
||||
|
||||
# -- strip_output --
|
||||
|
||||
r = Result(stdout=b' spaces ', status=0)
|
||||
assert r.stdout_str_or_none == ' spaces '
|
||||
|
||||
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)
|
||||
assert r.stdout_str_or_none == 'lines'
|
||||
|
||||
# -- cmd and wd setters --
|
||||
|
||||
r = Result(cmd=['echo', 'hello'], wd='/tmp')
|
||||
assert r.cmd == ['echo', 'hello']
|
||||
assert r.wd == '/tmp'
|
||||
|
||||
r.cmd = ['ls', '-la']
|
||||
r.wd = '/home'
|
||||
assert r.cmd == ['ls', '-la']
|
||||
assert r.wd == '/home'
|
||||
|
||||
# -- summary --
|
||||
|
||||
r = Result(stdout=b'output', status=0)
|
||||
assert 'status 0' in r.summary
|
||||
|
||||
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)
|
||||
assert r.matches_error('fatal error')
|
||||
|
||||
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)
|
||||
assert not r.matches_error('.*')
|
||||
|
||||
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')
|
||||
assert 'echo' in r.summary or 'status 0' in r.summary
|
||||
|
||||
# -- __repr__ --
|
||||
|
||||
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')
|
||||
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)
|
||||
assert r.stdout_str_or_none == ' x '
|
||||
r.strip_output = True
|
||||
# strip_output is a property setter, the value is stored
|
||||
# But __decode is called at property access time using current strip_output
|
||||
assert r.stdout_str_or_none == 'x'
|
||||
|
||||
print('All Result tests passed')
|
||||
7
test/unit/python/jw/pkg/lib/Uri/Makefile
Normal file
7
test/unit/python/jw/pkg/lib/Uri/Makefile
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
TOPDIR = ../../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/py-run.mk
|
||||
|
||||
all:
|
||||
test: run
|
||||
82
test/unit/python/jw/pkg/lib/Uri/test.py
Normal file
82
test/unit/python/jw/pkg/lib/Uri/test.py
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
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'
|
||||
|
||||
# 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)
|
||||
|
||||
print('All Uri tests passed')
|
||||
Loading…
Reference in a new issue