Skip to content

Commit 7f70c0b

Browse files
committed
Added parse_args and check_impl_args functions to the migration module as well as some unittests.
1 parent cdb558e commit 7f70c0b

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

utils/build_swift/migration.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,71 @@
1212
"""
1313

1414

15+
import os
16+
import sys
17+
1518
from swift_build_support.swift_build_support.targets import \
1619
StdlibDeploymentTarget
1720

21+
from . import shell
22+
1823

1924
__all__ = [
25+
'BUILD_SCRIPT_IMPL_PATH',
26+
'parse_args',
27+
'check_impl_args',
28+
2029
'UnknownSDKError',
2130
'migrate_swift_sdks',
2231
]
2332

2433

34+
BUILD_SCRIPT_IMPL_PATH = os.path.abspath(os.path.join(
35+
os.path.dirname(__file__),
36+
os.pardir,
37+
'build-script-impl',
38+
))
39+
40+
41+
def parse_args(parser, argv=None):
42+
"""Parse arguments list with given argparse.ArgumentParser.argv
43+
44+
Returns a preprocessed list of arguments. Unknown arguments are stored on
45+
the resulting namespace in the attribute `build_script_impl_args`. The
46+
deprecated '--' argument separator is removed from the parsed argument
47+
list.
48+
"""
49+
50+
if argv is None:
51+
argv = sys.argv
52+
53+
# Remove the '--' separator, which is no longer needed
54+
argv = [arg for arg in argv if arg != '--']
55+
56+
args, unknown_args = parser.parse_known_args(argv)
57+
args.build_script_impl_args = unknown_args
58+
59+
return args
60+
61+
62+
def check_impl_args(args, command_executor=None):
63+
"""Verifies that args are kown `build-script-impl` arguments. Raises a
64+
ValueError if an invalid argument is encountered.
65+
"""
66+
67+
sh = command_executor or shell.CommandExecutor()
68+
69+
command = [BUILD_SCRIPT_IMPL_PATH, '--check-args-only=1'] + args
70+
pipe = sh.popen(command, stdin=shell.PIPE, stderr=shell.PIPE)
71+
_, err = pipe.communicate()
72+
73+
if pipe.returncode != 0:
74+
message = err.splitlines()[0].strip()
75+
raise ValueError(message)
76+
77+
78+
# -----------------------------------------------------------------------------
79+
2580
_SDK_TARGETS = {
2681
'OSX': StdlibDeploymentTarget.OSX.targets,
2782
'IOS': StdlibDeploymentTarget.iOS.targets,
@@ -33,8 +88,6 @@
3388
}
3489

3590

36-
# -----------------------------------------------------------------------------
37-
3891
class UnknownSDKError(Exception):
3992
"""Error indicating an unknown SDK was encountered when migrating to target
4093
triples.

utils/build_swift/tests/test_migration.py

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

1010
from .utils import TestCase
1111
from .. import migration
12+
from .. import shell
1213

1314

1415
def _get_sdk_targets(sdk_names):
@@ -25,7 +26,7 @@ def _get_sdk_target_names(sdk_names):
2526

2627
# -----------------------------------------------------------------------------
2728

28-
class TestMigrateSwiftSDKsMeta(type):
29+
class TestMigrationMeta(type):
2930
"""Metaclass used to dynamically generate test methods.
3031
"""
3132

@@ -35,7 +36,7 @@ def __new__(cls, name, bases, attrs):
3536
test_name = 'test_migrate_swift_sdk_' + sdk_name
3637
attrs[test_name] = cls.generate_migrate_swift_sdks_test(sdk_name)
3738

38-
return super(TestMigrateSwiftSDKsMeta, cls).__new__(
39+
return super(TestMigrationMeta, cls).__new__(
3940
cls, name, bases, attrs)
4041

4142
@classmethod
@@ -52,11 +53,29 @@ def test(self):
5253
return test
5354

5455

55-
class TestMigrateSwiftSDKs(TestCase):
56+
class TestMigration(TestCase):
5657

57-
__metaclass__ = TestMigrateSwiftSDKsMeta
58+
__metaclass__ = TestMigrationMeta
5859

59-
def test_multiple_swift_sdk_flags(self):
60+
def test_parse_args(self):
61+
pass
62+
63+
def test_check_impl_args_calls_build_script_impl(self):
64+
sh = shell.NullExecutor()
65+
66+
migration.check_impl_args([], command_executor=sh)
67+
self.assertListEqual(sh.history(), [
68+
[migration.BUILD_SCRIPT_IMPL_PATH, '--check-args-only=1'],
69+
])
70+
71+
def test_check_impl_args_known_arg(self):
72+
migration.check_impl_args(['--reconfigure'])
73+
74+
def test_check_impl_args_unknown_arg(self):
75+
with self.assertRaises(ValueError):
76+
migration.check_impl_args(['--unknown-arg'])
77+
78+
def test_migrate_multiple_swift_sdk_flags(self):
6079
args = [
6180
'--swift-sdks=OSX',
6281
'--swift-sdks=OSX;IOS;IOS_SIMULATOR'

0 commit comments

Comments
 (0)