Skip to content

Commit 1749f98

Browse files
committed
Implemented a (mostly) comprehensive test suite for the argument parser (ab)using metaclasses to dynamically generate unit-tests for each valid argument and preset.
1 parent 9847f6a commit 1749f98

File tree

6 files changed

+1076
-6
lines changed

6 files changed

+1076
-6
lines changed

utils/build-script

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ class BuildScriptInvocation(object):
245245

246246
@staticmethod
247247
def apply_default_arguments(toolchain, args):
248-
driver_arguments.apply_default_arguments(args)
249-
250248
# infer if ninja is required
251249
ninja_required = (
252250
args.cmake_generator == 'Ninja' or args.build_foundation)

utils/build_swift/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# build_swift
2+
3+
The `build_swift` module contains data-structures and functions used by
4+
the Swift build-script.
5+
6+
## Unit Tests
7+
8+
You may run the unit test suite using the command:
9+
10+
```sh
11+
$ python -m unittest discover -s utils/build_swift
12+
```

utils/build_swift/driver_arguments.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,31 @@
2424

2525

2626
__all__ = [
27-
'apply_default_arguments',
2827
'create_argument_parser',
2928
]
3029

3130

32-
def apply_default_arguments(args):
33-
"""Preprocess argument namespace to apply default behaviors."""
31+
class _ApplyDefaultsArgumentParser(argparse.ArgumentParser):
32+
"""Wrapper class around the default ArgumentParser that allows for
33+
post-processing the parsed argument namespace to apply default argument
34+
transformations.
35+
"""
36+
37+
def __init__(self, apply_defaults=None, *args, **kwargs):
38+
self._apply_defaults = apply_defaults
39+
super(_ApplyDefaultsArgumentParser, self).__init__(*args, **kwargs)
40+
41+
def parse_known_args(self, args=None, namespace=None):
42+
args, argv = super(_ApplyDefaultsArgumentParser, self)\
43+
.parse_known_args(args, namespace)
44+
45+
self._apply_defaults(args)
46+
return args, argv
47+
48+
49+
def _apply_default_arguments(args):
50+
"""Preprocess argument namespace to apply default behaviors.
51+
"""
3452

3553
# Build cmark if any cmark-related options were specified.
3654
if (args.cmark_build_variant is not None):
@@ -245,7 +263,10 @@ def apply_default_arguments(args):
245263

246264

247265
def create_argument_parser():
248-
parser = argparse.ArgumentParser(
266+
"""Return a configured argument parser."""
267+
268+
parser = _ApplyDefaultsArgumentParser(
269+
apply_defaults=_apply_default_arguments,
249270
formatter_class=argparse.RawDescriptionHelpFormatter,
250271
usage=USAGE,
251272
description=DESCRIPTION,
@@ -1111,6 +1132,8 @@ def create_argument_parser():
11111132
return parser
11121133

11131134

1135+
# ----------------------------------------------------------------------------
1136+
11141137
USAGE = """
11151138
%(prog)s [-h | --help] [OPTION ...]
11161139
%(prog)s --preset=NAME [SUBSTITUTION ...]

utils/build_swift/tests/__init__.py

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 - 2017 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

0 commit comments

Comments
 (0)