28648cf74c049820511ef8c767ada99d79c9a85d
[fnpdjango.git] / runtests.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8
3 # This file is part of FNPDjango, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See README.md for more information.
5 #
6 """
7 Creates a simple Django configuration and runs tests for fnpdjango.
8 """
9 from __future__ import unicode_literals
10
11 import sys
12 import os
13 from os.path import dirname, abspath
14 from optparse import OptionParser
15 from shutil import rmtree
16
17 from django.conf import settings
18 from fnpdjango.utils.settings import LazyUGettextLazy as _
19
20
21 # For convenience configure settings if they are not pre-configured or if we
22 # haven't been provided settings to use by environment variable.
23 if not settings.configured and not os.environ.get('DJANGO_SETTINGS_MODULE'):
24     import tempfile
25     media_root = tempfile.mkdtemp(prefix='djangotest_')
26
27     settings.configure(
28         DATABASES={
29             'default': {
30                 'ENGINE': 'django.db.backends.sqlite3',
31             }
32         },
33         INSTALLED_APPS=[
34             'django.contrib.auth',
35             'django.contrib.contenttypes',
36             'django.contrib.sessions',
37             'django.contrib.sites',
38
39             'fnpdjango',
40             'tests',
41         ],
42         LANGUAGE_CODE='pl',
43         MEDIA_ROOT=media_root,
44         STATIC_URL='/static/',
45         MIDDLEWARE_CLASSES=[    # Django <= 1.9
46             'django.middleware.common.CommonMiddleware',
47             'fnpdjango.middleware.URLLocaleMiddleware',
48             'fnpdjango.middleware.SetRemoteAddrFromXRealIP',
49         ],
50         MIDDLEWARE=[
51             'django.middleware.common.CommonMiddleware',
52             'fnpdjango.middleware.URLLocaleMiddleware',
53             'fnpdjango.middleware.SetRemoteAddrFromXRealIP',
54         ],
55         FNPDJANGO_REALIP = True,
56         ROOT_URLCONF='tests.urls',
57         TEMPLATES=[
58             {
59                 'BACKEND': 'django.template.backends.django.DjangoTemplates',
60                 'APP_DIRS': True,
61             },
62         ],
63         TEST_LAZY_UGETTEXT_LAZY=_("Lazy setting."),
64         USE_I18N=True,
65     )
66 else:
67     media_root = None
68
69 try:
70     from django.test.runner import DiscoverRunner
71 except ImportError:
72     # Django < 1.6
73     from django.test.simple import DjangoTestSuiteRunner as DiscoverRunner
74
75
76 def runtests(*test_args, **kwargs):
77     """Actual test suite entry point."""
78     if not test_args:
79         test_args = ['tests']
80     parent = dirname(abspath(__file__))
81     sys.path.insert(0, parent)
82
83     # For Django 1.7+
84     try:
85         from django import setup
86     except ImportError:
87         pass
88     else:
89         setup()
90
91     test_runner = DiscoverRunner(
92         verbosity=kwargs.get('verbosity', 1),
93         interactive=kwargs.get('interactive', False),
94         failfast=kwargs.get('failfast'))
95     failures = test_runner.run_tests(test_args)
96     if media_root:
97         rmtree(media_root)
98     sys.exit(failures)
99
100 if __name__ == '__main__':
101     parser = OptionParser()
102     parser.add_option('--failfast', action='store_true',
103                       default=False, dest='failfast')
104
105     (options, args) = parser.parse_args()
106
107     runtests(failfast=options.failfast, *args)