Skip to content

Commit f8f3324

Browse files
authored
Merge pull request swiftlang#29306 from Rostepher/python-test-restructure
[Build System: Python] Test restructure
2 parents 63afeb1 + 68bf968 commit f8f3324

28 files changed

+482
-249
lines changed

utils/build-presets.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,6 @@ build-subdir=buildbot_incremental
533533
release
534534
assertions
535535

536-
build-swift-stdlib-unittest-extra
537-
538536
# We run the OS X tests and validation tests.
539537
test
540538
validation-test

utils/build_swift/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ the Swift build-script.
88
You may run the unit test suite using the command:
99

1010
```sh
11-
$ python -m unittest discover -s utils/build_swift/ -t utils/
11+
$ python utils/build_swift/run_tests.py
1212
```

utils/build_swift/build_swift/argparse/types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import re
2020
import shlex
2121

22+
import six
23+
2224
from . import ArgumentTypeError
2325
from ..versions import Version
2426

@@ -40,7 +42,7 @@ def _repr(cls, args):
4042
"""
4143

4244
_args = []
43-
for key, value in args.viewitems():
45+
for key, value in six.iteritems(args):
4446
_args.append('{}={}'.format(key, repr(value)))
4547

4648
return '{}({})'.format(type(cls).__name__, ', '.join(_args))

utils/build_swift/build_swift/migration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import itertools
1818
import subprocess
1919

20+
import six
2021
from six.moves import map
2122

2223
from swift_build_support.swift_build_support.targets import \
@@ -147,4 +148,4 @@ def check_impl_args(build_script_impl, args):
147148
_, err = pipe.communicate()
148149

149150
if pipe.returncode != 0:
150-
raise ValueError(unicode(err.splitlines()[0].decode()))
151+
raise ValueError(six.text_type(err.splitlines()[0].decode()))

utils/build_swift/run_tests.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python
2+
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See https://swift.org/LICENSE.txt for license information
9+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
11+
12+
"""
13+
Utility script used to easily run the build_swift module unit tests.
14+
"""
15+
16+
17+
from __future__ import absolute_import, unicode_literals
18+
19+
import argparse
20+
import os
21+
import sys
22+
import unittest
23+
24+
25+
MODULE_DIR = os.path.abspath(os.path.dirname(__file__))
26+
UTILS_DIR = os.path.abspath(os.path.join(MODULE_DIR, os.pardir))
27+
28+
29+
def parse_args():
30+
parser = argparse.ArgumentParser(
31+
description='Utility script used to run the build_swift module unit '
32+
'test suite.')
33+
34+
parser.set_defaults(verbosity=1)
35+
36+
parser.add_argument('-v', '--verbose',
37+
dest='verbosity',
38+
action='store_const',
39+
const=2,
40+
help='Verbose output')
41+
parser.add_argument('-q', '--quiet',
42+
dest='verbosity',
43+
action='store_const',
44+
const=0,
45+
help='Minimal output')
46+
47+
parser.add_argument('-f', '--failfast',
48+
action='store_true',
49+
help='Stop on first failure')
50+
parser.add_argument('-c', '--catch',
51+
action='store_true',
52+
help='Catch control-C and display results')
53+
parser.add_argument('-b', '--buffer',
54+
action='store_true',
55+
help='Buffer stdout and stderr during test runs')
56+
57+
parser.add_argument('-p', '--pattern',
58+
default='test*.py',
59+
help='Pattern to match tests ("%(default)s" default)')
60+
61+
return parser.parse_args()
62+
63+
64+
def main():
65+
args = parse_args()
66+
67+
if args.catch:
68+
unittest.installHandler()
69+
70+
runner = unittest.TextTestRunner(
71+
verbosity=args.verbosity,
72+
failfast=args.failfast,
73+
buffer=args.buffer)
74+
75+
# Add the swift/utils directory to the Python path.
76+
sys.path.append(UTILS_DIR)
77+
78+
# Discover all tests for the module.
79+
module_tests = unittest.defaultTestLoader.discover(
80+
MODULE_DIR, pattern=args.pattern)
81+
82+
# Create and run test suite.
83+
suite = unittest.TestSuite()
84+
suite.addTests(module_tests)
85+
result = runner.run(suite)
86+
87+
return not result.wasSuccessful()
88+
89+
90+
if __name__ == '__main__':
91+
sys.exit(main())
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See https://swift.org/LICENSE.txt for license information
7+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

utils/build_swift/tests/argparse/test_actions.py renamed to utils/build_swift/tests/build_swift/argparse/test_actions.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99

1010
from __future__ import absolute_import, unicode_literals
1111

12-
import six
12+
import unittest
1313

14-
from ..utils import TestCase, redirect_stderr
15-
from ...build_swift.argparse import (
14+
from build_swift.argparse import (
1615
ArgumentParser, BoolType, Nargs, PathType, SUPPRESS, actions)
1716

17+
import six
18+
19+
from ... import utils
20+
1821

1922
# -----------------------------------------------------------------------------
2023

21-
class TestAction(TestCase):
24+
class TestAction(unittest.TestCase):
2225

2326
def test_default_attributes(self):
2427
action = actions.Action(['--foo'], dests=['foo'])
@@ -56,7 +59,7 @@ def test_call_not_implemented(self):
5659
action(None, None, None, None)
5760

5861

59-
class TestAppendAction(TestCase):
62+
class TestAppendAction(unittest.TestCase):
6063

6164
def test_default_attributes(self):
6265
action = actions.AppendAction(['--foo'], dests=['foo'])
@@ -79,7 +82,7 @@ def test_append(self):
7982
self.assertEqual(args.foo, ['bar', 'baz'])
8083

8184

82-
class TestCustomCallAction(TestCase):
85+
class TestCustomCallAction(unittest.TestCase):
8386

8487
def test_non_callable(self):
8588
with self.assertRaises(TypeError):
@@ -102,7 +105,7 @@ def test_func(action, parser, namespace, values, option_string=None):
102105
self.assertEqual(args.foo, 'boo')
103106

104107

105-
class TestStoreAction(TestCase):
108+
class TestStoreAction(unittest.TestCase):
106109

107110
def test_default_attributes(self):
108111
action = actions.StoreAction(['--foo'], dests=['foo'], choices=['bar'])
@@ -120,7 +123,7 @@ def test_choices(self):
120123

121124
self.assertEqual(action.nargs, Nargs.OPTIONAL)
122125

123-
with self.quietOutput(), self.assertRaises(SystemExit):
126+
with utils.quiet_output(), self.assertRaises(SystemExit):
124127
parser.parse_args(['--foo', 'qux'])
125128

126129
args = parser.parse_args(['--foo', 'bar'])
@@ -169,7 +172,7 @@ def test_store_const_multiple_destinations(self):
169172
self.assertEqual(args.bar, 'baz')
170173

171174

172-
class TestStoreIntAction(TestCase):
175+
class TestStoreIntAction(unittest.TestCase):
173176

174177
def test_default_attributes(self):
175178
action = actions.StoreIntAction(['--foo'], dests=['foo'])
@@ -191,11 +194,11 @@ def test_invalid_int(self):
191194
parser.add_argument('--foo', action=actions.StoreIntAction)
192195

193196
for i in [0.0, True, 'bar']:
194-
with self.quietOutput(), self.assertRaises(SystemExit):
197+
with utils.quiet_output(), self.assertRaises(SystemExit):
195198
parser.parse_args(['--foo', six.text_type(i)])
196199

197200

198-
class TestStoreTrueAction(TestCase):
201+
class TestStoreTrueAction(unittest.TestCase):
199202

200203
def test_default_attributes(self):
201204
action = actions.StoreTrueAction(['--foo'], dests=['foo'])
@@ -221,7 +224,7 @@ def test_store_true(self):
221224
self.assertTrue(args.foo)
222225

223226

224-
class TestStoreFalseAction(TestCase):
227+
class TestStoreFalseAction(unittest.TestCase):
225228

226229
def test_default_attributes(self):
227230
action = actions.StoreFalseAction(['--foo'], dests=['foo'])
@@ -247,7 +250,7 @@ def test_store_false(self):
247250
self.assertFalse(args.foo)
248251

249252

250-
class TestStorePathAction(TestCase):
253+
class TestStorePathAction(unittest.TestCase):
251254

252255
def test_default_attributes(self):
253256
action = actions.StorePathAction(['--foo'], dests=['foo'])
@@ -267,7 +270,7 @@ def test_executable(self):
267270
self.assertTrue(action.type._assert_executable)
268271

269272

270-
class TestToggleTrueAction(TestCase):
273+
class TestToggleTrueAction(unittest.TestCase):
271274

272275
def test_default_attributes(self):
273276
action = actions.ToggleTrueAction(['--foo'], dests=['foo'])
@@ -329,7 +332,7 @@ def test_last_wins(self):
329332
self.assertTrue(args.foo)
330333

331334

332-
class TestToggleFalseAction(TestCase):
335+
class TestToggleFalseAction(unittest.TestCase):
333336

334337
def test_default_attributes(self):
335338
action = actions.ToggleFalseAction(['--foo'], dests=['foo'])
@@ -391,7 +394,7 @@ def test_last_wins(self):
391394
self.assertFalse(args.foo)
392395

393396

394-
class TestUnuspportedAction(TestCase):
397+
class TestUnuspportedAction(unittest.TestCase):
395398

396399
def test_default_attributes(self):
397400
action = actions.UnsupportedAction(['--foo'])
@@ -413,7 +416,7 @@ def test_raises_parser_error(self):
413416
parser = ArgumentParser()
414417
parser.add_argument('--foo', action=actions.UnsupportedAction)
415418

416-
with self.quietOutput(), self.assertRaises(SystemExit):
419+
with utils.quiet_output(), self.assertRaises(SystemExit):
417420
parser.parse_args(['--foo'])
418421

419422
def test_custom_error_message(self):
@@ -427,7 +430,7 @@ def test_custom_error_message(self):
427430

428431
self.assertEqual(action.message, message)
429432

430-
with redirect_stderr() as stderr, self.assertRaises(SystemExit):
433+
with utils.redirect_stderr() as stderr, self.assertRaises(SystemExit):
431434
parser.parse_args(['--foo'])
432435

433436
self.assertIn(message, stderr)

utils/build_swift/tests/argparse/test_parser.py renamed to utils/build_swift/tests/build_swift/argparse/test_parser.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99

1010
from __future__ import absolute_import, unicode_literals
1111

12+
import unittest
1213
from argparse import _ArgumentGroup, _MutuallyExclusiveGroup
1314

14-
from ..utils import TestCase
15-
from ...build_swift.argparse import ArgumentParser, actions
15+
from build_swift.argparse import ArgumentParser, actions
16+
17+
from ... import utils
1618

1719

1820
# -----------------------------------------------------------------------------
1921

20-
class TestBuilder(TestCase):
22+
class TestBuilder(unittest.TestCase):
2123

2224
def test_build(self):
2325
builder = ArgumentParser.builder()
@@ -79,7 +81,7 @@ def test_add_option(self):
7981
args = parser.parse_args(['--baz', '--baz=FALSE'])
8082
self.assertTrue(args.baz)
8183

82-
with self.quietOutput(), self.assertRaises(SystemExit):
84+
with utils.quiet_output(), self.assertRaises(SystemExit):
8385
parser.parse_args(['--qux'])
8486

8587
def test_set_defaults(self):
@@ -132,7 +134,7 @@ def test_mutually_exclusive_group(self):
132134
self.assertEqual(builder._current_group, builder._parser)
133135

134136

135-
class TestArgumentParser(TestCase):
137+
class TestArgumentParser(unittest.TestCase):
136138

137139
def test_builder(self):
138140
builder = ArgumentParser.builder(usage='Totally useless help message')

0 commit comments

Comments
 (0)