Skip to content

[Build System: Python] Test restructure #29306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions utils/build-presets.ini
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,6 @@ build-subdir=buildbot_incremental
release
assertions

build-swift-stdlib-unittest-extra

# We run the OS X tests and validation tests.
test
validation-test
Expand Down
2 changes: 1 addition & 1 deletion utils/build_swift/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ the Swift build-script.
You may run the unit test suite using the command:

```sh
$ python -m unittest discover -s utils/build_swift/ -t utils/
$ python utils/build_swift/run_tests.py
```
4 changes: 3 additions & 1 deletion utils/build_swift/build_swift/argparse/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import re
import shlex

import six

from . import ArgumentTypeError
from ..versions import Version

Expand All @@ -40,7 +42,7 @@ def _repr(cls, args):
"""

_args = []
for key, value in args.viewitems():
for key, value in six.iteritems(args):
_args.append('{}={}'.format(key, repr(value)))

return '{}({})'.format(type(cls).__name__, ', '.join(_args))
Expand Down
3 changes: 2 additions & 1 deletion utils/build_swift/build_swift/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import itertools
import subprocess

import six
from six.moves import map

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

if pipe.returncode != 0:
raise ValueError(unicode(err.splitlines()[0].decode()))
raise ValueError(six.text_type(err.splitlines()[0].decode()))
91 changes: 91 additions & 0 deletions utils/build_swift/run_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python

# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors


"""
Utility script used to easily run the build_swift module unit tests.
"""


from __future__ import absolute_import, unicode_literals

import argparse
import os
import sys
import unittest


MODULE_DIR = os.path.abspath(os.path.dirname(__file__))
UTILS_DIR = os.path.abspath(os.path.join(MODULE_DIR, os.pardir))


def parse_args():
parser = argparse.ArgumentParser(
description='Utility script used to run the build_swift module unit '
'test suite.')

parser.set_defaults(verbosity=1)

parser.add_argument('-v', '--verbose',
dest='verbosity',
action='store_const',
const=2,
help='Verbose output')
parser.add_argument('-q', '--quiet',
dest='verbosity',
action='store_const',
const=0,
help='Minimal output')

parser.add_argument('-f', '--failfast',
action='store_true',
help='Stop on first failure')
parser.add_argument('-c', '--catch',
action='store_true',
help='Catch control-C and display results')
parser.add_argument('-b', '--buffer',
action='store_true',
help='Buffer stdout and stderr during test runs')

parser.add_argument('-p', '--pattern',
default='test*.py',
help='Pattern to match tests ("%(default)s" default)')

return parser.parse_args()


def main():
args = parse_args()

if args.catch:
unittest.installHandler()

runner = unittest.TextTestRunner(
verbosity=args.verbosity,
failfast=args.failfast,
buffer=args.buffer)

# Add the swift/utils directory to the Python path.
sys.path.append(UTILS_DIR)

# Discover all tests for the module.
module_tests = unittest.defaultTestLoader.discover(
MODULE_DIR, pattern=args.pattern)

# Create and run test suite.
suite = unittest.TestSuite()
suite.addTests(module_tests)
result = runner.run(suite)

return not result.wasSuccessful()


if __name__ == '__main__':
sys.exit(main())
7 changes: 7 additions & 0 deletions utils/build_swift/tests/build_swift/argparse/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@

from __future__ import absolute_import, unicode_literals

import six
import unittest

from ..utils import TestCase, redirect_stderr
from ...build_swift.argparse import (
from build_swift.argparse import (
ArgumentParser, BoolType, Nargs, PathType, SUPPRESS, actions)

import six

from ... import utils


# -----------------------------------------------------------------------------

class TestAction(TestCase):
class TestAction(unittest.TestCase):

def test_default_attributes(self):
action = actions.Action(['--foo'], dests=['foo'])
Expand Down Expand Up @@ -56,7 +59,7 @@ def test_call_not_implemented(self):
action(None, None, None, None)


class TestAppendAction(TestCase):
class TestAppendAction(unittest.TestCase):

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


class TestCustomCallAction(TestCase):
class TestCustomCallAction(unittest.TestCase):

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


class TestStoreAction(TestCase):
class TestStoreAction(unittest.TestCase):

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

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

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

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


class TestStoreIntAction(TestCase):
class TestStoreIntAction(unittest.TestCase):

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

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


class TestStoreTrueAction(TestCase):
class TestStoreTrueAction(unittest.TestCase):

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


class TestStoreFalseAction(TestCase):
class TestStoreFalseAction(unittest.TestCase):

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


class TestStorePathAction(TestCase):
class TestStorePathAction(unittest.TestCase):

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


class TestToggleTrueAction(TestCase):
class TestToggleTrueAction(unittest.TestCase):

def test_default_attributes(self):
action = actions.ToggleTrueAction(['--foo'], dests=['foo'])
Expand Down Expand Up @@ -329,7 +332,7 @@ def test_last_wins(self):
self.assertTrue(args.foo)


class TestToggleFalseAction(TestCase):
class TestToggleFalseAction(unittest.TestCase):

def test_default_attributes(self):
action = actions.ToggleFalseAction(['--foo'], dests=['foo'])
Expand Down Expand Up @@ -391,7 +394,7 @@ def test_last_wins(self):
self.assertFalse(args.foo)


class TestUnuspportedAction(TestCase):
class TestUnuspportedAction(unittest.TestCase):

def test_default_attributes(self):
action = actions.UnsupportedAction(['--foo'])
Expand All @@ -413,7 +416,7 @@ def test_raises_parser_error(self):
parser = ArgumentParser()
parser.add_argument('--foo', action=actions.UnsupportedAction)

with self.quietOutput(), self.assertRaises(SystemExit):
with utils.quiet_output(), self.assertRaises(SystemExit):
parser.parse_args(['--foo'])

def test_custom_error_message(self):
Expand All @@ -427,7 +430,7 @@ def test_custom_error_message(self):

self.assertEqual(action.message, message)

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

self.assertIn(message, stderr)
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@

from __future__ import absolute_import, unicode_literals

import unittest
from argparse import _ArgumentGroup, _MutuallyExclusiveGroup

from ..utils import TestCase
from ...build_swift.argparse import ArgumentParser, actions
from build_swift.argparse import ArgumentParser, actions

from ... import utils


# -----------------------------------------------------------------------------

class TestBuilder(TestCase):
class TestBuilder(unittest.TestCase):

def test_build(self):
builder = ArgumentParser.builder()
Expand Down Expand Up @@ -79,7 +81,7 @@ def test_add_option(self):
args = parser.parse_args(['--baz', '--baz=FALSE'])
self.assertTrue(args.baz)

with self.quietOutput(), self.assertRaises(SystemExit):
with utils.quiet_output(), self.assertRaises(SystemExit):
parser.parse_args(['--qux'])

def test_set_defaults(self):
Expand Down Expand Up @@ -132,7 +134,7 @@ def test_mutually_exclusive_group(self):
self.assertEqual(builder._current_group, builder._parser)


class TestArgumentParser(TestCase):
class TestArgumentParser(unittest.TestCase):

def test_builder(self):
builder = ArgumentParser.builder(usage='Totally useless help message')
Expand Down
Loading