Skip to content

Commit 4bfbffd

Browse files
committed
TST: Replace ensure_clean with pytest temp_file/tmp_path fixtures in parser tests
1 parent 8476e0f commit 4bfbffd

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

pandas/tests/io/parser/common/test_read_errors.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def test_null_byte_char(request, all_parsers):
246246

247247

248248
@pytest.mark.filterwarnings("always::ResourceWarning")
249-
def test_open_file(all_parsers):
249+
def test_open_file(all_parsers, temp_file):
250250
# GH 39024
251251
parser = all_parsers
252252

@@ -259,14 +259,13 @@ def test_open_file(all_parsers):
259259
msg = "'utf-8' codec can't decode byte 0xe4"
260260
err = ValueError
261261

262-
with tm.ensure_clean() as path:
263-
file = Path(path)
264-
file.write_bytes(b"\xe4\na\n1")
262+
file = Path(temp_file)
263+
file.write_bytes(b"\xe4\na\n1")
265264

266-
with tm.assert_produces_warning(None):
267-
# should not trigger a ResourceWarning
268-
with pytest.raises(err, match=msg):
269-
parser.read_csv(file, sep=None, encoding_errors="replace")
265+
with tm.assert_produces_warning(None):
266+
# should not trigger a ResourceWarning
267+
with pytest.raises(err, match=msg):
268+
parser.read_csv(file, sep=None, encoding_errors="replace")
270269

271270

272271
def test_invalid_on_bad_line(all_parsers):

pandas/tests/io/parser/dtypes/test_dtypes_basic.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
@pytest.mark.parametrize("dtype", [str, object])
3030
@pytest.mark.parametrize("check_orig", [True, False])
3131
@pytest.mark.usefixtures("pyarrow_xfail")
32-
def test_dtype_all_columns(all_parsers, dtype, check_orig, using_infer_string):
32+
def test_dtype_all_columns(
33+
all_parsers, dtype, check_orig, using_infer_string, tmp_path
34+
):
3335
# see gh-3795, gh-6607
3436
parser = all_parsers
3537

@@ -39,20 +41,20 @@ def test_dtype_all_columns(all_parsers, dtype, check_orig, using_infer_string):
3941
index=["1A", "1B", "1C", "1D", "1E"],
4042
)
4143

42-
with tm.ensure_clean("__passing_str_as_dtype__.csv") as path:
43-
df.to_csv(path)
44+
path = tmp_path / "__passing_str_as_dtype__.csv"
45+
df.to_csv(path)
4446

45-
result = parser.read_csv(path, dtype=dtype, index_col=0)
47+
result = parser.read_csv(path, dtype=dtype, index_col=0)
4648

47-
if check_orig:
48-
expected = df.copy()
49-
result = result.astype(float)
50-
elif using_infer_string and dtype is str:
51-
expected = df.astype(str)
52-
else:
53-
expected = df.astype(str).astype(object)
49+
if check_orig:
50+
expected = df.copy()
51+
result = result.astype(float)
52+
elif using_infer_string and dtype is str:
53+
expected = df.astype(str)
54+
else:
55+
expected = df.astype(str).astype(object)
5456

55-
tm.assert_frame_equal(result, expected)
57+
tm.assert_frame_equal(result, expected)
5658

5759

5860
@pytest.mark.usefixtures("pyarrow_xfail")

pandas/tests/io/parser/test_c_parser_only.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,19 @@ def test_dtype_and_names_error(c_parser_only):
130130
],
131131
ids=["dt64-0", "dt64-1", "td64", f"{tm.ENDIAN}U8"],
132132
)
133-
def test_unsupported_dtype(c_parser_only, match, kwargs):
133+
def test_unsupported_dtype(c_parser_only, match, kwargs, tmp_path):
134134
parser = c_parser_only
135135
df = DataFrame(
136136
np.random.default_rng(2).random((5, 2)),
137137
columns=list("AB"),
138138
index=["1A", "1B", "1C", "1D", "1E"],
139139
)
140140

141-
with tm.ensure_clean("__unsupported_dtype__.csv") as path:
142-
df.to_csv(path)
141+
path = tmp_path / "__unsupported_dtype__.csv"
142+
df.to_csv(path)
143143

144-
with pytest.raises(TypeError, match=match):
145-
parser.read_csv(path, index_col=0, **kwargs)
144+
with pytest.raises(TypeError, match=match):
145+
parser.read_csv(path, index_col=0, **kwargs)
146146

147147

148148
@td.skip_if_32bit
@@ -563,27 +563,27 @@ def test_file_handles_mmap(c_parser_only, csv1):
563563
assert not m.closed
564564

565565

566-
def test_file_binary_mode(c_parser_only):
566+
def test_file_binary_mode(c_parser_only, temp_file):
567567
# see gh-23779
568568
parser = c_parser_only
569569
expected = DataFrame([[1, 2, 3], [4, 5, 6]])
570570

571-
with tm.ensure_clean() as path:
572-
with open(path, "w", encoding="utf-8") as f:
573-
f.write("1,2,3\n4,5,6")
571+
path = temp_file
572+
with open(path, "w", encoding="utf-8") as f:
573+
f.write("1,2,3\n4,5,6")
574574

575-
with open(path, "rb") as f:
576-
result = parser.read_csv(f, header=None)
577-
tm.assert_frame_equal(result, expected)
575+
with open(path, "rb") as f:
576+
result = parser.read_csv(f, header=None)
577+
tm.assert_frame_equal(result, expected)
578578

579579

580-
def test_unix_style_breaks(c_parser_only):
580+
def test_unix_style_breaks(c_parser_only, temp_file):
581581
# GH 11020
582582
parser = c_parser_only
583-
with tm.ensure_clean() as path:
584-
with open(path, "w", newline="\n", encoding="utf-8") as f:
585-
f.write("blah\n\ncol_1,col_2,col_3\n\n")
586-
result = parser.read_csv(path, skiprows=2, encoding="utf-8", engine="c")
583+
path = temp_file
584+
with open(path, "w", newline="\n", encoding="utf-8") as f:
585+
f.write("blah\n\ncol_1,col_2,col_3\n\n")
586+
result = parser.read_csv(path, skiprows=2, encoding="utf-8", engine="c")
587587
expected = DataFrame(columns=["col_1", "col_2", "col_3"])
588588
tm.assert_frame_equal(result, expected)
589589

0 commit comments

Comments
 (0)