Skip to content

Commit

Permalink
fix tempfile handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jscotka committed May 7, 2021
1 parent 4be1b6b commit 642b7ce
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 1,363 deletions.
24 changes: 24 additions & 0 deletions requre/helpers/tempfile.py
Expand Up @@ -27,14 +27,36 @@
from typing import Optional, Any
from requre.cassette import Cassette, CassetteExecution
from requre.objects import ObjectStorage
from requre.helpers.simple_object import Simple
from warnings import warn

logger = logging.getLogger(__name__)


class MkTemp(Simple):
"""
decorate mktemp method wrapper
"""

pass


class MkDTemp(Simple):
"""
decorate mkdtemp method wrapper
"""

def from_serializable(self, data: Any) -> Any:
os.makedirs(data, exist_ok=True)
return data


class TempFile(ObjectStorage):
"""
replace system tempfile module with own predictable names implementation
of temp files for mocking
Should be replaced by new implementations mkdtemp and mktemp
"""

root = "/tmp"
Expand Down Expand Up @@ -81,6 +103,7 @@ def mktemp(cls, cassette: Optional[Cassette] = None) -> Any:
:return: CassetteExecution class with function and cassette instance
"""
warn("Please replace it by MkTemp.decorator_plain()")
casex = CassetteExecution()
casex.cassette = cassette or cls.get_cassette()
casex.obj_cls = cls
Expand All @@ -105,6 +128,7 @@ def mkdtemp(cls, cassette: Optional[Cassette] = None) -> Any:
:return: CassetteExecution class with function and cassette instance
"""
warn("Please replace it by class mkdtemp MkDTemp.decorator_plain()")
casex = CassetteExecution()
casex.cassette = cassette or cls.get_cassette()
casex.obj_cls = cls
Expand Down
6 changes: 3 additions & 3 deletions requre/modules_decorate_all_methods.py
Expand Up @@ -4,7 +4,7 @@
apply_decorator_to_all_methods,
record_requests_for_all_methods,
)
from requre.helpers.tempfile import TempFile
from requre.helpers.tempfile import MkTemp, MkDTemp
from requre.cassette import Cassette
from requre.constants import TEST_METHOD_REGEXP
from requre.helpers.git.repo import Repo
Expand Down Expand Up @@ -47,8 +47,8 @@ def record_tempfile_module(
regexp_method_pattern=TEST_METHOD_REGEXP,
):
decorators = [
("tempfile.mkdtemp", TempFile.mkdtemp()),
("tempfile.mktemp", TempFile.mktemp()),
("tempfile.mkdtemp", MkDTemp.decorator_plain()),
("tempfile.mktemp", MkTemp.decorator_plain()),
]
return __replace_module_match_with_multiple_decorators(
*decorators,
Expand Down

0 comments on commit 642b7ce

Please sign in to comment.