Commit 4af458e3 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

ShellWriter: Add parameter to disable indentation



This will be used to write a more compact bash completion script.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarBernardo Dal Seno <bdalseno@google.com>
parent 94014b63
......@@ -245,11 +245,12 @@ class ShellWriter:
"""
INDENT_STR = " "
def __init__(self, fh):
def __init__(self, fh, indent=True):
"""Initializes this class.
"""
self._fh = fh
self._indent_enabled = indent
self._indent = 0
def IncIndent(self):
......@@ -276,7 +277,7 @@ class ShellWriter:
else:
line = txt
if line:
if line and self._indent_enabled:
# Indent only if there's something on the line
self._fh.write(self._indent * self.INDENT_STR)
......
......@@ -315,27 +315,44 @@ class TestShellWriter(unittest.TestCase):
sw = None
self.assertEqual(buf.getvalue(), "")
def testEmptyNoIndent(self):
buf = StringIO()
sw = utils.ShellWriter(buf, indent=False)
sw = None
self.assertEqual(buf.getvalue(), "")
@classmethod
def _AddLevel(cls, sw, level):
if level == 6:
return
sw.IncIndent()
try:
# Add empty line, it should not be indented
sw.Write("")
sw.Write(str(level))
cls._AddLevel(sw, level + 1)
finally:
sw.DecIndent()
def testEmptyLines(self):
buf = StringIO()
sw = utils.ShellWriter(buf)
def _AddLevel(level):
if level == 6:
return
sw.IncIndent()
try:
# Add empty line, it should not be indented
sw.Write("")
sw.Write(str(level))
_AddLevel(level + 1)
finally:
sw.DecIndent()
_AddLevel(1)
self._AddLevel(sw, 1)
self.assertEqual(buf.getvalue(),
"".join("\n%s%s\n" % (i * " ", i) for i in range(1, 6)))
def testEmptyLinesNoIndent(self):
buf = StringIO()
sw = utils.ShellWriter(buf, indent=False)
self._AddLevel(sw, 1)
self.assertEqual(buf.getvalue(),
"".join("\n%s\n" % i for i in range(1, 6)))
class TestNormalizeAndValidateMac(unittest.TestCase):
def testInvalid(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment