50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
from pathlib import Path
|
|
from subprocess import check_call
|
|
import gzip
|
|
import lzma
|
|
import io
|
|
import zipfile
|
|
|
|
from my.kython.kompress import kopen, kexists
|
|
|
|
|
|
import pytest # type: ignore
|
|
|
|
@pytest.fixture
|
|
def prepare(tmp_path: Path):
|
|
(tmp_path / 'file').write_text('just plaintext')
|
|
with (tmp_path / 'file.xz').open('wb') as f:
|
|
with lzma.open(f, 'w') as lzf:
|
|
lzf.write(b'compressed text')
|
|
with zipfile.ZipFile(tmp_path / 'file.zip', 'w') as zf:
|
|
zf.writestr('path/in/archive', 'data in zip')
|
|
try:
|
|
yield None
|
|
finally:
|
|
pass
|
|
|
|
|
|
def test_kopen(prepare, tmp_path: Path) -> None:
|
|
"Plaintext handled transparently"
|
|
assert kopen(tmp_path / 'file' ).read() == 'just plaintext'
|
|
assert kopen(tmp_path / 'file.xz').read() == 'compressed text'
|
|
|
|
"For zips behaviour is a bit different (not sure about all this, tbh...)"
|
|
assert kopen(tmp_path / 'file.zip', 'path/in/archive').read() == 'data in zip'
|
|
|
|
|
|
def test_kexists(prepare, tmp_path: Path) -> None:
|
|
assert kexists(str(tmp_path / 'file.zip'), 'path/in/archive')
|
|
assert not kexists(str(tmp_path / 'file.zip'), 'path/notin/archive')
|
|
|
|
# TODO not sure about this?
|
|
assert not kexists(tmp_path / 'nosuchzip.zip', 'path/in/archive')
|
|
|
|
|
|
def test_cpath():
|
|
# TODO
|
|
raise RuntimeError
|
|
|
|
|
|
# TODO FIXME these tests should def run on CI
|
|
# TODO get rid of all decode utf8?
|