Files
swift-mirror/utils/build_swift/tests/utils.py
Ross Bayer 85abdcd62d Argparse "Overlay" Module (#12873)
* Implemented a wrapper module around the standard argparse package, exposing the same interface with some extras on top, including a new builder type with expressive DSL for constructing complex argument parsers.

* Fixed imports in build_swift/argparse/__init__.py to make flake8 happy.

* More re-formmating to meet the exacting standards of the python_lint script.

* Added doc-strings to all the modules in the build_swift argparse overlay.

* Implemented a new BoolType for the argparse module which handles boolean-like values and replaces the hard-coded boolean values in the _ToggleAction class.

* Fixed the mess of imports in the tests sub-package to favor relative imports, so now the unit-tests will actually run as expected. The README has also been updated with a better command for executing the unit-test suite.

* Updated the add_positional method on the ArgumentParser builder class to only take a single action or default to the store action.

* Cleaned up the set_defaults method.

* Added validation test to run the build_swift unit-tests.

* Updated validation-test for the build_swift unit-test suite to use %utils.

* Fixed hard-coded default values in the expected_options module used for generating argument parser tests.

* Updated the comment in the Python validation test to run the build_swift unit-tests.
2017-11-27 21:49:44 -08:00

69 lines
1.7 KiB
Python

# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2017 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
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import os
import sys
import unittest
from contextlib import contextmanager
__all__ = [
'redirect_stderr',
'redirect_stdout',
'TestCase',
]
# -----------------------------------------------------------------------------
@contextmanager
def redirect_stderr(stream=None):
stream = stream or StringIO()
old_stderr, sys.stderr = sys.stderr, stream
try:
yield stream
finally:
sys.stderr = old_stderr
@contextmanager
def redirect_stdout(stream=None):
stream = stream or StringIO()
old_stdout, sys.stdout = sys.stdout, stream
try:
yield stream
finally:
sys.stdout = old_stdout
# -----------------------------------------------------------------------------
class TestCase(unittest.TestCase):
@contextmanager
def quietOutput(self):
with open(os.devnull, 'w') as devnull:
with redirect_stderr(devnull), redirect_stdout(devnull):
yield
@contextmanager
def assertNotRaises(self, exception=BaseException):
assert issubclass(exception, BaseException)
try:
yield
except exception as e:
message = '{} raised: {}'.format(exception.__name__, str(e))
raise self.failureException(message)