aecc9495a61d58a811b84f58d9d746de8b10753f
[nivanova/samba-autobuild/.git] / lib / testtools / scripts / all-pythons
1 #!/usr/bin/python
2
3 """Run the testtools test suite for all supported Pythons.
4
5 Prints output as a subunit test suite. If anything goes to stderr, that is
6 treated as a test error. If a Python is not available, then it is skipped.
7 """
8
9 from datetime import datetime
10 import os
11 import subprocess
12 import sys
13
14 import subunit
15 from subunit import (
16     iso8601,
17     _make_stream_binary,
18     TestProtocolClient,
19     TestProtocolServer,
20     )
21 from testtools import (
22     PlaceHolder,
23     TestCase,
24     )
25 from testtools.compat import BytesIO
26 from testtools.content import text_content
27
28
29 ROOT = os.path.dirname(os.path.dirname(__file__))
30
31
32 def run_for_python(version, result):
33     # XXX: This could probably be broken up and put into subunit.
34     python = 'python%s' % (version,)
35     # XXX: Correct API, but subunit doesn't support it. :(
36     # result.tags(set(python), set())
37     result.time(now())
38     test = PlaceHolder(''.join(c for c in python if c != '.'))
39     process = subprocess.Popen(
40         '%s -c pass' % (python,), shell=True,
41         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
42     process.communicate()
43
44     if process.returncode:
45         result.startTest(test)
46         result.addSkip(test, reason='%s not available' % (python,))
47         result.stopTest(test)
48         return
49
50     env = os.environ.copy()
51     if env.get('PYTHONPATH', None):
52         env['PYTHONPATH'] = os.pathsep.join([ROOT, env['PYTHONPATH']])
53     else:
54         env['PYTHONPATH'] = ROOT
55     result.time(now())
56     protocol = TestProtocolServer(result)
57     subunit_path = os.path.join(os.path.dirname(subunit.__file__), 'run.py')
58     cmd = [
59         python,
60         '-W', 'ignore:Module testtools was already imported',
61         subunit_path, 'testtools.tests.test_suite']
62     process = subprocess.Popen(
63         cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
64     _make_stream_binary(process.stdout)
65     _make_stream_binary(process.stderr)
66     # XXX: This buffers everything. Bad for memory, bad for getting progress
67     # on jenkins.
68     output, error = process.communicate()
69     protocol.readFrom(BytesIO(output))
70     if error:
71         result.startTest(test)
72         result.addError(test, details={
73             'stderr': text_content(error),
74            })
75         result.stopTest(test)
76     result.time(now())
77     # XXX: Correct API, but subunit doesn't support it. :(
78     #result.tags(set(), set(python))
79
80
81 def now():
82     return datetime.utcnow().replace(tzinfo=iso8601.Utc())
83
84
85
86 if __name__ == '__main__':
87     sys.path.append(ROOT)
88     result = TestProtocolClient(sys.stdout)
89     for version in '2.4 2.5 2.6 2.7 3.0 3.1 3.2'.split():
90         run_for_python(version, result)