From 3f7f4e945b4b4db65835a845a102ae635f244036 Mon Sep 17 00:00:00 2001 From: scivision Date: Thu, 9 Jan 2025 15:42:33 -0500 Subject: [PATCH] test: use Traversable for brevity and robustness test --- src/pylivestream/tests/test_class.py | 8 +-- src/pylivestream/tests/test_filein.py | 88 ++++++++++------------- src/pylivestream/tests/test_microphone.py | 32 ++++----- src/pylivestream/utils.py | 15 ++-- 4 files changed, 63 insertions(+), 80 deletions(-) diff --git a/src/pylivestream/tests/test_class.py b/src/pylivestream/tests/test_class.py index 1a856d4..56d7fd8 100755 --- a/src/pylivestream/tests/test_class.py +++ b/src/pylivestream/tests/test_class.py @@ -19,11 +19,9 @@ def test_exe(rex): def test_attrs(inp): assert not pls.utils.get_resolution(inp) - with importlib.resources.as_file( - importlib.resources.files("pylivestream.data").joinpath("bunny.avi") - ) as fn: - assert pls.utils.get_resolution(fn) == [426, 240] - assert pls.utils.get_framerate(fn) == approx(24.0) + vid = importlib.resources.files("pylivestream.data").joinpath("bunny.avi") + assert pls.utils.get_resolution(vid) == [426, 240] + assert pls.utils.get_framerate(vid) == approx(24.0) def test_config_not_found(tmp_path): diff --git a/src/pylivestream/tests/test_filein.py b/src/pylivestream/tests/test_filein.py index 868acf0..381159e 100644 --- a/src/pylivestream/tests/test_filein.py +++ b/src/pylivestream/tests/test_filein.py @@ -16,71 +16,57 @@ def test_props(): - with importlib.resources.as_file( - importlib.resources.files("pylivestream.data").joinpath("bunny.avi") - ) as fn: - S = pls.FileIn(ini, websites=sites, infn=fn) - for s in S.streams: - assert "-re" in S.streams[s].cmd - assert S.streams[s].fps == approx(24.0) + vid = importlib.resources.files("pylivestream.data").joinpath("bunny.avi") + S = pls.FileIn(ini, websites=sites, infn=vid) + for s in S.streams: + assert "-re" in S.streams[s].cmd + assert S.streams[s].fps == approx(24.0) - if int(S.streams[s].res[1]) == 480: - assert S.streams[s].video_kbps == 500 - elif int(S.streams[s].res[1]) == 720: - assert S.streams[s].video_kbps == 1800 + if int(S.streams[s].res[1]) == 480: + assert S.streams[s].video_kbps == 500 + elif int(S.streams[s].res[1]) == 720: + assert S.streams[s].video_kbps == 1800 def test_audio(): - with ( - importlib.resources.as_file( - importlib.resources.files("pylivestream.data").joinpath("logo.png") - ) as logo, - importlib.resources.as_file( - importlib.resources.files("pylivestream.data").joinpath("orch_short.ogg") - ) as fn, - ): - S = pls.FileIn(ini, websites=sites, infn=fn, image=logo) - for s in S.streams: - assert "-re" in S.streams[s].cmd - assert S.streams[s].fps is None + logo = importlib.resources.files("pylivestream.data").joinpath("logo.png") + snd = importlib.resources.files("pylivestream.data").joinpath("orch_short.ogg") - assert S.streams[s].video_kbps == 800 + S = pls.FileIn(ini, websites=sites, infn=snd, image=logo) + for s in S.streams: + assert "-re" in S.streams[s].cmd + assert S.streams[s].fps is None + + assert S.streams[s].video_kbps == 800 @pytest.mark.timeout(TIMEOUT) @pytest.mark.skipif(CI, reason="CI has no audio hardware typically") def test_simple(): """stream to localhost""" - with ( - importlib.resources.as_file( - importlib.resources.files("pylivestream.data").joinpath("logo.png") - ) as logo, - importlib.resources.as_file( - importlib.resources.files("pylivestream.data").joinpath("orch_short.ogg") - ) as fn, - ): - S = pls.FileIn(ini, websites="localhost", infn=fn, image=logo, yes=True, timeout=5) + logo = importlib.resources.files("pylivestream.data").joinpath("logo.png") + aud = importlib.resources.files("pylivestream.data").joinpath("orch_short.ogg") + + S = pls.FileIn(ini, websites="localhost", infn=aud, image=logo, yes=True, timeout=5) - S.golive() + S.golive() @pytest.mark.skipif(CI, reason="CI has no audio hardware typically") def test_script(): - with importlib.resources.as_file( - importlib.resources.files("pylivestream.data").joinpath("bunny.avi") - ) as fn: - subprocess.check_call( - [ - sys.executable, - "-m", - "pylivestream.fglob", - str(fn), - "localhost", - str(ini), - "--yes", - "--timeout", - "5", - ], - timeout=TIMEOUT, - ) + vid = importlib.resources.files("pylivestream.data").joinpath("bunny.avi") + subprocess.check_call( + [ + sys.executable, + "-m", + "pylivestream.fglob", + str(vid), + "localhost", + str(ini), + "--yes", + "--timeout", + "5", + ], + timeout=TIMEOUT, + ) diff --git a/src/pylivestream/tests/test_microphone.py b/src/pylivestream/tests/test_microphone.py index 6188257..cd44294 100644 --- a/src/pylivestream/tests/test_microphone.py +++ b/src/pylivestream/tests/test_microphone.py @@ -21,32 +21,28 @@ def test_microphone_props(): - with importlib.resources.as_file( - importlib.resources.files("pylivestream.data").joinpath("logo.png") - ) as logo: - S = pls.Microphone(ini, websites=sites, image=logo) + logo = importlib.resources.files("pylivestream.data").joinpath("logo.png") + S = pls.Microphone(ini, websites=sites, image=logo) - for s in S.streams: - assert "-re" not in S.streams[s].cmd - assert S.streams[s].fps is None - assert S.streams[s].res == [720, 540] + for s in S.streams: + assert "-re" not in S.streams[s].cmd + assert S.streams[s].fps is None + assert S.streams[s].res == [720, 540] - assert S.streams[s].video_kbps == 800 + assert S.streams[s].video_kbps == 800 def test_microphone_image(): - with importlib.resources.as_file( - importlib.resources.files("pylivestream.data").joinpath("check4k.png") - ) as img: - S = pls.Microphone(ini, websites=sites, image=img) + img = importlib.resources.files("pylivestream.data").joinpath("check4k.png") + S = pls.Microphone(ini, websites=sites, image=img) - for s in S.streams: - assert "-re" not in S.streams[s].cmd - assert S.streams[s].fps is None - assert S.streams[s].res == [3840, 2160] + for s in S.streams: + assert "-re" not in S.streams[s].cmd + assert S.streams[s].fps is None + assert S.streams[s].res == [3840, 2160] - assert S.streams[s].video_kbps == 4000 + assert S.streams[s].video_kbps == 4000 @pytest.mark.timeout(TIMEOUT) diff --git a/src/pylivestream/utils.py b/src/pylivestream/utils.py index e77f9ab..bafcbfe 100644 --- a/src/pylivestream/utils.py +++ b/src/pylivestream/utils.py @@ -1,11 +1,16 @@ from __future__ import annotations import logging -import contextlib import subprocess from pathlib import Path import sys + import importlib.resources +try: + from importlib.resources.abc import Traversable +except ImportError: # Python < 3.11 + from importlib.abc import Traversable + from .ffmpeg import get_meta, get_ffplay @@ -51,17 +56,15 @@ def check_device(cmd: list[str]) -> bool: def check_display(fn: Path | None = None) -> bool: """see if it's possible to display something with a test file""" - def _check_disp(fn: Path | contextlib.AbstractContextManager[Path]) -> int: + def _check_disp(fn: Path | Traversable) -> int: cmd = [get_ffplay(), "-loglevel", "error", "-t", "1.0", "-autoexit", str(fn)] return subprocess.run(cmd, timeout=10).returncode if fn: ret = _check_disp(fn) else: - with importlib.resources.as_file( - importlib.resources.files(f"{__package__}.data").joinpath("logo.png") - ) as f: - ret = _check_disp(f) + logo = importlib.resources.files(f"{__package__}.data").joinpath("logo.png") + ret = _check_disp(logo) return ret == 0