summaryrefslogtreecommitdiff
path: root/lib/testtools/scripts/all-pythons
blob: 5a0c41570833f1ffd266f83bff011ee1ba88c60c (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
#!/usr/bin/python

"""Run the testtools test suite for all supported Pythons.

Prints output as a subunit test suite. If anything goes to stderr, that is
treated as a test error. If a Python is not available, then it is skipped.
"""

from datetime import datetime
import os
import subprocess
import sys

import subunit
from subunit import (
    iso8601,
    _make_stream_binary,
    TestProtocolClient,
    TestProtocolServer,
    )
from testtools import (
    PlaceHolder,
    TestCase,
    )
from testtools.compat import BytesIO
from testtools.content import text_content


ROOT = os.path.dirname(os.path.dirname(__file__))


def run_for_python(version, result, tests):
    if not tests:
        tests = ['testtools.tests.test_suite']
    # XXX: This could probably be broken up and put into subunit.
    python = 'python%s' % (version,)
    # XXX: Correct API, but subunit doesn't support it. :(
    # result.tags(set(python), set())
    result.time(now())
    test = PlaceHolder(''.join(c for c in python if c != '.'))
    process = subprocess.Popen(
        '%s -c pass' % (python,), shell=True,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    process.communicate()

    if process.returncode:
        result.startTest(test)
        result.addSkip(test, reason='%s not available' % (python,))
        result.stopTest(test)
        return

    env = os.environ.copy()
    if env.get('PYTHONPATH', None):
        env['PYTHONPATH'] = os.pathsep.join([ROOT, env['PYTHONPATH']])
    else:
        env['PYTHONPATH'] = ROOT
    result.time(now())
    protocol = TestProtocolServer(result)
    subunit_path = os.path.join(os.path.dirname(subunit.__file__), 'run.py')
    cmd = [
        python,
        '-W', 'ignore:Module testtools was already imported',
        subunit_path]
    cmd.extend(tests)
    process = subprocess.Popen(
        cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
    _make_stream_binary(process.stdout)
    _make_stream_binary(process.stderr)
    # XXX: This buffers everything. Bad for memory, bad for getting progress
    # on jenkins.
    output, error = process.communicate()
    protocol.readFrom(BytesIO(output))
    if error:
        result.startTest(test)
        result.addError(test, details={
            'stderr': text_content(error),
           })
        result.stopTest(test)
    result.time(now())
    # XXX: Correct API, but subunit doesn't support it. :(
    #result.tags(set(), set(python))


def now():
    return datetime.utcnow().replace(tzinfo=iso8601.Utc())



if __name__ == '__main__':
    sys.path.append(ROOT)
    result = TestProtocolClient(sys.stdout)
    for version in '2.4 2.5 2.6 2.7 3.0 3.1 3.2'.split():
        run_for_python(version, result, sys.argv[1:])