Skip to content

Use tempfile for creating a file in home directory for a IO test #40122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 2, 2021
Merged
11 changes: 7 additions & 4 deletions pandas/tests/io/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
StringIO,
)
import mmap
import ntpath
import os
from pathlib import Path
import tempfile

import pytest

Expand Down Expand Up @@ -119,10 +121,11 @@ def test_infer_compression_from_path(self, extension, expected, path_type):
@pytest.mark.parametrize("path_type", [str, CustomFSPath, Path])
def test_get_handle_with_path(self, path_type):
# ignore LocalPath: it creates strange paths: /absolute/~/sometest
filename = path_type("~/sometest")
with icom.get_handle(filename, "w") as handles:
assert os.path.isabs(handles.handle.name)
assert os.path.expanduser(filename) == handles.handle.name
with tempfile.NamedTemporaryFile(dir=os.path.expanduser("~/")) as tmp:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use tm.ensure_clean

Copy link
Contributor Author

@chrispe chrispe Feb 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but current implementation of tm.ensure_clean doesn't support custom directories (which is needed in this test case). Should we then first extend that function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i guess this is fine. can you use pathlib.Path instead

filename = "~/" + ntpath.basename(tmp.name)
with icom.get_handle(filename, "r") as handles:
assert os.path.isabs(handles.handle.name)
assert os.path.expanduser(filename) == handles.handle.name

def test_get_handle_with_buffer(self):
input_buffer = StringIO()
Expand Down