From 6b53b11f0ec511fdb2bbba23b2e1884798ead0e2 Mon Sep 17 00:00:00 2001 From: evilchili Date: Wed, 21 Dec 2022 22:27:30 -0800 Subject: [PATCH] fixing test env --- groove/cli.py | 60 +++++++++++++++++++++++++---------------------- groove/path.py | 12 ++-------- pyproject.toml | 4 ++-- test/conftest.py | 5 ++-- test/fixtures/env | 17 ++------------ test/test_path.py | 5 ++-- 6 files changed, 42 insertions(+), 61 deletions(-) diff --git a/groove/cli.py b/groove/cli.py index ff6e059..45cc840 100644 --- a/groove/cli.py +++ b/groove/cli.py @@ -19,6 +19,36 @@ from groove.webserver import webserver from groove.exceptions import ConfigurationError from groove.console import Console +SETUP_HELP = """ +Please make sure you set MEDIA_ROOT and SECRET_KEY in your environment. +By default, Groove on Demand will attempt to load these variables from +~/.groove, which may contain the following variables as well. See also +the --env paramter. + +# Set this one. The path containing your media files +MEDIA_ROOT= + +# the kinds of files to import +# MEDIA_GLOB=*.mp3,*.flac,*.m4a + +# where to store the groove_on_demand.db sqlite database. +# DATABASE_PATH=~ + +# Try 'groove themes' to see a list of available themes. +# DEFAULT_THEME=blue_train + +# Web interface configuration +# HOST=127.0.0.1 +# PORT=2323 + +# Set this to a suitably random string. +SECRET_KEY=much secret very private + +# Console configuration +# EDITOR= +# CONSOLE_WIDTH=auto +""" + app = typer.Typer() @@ -47,7 +77,7 @@ def main( groove.path.themes_root() groove.path.database() except ConfigurationError as e: - sys.stderr.write(f'{e}\n') + sys.stderr.write(f'{e}\n\n{SETUP_HELP}') sys.exit(1) @@ -60,33 +90,7 @@ def setup(context: typer.Context): """ Interactive setup is not yet available. Sorry! - In the mean time, please make sure you set MEDIA_ROOT and SECRET_KEY - in your environment. By default, Groove on Demand will attempt to load - these variables from ~/.groove, which may contain the following - variables as well. See also the --env paramter. - - # Set this one. The path containing your media files - MEDIA_ROOT= - - # the kinds of files to import - # MEDIA_GLOB=*.mp3,*.flac,*.m4a - - # where to store the groove_on_demand.db sqlite database. - # DATABASE_PATH=~ - - # Try 'groove themes' to see a list of available themes. - # DEFAULT_THEME=blue_train - - # Web interface configuration - # HOST=127.0.0.1 - # PORT=2323 - - # Set this to a suitably random string. - SECRET_KEY=much secret very private - - # Console configuration - # EDITOR= - # CONSOLE_WIDTH=auto + {SETUP_HELP} """ )) diff --git a/groove/path.py b/groove/path.py index 1035d75..e2db3b2 100644 --- a/groove/path.py +++ b/groove/path.py @@ -9,15 +9,7 @@ _reinstall_hint = "You might need to reinstall Groove On Demand to fix this erro def root(): - path = os.environ.get('GROOVE_ON_DEMAND_ROOT', None) - if not path: - raise ConfigurationError(f"GROOVE_ON_DEMAND_ROOT is not defined in your environment.\n{_setup_hint}") - path = Path(path).expanduser() - if not path.exists() or not path.is_dir(): - raise ConfigurationError( - "The Groove on Demand root directory (GROOVE_ON_DEMAND_ROOT) " - f"does not exist or isn't a directory.\n\n{_reinstall_hint}" - ) + path = Path(__file__).parent.parent logging.debug(f"Root is {path}") return Path(path) @@ -43,12 +35,12 @@ def media(relpath): def static_root(): dirname = os.environ.get('STATIC_PATH', 'static') path = root() / Path(dirname) + logging.debug(f"Static root is {path}") if not path.exists() or not path.is_dir(): raise ConfigurationError( # pragma: no cover f"The static assets directory {dirname} (STATIC_PATH) " f"doesn't exist, or isn't a directory.\n\n{_reinstall_hint}" ) - logging.debug(f"Static root is {path}") return path diff --git a/pyproject.toml b/pyproject.toml index 2811741..9fab1da 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "grooveondemand" -version = "0.1.0" +version = "0.9" description = "audio playlist server" authors = ["evilchili "] license = "MIT License" @@ -9,7 +9,7 @@ packages = [ ] [tool.poetry.dependencies] -python = "^3.10" +python = "^3.7" bottle = "^0.12.23" typer = "^0.7.0" python-dotenv = "^0.21.0" diff --git a/test/conftest.py b/test/conftest.py index d7eb0c4..934d687 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -14,12 +14,11 @@ from unittest.mock import MagicMock @pytest.fixture(autouse=True, scope='function') -def env(): +def env(monkeypatch): root = Path(__file__).parent / Path('fixtures') + monkeypatch.setattr('groove.path.root', MagicMock(return_value=str(root))) load_dotenv(Path('test/fixtures/env')) - os.environ['GROOVE_ON_DEMAND_ROOT'] = str(root) os.environ['MEDIA_ROOT'] = str(root / Path('media')) - os.environ['DATABASE_PATH'] = '' return os.environ diff --git a/test/fixtures/env b/test/fixtures/env index 0c6fd6d..f7e3900 100644 --- a/test/fixtures/env +++ b/test/fixtures/env @@ -1,25 +1,12 @@ -# Will be overwritten by test setup -GROOVE_ON_DEMAND_ROOT=. -MEDIA_ROOT=. - -# Admin user credentials USERNAME=test_username PASSWORD=test_password - -# Web interface configuration HOST=127.0.0.1 PORT=2323 -THEMES_PATH=themes -STATIC_PATH=static DEFAULT_THEME=default_theme SECRET_KEY=fnord - -# Media scanner configuration MEDIA_GLOB=*.mp3,*.flac,*.m4a - -# Set this value to enable debugging DEBUG=1 - EDITOR=ed -CONSOLE_THEMES=True CONSOLE_WIDTH=80 +DATABASE_PATH= +MEDIA_ROOT= diff --git a/test/test_path.py b/test/test_path.py index ca0fe64..ca67f1c 100644 --- a/test/test_path.py +++ b/test/test_path.py @@ -20,10 +20,9 @@ def test_static(monkeypatch): assert path.static('foo', theme=themes.load_theme('default_theme')) -@pytest.mark.parametrize('root', ['/dev/null/missing', None]) -def test_missing_theme_root(monkeypatch, root): +def test_missing_theme_root(monkeypatch): broken_env = {k: v for (k, v) in os.environ.items()} - broken_env['GROOVE_ON_DEMAND_ROOT'] = root + broken_env['THEMES_PATH'] = '/dev/null/enoexist' monkeypatch.setattr(os, 'environ', broken_env) with pytest.raises(ConfigurationError): path.themes_root()