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