summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristophe de Vienne <christophe@unlish.com>2016-01-11 10:24:32 +0100
committerChristophe de Vienne <christophe@unlish.com>2016-01-11 10:24:32 +0100
commit0533324e9a9f1dd7ec31c5beb7cd373eff434e38 (patch)
tree44cf8c83b9aa16fceda85c34c9b828c4ebc4450b
parent26f0436667158ac9ed0b2e5b085158787fd6209b (diff)
downloadlogilab-common-0533324e9a9f1dd7ec31c5beb7cd373eff434e38.tar.gz
[configuration] Fix multi-line strings handling
Closes #3691182
-rw-r--r--logilab/common/configuration.py3
-rw-r--r--test/unittest_configuration.py44
2 files changed, 47 insertions, 0 deletions
diff --git a/logilab/common/configuration.py b/logilab/common/configuration.py
index bdea905..52ac6df 100644
--- a/logilab/common/configuration.py
+++ b/logilab/common/configuration.py
@@ -401,6 +401,9 @@ def ini_format(stream, options, encoding):
print('#%s=' % optname, file=stream)
else:
value = _encode(value, encoding).strip()
+ if optdict.get('type') == 'string' and '\n' in value:
+ prefix = '\n '
+ value = prefix + prefix.join(value.split('\n'))
print('%s=%s' % (optname, value), file=stream)
format_section = ini_format_section
diff --git a/test/unittest_configuration.py b/test/unittest_configuration.py
index 2a32653..f59a0e0 100644
--- a/test/unittest_configuration.py
+++ b/test/unittest_configuration.py
@@ -271,6 +271,50 @@ diffgroup=pouet
#opt-b-2=""")
+ def test_generate_config_with_multiline_string(self):
+ self.cfg['value'] = 'line1\nline2\nline3'
+ stream = StringIO()
+ self.cfg.generate_config(stream)
+ self.assertMultiLineEqual(stream.getvalue().strip(), """[TEST]
+
+dothis=yes
+
+value=
+ line1
+ line2
+ line3
+
+# you can also document the option
+multiple=yop,yep
+
+# boom
+number=2
+
+bytes=1KB
+
+choice=yo
+
+multiple-choice=yo,ye
+
+named=key:val
+
+reset-value=
+ line1
+ line2
+ line3
+
+
+[AGROUP]
+
+diffgroup=pouet
+
+
+[BGROUP]
+
+#opt-b-1=
+
+#opt-b-2=""")
+
def test_roundtrip(self):
cfg = self.cfg