[configuration] Fix multi-line strings handling

Closes #3691182

authorChristophe de Vienne <christophe@unlish.com>
changeset9a8f910ac6f9
branchdefault
phasepublic
hiddenno
parent revision#03ef72ac6fdb [testlib] Deprecate doctest things in favour of stdlib module
child revision#62e014ec3b53 Fix test breakage in modutils with python 3
files modified by this revision
logilab/common/configuration.py
test/unittest_configuration.py
# HG changeset patch
# User Christophe de Vienne <christophe@unlish.com>
# Date 1452504272 -3600
# Mon Jan 11 10:24:32 2016 +0100
# Node ID 9a8f910ac6f98b3f4aa44f039ca0ba1104f8b4a5
# Parent 03ef72ac6fdb07a0461ddee4d91cb850e6b46407
[configuration] Fix multi-line strings handling

Closes #3691182

diff --git a/logilab/common/configuration.py b/logilab/common/configuration.py
@@ -399,10 +399,13 @@
1              print(file=stream)
2          if value is None:
3              print('#%s=' % optname, file=stream)
4          else:
5              value = _encode(value, encoding).strip()
6 +            if optdict.get('type') == 'string' and '\n' in value:
7 +                prefix = '\n    '
8 +                value = prefix + prefix.join(value.split('\n'))
9              print('%s=%s' % (optname, value), file=stream)
10 
11  format_section = ini_format_section
12 
13  def rest_format_section(stream, section, options, encoding=None, doc=None):
diff --git a/test/unittest_configuration.py b/test/unittest_configuration.py
@@ -269,10 +269,54 @@
14 
15  #opt-b-1=
16 
17  #opt-b-2=""")
18 
19 +    def test_generate_config_with_multiline_string(self):
20 +        self.cfg['value'] = 'line1\nline2\nline3'
21 +        stream = StringIO()
22 +        self.cfg.generate_config(stream)
23 +        self.assertMultiLineEqual(stream.getvalue().strip(), """[TEST]
24 +
25 +dothis=yes
26 +
27 +value=
28 +    line1
29 +    line2
30 +    line3
31 +
32 +# you can also document the option
33 +multiple=yop,yep
34 +
35 +# boom
36 +number=2
37 +
38 +bytes=1KB
39 +
40 +choice=yo
41 +
42 +multiple-choice=yo,ye
43 +
44 +named=key:val
45 +
46 +reset-value=
47 +    line1
48 +    line2
49 +    line3
50 +
51 +
52 +[AGROUP]
53 +
54 +diffgroup=pouet
55 +
56 +
57 +[BGROUP]
58 +
59 +#opt-b-1=
60 +
61 +#opt-b-2=""")
62 +
63 
64      def test_roundtrip(self):
65          cfg = self.cfg
66          f = tempfile.mktemp()
67          stream = open(f, 'w')