Skip to content

Commit d7f1b43

Browse files
committed
Add context manager for changing directories temporarily
1 parent 4ce288b commit d7f1b43

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

cli_test_helpers/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
__all__ = [
66
"ArgvContext",
77
"EnvironContext",
8+
"RandomDirectoryContext",
89
"shell",
910
]
1011

1112
from .commands import shell
12-
from .decorators import ArgvContext, EnvironContext
13+
from .decorators import ArgvContext, EnvironContext, RandomDirectoryContext

cli_test_helpers/decorators.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"""
44

55
import contextlib
6+
import os
67
import sys
8+
from tempfile import TemporaryDirectory
79
from unittest.mock import patch
810

911
__all__ = []
@@ -50,3 +52,21 @@ def __enter__(self):
5052
for key in self.clear_variables:
5153
with contextlib.suppress(KeyError):
5254
self.in_dict.pop(key)
55+
56+
57+
class RandomDirectoryContext(TemporaryDirectory):
58+
"""
59+
Change the execution directory to a random location, temporarily.
60+
"""
61+
62+
def __enter__(self):
63+
"""Create a temporary directory and ``cd`` into it."""
64+
self.__prev_dir = os.getcwd()
65+
super().__enter__()
66+
os.chdir(self.name)
67+
return self.name
68+
69+
def __exit__(self, exc_type, exc_value, traceback):
70+
"""Return to the original directory before execution."""
71+
os.chdir(self.__prev_dir)
72+
return super().__exit__(exc_type, exc_value, traceback)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ extend-ignore = [
6767
"D203",
6868
"D205",
6969
"D212",
70+
"PTH109",
7071
"Q000",
7172
]
7273

tests/test_decorators.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import sys
55

6-
from cli_test_helpers import ArgvContext, EnvironContext
6+
from cli_test_helpers import ArgvContext, EnvironContext, RandomDirectoryContext
77

88

99
def test_argv_context():
@@ -45,3 +45,20 @@ def test_environ_context():
4545
assert os.environ == old_environ, "object os.environ was not restored"
4646
assert os.getenv("PATH") == old_path, "env var PATH was not restored"
4747
assert os.getenv("FOO") is None, "env var FOO was not cleared"
48+
49+
50+
def test_random_directory_context():
51+
"""
52+
In a directory context, are we effectively in a different location?
53+
"""
54+
before_dir = os.getcwd()
55+
56+
with RandomDirectoryContext() as random_dir:
57+
new_dir = os.getcwd()
58+
59+
assert new_dir == random_dir, "Does't behave like TemporaryDirectory"
60+
assert new_dir != before_dir, "Context not in a different file system location"
61+
62+
after_dir = os.getcwd()
63+
64+
assert after_dir == before_dir, "Execution directory not restored to original"

0 commit comments

Comments
 (0)