Add value in set_directive after a commented-out version

When setting a value using set_directive() look for a commented-out
version of the directive and add the new value immediately after
that to keep the proper context.

Related: https://pagure.io/freeipa/issue/3757
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
This commit is contained in:
Rob Crittenden
2017-12-05 17:06:48 -05:00
committed by Stanislav Laznicka
parent 5531c9f26b
commit 4d2c7a4a75
2 changed files with 29 additions and 10 deletions

View File

@@ -30,17 +30,17 @@ def tempdir(request):
class test_set_directive_lines(object):
def test_remove_directive(self):
lines = installutils.set_directive_lines(
False, '=', 'foo', None, EXAMPLE_CONFIG)
False, '=', 'foo', None, EXAMPLE_CONFIG, comment="#")
assert list(lines) == ['foobar=2\n']
def test_add_directive(self):
lines = installutils.set_directive_lines(
False, '=', 'baz', '4', EXAMPLE_CONFIG)
False, '=', 'baz', '4', EXAMPLE_CONFIG, comment="#")
assert list(lines) == ['foo=1\n', 'foobar=2\n', 'baz=4\n']
def test_set_directive_does_not_clobber_suffix_key(self):
lines = installutils.set_directive_lines(
False, '=', 'foo', '3', EXAMPLE_CONFIG)
False, '=', 'foo', '3', EXAMPLE_CONFIG, comment="#")
assert list(lines) == ['foo=3\n', 'foobar=2\n']
@@ -56,7 +56,7 @@ class test_set_directive(object):
for line in EXAMPLE_CONFIG:
f.write(line)
installutils.set_directive(filename, 'foo', '3', False, '=')
installutils.set_directive(filename, 'foo', '3', False, '=', "#")
stat_post = os.stat(filename)
with open(filename, 'r') as f: