fix: parse strings prefixed with "#" in `config save --set ...`

Pound keys were interpreted as comments. This is annoying when we want
to parse html color codes, such as in:

    $ tutor config save --set "INDIGO_PRIMARY_COLOR=#225522"
    $ tutor config printvalue INDIGO_PRIMARY_COLOR
    null

Close #866.
This commit is contained in:
Régis Behmo 2023-09-06 10:52:02 +02:00 committed by Régis Behmo
parent 8eccaa61e9
commit 51928b0075
3 changed files with 9 additions and 0 deletions

View File

@ -0,0 +1 @@
- [Bugfix] Correctly parse strings prefixed with pound "#" key in `tutor config save --set KEY=#value` commands. (by @regisb)

View File

@ -41,6 +41,10 @@ class SerializeTests(unittest.TestCase):
"x=key1:\n subkey: value\nkey2:\n subkey: value"
),
)
self.assertEqual(
("INDIGO_PRIMARY_COLOR", "#225522"),
serialize.parse_key_value("INDIGO_PRIMARY_COLOR=#225522"),
)
def test_str_format(self) -> None:
self.assertEqual("true", serialize.str_format(True))

View File

@ -73,4 +73,8 @@ def parse_key_value(text: str) -> t.Optional[tuple[str, t.Any]]:
if not value:
# Empty strings are interpreted as null values, which is incorrect.
value = "''"
elif "\n" not in value and value.startswith("#"):
# Single-line string that starts with a pound # key
# We need to escape the string, otherwise pound will be interpreted as a comment.
value = f'"{value}"'
return key, parse(value)