License classifier: AGPLv3+
[django-ssify.git] / runtests.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8
3 # This file is part of django-ssify, 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 django-ssify.
8 """
9 from __future__ import unicode_literals
10 import sys
11 import os
12 from os.path import dirname, abspath
13 from optparse import OptionParser
14
15 from django.conf import settings
16
17 # For convenience configure settings if they are not pre-configured or if we
18 # haven't been provided settings to use by environment variable.
19 if not settings.configured and not os.environ.get('DJANGO_SETTINGS_MODULE'):
20     settings.configure(
21         CACHES={
22             'default': {'BACKEND':
23                         'django.core.cache.backends.locmem.LocMemCache'},
24             'ssify': {'BACKEND':
25                       'django.core.cache.backends.locmem.LocMemCache'},
26         },
27         DATABASES={
28             'default': {
29                 'ENGINE': 'django.db.backends.sqlite3',
30             }
31         },
32         INSTALLED_APPS=[
33             'django.contrib.auth',
34             'django.contrib.contenttypes',
35             'django.contrib.sessions',
36             'django.contrib.sites',
37
38             'ssify',
39             'tests',
40         ],
41         LANGUAGE_CODE='pl',
42         MEDIA_URL='/media/',
43         MIDDLEWARE_CLASSES=[
44             'django.middleware.csrf.CsrfViewMiddleware',
45             'ssify.middleware.SsiMiddleware',
46             'django.middleware.cache.UpdateCacheMiddleware',
47             'ssify.middleware.PrepareForCacheMiddleware',
48             'django.middleware.common.CommonMiddleware',
49             'django.contrib.sessions.middleware.SessionMiddleware',
50             'ssify.middleware.LocaleMiddleware',
51             'django.middleware.cache.FetchFromCacheMiddleware',
52         ],
53         STATIC_URL='/static/',
54         ROOT_URLCONF='tests.urls',
55         SITE_ID=1,
56         SSIFY_DEBUG_VERBOSE=False,
57         TEMPLATE_CONTEXT_PROCESSORS=(
58             "django.core.context_processors.debug",
59             "django.core.context_processors.i18n",
60             "django.core.context_processors.tz",
61             "django.core.context_processors.request",
62         ),
63     )
64
65 try:
66     from django.test.runner import DiscoverRunner
67 except ImportError:
68     # Django < 1.6
69     from django.test.simple import DjangoTestSuiteRunner as DiscoverRunner
70
71
72 def runtests(*test_args, **kwargs):
73     """Actual test suite entry point."""
74     if not test_args:
75         test_args = ['tests']
76     parent = dirname(abspath(__file__))
77     sys.path.insert(0, parent)
78
79     # For Django 1.7+
80     try:
81         from django import setup
82     except ImportError:
83         pass
84     else:
85         setup()
86
87     test_runner = DiscoverRunner(
88         verbosity=kwargs.get('verbosity', 1),
89         interactive=kwargs.get('interactive', False),
90         failfast=kwargs.get('failfast'))
91     failures = test_runner.run_tests(test_args)
92     sys.exit(failures)
93
94 if __name__ == '__main__':
95     parser = OptionParser()
96     parser.add_option('--failfast', action='store_true',
97                       default=False, dest='failfast')
98
99     (options, args) = parser.parse_args()
100
101     runtests(failfast=options.failfast, *args)