bugfix: reorder variants in Spec strings (#16462)

* change print order for variants to avoid zsh parsing bugs

* change tests for new variant parse order
This commit is contained in:
Greg Becker 2020-05-12 10:23:42 -07:00 committed by Gregory Becker
parent 473424ad60
commit dc59fc7ab8
3 changed files with 17 additions and 18 deletions

View File

@ -177,7 +177,7 @@ def test_full_specs(self):
" ^stackwalker@8.1_1e")
self.check_parse(
"mvapich_foo"
" ^_openmpi@1.2:1.4,1.6%intel@12.1 debug=2 ~qt_4"
" ^_openmpi@1.2:1.4,1.6%intel@12.1~qt_4 debug=2"
" ^stackwalker@8.1_1e")
self.check_parse(
'mvapich_foo'
@ -185,7 +185,7 @@ def test_full_specs(self):
' ^stackwalker@8.1_1e')
self.check_parse(
"mvapich_foo"
" ^_openmpi@1.2:1.4,1.6%intel@12.1 debug=2 ~qt_4"
" ^_openmpi@1.2:1.4,1.6%intel@12.1~qt_4 debug=2"
" ^stackwalker@8.1_1e arch=test-redhat6-x86")
def test_canonicalize(self):

View File

@ -694,7 +694,7 @@ def test_str(self):
c['foobar'] = SingleValuedVariant('foobar', 'fee')
c['feebar'] = SingleValuedVariant('feebar', 'foo')
c['shared'] = BoolValuedVariant('shared', True)
assert str(c) == ' feebar=foo foo=bar,baz foobar=fee +shared'
assert str(c) == '+shared feebar=foo foo=bar,baz foobar=fee'
def test_disjoint_set_initialization_errors():

View File

@ -567,25 +567,24 @@ def __str__(self):
# print keys in order
sorted_keys = sorted(self.keys())
# Separate boolean variants from key-value pairs as they print
# differently. All booleans go first to avoid ' ~foo' strings that
# break spec reuse in zsh.
bool_keys = []
kv_keys = []
for key in sorted_keys:
bool_keys.append(key) if isinstance(self[key].value, bool) \
else kv_keys.append(key)
# add spaces before and after key/value variants.
string = StringIO()
kv = False
for key in sorted_keys:
vspec = self[key]
for key in bool_keys:
string.write(str(self[key]))
if not isinstance(vspec.value, bool):
# add space before all kv pairs.
for key in kv_keys:
string.write(' ')
kv = True
else:
# not a kv pair this time
if kv:
# if it was LAST time, then pad after.
string.write(' ')
kv = False
string.write(str(vspec))
string.write(str(self[key]))
return string.getvalue()