[damned-lies] Updated patch_shell_command strategy



commit b22b1c28b4c52e3737320c8b2abe8cdccba4850a
Author: Claude Paroz <claude 2xlibre net>
Date:   Tue Apr 24 15:46:14 2018 +0200

    Updated patch_shell_command strategy

 stats/tests/tests.py |    8 +++++---
 stats/tests/utils.py |   13 +++++++++----
 2 files changed, 14 insertions(+), 7 deletions(-)
---
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index ea0ab46..fc2b685 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -38,11 +38,13 @@ class ModuleTestCase(TestCase):
         ('intltool', 'intltool-update'),
         ('gnome-doc-utils', 'xml2po'),
     )
-    def __init__(self, name):
-        super().__init__(name)
-        for package, prog in self.SYS_DEPENDENCIES:
+
+    @classmethod
+    def setUpClass(cls):
+        for package, prog in cls.SYS_DEPENDENCIES:
             if not utils.check_program_presence(prog):
                 raise Exception("You are missing a required system package needed by Damned Lies (%s)" % 
package)
+        super().setUpClass()
 
     def setUp(self):
         Branch.checkout_on_creation = False
diff --git a/stats/tests/utils.py b/stats/tests/utils.py
index 48160cc..17b48ef 100644
--- a/stats/tests/utils.py
+++ b/stats/tests/utils.py
@@ -3,6 +3,7 @@ import shutil
 import tarfile
 import tempfile
 from functools import wraps
+from unittest.mock import patch
 
 from django.conf import settings
 
@@ -19,8 +20,10 @@ class patch_shell_command:
 
     def __enter__(self):
         self.cmds = []
-        self.saved_run_shell_command = utils.run_shell_command
-        utils.run_shell_command = self.mocked_run_shell_command
+        self.patcher1 = patch('stats.models.run_shell_command', side_effect=self.mocked_run_shell_command)
+        self.patcher1.start()
+        self.patcher2 = patch('stats.utils.run_shell_command', side_effect=self.mocked_run_shell_command)
+        self.patcher2.start()
         return self.cmds
 
     def mocked_run_shell_command(self, cmd, *args, **kwargs):
@@ -28,13 +31,15 @@ class patch_shell_command:
         self.cmds.append(cmd_str)
         if self.only is not None and not any(needle in cmd_str for needle in self.only):
             # Pass the command to the real utils.run_shell_command
-            return self.saved_run_shell_command(cmd, *args, **kwargs)
+            from common.utils import run_shell_command
+            return run_shell_command(cmd, *args, **kwargs)
         else:
             # Pretend the command was successfull
             return 0, '', ''
 
     def __exit__(self, *args):
-        utils.run_shell_command = self.saved_run_shell_command
+        self.patcher1.stop()
+        self.patcher2.stop()
 
 
 def test_scratchdir(test_func):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]