Skip to content
This repository was archived by the owner on Mar 6, 2025. It is now read-only.

Commit 3f7f4e9

Browse files
committed
test: use Traversable for brevity and robustness test
1 parent f413cee commit 3f7f4e9

File tree

4 files changed

+63
-80
lines changed

4 files changed

+63
-80
lines changed

src/pylivestream/tests/test_class.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ def test_exe(rex):
1919
def test_attrs(inp):
2020
assert not pls.utils.get_resolution(inp)
2121

22-
with importlib.resources.as_file(
23-
importlib.resources.files("pylivestream.data").joinpath("bunny.avi")
24-
) as fn:
25-
assert pls.utils.get_resolution(fn) == [426, 240]
26-
assert pls.utils.get_framerate(fn) == approx(24.0)
22+
vid = importlib.resources.files("pylivestream.data").joinpath("bunny.avi")
23+
assert pls.utils.get_resolution(vid) == [426, 240]
24+
assert pls.utils.get_framerate(vid) == approx(24.0)
2725

2826

2927
def test_config_not_found(tmp_path):

src/pylivestream/tests/test_filein.py

+37-51
Original file line numberDiff line numberDiff line change
@@ -16,71 +16,57 @@
1616

1717
def test_props():
1818

19-
with importlib.resources.as_file(
20-
importlib.resources.files("pylivestream.data").joinpath("bunny.avi")
21-
) as fn:
22-
S = pls.FileIn(ini, websites=sites, infn=fn)
23-
for s in S.streams:
24-
assert "-re" in S.streams[s].cmd
25-
assert S.streams[s].fps == approx(24.0)
19+
vid = importlib.resources.files("pylivestream.data").joinpath("bunny.avi")
20+
S = pls.FileIn(ini, websites=sites, infn=vid)
21+
for s in S.streams:
22+
assert "-re" in S.streams[s].cmd
23+
assert S.streams[s].fps == approx(24.0)
2624

27-
if int(S.streams[s].res[1]) == 480:
28-
assert S.streams[s].video_kbps == 500
29-
elif int(S.streams[s].res[1]) == 720:
30-
assert S.streams[s].video_kbps == 1800
25+
if int(S.streams[s].res[1]) == 480:
26+
assert S.streams[s].video_kbps == 500
27+
elif int(S.streams[s].res[1]) == 720:
28+
assert S.streams[s].video_kbps == 1800
3129

3230

3331
def test_audio():
3432

35-
with (
36-
importlib.resources.as_file(
37-
importlib.resources.files("pylivestream.data").joinpath("logo.png")
38-
) as logo,
39-
importlib.resources.as_file(
40-
importlib.resources.files("pylivestream.data").joinpath("orch_short.ogg")
41-
) as fn,
42-
):
43-
S = pls.FileIn(ini, websites=sites, infn=fn, image=logo)
44-
for s in S.streams:
45-
assert "-re" in S.streams[s].cmd
46-
assert S.streams[s].fps is None
33+
logo = importlib.resources.files("pylivestream.data").joinpath("logo.png")
34+
snd = importlib.resources.files("pylivestream.data").joinpath("orch_short.ogg")
4735

48-
assert S.streams[s].video_kbps == 800
36+
S = pls.FileIn(ini, websites=sites, infn=snd, image=logo)
37+
for s in S.streams:
38+
assert "-re" in S.streams[s].cmd
39+
assert S.streams[s].fps is None
40+
41+
assert S.streams[s].video_kbps == 800
4942

5043

5144
@pytest.mark.timeout(TIMEOUT)
5245
@pytest.mark.skipif(CI, reason="CI has no audio hardware typically")
5346
def test_simple():
5447
"""stream to localhost"""
55-
with (
56-
importlib.resources.as_file(
57-
importlib.resources.files("pylivestream.data").joinpath("logo.png")
58-
) as logo,
59-
importlib.resources.as_file(
60-
importlib.resources.files("pylivestream.data").joinpath("orch_short.ogg")
61-
) as fn,
62-
):
63-
S = pls.FileIn(ini, websites="localhost", infn=fn, image=logo, yes=True, timeout=5)
48+
logo = importlib.resources.files("pylivestream.data").joinpath("logo.png")
49+
aud = importlib.resources.files("pylivestream.data").joinpath("orch_short.ogg")
50+
51+
S = pls.FileIn(ini, websites="localhost", infn=aud, image=logo, yes=True, timeout=5)
6452

