Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/runalltests.py
blob: e5dada651930dfb3585f08f7d751b77b4cb7030b (plain)
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
105
106
#!/usr/bin/python
#
# Runs all tests in the current directory
#
# Execute like:
#   python runalltests.py
#
import doctest
import logging
import os
import shutil
import signal
import subprocess
import sys
import tempfile
import unittest

logging.basicConfig(level=logging.WARN,
    format="%(asctime)-15s %(name)s %(levelname)s: %(message)s",
    stream=sys.stderr)

doctests = [
    "basic_api.txt",
]

doctest_options = doctest.ELLIPSIS
#doctest_options |= doctest.REPORT_ONLY_FIRST_FAILURE


def setup():
    """Prepare test environment

    Sets HOME and creates a new process group so we can clean up easily later.
    """
    os.environ['HOME'] = tempfile.mkdtemp(prefix='datastore-test')
    if 'PYTHONPATH' in os.environ :
        python_path = os.environ.get('PYTHONPATH').split(':')
    else :
        python_path = []

    # Run tests on sources instead of on installed files.
    python_path = [os.path.abspath('../src')] + python_path
    os.environ['PYTHONPATH'] = ':'.join(python_path)
    os.environ['PATH'] = os.path.abspath('../bin')+':'+os.environ['PATH']

    os.setpgid(0, 0)
    # prevent suicide in cleanup()
    signal.signal(signal.SIGTERM, signal.SIG_IGN)


def cleanup():
    """Clean up test environment.

    Kills all children and removes home directory.
    """
    os.kill(0, signal.SIGTERM)
    shutil.rmtree(os.environ['HOME'])


def test_suite(tests=None):
    global doctests
    suite = unittest.TestSuite()
    if tests:
        doctests = tests

    for dt in doctests:
        suite.addTest(doctest.DocFileSuite(dt,
    optionflags=doctest_options))

    if len(sys.argv) <= 1:
        tests = os.listdir(os.curdir)
        tests = [n[:-3] for n in tests if n.startswith('test') and
                 n.endswith('.py')]

        for test in tests:
            m = __import__(test)
            if hasattr(m, 'test_suite'):
                suite.addTest(m.test_suite())
    return suite


def run_tests(tests):
    runner = unittest.TextTestRunner(verbosity=1)
    suite = test_suite(tests)
    runner.run(suite)


def main(my_name, arguments):
    if "--have-dbus" not in arguments:
        setup()
        try:
            pipe = subprocess.Popen(['dbus-launch',
                os.path.abspath(my_name), '--have-dbus']+arguments,
                cwd=os.environ['HOME'])
            return pipe.wait()

        finally:
            cleanup()

    else:
        run_tests(arguments[1:])
        return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[0], sys.argv[1:]))