[configuration] Fix multi-line strings handling

Closes #3691182

authorChristophe de Vienne <christophe@unlish.com>
changeset86e00b30862a
branchdefault
phasedraft
hiddenyes
parent revision#78b848c85b0b Added tag 1.1.0, debian/1.1.0-1, centos/1.1.0-1 for changeset ca5d6124428b
child revision<not specified>
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 86e00b30862a3738a6c88bb0671a4d193403f1f4
# Parent 78b848c85b0b3aabdbf8b12596430468deced72e
[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')