The cms/startup.py and lms/startup.py files were created to
allow us to do a lot of custom initialization around things
like the ModuleStore, monkey-patching, adding MIME types to
our process, etc. As far back as 2017, we recognized that
this was a bad thing, marked these modules as "deprecated",
and started removing things or putting them in the standard
Django locations for them (0279181).
In its current state, these startup modules no longer do any
custom work, and just invoke django.startup(). But this is
meant for running Django code in "standalone" usage, e.g. if
you have a script that isn't a management command but needs
some Django functionality.
The "runserver" command used during development normally
launches a child process to serve requests and knows how to
kill and respawn that process when files are modified, so
that changes are reflected. It can also normally handle the
case where there's a SyntaxError in the child process, and
fixing that error will reload the code again.
Something about running django.startup() manually interferes
with this functionality in "runserver". It still reloads the
code in response to changes, but if the code gets into a
broken state for any reason (like a syntax error), the master
process itself dies. That causes the container to restart,
only to die again shortly afterwards in a loop until the
error is fixed. The container restarts will break any shell
you had opened into the container, as well as any IDE
integrations that connected to that container to access the
files and Python instance.
Getting rid of the custom startup code fixes this and moves
us one small step closer to being a more normal Django
project.
63 lines
2.1 KiB
Python
63 lines
2.1 KiB
Python
"""
|
|
Studio unit test configuration and fixtures.
|
|
|
|
This module needs to exist because the pytest.ini in the cms package stops
|
|
pytest from looking for the conftest.py module in the parent directory when
|
|
only running cms tests.
|
|
"""
|
|
|
|
import logging
|
|
|
|
import pytest
|
|
|
|
from openedx.core.pytest_hooks import DeferPlugin
|
|
|
|
# Patch the xml libs before anything else.
|
|
from openedx.core.lib.safe_lxml import defuse_xml_libs # isort:skip
|
|
defuse_xml_libs()
|
|
|
|
|
|
def pytest_configure(config):
|
|
"""
|
|
Do core setup operations from manage.py before collecting tests.
|
|
"""
|
|
if config.pluginmanager.hasplugin("pytest_jsonreport") or config.pluginmanager.hasplugin("json-report"):
|
|
config.pluginmanager.register(DeferPlugin())
|
|
else:
|
|
logging.info("pytest did not register json_report correctly")
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='function')
|
|
def _django_clear_site_cache():
|
|
"""
|
|
pytest-django uses this fixture to automatically clear the Site object
|
|
cache by replacing it with a new dictionary. edx-django-sites-extensions
|
|
grabs the cache dictionary at startup, and uses that one for all lookups
|
|
from then on. Our CacheIsolationMixin class tries to clear the cache by
|
|
grabbing the current dictionary from the site models module and clearing
|
|
it. Long story short: if you use this all together, neither cache
|
|
clearing mechanism actually works. So override this fixture to not mess
|
|
with what has been working for us so far.
|
|
"""
|
|
pass # lint-amnesty, pylint: disable=unnecessary-pass
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def no_webpack_loader(monkeypatch):
|
|
"""
|
|
Monkeypatch webpack_loader to make sure that webpack assets don't need to be
|
|
compiled before unit tests are run.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"webpack_loader.templatetags.webpack_loader.render_bundle",
|
|
lambda entry, extension=None, config='DEFAULT', attrs='': ''
|
|
)
|
|
monkeypatch.setattr(
|
|
"webpack_loader.utils.get_as_tags",
|
|
lambda entry, extension=None, config='DEFAULT', attrs='': []
|
|
)
|
|
monkeypatch.setattr(
|
|
"webpack_loader.utils.get_files",
|
|
lambda entry, extension=None, config='DEFAULT', attrs='': []
|
|
)
|