Skip to content

Commit 009b6aa

Browse files
committed
Switched the expansion algorithm to resolve mixins in-place (little known feature) and also changed the parser to skip all non-preset sections. Tests are included for these two behaviors.
1 parent 4342207 commit 009b6aa

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

utils/build_swift/presets.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414

1515
from collections import namedtuple
16-
from copy import copy
1716

1817
try:
1918
# Python 3
@@ -37,7 +36,9 @@
3736

3837
# -----------------------------------------------------------------------------
3938

40-
_RawPreset = namedtuple('_RawPreset', ['name', 'mixins', 'args'])
39+
_Mixin = namedtuple('_Mixin', ['name'])
40+
_Argument = namedtuple('_Argument', ['name', 'value'])
41+
_RawPreset = namedtuple('_RawPreset', ['name', 'options'])
4142

4243

4344
def _interpolate_string(string, values):
@@ -128,25 +129,29 @@ def _parse_raw_preset(self, section):
128129
except configparser.InterpolationMissingOptionError as e:
129130
raise MissingOptionError(e)
130131

131-
args, mixins = [], []
132+
args = []
132133
for (name, value) in section_items:
133134
# Ignore the '--' separator, it's no longer necessary
134135
if name == 'dash-dash':
135136
continue
136137

137138
# Parse out mixin names
138139
if name == 'mixin-preset':
139-
mixins = list(map(lambda name: name.strip(),
140-
value.strip().splitlines()))
140+
lines = value.strip().splitlines()
141+
args += [_Mixin(name.strip()) for name in lines]
141142
continue
142143

143144
name = '--' + name # Format as an option name
144-
args.append((name, value))
145+
args.append(_Argument(name, value))
145146

146-
return _RawPreset(preset_name, mixins, args)
147+
return _RawPreset(preset_name, args)
147148

148149
def _parse_raw_presets(self):
149150
for section in self._parser.sections():
151+
# Skip all non-preset sections
152+
if not section.startswith(PresetParser._PRESET_PREFIX):
153+
continue
154+
150155
raw_preset = self._parse_raw_preset(section)
151156
self._presets[raw_preset.name] = raw_preset
152157

@@ -203,11 +208,14 @@ def _resolve_preset_mixins(self, raw_preset):
203208
assert isinstance(raw_preset, _RawPreset)
204209

205210
# Expand mixin arguments
206-
args = copy(raw_preset.args)
207-
for mixin_name in raw_preset.mixins:
208-
mixin = self._get_preset(mixin_name)
209-
210-
args += mixin.args
211+
args = []
212+
for option in raw_preset.options:
213+
if isinstance(option, _Mixin):
214+
args += self._get_preset(option.name).args
215+
elif isinstance(option, _Argument):
216+
args.append((option.name, option.value))
217+
else:
218+
raise ValueError('invalid argument type: {}', option.__class__)
211219

212220
return Preset(raw_preset.name, args)
213221

utils/build_swift/tests/test_presets.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@
7070
ios
7171
"""
7272

73+
IGNORED_SECTION = """
74+
[section_name]
75+
76+
random-options=1
77+
"""
78+
79+
MIXIN_ORDER_PRESETS = """
80+
[preset: test_mixin]
81+
first-opt=0
82+
second-opt=1
83+
84+
85+
[preset: test]
86+
first-opt=1
87+
mixin-preset=test_mixin
88+
second-opt=2
89+
"""
90+
7391

7492
# -----------------------------------------------------------------------------
7593

@@ -154,6 +172,31 @@ def test_read_string(self):
154172
(u'--install-symroot', u'/tmp')
155173
])
156174

175+
def test_parser_ignores_non_preset_sections(self):
176+
parser = PresetParser()
177+
178+
parser.read_string(IGNORED_SECTION)
179+
self.assertEqual(len(parser._presets), 0)
180+
181+
def test_mixin_expansion_preserves_argument_order(self):
182+
"""Mixins should be expanded in-place.
183+
"""
184+
185+
parser = PresetParser()
186+
187+
parser.read_string(MIXIN_ORDER_PRESETS)
188+
189+
preset = parser.get_preset('test')
190+
self.assertListEqual(preset.format_args(), [
191+
'--first-opt=1',
192+
193+
# Mixin arguments
194+
'--first-opt=0',
195+
'--second-opt=1',
196+
197+
'--second-opt=2',
198+
])
199+
157200
def test_duplicate_option_error(self):
158201
parser = PresetParser()
159202

0 commit comments

Comments
 (0)