def test_pre_setup(request, tmpdir, settings): from django.core.cache import caches from django.utils import translation from olympia import amo, core from olympia.translations.hold import clean_translations from waffle.utils import get_cache as waffle_get_cache from waffle import models as waffle_models # Clear all cache-instances. They'll be re-initialized by Django # This will make sure that our random `KEY_PREFIX` is applied # appropriately. # This is done by Django too whenever `settings` is changed # directly but because we're using the `settings` fixture # here this is not detected correctly. caches._caches.caches = {} # Randomize the cache key prefix to keep # tests isolated from each other. prefix = uuid.uuid4().hex settings.CACHES['default']['KEY_PREFIX'] = 'amo:{0}:'.format(prefix) # Reset global django-waffle cache instance to make sure it's properly # using our new key prefix waffle_models.cache = waffle_get_cache() translation.trans_real.deactivate() # Django fails to clear this cache. translation.trans_real._translations = {} translation.trans_real.activate(settings.LANGUAGE_CODE) def _path(*args): path = str(os.path.join(*args)) if not os.path.exists(path): os.makedirs(path) return path settings.STORAGE_ROOT = storage_root = _path(tmpdir.mkdir('storage')) settings.SHARED_STORAGE = shared_storage = _path(storage_root, 'shared_storage') settings.ADDONS_PATH = _path(storage_root, 'files') settings.GUARDED_ADDONS_PATH = _path(storage_root, 'guarded-addons') settings.GIT_FILE_STORAGE_PATH = _path(storage_root, 'git-storage') settings.MEDIA_ROOT = _path(shared_storage, 'uploads') settings.TMP_PATH = _path(shared_storage, 'tmp') # Reset the prefixer and urlconf after updating media root default_prefixer(settings) from django.urls import clear_url_caches, set_urlconf def _clear_urlconf(): clear_url_caches() set_urlconf(None) _clear_urlconf() request.addfinalizer(_clear_urlconf) yield core.set_user(None) clean_translations(None) # Make sure queued translations are removed. # Make sure we revert everything we might have changed to prefixers. amo.urlresolvers.clean_url_prefixes()
def test_pre_setup(request, tmpdir, settings): from django.core.cache import caches from django.utils import translation from olympia import amo, core from olympia.translations.hold import clean_translations from waffle.utils import get_cache as waffle_get_cache from waffle import models as waffle_models # Ignore ResourceWarning for now. It's a Python 3 thing so it's done # dynamically here. if six.PY3: warnings.filterwarnings('ignore', category=ResourceWarning) # noqa # Clear all cache-instances. They'll be re-initialized by Django # This will make sure that our random `KEY_PREFIX` is applied # appropriately. # This is done by Django too whenever `settings` is changed # directly but because we're using the `settings` fixture # here this is not detected correctly. caches._caches.caches = {} # Randomize the cache key prefix to keep # tests isolated from each other. prefix = uuid.uuid4().hex settings.CACHES['default']['KEY_PREFIX'] = 'amo:{0}:'.format(prefix) # Reset global django-waffle cache instance to make sure it's properly # using our new key prefix waffle_models.cache = waffle_get_cache() translation.trans_real.deactivate() # Django fails to clear this cache. translation.trans_real._translations = {} translation.trans_real.activate(settings.LANGUAGE_CODE) def _path(*args): path = str(os.path.join(*args)) if not os.path.exists(path): os.makedirs(path) return path settings.STORAGE_ROOT = storage_root = _path(str(tmpdir.mkdir('storage'))) settings.SHARED_STORAGE = shared_storage = _path( storage_root, 'shared_storage') settings.ADDONS_PATH = _path(storage_root, 'files') settings.GUARDED_ADDONS_PATH = _path(storage_root, 'guarded-addons') settings.GIT_FILE_STORAGE_PATH = _path(storage_root, 'git-storage') settings.MEDIA_ROOT = _path(shared_storage, 'uploads') settings.TMP_PATH = _path(shared_storage, 'tmp') # Reset the prefixer and urlconf after updating media root default_prefixer(settings) from django.urls import clear_url_caches, set_urlconf def _clear_urlconf(): clear_url_caches() set_urlconf(None) _clear_urlconf() request.addfinalizer(_clear_urlconf) yield core.set_user(None) clean_translations(None) # Make sure queued translations are removed. # Make sure we revert everything we might have changed to prefixers. amo.urlresolvers.clean_url_prefixes()