From a5cf97870b748abfbd6dd77f3eab65f00563f002 Mon Sep 17 00:00:00 2001 From: evilchili Date: Sun, 10 Mar 2024 12:08:45 -0700 Subject: [PATCH] remove dead code, add pidfile tests --- croaker/cli.py | 24 ++++++++++-------------- croaker/exceptions.py | 16 ---------------- croaker/path.py | 5 ----- test/test_pidfile.py | 24 ++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 35 deletions(-) delete mode 100644 croaker/exceptions.py create mode 100644 test/test_pidfile.py diff --git a/croaker/cli.py b/croaker/cli.py index a03f0ae..78fd5ec 100644 --- a/croaker/cli.py +++ b/croaker/cli.py @@ -10,20 +10,19 @@ import typer from dotenv import load_dotenv from typing_extensions import Annotated -import croaker.path -from croaker.exceptions import ConfigurationError +from croaker import path from croaker.playlist import Playlist from croaker.server import server -SETUP_HELP = """ +SETUP_HELP = f""" # Root directory for croaker configuration and logs. See also croaker --root. -CROAKER_ROOT=~/.dnd/croaker +CROAKER_ROOT={path.root()} # where to store playlist sources -#PLAYLIST_ROOT=$CROAKER_ROOT/playlists +#PLAYLIST_ROOT={path.root()}/playlists # Where the record the daemon's PID -#PIDFILE=$CROAKER_ROOT/croaker.pid +#PIDFILE={path.root()}/croaker.pid # Command and Control TCP Server bind address HOST=0.0.0.0 @@ -68,20 +67,17 @@ def main( level=logging.DEBUG if debug else logging.INFO, ) - try: - croaker.path.root() - croaker.path.playlist_root() - except ConfigurationError as e: - sys.stderr.write(f"{e}\n\n{SETUP_HELP}") - sys.exit(1) - @app.command() def setup(context: typer.Context): """ (Re)Initialize Croaker. """ - sys.stderr.write("Interactive setup is not yet available. Sorry!\n") + + sys.stderr.write( + "Interactive setup is not available, but you can redirect " + "this command's output to a defaults file of your choice.\n" + ) print(dedent(SETUP_HELP)) diff --git a/croaker/exceptions.py b/croaker/exceptions.py deleted file mode 100644 index 4ecbf84..0000000 --- a/croaker/exceptions.py +++ /dev/null @@ -1,16 +0,0 @@ -class APIHandlingException(Exception): - """ - An API reqeust could not be encoded or decoded. - """ - - -class ConfigurationError(Exception): - """ - An error was discovered with the Groove on Demand configuration. - """ - - -class InvalidPathError(Exception): - """ - The specified path was invalid -- either it was not the expected type or wasn't accessible. - """ diff --git a/croaker/path.py b/croaker/path.py index 2e81eb1..515867e 100644 --- a/croaker/path.py +++ b/croaker/path.py @@ -9,11 +9,6 @@ def root(): return Path(os.environ.get("CROAKER_ROOT", "~/.dnd/croaker")).expanduser() -def cache_root(): - path = Path(os.environ.get("CACHE_ROOT", root() / "cache")).expanduser() - return path - - def playlist_root(): path = Path(os.environ.get("PLAYLIST_ROOT", root() / "playlists")).expanduser() return path diff --git a/test/test_pidfile.py b/test/test_pidfile.py new file mode 100644 index 0000000..02cbcba --- /dev/null +++ b/test/test_pidfile.py @@ -0,0 +1,24 @@ +from pathlib import Path +from unittest.mock import MagicMock + +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 +]) +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] + })) + + ret = pidfile.pidfile(pidfile_path=Path('/dev/null'), terminate_if_running=terminate) + assert ret.break_lock.called == broken