Fix config.py output when a symbol has acquired or lost a value

Normally a valueless symbol remains valueless and a symbol with a
value keeps having one. But just in case a symbol does get changed
from valueless to having a value, make sure there's a space between
the symbol and the value. And if a symbol gets changed from having a
value to valueless, strip trailing whitespace.

Add corresponding tests.

Also fix the case of a valueless symbol added with the set method,
which would have resulted in attempting to use None as a string. This
only happened with the Python API, not with the command line API.
This commit is contained in:
Gilles Peskine 2019-09-04 22:51:47 +02:00
parent e3734bd13a
commit f686042554
2 changed files with 16 additions and 1 deletions

View File

@ -302,10 +302,22 @@ class ConfigFile(Config):
where <middle> is "#define <name> ". where <middle> is "#define <name> ".
""" """
setting = self.settings[name] setting = self.settings[name]
value = setting.value
if value is None:
value = ''
# Normally the whitespace to separte the symbol name from the
# value is part of middle, and there's no whitespace for a symbol
# with no value. But if a symbol has been changed from having a
# value to not having one, the whitespace is wrong, so fix it.
if value:
if middle[-1] not in '\t ':
middle += ' '
else:
middle = middle.rstrip()
return ''.join([indent, return ''.join([indent,
'' if setting.active else '//', '' if setting.active else '//',
middle, middle,
setting.value]).rstrip() value]).rstrip()
def write_to_stream(self, output): def write_to_stream(self, output):
"""Write the whole configuration to output.""" """Write the whole configuration to output."""

View File

@ -129,6 +129,7 @@ def run_one(options, args):
TEST_SYMBOLS = [ TEST_SYMBOLS = [
'CUSTOM_OPTION', 'CUSTOM_OPTION',
'MBEDTLS_AES_C', 'MBEDTLS_AES_C',
'MBEDTLS_MPI_MAX_SIZE',
'MBEDTLS_NO_UDBL_DIVISION', 'MBEDTLS_NO_UDBL_DIVISION',
'MBEDTLS_PLATFORM_ZEROIZE_ALT', 'MBEDTLS_PLATFORM_ZEROIZE_ALT',
] ]
@ -136,6 +137,7 @@ TEST_SYMBOLS = [
### A list of symbols to test with set with a value. ### A list of symbols to test with set with a value.
TEST_SYMBOLS_WITH_VALUE = [ TEST_SYMBOLS_WITH_VALUE = [
'CUSTOM_VALUE', 'CUSTOM_VALUE',
'MBEDTLS_AES_C',
'MBEDTLS_MPI_MAX_SIZE', 'MBEDTLS_MPI_MAX_SIZE',
] ]
@ -151,6 +153,7 @@ def run_all(options):
for symbol in TEST_SYMBOLS_WITH_VALUE: for symbol in TEST_SYMBOLS_WITH_VALUE:
run_one(options, ['set', symbol, 'value']) run_one(options, ['set', symbol, 'value'])
run_one(options, ['--force', 'set', symbol, 'value']) run_one(options, ['--force', 'set', symbol, 'value'])
run_one(options, ['unset', symbol])
def main(): def main():
"""Command line entry point.""" """Command line entry point."""