Skip to content

Commit 3f7b737

Browse files
committed
Refactored tests/test_explicit_profile.py
tests/test_explicit_profile.py::enter_tmpdir Now explicitly defined as a class, instead of using `@contextlib.contextmanager` (see pyutils#340)
1 parent 1eb3b01 commit 3f7b737

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

tests/test_explicit_profile.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
1-
import contextlib
21
import os
32
import re
43
import sys
54
import tempfile
5+
from contextlib import ExitStack
66

77
import pytest
88
import ubelt as ub
99

1010

11-
@contextlib.contextmanager
12-
def enter_tmpdir():
13-
with contextlib.ExitStack() as stack:
14-
enter = stack.enter_context
11+
class enter_tmpdir:
12+
"""
13+
Set up a temporary directory and :cmd:`chdir` into it.
14+
"""
15+
def __init__(self):
16+
self.stack = ExitStack()
17+
18+
def __enter__(self):
19+
"""
20+
Returns:
21+
curdir (ubelt.Path)
22+
Temporary directory :cmd:`chdir`-ed into.
23+
24+
Side effects:
25+
``curdir`` created and :cmd:`chdir`-ed into.
26+
"""
27+
enter = self.stack.enter_context
1528
tmpdir = os.path.abspath(enter(tempfile.TemporaryDirectory()))
1629
enter(ub.ChDir(tmpdir))
17-
yield ub.Path(tmpdir)
30+
return ub.Path(tmpdir)
31+
32+
def __exit__(self, *_, **__):
33+
"""
34+
Side effects:
35+
* Original working directory restored.
36+
* Temporary directory created deleted.
37+
"""
38+
self.stack.close()
1839

1940

2041
def test_simple_explicit_nonglobal_usage():

0 commit comments

Comments
 (0)