Beispiel #1
0
    def test_method_context_replace_module(self):
        """
        Test if it is able to replace whole module by own implemetation
        Test also own implementation of static tempfile module via class
        """

        with UpgradeImportSystem().replace_module(
            where="^tempfile$", replacement=TempFile, who_name=SELECTOR
        ):
            import tempfile

            tmpfile = tempfile.mktemp()
            tmpdir = tempfile.mkdtemp()
            self.assertIn(
                f"/tmp/{os.path.basename(PersistentObjectStorage().storage_file)}/static_tmp",
                tmpfile,
            )
            self.assertIn(
                f"/tmp/{os.path.basename(PersistentObjectStorage().storage_file)}/static_tmp",
                tmpdir,
            )
            self.assertFalse(os.path.exists(tmpfile))
            self.assertTrue(os.path.exists(tmpdir))
            self.assertTrue(os.path.isdir(tmpdir))
            os.removedirs(tmpdir)
Beispiel #2
0
    def test_no_filter(self):
        old_imports = builtins.__import__
        with UpgradeImportSystem().upgrade():
            import tempfile

            self.assertNotIn("decorated", tempfile.mktemp())
            self.assertIn("/tmp", tempfile.mktemp())
        assert builtins.__import__ == old_imports
Beispiel #3
0
 def test_filter_import_from_reverted(self):
     with UpgradeImportSystem().upgrade(
             what="shlex.split",
             replace_type=ReplaceType.DECORATOR,
             replacement=lambda x: lambda y: f"mocked {x(y)}",
     ):
         self.assertEqual("mocked ['a', 'b']", split("a b"))
     self.assertEqual(["a", "b"], split("a b"))
Beispiel #4
0
    def test_filter_already_in_sys(self):
        old_imports = builtins.__import__
        with UpgradeImportSystem().upgrade(
                what="tempfile.mktemp",
                replace_type=ReplaceType.DECORATOR,
                replacement=lambda x: lambda: f"decorated {x()}",
        ):
            import tempfile

            self.assertIn("decorated", tempfile.mktemp())
            self.assertIn("/tmp", tempfile.mktemp())
        assert old_imports == builtins.__import__
Beispiel #5
0
    def test_method_context_decorate(self):
        """
        Test patching by decorator_all_keys, not replacing whole function
        """
        with UpgradeImportSystem().decorate(
            what="tempfile.mktemp", decorator=lambda x: lambda: f"decorated_b {x()}"
        ):
            import tempfile

            self.assertIn("decorated_b", tempfile.mktemp())
            self.assertIn("/tmp", tempfile.mktemp())
            tempfile.mktemp = original_mktemp
Beispiel #6
0
    def test_method_replace(self):
        """
        Test improving of import system with import statement
        Check also debug file output if it contains proper debug data
        """

        UpgradeImportSystem().replace(what="tempfile.mktemp", replacement=lambda: "a")
        import tempfile

        self.assertNotIn("/tmp", tempfile.mktemp())
        self.assertIn("a", tempfile.mktemp())
        tempfile.mktemp = original_mktemp
Beispiel #7
0
    def test_method_context_decorate(self):
        """
        Test patching by decorator_all_keys, not replacing whole function
        """
        with UpgradeImportSystem().decorate(
            where="^tempfile$",
            what="mktemp",
            decorator=lambda x: lambda: f"decorated {x()}",
            who_name=SELECTOR,
        ):
            import tempfile

            self.assertIn("decorated", tempfile.mktemp())
            self.assertIn("/tmp", tempfile.mktemp())
Beispiel #8
0
    def test_filter_import_random(self):
        old_imports = builtins.__import__

        with UpgradeImportSystem().upgrade(
                what="random.random",
                replace_type=ReplaceType.DECORATOR,
                replacement=lambda x: lambda: f"decorated {x()}",
        ):
            import random

            self.assertIn("decorated", random.random())
            self.assertIn("0.", random.random())
        self.assertIsInstance(random.random(), float)
        assert old_imports == builtins.__import__
Beispiel #9
0
    def test_filter(self):
        old_imports = builtins.__import__
        with UpgradeImportSystem().upgrade((
                "^tempfile$",
            {
                "who_name": SELECTOR
            },
            {
                "mktemp": [
                    ReplaceType.DECORATOR,
                    lambda x: lambda: f"decorated {x()}",
                ]
            },
        )):
            assert old_imports != builtins.__import__
            import tempfile

            self.assertIn("decorated", tempfile.mktemp())
            self.assertIn("/tmp", tempfile.mktemp())
        assert old_imports == builtins.__import__
Beispiel #10
0
    def test_method_context_replace(self):
        """
        Test improving of import system with import statement
        Check also debug file output if it contains proper debug data
        """
        debug_file = "__modules.log"
        with UpgradeImportSystem(debug_file=debug_file).replace(
            where="^tempfile$",
            what="mktemp",
            replacement=lambda: "a",
            who_name="test_import_system",
        ):
            import tempfile

            self.assertNotIn("/tmp", tempfile.mktemp())
            self.assertIn("a", tempfile.mktemp())
            with open(debug_file, "r") as fd:
                output = fd.read()
                self.assertIn(SELECTOR, output)
                self.assertIn("replacing mktemp by function", output)
        os.remove(debug_file)
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

from requre.import_system import UpgradeImportSystem
from requre.simple_object import Simple

FILTERS = UpgradeImportSystem().decorate("time.sleep",
                                         Simple.decorator_plain())
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

from requre.import_system import UpgradeImportSystem
from requre.helpers.tempfile import TempFile

special = UpgradeImportSystem().decorate("tempfile.mktemp", TempFile.mktemp())
Beispiel #13
0
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

from requre.import_system import UpgradeImportSystem
from requre.helpers.tempfile import TempFile

FILTERS = UpgradeImportSystem().decorate(what="tempfile.mktemp",
                                         decorator=TempFile.mktemp())