testtools: Update to latest version.
[metze/samba/wip.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, tests):
33     if not tests:
34         tests = ['testtools.tests.test_suite']
35     # XXX: This could probably be broken up and put into subunit.
36     python = 'python%s' % (version,)
37     # XXX: Correct API, but subunit doesn't support it. :(
38     # result.tags(set(python), set())
39     result.time(now())
40     test = PlaceHolder(''.join(c for c in python if c != '.'))
41     process = subprocess.Popen(
42         '%s -c pass' % (python,), shell=True,
43         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
44     process.communicate()
45
46     if process.returncode:
47         result.startTest(test)
48         result.addSkip(test, reason='%s not available' % (python,))
49         result.stopTest(test)
50         return
51
52     env = os.environ.copy()
53     if env.get('PYTHONPATH', None):
54         env['PYTHONPATH'] = os.pathsep.join([ROOT, env['PYTHONPATH']])
55     else:
56         env['PYTHONPATH'] = ROOT
57     result.time(now())
58     protocol = TestProtocolServer(result)
59     subunit_path = os.path.join(os.path.dirname(subunit.__file__), 'run.py')
60     cmd = [
61         python,
62         '-W', 'ignore:Module testtools was already imported',
63         subunit_path]
64     cmd.extend(tests)
65     process = subprocess.Popen(
66         cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
67     _make_stream_binary(process.stdout)
68     _make_stream_binary(process.stderr)
69     # XXX: This buffers everything. Bad for memory, bad for getting progress
70     # on jenkins.
71     output, error = process.communicate()
72     protocol.readFrom(BytesIO(output))
73     if error:
74         result.startTest(test)
75         result.addError(test, details={
76             'stderr': text_content(error),
77            })
78         result.stopTest(test)
79     result.time(now())
80     # XXX: Correct API, but subunit doesn't support it. :(
81     #result.tags(set(), set(python))
82
83
84 def now():
85     return datetime.utcnow().replace(tzinfo=iso8601.Utc())
86
87
88
89 if __name__ == '__main__':
90     sys.path.append(ROOT)
91     result = TestProtocolClient(sys.stdout)
92     for version in '2.6 2.7 3.0 3.1 3.2'.split():
93         run_for_python(version, result, sys.argv[1:])