From 731de6a747542298b5a847751ec8ce651e667d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Tue, 21 Jan 2020 17:09:58 +0100 Subject: [PATCH] Fix incorrectly parsed empty strings in `config save --set ...=...` When running `config save --set KEY=""`, KEY was incorrectly interpreted as null. --- CHANGELOG.md | 4 ++++ tests/test_serialize.py | 8 ++++++++ tutor/commands/config.py | 5 +++++ tutor/serialize.py | 6 +++++- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7371ba..5a98211 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Note: Breaking changes between versions are indicated by "💥". +## Unreleased + +- [Bugfix] Fix incorrectly parsed empty strings for `config save --set ...=...` commands + ## 3.11.2 (2020-01-17) - [Bugfix] Make sure `docker-compose.override.yml` are loaded in dev and local contexts diff --git a/tests/test_serialize.py b/tests/test_serialize.py index 0534b79..3a78d26 100644 --- a/tests/test_serialize.py +++ b/tests/test_serialize.py @@ -25,3 +25,11 @@ class SerializeTests(unittest.TestCase): def test_parse_weird_chars(self): self.assertEqual("*@google.com", serialize.parse("*@google.com")) + + def test_parse_empty_string(self): + self.assertEqual("", serialize.parse("''")) + + # def test_dump_null(self):# # Unfortunately, this fails as the output is "null\n...\n" + + +# self.assertEqual("null", serialize.dumps(None)) diff --git a/tutor/commands/config.py b/tutor/commands/config.py index fe74bab..f55ad13 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -25,6 +25,10 @@ class YamlParamType(click.ParamType): k, v = value.split("=") except ValueError: self.fail("'{}' is not of the form 'key=value'.".format(value), param, ctx) + if not v: + # Empty strings are incorrectly interpreted as null values, which is + # incorrect. + v = "''" return k, serialize.parse(v) @@ -93,6 +97,7 @@ def printroot(context): def printvalue(context, key): config = tutor_config.load(context.root) try: + # Note that this will incorrectly print None values fmt.echo(config[key]) except KeyError: raise exceptions.TutorError("Missing configuration value: {}".format(key)) diff --git a/tutor/serialize.py b/tutor/serialize.py index ca6c3b7..99a29da 100644 --- a/tutor/serialize.py +++ b/tutor/serialize.py @@ -8,7 +8,11 @@ def load(stream): def dump(content, fileobj): - yaml.dump(content, fileobj, default_flow_style=False) + yaml.dump(content, stream=fileobj, default_flow_style=False) + + +def dumps(content): + return yaml.dump(content, stream=None, default_flow_style=False) def parse(v):