| 1 |
# ----------------------------------------------------------------------------- |
| 2 |
# Copyright (C) 2012- The IPython Development Team |
| 3 |
# |
| 4 |
# Distributed under the terms of the BSD License. The full license is in |
| 5 |
# the file COPYING, distributed as part of this software. |
| 6 |
# ----------------------------------------------------------------------------- |
| 7 |
|
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
from IPython.utils.tempdir import NamedFileInTemporaryDirectory |
| 11 |
from IPython.utils.tempdir import TemporaryWorkingDirectory |
| 12 |
|
| 13 |
|
| 14 |
def test_named_file_in_temporary_directory(): |
| 15 |
with NamedFileInTemporaryDirectory("filename", "wb") as file: |
| 16 |
name = file.name |
| 17 |
assert not file.closed |
| 18 |
assert Path(name).exists() |
| 19 |
file.write(b"test") |
| 20 |
assert file.closed |
| 21 |
assert not Path(name).exists() |
| 22 |
|
| 23 |
|
| 24 |
def test_temporary_working_directory(): |
| 25 |
with TemporaryWorkingDirectory() as directory: |
| 26 |
directory_path = Path(directory).resolve() |
| 27 |
assert directory_path.exists() |
| 28 |
assert Path.cwd().resolve() == directory_path |
| 29 |
assert not directory_path.exists() |
| 30 |
assert Path.cwd().resolve() != directory_path |