UPD: improved global test-suite.
This commit is contained in:
12
dav_base/tests/utils.py
Normal file
12
dav_base/tests/utils.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
import os
|
||||||
|
from tempfile import mkdtemp as _mkdtmp
|
||||||
|
|
||||||
|
|
||||||
|
def mkdtemp(prefix):
|
||||||
|
dirname = os.path.dirname
|
||||||
|
pkg_base_dir = dirname(dirname(dirname(__file__)))
|
||||||
|
tmp_dir = os.path.join(pkg_base_dir, 'tmp')
|
||||||
|
os.makedirs(tmp_dir, exist_ok=True)
|
||||||
|
return _mkdtmp(prefix=prefix, dir=tmp_dir)
|
||||||
@@ -1,21 +1,31 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
import datetime
|
import datetime
|
||||||
import django
|
import django
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
from django.conf import settings
|
|
||||||
from django.test.utils import get_runner
|
from django.test.utils import get_runner
|
||||||
from tempfile import mkdtemp
|
|
||||||
|
#from dav_base.console_scripts.admin import DJANGO_MAIN_MODULE
|
||||||
|
DJANGO_MAIN_MODULE = 'main'
|
||||||
|
from dav_base.tests.utils import mkdtemp
|
||||||
|
|
||||||
|
|
||||||
class DjangoEnvironment(object):
|
class DjangoEnvironment(object):
|
||||||
def __init__(self, path=None, base_path=None, remove_after=True,
|
@staticmethod
|
||||||
|
def _install_djangoproject(path, modules=None):
|
||||||
|
cmd = 'django-dav-admin setup "{}"'.format(path)
|
||||||
|
os.system(cmd)
|
||||||
|
if modules is not None:
|
||||||
|
for mod in modules:
|
||||||
|
cmd = 'django-dav-admin enable_module "{}" "{}"'.format(path, mod)
|
||||||
|
os.system(cmd)
|
||||||
|
|
||||||
|
def __init__(self, path=None, remove_after=True,
|
||||||
enable_modules=None):
|
enable_modules=None):
|
||||||
self.path = path
|
self.path = path
|
||||||
if base_path is not None:
|
|
||||||
self._base_path = base_path
|
|
||||||
else:
|
|
||||||
self._base_path = os.getcwd()
|
|
||||||
self._remove_after = remove_after
|
self._remove_after = remove_after
|
||||||
self._original_sys_path = None
|
self._original_sys_path = None
|
||||||
self._modified_sys_path = None
|
self._modified_sys_path = None
|
||||||
@@ -24,30 +34,25 @@ class DjangoEnvironment(object):
|
|||||||
else:
|
else:
|
||||||
self._enable_modules = []
|
self._enable_modules = []
|
||||||
|
|
||||||
def admin_cmd(self, *args):
|
|
||||||
cmd = 'django-dav-admin'
|
|
||||||
for arg in args:
|
|
||||||
cmd += ' "{}"'.format(arg)
|
|
||||||
os.system(cmd)
|
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
if self.path is None:
|
if self.path is None:
|
||||||
prefix = 'testrun-{datetime}-'.format(
|
prefix = 'testrun-{datetime}-'.format(
|
||||||
datetime=datetime.datetime.now().strftime('%Y%m%d-%H%M')
|
datetime=datetime.datetime.now().strftime('%Y%m%d-%H%M')
|
||||||
)
|
)
|
||||||
self.path = mkdtemp(prefix=prefix, dir=self._base_path)
|
self.path = mkdtemp(prefix=prefix)
|
||||||
|
|
||||||
self.admin_cmd('setup', self.path)
|
self._install_djangoproject(self.path, modules=self._enable_modules)
|
||||||
for mod in self._enable_modules:
|
|
||||||
self.admin_cmd('enable_module', self.path, mod)
|
|
||||||
|
|
||||||
self._original_sys_path = sys.path
|
self._original_sys_path = sys.path
|
||||||
sys.path.append(self.path)
|
sys.path.append(self.path)
|
||||||
self._modified_sys_path = sys.path
|
self._modified_sys_path = sys.path
|
||||||
|
|
||||||
os.environ['DJANGO_SETTINGS_MODULE'] = 'main.settings'
|
os.environ['DJANGO_SETTINGS_MODULE'] = '{}.settings'.format(DJANGO_MAIN_MODULE)
|
||||||
django.setup()
|
django.setup()
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
self.settings = settings
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
@@ -59,16 +64,19 @@ class DjangoEnvironment(object):
|
|||||||
|
|
||||||
|
|
||||||
class TestSuite(object):
|
class TestSuite(object):
|
||||||
def __call__(self):
|
@staticmethod
|
||||||
modules = ['dav_auth']
|
def run():
|
||||||
|
modules = ['dav_auth', 'dav_events', 'dav_registration']
|
||||||
tests = ['dav_base'] + modules
|
tests = ['dav_base'] + modules
|
||||||
test_tags = None
|
test_tags = None
|
||||||
exclude_test_tags = ['browser']
|
exclude_test_tags = ['browser']
|
||||||
failures = 0
|
|
||||||
|
|
||||||
with DjangoEnvironment(enable_modules=modules):
|
with DjangoEnvironment(enable_modules=modules) as env:
|
||||||
test_runner_class = get_runner(settings)
|
test_runner_class = get_runner(env.settings)
|
||||||
test_runner = test_runner_class(tags=test_tags, exclude_tags=exclude_test_tags)
|
test_runner = test_runner_class(tags=test_tags, exclude_tags=exclude_test_tags)
|
||||||
failures = test_runner.run_tests(tests)
|
failures = test_runner.run_tests(tests)
|
||||||
|
|
||||||
sys.exit(bool(failures))
|
return bool(failures)
|
||||||
|
|
||||||
|
def __call__(self):
|
||||||
|
sys.exit(self.run())
|
||||||
|
|||||||
Reference in New Issue
Block a user