# Run the following command to test:
#
#     (in /usr/local/googkit)
#     $ python -m {test_module_name}
#
# See also: http://docs.python.org/3.3/library/unittest.html#command-line-interface
#
# We cannot use unittest.mock on python 2.x!
# Please install the Mock module when you use Python 2.x.
#
#     $ easy_install -U Mock
#
# See also: http://www.voidspace.org.uk/python/mock/#installing

import unittest

try:
    # Python 3.3 or later
    import unittest.mock as mock
except ImportError:
    # Python 2.x or 3.2-
    import mock

from lib.error import GoogkitError
from command.base_command import BaseCommand


class EnvironmentStub(object):
    def __init__(self):
        self.config = None


class TestBaseCommand(unittest.TestCase):
    # run {{{
    def test_run_on_cmd(self):
        class ConcreteCommand(BaseCommand):
            pass

        env1 = EnvironmentStub()
        env1.config = mock.MagicMock()

        cmd1 = ConcreteCommand(env1)
        cmd1.run_internal = mock.MagicMock()
        cmd1.run()
        cmd1.run_internal.assert_called_once_with()

        env2 = EnvironmentStub()
        env2.config = None

        cmd2 = ConcreteCommand(env2)
        cmd2.run_internal = mock.MagicMock()
        cmd2.run()
        cmd2.run_internal.assert_called_once_with()


    def test_run_on_cmd_needs_config(self):
        class ConcreteCommandNeedsConfig(BaseCommand):
            @classmethod
            def needs_config(cls):
                return True

        env1 = EnvironmentStub()
        env1.config = mock.MagicMock()

        cmd1 = ConcreteCommandNeedsConfig(env1)
        cmd1.run_internal = mock.MagicMock()
        cmd1.run()
        cmd1.run_internal.assert_called_once_with()

        env2 = EnvironmentStub()
        env2.config = None

        cmd2 = ConcreteCommandNeedsConfig(env2)
        cmd2.run_internal = mock.MagicMock()
        with self.assertRaises(GoogkitError):
            cmd2.run()
    # }}}


if __name__ == '__main__':
    unittest.main()

# vim: fdm=marker

@@ -403,16 +403,50 @@
 unittest
+%0Aimport sys%0Afrom io import BytesIO
 %0A%0Atry:%0A 
@@ -647,16 +647,278 @@
 mmand%0A%0A%0A
+class StdoutHook():%0A    def __init__(self):%0A        self.orig_stdout = sys.stdout%0A%0A%0A    def __enter__(self):%0A        io_base = BytesIO()%0A        sys.stdout = io_base%0A        return io_base%0A%0A%0A    def __exit__(self, *args):%0A        sys.stdout = self.orig_stdout%0A%0A%0A
 class En
@@ -1142,16 +1142,47 @@
   pass%0A%0A
+        with StdoutHook():%0A    
         
@@ -1198,32 +1198,36 @@
 vironmentStub()%0A
+    
         env1.con
@@ -1250,32 +1250,36 @@
 Mock()%0A%0A        
+ 
+   
 cmd1 = ConcreteC
@@ -1287,24 +1287,28 @@
 mmand(env1)%0A
+    
         cmd1
@@ -1332,32 +1332,36 @@
 ock.MagicMock()%0A
+    
         cmd1.run
@@ -1363,32 +1363,36 @@
 1.run()%0A        
+    
 cmd1.run_interna
@@ -1420,32 +1420,36 @@
 with()%0A%0A        
+   
+ 
 env2 = Environme
@@ -1457,32 +1457,36 @@
 tStub()%0A        
+    
 env2.config = No
@@ -1489,32 +1489,36 @@
 = None%0A%0A        
+ 
+   
 cmd2 = ConcreteC
@@ -1526,24 +1526,28 @@
 mmand(env2)%0A
+    
         cmd2
@@ -1579,32 +1579,36 @@
 cMock()%0A        
+    
 cmd2.run()%0A     
@@ -1602,32 +1602,36 @@
 2.run()%0A        
+  
+  
 cmd2.run_interna
@@ -1848,16 +1848,47 @@
 n True%0A%0A
+        with StdoutHook():%0A    
         
@@ -1904,32 +1904,36 @@
 vironmentStub()%0A
+    
         env1.con
@@ -1956,32 +1956,36 @@
 Mock()%0A%0A        
+  
+  
 cmd1 = ConcreteC
@@ -2008,32 +2008,36 @@
 g(env1)%0A        
+    
 cmd1.run_interna
@@ -2057,32 +2057,36 @@
 cMock()%0A        
+ 
+   
 cmd1.run()%0A     
@@ -2080,32 +2080,36 @@
 1.run()%0A        
+   
+ 
 cmd1.run_interna
@@ -2137,32 +2137,36 @@
 with()%0A%0A        
+    
 env2 = Environme
@@ -2174,32 +2174,36 @@
 tStub()%0A        
+  
+  
 env2.config = No
@@ -2198,32 +2198,36 @@
 .config = None%0A%0A
+    
         cmd2 = C
@@ -2258,32 +2258,36 @@
 g(env2)%0A        
+    
 cmd2.run_interna
@@ -2311,24 +2311,28 @@
 k()%0A        
+  
+  
 with self.as
@@ -2357,16 +2357,20 @@
 Error):%0A
+    
         

Make test silent