From 98fa1598535b506b1a0189f248aa4dd926c3c7c2 Mon Sep 17 00:00:00 2001 From: Hassan Abouelela Date: Tue, 12 Jul 2022 07:39:10 +0400 Subject: Run Migrations For Tests Call the migration handler in manage.py when running tests, before handing control back to django. Signed-off-by: Hassan Abouelela --- manage.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'manage.py') diff --git a/manage.py b/manage.py index 90912da3..697960c6 100755 --- a/manage.py +++ b/manage.py @@ -95,13 +95,15 @@ class SiteManager: name="pythondiscord.local:8000" ) - def prepare_server(self) -> None: - """Perform preparation tasks before running the server.""" + def prepare_environment(self) -> None: + """Perform common preparation tasks.""" django.setup() print("Applying migrations.") call_command("migrate", verbosity=self.verbosity) + def prepare_server(self) -> None: + """Preform runserver-specific preparation tasks.""" if self.debug: # In Production, collectstatic is ran in the Docker image print("Collecting static files.") @@ -121,6 +123,7 @@ class SiteManager: # Prevent preparing twice when in dev mode due to reloader if not self.debug or in_reloader: + self.prepare_environment() self.prepare_server() print("Starting server.") @@ -148,6 +151,11 @@ class SiteManager: # Run gunicorn for the production server. gunicorn.app.wsgiapp.run() + def run_tests(self) -> None: + """Prepare and run the test suite.""" + self.prepare_environment() + call_command(*sys.argv[1:]) + def clean_up_static_files(build_folder: Path) -> None: """Recursively loop over the build directory and fix links.""" @@ -168,8 +176,12 @@ def clean_up_static_files(build_folder: Path) -> None: def main() -> None: """Entry point for Django management script.""" # Use the custom site manager for launching the server - if len(sys.argv) > 1 and sys.argv[1] == "run": - SiteManager(sys.argv).run_server() + if len(sys.argv) > 1 and sys.argv[1] in ("run", "test"): + manager = SiteManager(sys.argv) + if sys.argv[1] == "run": + manager.run_server() + elif sys.argv[1] == "test": + manager.run_tests() # Pass any others directly to standard management commands else: -- cgit v1.2.3 From 74326128d18e26da6ce007b543e981662aa9777f Mon Sep 17 00:00:00 2001 From: Hassan Abouelela Date: Fri, 19 Aug 2022 02:00:23 +0400 Subject: Ignore Whitenoise's Static Directory Warning Whitenoise raises a warning when the static content folder does not exist, which is the case during tests in CI. This is not an issue though, and static content does get used properly in tests. Thus, the warning is silenced. Signed-off-by: Hassan Abouelela --- manage.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'manage.py') diff --git a/manage.py b/manage.py index 697960c6..37fb141f 100755 --- a/manage.py +++ b/manage.py @@ -7,6 +7,7 @@ from pathlib import Path import django from django.contrib.auth import get_user_model from django.core.management import call_command, execute_from_command_line +from django.test.utils import ignore_warnings DEFAULT_ENVS = { "DJANGO_SETTINGS_MODULE": "pydis_site.settings", @@ -154,7 +155,16 @@ class SiteManager: def run_tests(self) -> None: """Prepare and run the test suite.""" self.prepare_environment() - call_command(*sys.argv[1:]) + # The whitenoise package expects a staticfiles directory to exist during startup, + # else it raises a warning. This is fine under normal application, but during + # tests, staticfiles are not, and do not need to be generated. + # The following line suppresses the warning. + # Reference: https://github.com/evansd/whitenoise/issues/215 + with ignore_warnings( + message=r"No directory at: .*staticfiles", + module="whitenoise.base", + ): + call_command(*sys.argv[1:]) def clean_up_static_files(build_folder: Path) -> None: -- cgit v1.2.3 From 182b7e5c2479ea7c35191e99656f82642d8aa24c Mon Sep 17 00:00:00 2001 From: wookie184 Date: Sat, 19 Nov 2022 13:57:37 +0000 Subject: Fix error when invoking manage.py with no commands --- manage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'manage.py') diff --git a/manage.py b/manage.py index 37fb141f..afca6121 100755 --- a/manage.py +++ b/manage.py @@ -195,7 +195,7 @@ def main() -> None: # Pass any others directly to standard management commands else: - _static_build = "distill" in sys.argv[1] + _static_build = len(sys.argv) > 1 and "distill" in sys.argv[1] if _static_build: # Build a static version of the site with no databases and API support -- cgit v1.2.3 From 0a897b9f717bf081f7ec931f3abf67a5874789ec Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Tue, 11 Jul 2023 21:54:59 +0200 Subject: Fix warning ignore Additionally, use stdlib API instead of undocumented Django test API Co-authored-by: Chris Lovering --- manage.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'manage.py') diff --git a/manage.py b/manage.py index afca6121..80601935 100755 --- a/manage.py +++ b/manage.py @@ -2,12 +2,12 @@ import os import platform import sys +import warnings from pathlib import Path import django from django.contrib.auth import get_user_model from django.core.management import call_command, execute_from_command_line -from django.test.utils import ignore_warnings DEFAULT_ENVS = { "DJANGO_SETTINGS_MODULE": "pydis_site.settings", @@ -160,11 +160,12 @@ class SiteManager: # tests, staticfiles are not, and do not need to be generated. # The following line suppresses the warning. # Reference: https://github.com/evansd/whitenoise/issues/215 - with ignore_warnings( - message=r"No directory at: .*staticfiles", - module="whitenoise.base", - ): - call_command(*sys.argv[1:]) + warnings.filterwarnings( + action='ignore', + category=UserWarning, + message=r"^No directory at: .*staticfiles/$" + ) + call_command(*sys.argv[1:]) def clean_up_static_files(build_folder: Path) -> None: -- cgit v1.2.3