Skip to content

Commit

Permalink
Add: Extend temp_python_module to allow changing current working dir
Browse files Browse the repository at this point in the history
In some testing use case it might be necessary to change the current
working directory to the created temporary directory.
  • Loading branch information
bjoernricks authored and y0urself committed Jan 13, 2023
1 parent f9cbd5d commit 60724e6
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pontos/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def temp_file(

@contextmanager
def temp_python_module(
content: str, *, name: str = "foo"
content: str, *, name: str = "foo", change_into: bool = False
) -> Generator[Path, None, None]:
"""
A Context Manager to create a new Python module in a temporary directory.
Expand All @@ -187,6 +187,8 @@ def temp_python_module(
Args:
content: Python code to write into the temporary module.
name: Name of the new Python module. By default: "foo".
change_into: Adjust the current working directory to the temporary
directory.
Returns:
A path to the created temporary Python module file
Expand All @@ -203,7 +205,7 @@ def temp_python_module(
"""
with temp_directory(
add_to_sys_path=True,
add_to_sys_path=True, change_into=change_into
) as tmp_dir, ensure_unload_module(name):
test_file = tmp_dir / f"{name}.py"
test_file.write_text(content, encoding="utf8")
Expand Down

0 comments on commit 60724e6

Please sign in to comment.