~chicharreros/magicicada-server/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python

# Copyright 2008-2015 Canonical
# Copyright 2015-2016 Chicharreros (https://launchpad.net/~chicharreros)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# For further info, check  http://launchpad.net/magicicada-server

"""Run tests."""

import os
import sys

sys.path.insert(0, os.path.abspath('lib'))

TESTDIRS = [
    'magicicada',
]
for td in TESTDIRS:
    sys.path.insert(0, os.path.abspath(td))

# import unittest first, to break an import loop in the twisted.trial 
# package: reporter -> unittest -> _asyncrunner -> reporter
from twisted.trial import unittest


def setup_environment(django_settings):
    """Setup the environment for running tests."""
    ROOTDIR = os.path.abspath(os.path.curdir)
    os.environ.setdefault('MAGICICADA_DEBUG', '1')
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', django_settings)
    os.environ.setdefault(
        'XDG_CACHE_HOME', os.path.join(ROOTDIR, 'tmp', 'xdg_cache'))

    # repeated setting from makefile, as some tests check this, to work 
    # ok when running them directly from ./test
    os.environ.setdefault('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION', 'cpp')
    os.environ.setdefault('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION', '2')

    dbus_address_file = os.path.join(ROOTDIR, 'tmp', 'dbus.address')
    if os.path.exists(dbus_address_file):
        with open(dbus_address_file) as fh:
            os.environ["DBUS_SESSION_BUS_ADDRESS"] = fh.read().strip()

    # install the glib2reactor before any import of the reactor to avoid
    # using the default SelectReactor and be able to run the dbus tests
    from twisted.internet import glib2reactor
    glib2reactor.install()


if __name__ == "__main__":
    from optparse import OptionParser
    usage = '%prog [options] path'
    parser = OptionParser(usage=usage)
    parser.add_option("-c", "--coverage", dest="coverage", action='store_true',
                      help="run test coverage")
    parser.add_option("-d", "--debug", dest="debug", action='store_true',
                      help="set twisted.internet.base.DelayedCall.debug=True")
    parser.add_option("-t", "--test", dest="test",
                      help="run specific tests, e.g: className.methodName")
    parser.add_option("-i", "--ignore", dest="ignore",
                      help="ignore/skip specific tests, "
                           "e.g: className.methodName")
    parser.add_option("-l", "--loop", dest="loops", type="int", default=1,
                      help="loop selected tests LOOPS number of times",
                      metavar="LOOPS")
    parser.add_option("-1", "--one", dest="one", action='store_true',
                      help="Stop when one test fails")
    parser.add_option("--subunit", dest="subunit", action='store_true',
                      help="Use the subunit reporter.", default=False)
    parser.add_option("-e", "--logs-on-failure", dest="logs_on_failure",
                      action='store_true',
                      help="Show logs on stdout if error or failure")
    parser.add_option("-v", "--verbosity", dest="verbosity",
                      type='int', default=1, help="Verbosity, default is 1")

    (options, args) = parser.parse_args()

    setup_environment("magicicada.settings.testing")

    from utilities.testlib import test_with_trial

    testroots = args
    topdir = os.path.abspath(os.path.dirname(__file__))
    status = test_with_trial(options, topdir, TESTDIRS, testroots)

    if sys.stdout.isatty():
        if status:
            os.system('cat dev-scripts/failure.txt')
        else:
            os.system('cat dev-scripts/success.txt')
    sys.exit(status)