diff --git a/ipaserver/install/installutils.py b/ipaserver/install/installutils.py index 6a7a19b31..fbec40efa 100644 --- a/ipaserver/install/installutils.py +++ b/ipaserver/install/installutils.py @@ -565,11 +565,15 @@ def set_directive_lines(quotes, separator, k, v, lines, comment): v_quoted = quote_directive_value(v, '"') if quotes else v new_line = ''.join([k, separator, v_quoted, '\n']) + # Special case: consider space as "white space" so tabs are allowed + if separator == ' ': + separator = '[ \t]+' + found = False addnext = False # add on next line, found a comment - matcher = re.compile(r'\s*{}'.format(re.escape(k + separator))) - cmatcher = re.compile(r'\s*{}\s*{}'.format(comment, - re.escape(k + separator))) + matcher = re.compile(r'\s*{}\s*{}'.format(re.escape(k), separator)) + cmatcher = re.compile(r'\s*{}\s*{}\s*{}'.format(comment, + re.escape(k), separator)) for line in lines: if matcher.match(line): found = True diff --git a/ipatests/test_ipaserver/test_install/test_installutils.py b/ipatests/test_ipaserver/test_install/test_installutils.py index 66b4585dd..0a426fe63 100644 --- a/ipatests/test_ipaserver/test_install/test_installutils.py +++ b/ipatests/test_ipaserver/test_install/test_installutils.py @@ -15,6 +15,11 @@ EXAMPLE_CONFIG = [ 'foobar=2\n', ] +WHITESPACE_CONFIG = [ + 'foo 1\n', + 'foobar\t2\n', +] + @pytest.fixture def tempdir(request): @@ -44,6 +49,28 @@ class test_set_directive_lines(object): assert list(lines) == ['foo=3\n', 'foobar=2\n'] +class test_set_directive_lines_whitespace(object): + def test_remove_directive(self): + lines = installutils.set_directive_lines( + False, ' ', 'foo', None, WHITESPACE_CONFIG, comment="#") + assert list(lines) == ['foobar\t2\n'] + + def test_add_directive(self): + lines = installutils.set_directive_lines( + False, ' ', 'baz', '4', WHITESPACE_CONFIG, comment="#") + assert list(lines) == ['foo 1\n', 'foobar\t2\n', 'baz 4\n'] + + def test_set_directive_does_not_clobber_suffix_key(self): + lines = installutils.set_directive_lines( + False, ' ', 'foo', '3', WHITESPACE_CONFIG, comment="#") + assert list(lines) == ['foo 3\n', 'foobar\t2\n'] + + def test_set_directive_with_tab(self): + lines = installutils.set_directive_lines( + False, ' ', 'foobar', '6', WHITESPACE_CONFIG, comment="#") + assert list(lines) == ['foo 1\n', 'foobar 6\n'] + + class test_set_directive(object): def test_set_directive(self): """Check that set_directive writes the new data and preserves mode."""