cubicweb/test/unittest_cwctl.py
changeset 12692 8673da7c2f85
parent 12691 3f0e64630d94
child 12693 5e7212209ea6
--- a/cubicweb/test/unittest_cwctl.py	Mon Jul 22 10:54:22 2019 +0200
+++ b/cubicweb/test/unittest_cwctl.py	Tue May 21 16:47:13 2019 +0200
@@ -24,6 +24,7 @@
 
 from logilab.common.clcommands import CommandLine
 
+from cubicweb import cwctl
 from cubicweb.cwctl import ListCommand, InstanceCommand
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.server.migractions import ServerMigrationHelper
@@ -89,24 +90,64 @@
         pass
 
 
+class _TestFailCommand(InstanceCommand):
+    "I need some doc"
+    name = "test_fail"
+
+    def test_fail_instance(self, appid):
+        raise Exception()
+
+
 class InstanceCommandTest(unittest.TestCase):
-    def test_getting_called(self):
-        CWCTL = CommandLine('cubicweb-ctl', 'The CubicWeb swiss-knife.',
-                            version=cw_version, check_duplicated_command=False)
+    def setUp(self):
+        self.CWCTL = CommandLine('cubicweb-ctl', 'The CubicWeb swiss-knife.',
+                                 version=cw_version, check_duplicated_command=False)
         cwcfg.load_cwctl_plugins()
-        CWCTL.register(_TestCommand)
-
-        _TestCommand.test_instance = MagicMock(return_value=0)
+        self.CWCTL.register(_TestCommand)
+        self.CWCTL.register(_TestFailCommand)
 
         # pretend that this instance exists
         cwcfg.config_for = MagicMock(return_value=object())
 
+    def test_getting_called(self):
+        _TestCommand.test_instance = MagicMock(return_value=0)
+
         try:
-            CWCTL.run(["test", "some_instance"])
+            self.CWCTL.run(["test", "some_instance"])
         except SystemExit as ex:  # CWCTL will finish the program after that
             self.assertEqual(ex.code, 0)
         _TestCommand.test_instance.assert_called_with("some_instance")
 
+    @patch.object(cwctl, 'get_pdb')
+    def test_pdb_not_called(self, get_pdb):
+        try:
+            self.CWCTL.run(["test", "some_instance"])
+        except SystemExit as ex:  # CWCTL will finish the program after that
+            self.assertEqual(ex.code, 0)
+
+        get_pdb.assert_not_called()
+
+    @patch.object(cwctl, 'get_pdb')
+    def test_pdb_called(self, get_pdb):
+        post_mortem = get_pdb.return_value.post_mortem
+        try:
+            self.CWCTL.run(["test_fail", "some_instance", "--pdb"])
+        except SystemExit as ex:  # CWCTL will finish the program after that
+            self.assertEqual(ex.code, 8)
+
+        get_pdb.assert_called_once()
+        post_mortem.assert_called_once()
+
+    @patch.dict(sys.modules, ipdb=MagicMock())
+    def test_ipdb_selected_and_called(self):
+        ipdb = sys.modules['ipdb']
+        try:
+            self.CWCTL.run(["test_fail", "some_instance", "--pdb"])
+        except SystemExit as ex:  # CWCTL will finish the program after that
+            self.assertEqual(ex.code, 8)
+
+        ipdb.post_mortem.assert_called_once()
+
 
 if __name__ == '__main__':
     unittest.main()