From 1133274b75419792db26c0bb817fc7b4f4e3fe82 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Sun, 18 Sep 2022 17:52:34 +0200 Subject: [PATCH] tests/conftest.py --- Makefile | 2 +- conf/urls.py | 2 -- run_pytest.py | 25 ------------------------- tests/conftest.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 28 deletions(-) delete mode 100644 run_pytest.py create mode 100644 tests/conftest.py diff --git a/Makefile b/Makefile index f55f7a3..575c7c7 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ tox: check-poetry ## Run pytest via tox with all environments poetry run tox pytest: install ## Run pytest - poetry run python3 ./run_pytest.py + poetry run pytest local-test: install ## Run local_test.py to run the project locally poetry run python3 ./local_test.py diff --git a/conf/urls.py b/conf/urls.py index 5806cb6..f0fd05f 100644 --- a/conf/urls.py +++ b/conf/urls.py @@ -16,8 +16,6 @@ if settings.PATH_URL: # TODO: https://github.com/jedie/django-for-runners/issues/25 # path(settings.MEDIA_URL.lstrip('/'), include('django_tools.serve_media_app.urls')), ] - if settings.SERVE_FILES: - urlpatterns += static.static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) else: # Installed to domain root, without a path prefix # Just use the default project urls.py diff --git a/run_pytest.py b/run_pytest.py deleted file mode 100644 index 086b5ef..0000000 --- a/run_pytest.py +++ /dev/null @@ -1,25 +0,0 @@ -""" - Run pytest against local test creation -""" - -from pathlib import Path - - -try: - from django_yunohost_integration.pytest_helper import run_pytest -except ImportError as err: - raise ImportError('Did you forget to activate a virtual environment?') from err - - -BASE_PATH = Path(__file__).parent - - -def main(): - run_pytest( - django_settings_path=BASE_PATH / 'conf' / 'settings.py', - destination=BASE_PATH / 'local_test', - ) - - -if __name__ == '__main__': - main() diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..e7d56d8 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,44 @@ +""" + Special pytest init: + + - Build a "local_test" YunoHost installation + - init Django with this local test installation + + So the pytests will run against this local test installation +""" +import os +import sys +from pathlib import Path + +import django + +from django_yunohost_integration.local_test import create_local_test + + +BASE_PATH = Path(__file__).parent.parent + +os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' + + +def pytest_configure(): + print('Compile YunoHost files...') + final_path = create_local_test( + django_settings_path=BASE_PATH / 'conf' / 'settings.py', + destination=BASE_PATH / 'local_test', + runserver=False, + extra_replacements={ + '__DEBUG_ENABLED__': '0', + '__LOG_LEVEL__': 'INFO', + '__ADMIN_EMAIL__': 'foo-bar@test.tld', + '__DEFAULT_FROM_EMAIL__': 'django_app@test.tld', + }, + ) + print('Local test files created here:') + print(f'"{final_path}"') + + os.chdir(final_path) + final_home_str = str(final_path) + if final_home_str not in sys.path: + sys.path.insert(0, final_home_str) + + django.setup()