65-
S.golive()
53+
S.golive()
6654

6755

6856
@pytest.mark.skipif(CI, reason="CI has no audio hardware typically")
6957
def test_script():
70-
with importlib.resources.as_file(
71-
importlib.resources.files("pylivestream.data").joinpath("bunny.avi")
72-
) as fn:
73-
subprocess.check_call(
74-
[
75-
sys.executable,
76-
"-m",
77-
"pylivestream.fglob",
78-
str(fn),
79-
"localhost",
80-
str(ini),
81-
"--yes",
82-
"--timeout",
83-
"5",
84-
],
85-
timeout=TIMEOUT,
86-
)
58+
vid = importlib.resources.files("pylivestream.data").joinpath("bunny.avi")
59+
subprocess.check_call(
60+
[
61+
sys.executable,
62+
"-m",
63+
"pylivestream.fglob",
64+
str(vid),
65+
"localhost",
66+
str(ini),
67+
"--yes",
68+
"--timeout",
69+
"5",
70+
],
71+
timeout=TIMEOUT,
72+
)

src/pylivestream/tests/test_microphone.py

+14-18
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,28 @@
2121

2222
def test_microphone_props():
2323

24-
with importlib.resources.as_file(
25-
importlib.resources.files("pylivestream.data").joinpath("logo.png")
26-
) as logo:
27-
S = pls.Microphone(ini, websites=sites, image=logo)
24+
logo = importlib.resources.files("pylivestream.data").joinpath("logo.png")
25+
S = pls.Microphone(ini, websites=sites, image=logo)
2826

29-
for s in S.streams:
30-
assert "-re" not in S.streams[s].cmd
31-
assert S.streams[s].fps is None
32-
assert S.streams[s].res == [720, 540]
27+
for s in S.streams:
28+
assert "-re" not in S.streams[s].cmd
29+
assert S.streams[s].fps is None
30+
assert S.streams[s].res == [720, 540]
3331

34-
assert S.streams[s].video_kbps == 800
32+
assert S.streams[s].video_kbps == 800
3533

3634

3735
def test_microphone_image():
3836

39-
with importlib.resources.as_file(
40-
importlib.resources.files("pylivestream.data").joinpath("check4k.png")
41-
) as img:
42-
S = pls.Microphone(ini, websites=sites, image=img)
37+
img = importlib.resources.files("pylivestream.data").joinpath("check4k.png")
38+
S = pls.Microphone(ini, websites=sites, image=img)
4339

44-
for s in S.streams:
45-
assert "-re" not in S.streams[s].cmd
46-
assert S.streams[s].fps is None
47-
assert S.streams[s].res == [3840, 2160]
40+
for s in S.streams:
41+
assert "-re" not in S.streams[s].cmd
42+
assert S.streams[s].fps is None
43+
assert S.streams[s].res == [3840, 2160]
4844

49-
assert S.streams[s].video_kbps == 4000
45+
assert S.streams[s].video_kbps == 4000
5046

5147

5248
@pytest.mark.timeout(TIMEOUT)

src/pylivestream/utils.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from __future__ import annotations
22
import logging
3-
import contextlib
43
import subprocess
54
from pathlib import Path
65
import sys
6+
77
import importlib.resources
88

9+
try:
10+
from importlib.resources.abc import Traversable
11+
except ImportError: # Python < 3.11
12+
from importlib.abc import Traversable
13+
914
from .ffmpeg import get_meta, get_ffplay
1015

1116

@@ -51,17 +56,15 @@ def check_device(cmd: list[str]) -> bool:
5156
def check_display(fn: Path | None = None) -> bool:
5257
"""see if it's possible to display something with a test file"""
5358

54-
def _check_disp(fn: Path | contextlib.AbstractContextManager[Path]) -> int:
59+
def _check_disp(fn: Path | Traversable) -> int:
5560
cmd = [get_ffplay(), "-loglevel", "error", "-t", "1.0", "-autoexit", str(fn)]
5661
return subprocess.run(cmd, timeout=10).returncode
5762

5863
if fn:
5964
ret = _check_disp(fn)
6065
else:
61-
with importlib.resources.as_file(
62-
importlib.resources.files(f"{__package__}.data").joinpath("logo.png")
63-
) as f:
64-
ret = _check_disp(f)
66+
logo = importlib.resources.files(f"{__package__}.data").joinpath("logo.png")
67+
ret = _check_disp(logo)
6568

6669
return ret == 0
6770

0 commit comments

Comments
 (0)