restructure project for poetry-slam
This commit is contained in:
+9
-9
@@ -5,12 +5,12 @@ import pytest
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_env(monkeypatch):
|
||||
fixtures = Path(__file__).parent / 'fixtures'
|
||||
monkeypatch.setenv('CROAKER_ROOT', str(fixtures))
|
||||
monkeypatch.setenv('MEDIA_GLOB', '*.mp3,*.foo,*.bar')
|
||||
monkeypatch.setenv('ICECAST_URL', 'http://127.0.0.1')
|
||||
monkeypatch.setenv('ICECAST_HOST', 'localhost')
|
||||
monkeypatch.setenv('ICECAST_MOUNT', 'mount')
|
||||
monkeypatch.setenv('ICECAST_PORT', '6523')
|
||||
monkeypatch.setenv('ICECAST_PASSWORD', 'password')
|
||||
monkeypatch.setenv('DEBUG', '1')
|
||||
fixtures = Path(__file__).parent / "fixtures"
|
||||
monkeypatch.setenv("CROAKER_ROOT", str(fixtures))
|
||||
monkeypatch.setenv("MEDIA_GLOB", "*.mp3,*.foo,*.bar")
|
||||
monkeypatch.setenv("ICECAST_URL", "http://127.0.0.1")
|
||||
monkeypatch.setenv("ICECAST_HOST", "localhost")
|
||||
monkeypatch.setenv("ICECAST_MOUNT", "mount")
|
||||
monkeypatch.setenv("ICECAST_PORT", "6523")
|
||||
monkeypatch.setenv("ICECAST_PASSWORD", "password")
|
||||
monkeypatch.setenv("DEBUG", "1")
|
||||
|
||||
+24
-13
@@ -6,19 +6,30 @@ import pytest
|
||||
from croaker import pidfile
|
||||
|
||||
|
||||
@pytest.mark.parametrize('pid,terminate,kill_result,broken', [
|
||||
('pid', False, None, False), # running proc, no terminate
|
||||
('pid', True, True, False), # running proc, terminate
|
||||
('pid', True, ProcessLookupError, True), # stale pid
|
||||
(None, None, None, False), # no running proc
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"pid,terminate,kill_result,broken",
|
||||
[
|
||||
("pid", False, None, False), # running proc, no terminate
|
||||
("pid", True, True, False), # running proc, terminate
|
||||
("pid", True, ProcessLookupError, True), # stale pid
|
||||
(None, None, None, False), # no running proc
|
||||
],
|
||||
)
|
||||
def test_pidfile(monkeypatch, pid, terminate, kill_result, broken):
|
||||
monkeypatch.setattr(pidfile._pidfile, 'TimeoutPIDLockFile', MagicMock(**{
|
||||
'return_value.read_pid.return_value': pid,
|
||||
}))
|
||||
monkeypatch.setattr(pidfile.os, 'kill', MagicMock(**{
|
||||
'side_effect': kill_result if type(kill_result) is Exception else [kill_result]
|
||||
}))
|
||||
monkeypatch.setattr(
|
||||
pidfile._pidfile,
|
||||
"TimeoutPIDLockFile",
|
||||
MagicMock(
|
||||
**{
|
||||
"return_value.read_pid.return_value": pid,
|
||||
}
|
||||
),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
pidfile.os,
|
||||
"kill",
|
||||
MagicMock(**{"side_effect": kill_result if type(kill_result) is Exception else [kill_result]}),
|
||||
)
|
||||
|
||||
ret = pidfile.pidfile(pidfile_path=Path('/dev/null'), terminate_if_running=terminate)
|
||||
ret = pidfile.pidfile(pidfile_path=Path("/dev/null"), terminate_if_running=terminate)
|
||||
assert ret.break_lock.called == broken
|
||||
|
||||
+16
-13
@@ -2,17 +2,17 @@ from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
import croaker.playlist
|
||||
import croaker.path
|
||||
import croaker.playlist
|
||||
|
||||
|
||||
def test_playlist_loading():
|
||||
pl = croaker.playlist.Playlist(name='test_playlist')
|
||||
pl = croaker.playlist.Playlist(name="test_playlist")
|
||||
path = str(pl.path)
|
||||
tracks = [str(t) for t in pl.tracks]
|
||||
|
||||
assert path == str(croaker.path.playlist_root() / pl.name)
|
||||
assert pl.name == 'test_playlist'
|
||||
assert pl.name == "test_playlist"
|
||||
assert tracks[0] == f"{path}/_theme.mp3"
|
||||
assert f"{path}/one.mp3" in tracks
|
||||
assert f"{path}/two.mp3" in tracks
|
||||
@@ -20,22 +20,25 @@ def test_playlist_loading():
|
||||
assert f"{path}/one.baz" not in tracks
|
||||
|
||||
|
||||
@pytest.mark.parametrize('paths, make_theme, expected_count', [
|
||||
(['test_playlist'], True, 4),
|
||||
(['test_playlist'], False, 4),
|
||||
(['test_playlist', 'sources/one.mp3'], True, 5),
|
||||
(['test_playlist', 'sources/one.mp3'], False, 5),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"paths, make_theme, expected_count",
|
||||
[
|
||||
(["test_playlist"], True, 4),
|
||||
(["test_playlist"], False, 4),
|
||||
(["test_playlist", "sources/one.mp3"], True, 5),
|
||||
(["test_playlist", "sources/one.mp3"], False, 5),
|
||||
],
|
||||
)
|
||||
def test_playlist_creation(monkeypatch, paths, make_theme, expected_count):
|
||||
new_symlinks = []
|
||||
|
||||
def symlink(target):
|
||||
new_symlinks.append(target)
|
||||
|
||||
pl = croaker.playlist.Playlist(name='foo')
|
||||
monkeypatch.setattr(croaker.playlist.Path, 'unlink', MagicMock())
|
||||
monkeypatch.setattr(croaker.playlist.Path, 'symlink_to', MagicMock(side_effect=symlink))
|
||||
monkeypatch.setattr(croaker.playlist.Path, 'mkdir', MagicMock())
|
||||
pl = croaker.playlist.Playlist(name="foo")
|
||||
monkeypatch.setattr(croaker.playlist.Path, "unlink", MagicMock())
|
||||
monkeypatch.setattr(croaker.playlist.Path, "symlink_to", MagicMock(side_effect=symlink))
|
||||
monkeypatch.setattr(croaker.playlist.Path, "mkdir", MagicMock())
|
||||
|
||||
pl.add([croaker.path.playlist_root() / p for p in paths], make_theme)
|
||||
assert len(new_symlinks) == expected_count
|
||||
|
||||
+19
-19
@@ -1,14 +1,13 @@
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import io
|
||||
import queue
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
import shout
|
||||
|
||||
from croaker import streamer, playlist
|
||||
from croaker import playlist, streamer
|
||||
|
||||
|
||||
def get_stream_output(stream):
|
||||
@@ -16,9 +15,9 @@ def get_stream_output(stream):
|
||||
return stream.read()
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
@pytest.fixture(scope="session")
|
||||
def silence_bytes():
|
||||
return (Path(streamer.__file__).parent / 'silence.mp3').read_bytes()
|
||||
return (Path(streamer.__file__).parent / "silence.mp3").read_bytes()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -30,10 +29,9 @@ def output_stream():
|
||||
def mock_shout(output_stream, monkeypatch):
|
||||
def handle_send(buf):
|
||||
output_stream.write(buf)
|
||||
mm = MagicMock(spec=shout.Shout, **{
|
||||
'return_value.send.side_effect': handle_send
|
||||
})
|
||||
monkeypatch.setattr('shout.Shout', mm)
|
||||
|
||||
mm = MagicMock(spec=shout.Shout, **{"return_value.send.side_effect": handle_send})
|
||||
monkeypatch.setattr("shout.Shout", mm)
|
||||
return mm
|
||||
|
||||
|
||||
@@ -41,6 +39,7 @@ def mock_shout(output_stream, monkeypatch):
|
||||
def input_queue():
|
||||
return queue.Queue()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def skip_event():
|
||||
return threading.Event()
|
||||
@@ -80,7 +79,7 @@ def test_streamer_load(audio_streamer, load_event, output_stream):
|
||||
|
||||
|
||||
def test_clear_queue(audio_streamer, input_queue):
|
||||
pl = playlist.Playlist(name='test_playlist')
|
||||
pl = playlist.Playlist(name="test_playlist")
|
||||
for track in pl.tracks:
|
||||
input_queue.put(bytes(track))
|
||||
assert input_queue.not_empty
|
||||
@@ -90,7 +89,7 @@ def test_clear_queue(audio_streamer, input_queue):
|
||||
|
||||
def test_streamer_defaults_to_silence(audio_streamer, input_queue, output_stream, silence_bytes):
|
||||
audio_streamer.do_one_loop()
|
||||
track = playlist.Playlist(name='test_playlist').tracks[0]
|
||||
track = playlist.Playlist(name="test_playlist").tracks[0]
|
||||
input_queue.put(bytes(track))
|
||||
audio_streamer.do_one_loop()
|
||||
audio_streamer.do_one_loop()
|
||||
@@ -98,15 +97,16 @@ def test_streamer_defaults_to_silence(audio_streamer, input_queue, output_stream
|
||||
|
||||
|
||||
def test_streamer_plays_silence_on_error(monkeypatch, audio_streamer, input_queue, output_stream, silence_bytes):
|
||||
monkeypatch.setattr(audio_streamer, 'play_file', MagicMock(side_effect=Exception))
|
||||
track = playlist.Playlist(name='test_playlist').tracks[0]
|
||||
monkeypatch.setattr(audio_streamer, "play_file", MagicMock(side_effect=Exception))
|
||||
track = playlist.Playlist(name="test_playlist").tracks[0]
|
||||
input_queue.put(bytes(track))
|
||||
audio_streamer.do_one_loop()
|
||||
assert get_stream_output(output_stream) == silence_bytes
|
||||
|
||||
|
||||
def test_streamer_plays_from_queue(audio_streamer, input_queue, output_stream):
|
||||
pl = playlist.Playlist(name='test_playlist')
|
||||
expected = b''
|
||||
pl = playlist.Playlist(name="test_playlist")
|
||||
expected = b""
|
||||
for track in pl.tracks:
|
||||
input_queue.put(bytes(track))
|
||||
expected += track.read_bytes()
|
||||
@@ -119,14 +119,14 @@ def test_streamer_handles_stop_interrupt(audio_streamer, output_stream, stop_eve
|
||||
stop_event.set()
|
||||
audio_streamer.silence.seek(0, 0)
|
||||
audio_streamer.play_from_stream(audio_streamer.silence)
|
||||
assert get_stream_output(output_stream) == b''
|
||||
assert get_stream_output(output_stream) == b""
|
||||
|
||||
|
||||
def test_streamer_handles_load_interrupt(audio_streamer, input_queue, output_stream, load_event):
|
||||
pl = playlist.Playlist(name='test_playlist')
|
||||
pl = playlist.Playlist(name="test_playlist")
|
||||
input_queue.put(bytes(pl.tracks[0]))
|
||||
load_event.set()
|
||||
audio_streamer.silence.seek(0, 0)
|
||||
audio_streamer.play_from_stream(audio_streamer.silence)
|
||||
assert get_stream_output(output_stream) == b''
|
||||
assert get_stream_output(output_stream) == b""
|
||||
assert input_queue.empty
|
||||
|
||||
+22
-13
@@ -3,23 +3,32 @@ from unittest.mock import MagicMock
|
||||
import ffmpeg
|
||||
import pytest
|
||||
|
||||
from croaker import playlist
|
||||
from croaker import transcoder
|
||||
from croaker import playlist, transcoder
|
||||
|
||||
|
||||
@pytest.mark.parametrize('suffix, expected', [
|
||||
('.mp3', b'_theme.mp3\n'),
|
||||
('.foo', b'transcoding!\n'),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"suffix, expected",
|
||||
[
|
||||
(".mp3", b"_theme.mp3\n"),
|
||||
(".foo", b"transcoding!\n"),
|
||||
],
|
||||
)
|
||||
def test_transcoder_open(monkeypatch, suffix, expected):
|
||||
monkeypatch.setattr(transcoder, 'ffmpeg', MagicMock(spec=ffmpeg, **{
|
||||
'input.return_value.'
|
||||
'output.return_value.'
|
||||
'global_args.return_value.'
|
||||
'compile.return_value': ['echo', 'transcoding!'],
|
||||
}))
|
||||
monkeypatch.setattr(
|
||||
transcoder,
|
||||
"ffmpeg",
|
||||
MagicMock(
|
||||
spec=ffmpeg,
|
||||
**{
|
||||
"input.return_value."
|
||||
"output.return_value."
|
||||
"global_args.return_value."
|
||||
"compile.return_value": ["echo", "transcoding!"],
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
pl = playlist.Playlist(name='test_playlist')
|
||||
pl = playlist.Playlist(name="test_playlist")
|
||||
track = [t for t in pl.tracks if t.suffix == suffix][0]
|
||||
with transcoder.open(track) as handle:
|
||||
assert handle.read() == expected
|
||||
|
||||
Reference in New Issue
Block a user