Skip to content

Commit

Permalink
test: use Traversable for brevity and robustness test
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jan 9, 2025
1 parent f413cee commit 6e154d8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 80 deletions.
8 changes: 3 additions & 5 deletions src/pylivestream/tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
88 changes: 37 additions & 51 deletions src/pylivestream/tests/test_filein.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
32 changes: 14 additions & 18 deletions src/pylivestream/tests/test_microphone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions src/pylivestream/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations
import logging
import contextlib
from importlib.resources.abc import Traversable
import subprocess
from pathlib import Path
import sys
Expand Down Expand Up @@ -51,17 +51,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

Expand Down

0 comments on commit 6e154d8

Please sign in to comment.