##// END OF EJS Templates
Use pathlib in utils/tempdir.
Jakub Klus -
Show More
@@ -5,6 +5,7 b' creating a context manager for the working directory which is also temporary.'
5 """
5 """
6
6
7 import os as _os
7 import os as _os
8 from pathlib import Path
8 from tempfile import TemporaryDirectory
9 from tempfile import TemporaryDirectory
9
10
10
11
@@ -22,7 +23,7 b' class NamedFileInTemporaryDirectory(object):'
22
23
23 """
24 """
24 self._tmpdir = TemporaryDirectory(**kwds)
25 self._tmpdir = TemporaryDirectory(**kwds)
25 path = _os.path.join(self._tmpdir.name, filename)
26 path = Path(self._tmpdir.name) / filename
26 self.file = open(path, mode, bufsize)
27 self.file = open(path, mode, bufsize)
27
28
28 def cleanup(self):
29 def cleanup(self):
@@ -48,7 +49,7 b' class TemporaryWorkingDirectory(TemporaryDirectory):'
48 ...
49 ...
49 """
50 """
50 def __enter__(self):
51 def __enter__(self):
51 self.old_wd = _os.getcwd()
52 self.old_wd = Path.cwd()
52 _os.chdir(self.name)
53 _os.chdir(self.name)
53 return super(TemporaryWorkingDirectory, self).__enter__()
54 return super(TemporaryWorkingDirectory, self).__enter__()
54
55
@@ -5,7 +5,7 b''
5 # the file COPYING, distributed as part of this software.
5 # the file COPYING, distributed as part of this software.
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7
7
8 import os
8 from pathlib import Path
9
9
10 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
10 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
11 from IPython.utils.tempdir import TemporaryWorkingDirectory
11 from IPython.utils.tempdir import TemporaryWorkingDirectory
@@ -15,14 +15,15 b' def test_named_file_in_temporary_directory():'
15 with NamedFileInTemporaryDirectory('filename') as file:
15 with NamedFileInTemporaryDirectory('filename') as file:
16 name = file.name
16 name = file.name
17 assert not file.closed
17 assert not file.closed
18 assert os.path.exists(name)
18 assert Path(name).exists()
19 file.write(b'test')
19 file.write(b'test')
20 assert file.closed
20 assert file.closed
21 assert not os.path.exists(name)
21 assert not Path(name).exists()
22
22
23 def test_temporary_working_directory():
23 def test_temporary_working_directory():
24 with TemporaryWorkingDirectory() as dir:
24 with TemporaryWorkingDirectory() as directory:
25 assert os.path.exists(dir)
25 directory_path = Path(directory)
26 assert os.path.realpath(os.curdir) == os.path.realpath(dir)
26 assert directory_path.exists()
27 assert not os.path.exists(dir)
27 assert Path.cwd() == directory_path.resolve()
28 assert os.path.abspath(os.curdir) != dir
28 assert not directory_path.exists()
29 assert Path.cwd() != directory_path.resolve()
General Comments 0
You need to be logged in to leave comments. Login now