Skip to content

Commit dfec26e

Browse files
committed
TST: use pytest fixture instead of ensure_clean_path
1 parent 53dba1a commit dfec26e

File tree

1 file changed

+17
-37
lines changed

1 file changed

+17
-37
lines changed

pandas/tests/io/pytables/test_compat.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,13 @@
1212
tables = pytest.importorskip('tables')
1313

1414

15-
def safe_remove(path):
16-
if path is not None:
17-
try:
18-
os.remove(path)
19-
except OSError:
20-
pass
21-
22-
23-
def create_tempfile(path):
24-
return os.path.join(tempfile.gettempdir(), path)
25-
26-
27-
@contextmanager
28-
def ensure_clean_path(path):
29-
try:
30-
if isinstance(path, list):
31-
filenames = [create_tempfile(p) for p in path]
32-
yield filenames
33-
else:
34-
filenames = [create_tempfile(path)]
35-
yield filenames[0]
36-
finally:
37-
for f in filenames:
38-
safe_remove(f)
39-
40-
4115
@pytest.fixture
42-
def pytables_hdf5_file():
43-
"""Use PyTables to create a simple HDF5 file."""
16+
def pytables_hdf5_file(tmp_path):
17+
"""Use PyTables to create a simple HDF5 file.
18+
19+
There is no need for temporary file cleanup, pytest makes sure that there is
20+
no collision between tests and that storage needs to not grow indefinitely.
21+
"""
4422

4523
table_schema = {
4624
'c0': tables.Time64Col(pos=0),
@@ -59,16 +37,18 @@ def pytables_hdf5_file():
5937

6038
objname = 'pandas_test_timeseries'
6139

62-
with ensure_clean_path('pytables_hdf5_file') as path:
63-
# The `ensure_clean_path` context mgr removes the temp file upon exit.
64-
with tables.open_file(path, mode='w') as f:
65-
t = f.create_table('/', name=objname, description=table_schema)
66-
for sample in testsamples:
67-
for key, value in sample.items():
68-
t.row[key] = value
69-
t.row.append()
40+
# The `tmp_path` fixture provides a temporary directory unique to the
41+
# individual test invocation. Create a file in there.
42+
h5path = tmp_path / 'written_with_pytables.h5'
43+
44+
with tables.open_file(h5path, mode='w') as f:
45+
t = f.create_table('/', name=objname, description=table_schema)
46+
for sample in testsamples:
47+
for key, value in sample.items():
48+
t.row[key] = value
49+
t.row.append()
7050

71-
yield path, objname, pd.DataFrame(testsamples)
51+
return h5path, objname, pd.DataFrame(testsamples)
7252

7353

7454
class TestReadPyTablesHDF5:

0 commit comments

Comments
 (0)