Skip to content

Commit 5959dd2

Browse files
committed
[build-script] Reject user-supplied '--common-cmake-options' argument
build-script doesn't support manually supplied `--common-cmake-options`. Introduced argparse action 'arguments.action.unavailable'
1 parent eaa3201 commit 5959dd2

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

utils/build-script

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,12 @@ details of the setups of other systems or automated environments.""")
962962
default=False,
963963
const=True)
964964

965+
parser.add_argument(
966+
# Explicitly unavailable options here.
967+
"--build-jobs",
968+
"--common-cmake-options",
969+
action=arguments.action.unavailable)
970+
965971
args = migration.parse_args(parser, sys.argv[1:])
966972

967973
build_script_impl = os.path.join(

utils/swift_build_support/swift_build_support/arguments.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
import shlex
2323

2424
__all__ = [
25-
"type"
25+
"action",
26+
"type",
2627
]
2728

2829

@@ -34,6 +35,7 @@ def _register(registry, name, value):
3435
setattr(registry, name, value)
3536

3637

38+
# Types ----------------------------------------------------------------------
3739
type = _Registry()
3840

3941

@@ -99,3 +101,30 @@ def type_executable(string):
99101
"%r is not executable" % string)
100102

101103
_register(type, 'executable', type_executable)
104+
105+
# Actions --------------------------------------------------------------------
106+
action = _Registry()
107+
108+
109+
class _UnavailableAction(argparse.Action):
110+
def __init__(self,
111+
option_strings,
112+
dest=argparse.SUPPRESS,
113+
default=argparse.SUPPRESS,
114+
nargs='?',
115+
help=None):
116+
super(_UnavailableAction, self).__init__(
117+
option_strings=option_strings,
118+
dest=dest,
119+
default=default,
120+
nargs=nargs,
121+
help=help)
122+
123+
def __call__(self, parser, namespace, values, option_string=None):
124+
if option_string is not None:
125+
arg = option_string
126+
else:
127+
arg = str(values)
128+
parser.error('unknown argument: %s' % arg)
129+
130+
_register(action, 'unavailable', _UnavailableAction)

utils/swift_build_support/tests/test_arguments.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@
1414
import os
1515
import sys
1616
import unittest
17+
try:
18+
# py2
19+
from StringIO import StringIO
20+
except ImportError:
21+
# py3
22+
from io import StringIO
1723

18-
from swift_build_support.arguments import type as argtype
24+
from swift_build_support.arguments import (
25+
type as argtype,
26+
action as argaction,
27+
)
1928

2029

2130
class ArgumentsTypeTestCase(unittest.TestCase):
@@ -87,3 +96,41 @@ def test_executable(self):
8796
self.assertRaises(
8897
argparse.ArgumentTypeError,
8998
argtype.executable, "../example-command-not-exist")
99+
100+
101+
class ArgumentsActionTestCase(unittest.TestCase):
102+
103+
def test_unavailable(self):
104+
orig_stderr = sys.stderr
105+
106+
parser = argparse.ArgumentParser()
107+
parser.add_argument("--foo")
108+
parser.add_argument(
109+
"--do-not-use",
110+
"--never-ever",
111+
action=argaction.unavailable)
112+
113+
args, unknown_args = parser.parse_known_args(
114+
['--foo', 'bar', '--baz', 'qux'])
115+
116+
self.assertEqual(args.foo, 'bar')
117+
self.assertEqual(unknown_args, ['--baz', 'qux'])
118+
self.assertFalse(hasattr(args, 'sentinel'))
119+
120+
stderr = StringIO()
121+
sys.stderr = stderr
122+
self.assertRaises(
123+
SystemExit,
124+
parser.parse_known_args,
125+
['--foo', 'bar', '--do-not-use', 'baz'])
126+
self.assertIn('--do-not-use', stderr.getvalue())
127+
128+
stderr = StringIO()
129+
sys.stderr = stderr
130+
self.assertRaises(
131+
SystemExit,
132+
parser.parse_known_args,
133+
['--foo', 'bar', '--never-ever=baz'])
134+
self.assertIn('--never-ever', stderr.getvalue())
135+
136+
sys.stderr = orig_stderr

0 commit comments

Comments
 (0)