From 858905fbf0bd8634bbe8c36cad66b615e9c8f3b0 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Wed, 14 Jul 2010 19:32:23 +0200
Subject: [PATCH] Move ShellWriter class to utils

Also add unittest.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 autotools/build-bash-completion | 39 +----------------------------
 lib/utils.py                    | 42 +++++++++++++++++++++++++++++++
 test/ganeti.utils_unittest.py   | 44 +++++++++++++++++++++++++++++++++
 3 files changed, 87 insertions(+), 38 deletions(-)

diff --git a/autotools/build-bash-completion b/autotools/build-bash-completion
index 79258c2cc..bcb1239d8 100755
--- a/autotools/build-bash-completion
+++ b/autotools/build-bash-completion
@@ -40,43 +40,6 @@ from ganeti import build
 from ganeti import _autoconf
 
 
-class ShellWriter:
-  """Helper class to write scripts with indentation.
-
-  """
-  INDENT_STR = "  "
-
-  def __init__(self, fh):
-    self._fh = fh
-    self._indent = 0
-
-  def IncIndent(self):
-    """Increase indentation level by 1.
-
-    """
-    self._indent += 1
-
-  def DecIndent(self):
-    """Decrease indentation level by 1.
-
-    """
-    assert self._indent > 0
-    self._indent -= 1
-
-  def Write(self, txt, *args):
-    """Write line to output file.
-
-    """
-    self._fh.write(self._indent * self.INDENT_STR)
-
-    if args:
-      self._fh.write(txt % args)
-    else:
-      self._fh.write(txt)
-
-    self._fh.write("\n")
-
-
 def WritePreamble(sw):
   """Writes the script preamble.
 
@@ -630,7 +593,7 @@ def GetCommands(filename, module):
 
 def main():
   buf = StringIO()
-  sw = ShellWriter(buf)
+  sw = utils.ShellWriter(buf)
 
   WritePreamble(sw)
 
diff --git a/lib/utils.py b/lib/utils.py
index 40f8fc460..6caa0fef5 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -1548,6 +1548,48 @@ def ShellQuoteArgs(args):
   return ' '.join([ShellQuote(i) for i in args])
 
 
+class ShellWriter:
+  """Helper class to write scripts with indentation.
+
+  """
+  INDENT_STR = "  "
+
+  def __init__(self, fh):
+    """Initializes this class.
+
+    """
+    self._fh = fh
+    self._indent = 0
+
+  def IncIndent(self):
+    """Increase indentation level by 1.
+
+    """
+    self._indent += 1
+
+  def DecIndent(self):
+    """Decrease indentation level by 1.
+
+    """
+    assert self._indent > 0
+    self._indent -= 1
+
+  def Write(self, txt, *args):
+    """Write line to output file.
+
+    """
+    assert self._indent >= 0
+
+    self._fh.write(self._indent * self.INDENT_STR)
+
+    if args:
+      self._fh.write(txt % args)
+    else:
+      self._fh.write(txt)
+
+    self._fh.write("\n")
+
+
 def ListVisibleFiles(path):
   """Returns a list of visible files in a directory.
 
diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py
index 91c269d68..5ff5fc9f7 100755
--- a/test/ganeti.utils_unittest.py
+++ b/test/ganeti.utils_unittest.py
@@ -38,6 +38,7 @@ import time
 import unittest
 import warnings
 import OpenSSL
+from cStringIO import StringIO
 
 import testutils
 from ganeti import constants
@@ -2242,5 +2243,48 @@ class TestIgnoreProcessNotFound(unittest.TestCase):
     self.assertFalse(utils.IgnoreProcessNotFound(os.kill, pid, 0))
 
 
+class TestShellWriter(unittest.TestCase):
+  def test(self):
+    buf = StringIO()
+    sw = utils.ShellWriter(buf)
+    sw.Write("#!/bin/bash")
+    sw.Write("if true; then")
+    sw.IncIndent()
+    try:
+      sw.Write("echo true")
+
+      sw.Write("for i in 1 2 3")
+      sw.Write("do")
+      sw.IncIndent()
+      try:
+        self.assertEqual(sw._indent, 2)
+        sw.Write("date")
+      finally:
+        sw.DecIndent()
+      sw.Write("done")
+    finally:
+      sw.DecIndent()
+    sw.Write("echo %s", utils.ShellQuote("Hello World"))
+    sw.Write("exit 0")
+
+    self.assertEqual(sw._indent, 0)
+
+    output = buf.getvalue()
+
+    self.assert_(output.endswith("\n"))
+
+    lines = output.splitlines()
+    self.assertEqual(len(lines), 9)
+    self.assertEqual(lines[0], "#!/bin/bash")
+    self.assert_(re.match(r"^\s+date$", lines[5]))
+    self.assertEqual(lines[7], "echo 'Hello World'")
+
+  def testEmpty(self):
+    buf = StringIO()
+    sw = utils.ShellWriter(buf)
+    sw = None
+    self.assertEqual(buf.getvalue(), "")
+
+
 if __name__ == '__main__':
   testutils.GanetiTestProgram()
-- 
GitLab