Repository: modin-project/modin

Issue:
use `tmp_path` fixture instead of `ensure_clean_dir` as pandas 2.0.0 does



Discussion:

Resolving patch:
diff --git a/modin/conftest.py b/modin/conftest.py
--- a/modin/conftest.py
+++ b/modin/conftest.py
@@ -506,13 +506,11 @@ def make_sql_connection():
     Yields:
         Factory that generates sql connection objects
     """
-    filenames = []
 
     def _sql_connection(filename, table=""):
         # Remove file if exists
         if os.path.exists(filename):
             os.remove(filename)
-        filenames.append(filename)
         # Create connection and, if needed, table
         conn = "sqlite:///{}".format(filename)
         if table:


Test patch:
diff --git a/modin/experimental/pandas/test/test_io_exp.py b/modin/experimental/pandas/test/test_io_exp.py
--- a/modin/experimental/pandas/test/test_io_exp.py
+++ b/modin/experimental/pandas/test/test_io_exp.py
@@ -12,13 +12,12 @@
 # governing permissions and limitations under the License.
 
 from contextlib import nullcontext
-import os
 import glob
 import json
 
 import numpy as np
 import pandas
-from pandas._testing import ensure_clean, ensure_clean_dir
+from pandas._testing import ensure_clean
 import pytest
 
 import modin.experimental.pandas as pd
@@ -37,30 +36,29 @@
     Engine.get() not in ("Ray", "Unidist", "Dask"),
     reason=f"{Engine.get()} does not have experimental API",
 )
-def test_from_sql_distributed(make_sql_connection):
-    with ensure_clean_dir() as dirname:
-        filename = "test_from_sql_distributed.db"
-        table = "test_from_sql_distributed"
-        conn = make_sql_connection(os.path.join(dirname, filename), table)
-        query = "select * from {0}".format(table)
-
-        pandas_df = pandas.read_sql(query, conn)
-        modin_df_from_query = pd.read_sql(
-            query,
-            conn,
-            partition_column="col1",
-            lower_bound=0,
-            upper_bound=6,
-            max_sessions=2,
-        )
-        modin_df_from_table = pd.read_sql(
-            table,
-            conn,
-            partition_column="col1",
-            lower_bound=0,
-            upper_bound=6,
-            max_sessions=2,
-        )
+def test_from_sql_distributed(tmp_path, make_sql_connection):
+    filename = "test_from_sql_distributed.db"
+    table = "test_from_sql_distributed"
+    conn = make_sql_connection(tmp_path / filename, table)
+    query = "select * from {0}".format(table)
+
+    pandas_df = pandas.read_sql(query, conn)
+    modin_df_from_query = pd.read_sql(
+        query,
+        conn,
+        partition_column="col1",
+        lower_bound=0,
+        upper_bound=6,
+        max_sessions=2,
+    )
+    modin_df_from_table = pd.read_sql(
+        table,
+        conn,
+        partition_column="col1",
+        lower_bound=0,
+        upper_bound=6,
+        max_sessions=2,
+    )
 
     df_equals(modin_df_from_query, pandas_df)
     df_equals(modin_df_from_table, pandas_df)
@@ -70,18 +68,17 @@ def test_from_sql_distributed(make_sql_connection):
     Engine.get() not in ("Ray", "Unidist", "Dask"),
     reason=f"{Engine.get()} does not have experimental API",
 )
-def test_from_sql_defaults(make_sql_connection):
-    with ensure_clean_dir() as dirname:
-        filename = "test_from_sql_distributed.db"
-        table = "test_from_sql_distributed"
-        conn = make_sql_connection(os.path.join(dirname, filename), table)
-        query = "select * from {0}".format(table)
-
-        pandas_df = pandas.read_sql(query, conn)
-        with pytest.warns(UserWarning):
-            modin_df_from_query = pd.read_sql(query, conn)
-        with pytest.warns(UserWarning):
-            modin_df_from_table = pd.read_sql(table, conn)
+def test_from_sql_defaults(tmp_path, make_sql_connection):
+    filename = "test_from_sql_distributed.db"
+    table = "test_from_sql_distributed"
+    conn = make_sql_connection(tmp_path / filename, table)
+    query = "select * from {0}".format(table)
+
+    pandas_df = pandas.read_sql(query, conn)
+    with pytest.warns(UserWarning):
+        modin_df_from_query = pd.read_sql(query, conn)
+    with pytest.warns(UserWarning):
+        modin_df_from_table = pd.read_sql(table, conn)
 
     df_equals(modin_df_from_query, pandas_df)
     df_equals(modin_df_from_table, pandas_df)
@@ -308,7 +305,7 @@ def _custom_parser(io_input, **kwargs):
         )
         if AsyncReadMode.get():
             # If read operations are asynchronous, then the dataframes
-            # check should be inside `ensure_clean_dir` context
+            # check should be inside `ensure_clean` context
             # because the file may be deleted before actual reading starts
             df_equals(df1, df2)
     if not AsyncReadMode.get():
@@ -365,7 +362,7 @@ def columns_callback(io_input, **kwargs):
         )
         if AsyncReadMode.get():
             # If read operations are asynchronous, then the dataframes
-            # check should be inside `ensure_clean_dir` context
+            # check should be inside `ensure_clean` context
             # because the file may be deleted before actual reading starts
             df_equals(df1, df2)
     if not AsyncReadMode.get():
diff --git a/modin/pandas/test/test_io.py b/modin/pandas/test/test_io.py
--- a/modin/pandas/test/test_io.py
+++ b/modin/pandas/test/test_io.py
@@ -19,7 +19,7 @@
 from pandas.errors import ParserWarning
 import pandas._libs.lib as lib
 from pandas.core.dtypes.common import is_list_like
-from pandas._testing import ensure_clean, ensure_clean_dir
+from pandas._testing import ensure_clean
 from pathlib import Path
 from collections import OrderedDict
 from modin.config.envvars import MinPartitionSize
@@ -46,7 +46,6 @@
 import sys
 import sqlalchemy as sa
 import csv
-import tempfile
 from typing import Dict
 
 from .utils import (
@@ -141,12 +140,14 @@ def setup_clipboard(row_size=NROWS):
     df.to_clipboard()
 
 
-def parquet_eval_to_file(modin_obj, pandas_obj, fn, extension, **fn_kwargs):
+def parquet_eval_to_file(tmp_dir, modin_obj, pandas_obj, fn, extension, **fn_kwargs):
     """
     Helper function to test `to_parquet` method.
 
     Parameters
     ----------
+    tmp_dir : Union[str, Path]
+        Temporary directory.
     modin_obj : pd.DataFrame
         A Modin DataFrame or a Series to test `to_parquet` method.
     pandas_obj: pandas.DataFrame
@@ -156,75 +157,71 @@ def parquet_eval_to_file(modin_obj, pandas_obj, fn, extension, **fn_kwargs):
     extension : str
         Extension of the test file.
     """
-    with ensure_clean_dir() as dirname:
-        unique_filename_modin = get_unique_filename(
-            extension=extension, data_dir=dirname
-        )
-        unique_filename_pandas = get_unique_filename(
-            extension=extension, data_dir=dirname
-        )
+    unique_filename_modin = get_unique_filename(extension=extension, data_dir=tmp_dir)
+    unique_filename_pandas = get_unique_filename(extension=extension, data_dir=tmp_dir)
 
-        engine = fn_kwargs.get("engine", "auto")
+    engine = fn_kwargs.get("engine", "auto")
 
-        getattr(modin_obj, fn)(unique_filename_modin, **fn_kwargs)
-        getattr(pandas_obj, fn)(unique_filename_pandas, **fn_kwargs)
+    getattr(modin_obj, fn)(unique_filename_modin, **fn_kwargs)
+    getattr(pandas_obj, fn)(unique_filename_pandas, **fn_kwargs)
 
-        pandas_df = pandas.read_parquet(unique_filename_pandas, engine=engine)
-        modin_df = pd.read_parquet(unique_filename_modin, engine=engine)
+    pandas_df = pandas.read_parquet(unique_filename_pandas, engine=engine)
+    modin_df = pd.read_parquet(unique_filename_modin, engine=engine)
     df_equals(pandas_df, modin_df)
 
 
-def eval_to_file(modin_obj, pandas_obj, fn, extension, **fn_kwargs):
-    """Helper function to test `to_<extension>` methods.
+def eval_to_file(tmp_dir, modin_obj, pandas_obj, fn, extension, **fn_kwargs):
+    """
+    Test `fn` method of `modin_obj` and `pandas_obj`.
 
-    Args:
-        modin_obj: Modin DataFrame or Series to test `to_<extension>` method.
-        pandas_obj: Pandas DataFrame or Series to test `to_<extension>` method.
-        fn: name of the method, that should be tested.
-        extension: Extension of the test file.
+    Parameters
+    ----------
+    tmp_dir : Union[str, Path]
+        Temporary directory.
+    modin_obj: Modin DataFrame or Series
+        Object to test.
+    pandas_obj: Pandas DataFrame or Series
+        Object to test.
+    fn: str
+        Name of the method, that should be tested.
+    extension: str
+        Extension of the test file.
     """
-    with ensure_clean_dir() as dirname:
-        unique_filename_modin = get_unique_filename(
-            extension=extension, data_dir=dirname
-        )
-        unique_filename_pandas = get_unique_filename(
-            extension=extension, data_dir=dirname
-        )
-
-        # parameter `max_retries=0` is set for `to_csv` function on Ray engine,
-        # in order to increase the stability of tests, we repeat the call of
-        # the entire function manually
-        last_exception = None
-        for _ in range(3):
-            try:
-                getattr(modin_obj, fn)(unique_filename_modin, **fn_kwargs)
-            except EXCEPTIONS as err:
-                last_exception = err
-                continue
-            break
-        # If we do have an exception that's valid let's raise it
-        if last_exception:
-            raise last_exception
-
-        getattr(pandas_obj, fn)(unique_filename_pandas, **fn_kwargs)
+    unique_filename_modin = get_unique_filename(extension=extension, data_dir=tmp_dir)
+    unique_filename_pandas = get_unique_filename(extension=extension, data_dir=tmp_dir)
+
+    # parameter `max_retries=0` is set for `to_csv` function on Ray engine,
+    # in order to increase the stability of tests, we repeat the call of
+    # the entire function manually
+    last_exception = None
+    for _ in range(3):
+        try:
+            getattr(modin_obj, fn)(unique_filename_modin, **fn_kwargs)
+        except EXCEPTIONS as err:
+            last_exception = err
+            continue
+        break
+    # If we do have an exception that's valid let's raise it
+    if last_exception:
+        raise last_exception
 
-        assert assert_files_eq(unique_filename_modin, unique_filename_pandas)
+    getattr(pandas_obj, fn)(unique_filename_pandas, **fn_kwargs)
 
+    assert assert_files_eq(unique_filename_modin, unique_filename_pandas)
 
-@pytest.fixture
-def make_parquet_dir():
-    with tempfile.TemporaryDirectory() as tmp_dir:
 
-        def _make_parquet_dir(
-            dfs_by_filename: Dict[str, pandas.DataFrame], row_group_size: int
-        ):
-            for filename, df in dfs_by_filename.items():
-                df.to_parquet(
-                    os.path.join(tmp_dir, filename), row_group_size=row_group_size
-                )
-            return tmp_dir
+@pytest.fixture
+def make_parquet_dir(tmp_path):
+    def _make_parquet_dir(
+        dfs_by_filename: Dict[str, pandas.DataFrame], row_group_size: int
+    ):
+        for filename, df in dfs_by_filename.items():
+            df.to_parquet(
+                os.path.join(tmp_path, filename), row_group_size=row_group_size
+            )
+        return tmp_path
 
-        yield _make_parquet_dir
+    yield _make_parquet_dir
 
 
 @pytest.mark.usefixtures("TestReadCSVFixture")
@@ -1115,11 +1112,12 @@ def test_read_csv_wrong_path(self):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #2340",
     )
-    def test_to_csv(self, header, mode):
+    def test_to_csv(self, tmp_path, header, mode):
         pandas_df = generate_dataframe()
         modin_df = pd.DataFrame(pandas_df)
 
         eval_to_file(
+            tmp_path,
             modin_obj=modin_df,
             pandas_obj=pandas_df,
             fn="to_csv",
@@ -1136,11 +1134,15 @@ def test_to_csv(self, header, mode):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #2340",
     )
-    def test_dataframe_to_csv(self):
+    def test_dataframe_to_csv(self, tmp_path):
         pandas_df = pandas.read_csv(pytest.csvs_names["test_read_csv_regular"])
         modin_df = pd.DataFrame(pandas_df)
         eval_to_file(
-            modin_obj=modin_df, pandas_obj=pandas_df, fn="to_csv", extension="csv"
+            tmp_path,
+            modin_obj=modin_df,
+            pandas_obj=pandas_df,
+            fn="to_csv",
+            extension="csv",
         )
 
     @pytest.mark.skipif(
@@ -1151,13 +1153,17 @@ def test_dataframe_to_csv(self):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #2340",
     )
-    def test_series_to_csv(self):
+    def test_series_to_csv(self, tmp_path):
         pandas_s = pandas.read_csv(
             pytest.csvs_names["test_read_csv_regular"], usecols=["col1"]
         ).squeeze()
         modin_s = pd.Series(pandas_s)
         eval_to_file(
-            modin_obj=modin_s, pandas_obj=pandas_s, fn="to_csv", extension="csv"
+            tmp_path,
+            modin_obj=modin_s,
+            pandas_obj=pandas_s,
+            fn="to_csv",
+            extension="csv",
         )
 
     def test_read_csv_within_decorator(self):
@@ -1211,7 +1217,7 @@ def test_read_csv_file_handle(
                 modin_df = pd.read_csv(buffer)
             if AsyncReadMode.get():
                 # If read operations are asynchronous, then the dataframes
-                # check should be inside `ensure_clean_dir` context
+                # check should be inside `ensure_clean` context
                 # because the file may be deleted before actual reading starts
                 df_equals(modin_df, pandas_df)
         if not AsyncReadMode.get():
@@ -1271,7 +1277,7 @@ def test_read_csv_skiprows_corner_cases(self, skiprows, header):
             dtype="str",  # to avoid issues with heterogeneous data
         )
 
-    def test_to_csv_with_index(self):
+    def test_to_csv_with_index(self, tmp_path):
         cols = 100
         arows = 20000
         keyrange = 100
@@ -1289,7 +1295,7 @@ def test_to_csv_with_index(self):
             values,
             columns=["key"] + ["avalue" + str(i) for i in range(1, 1 + cols)],
         ).set_index("key")
-        eval_to_file(modin_df, pandas_df, "to_csv", "csv")
+        eval_to_file(tmp_path, modin_df, pandas_df, "to_csv", "csv")
 
     @pytest.mark.parametrize("set_async_read_mode", [False, True], indirect=True)
     def test_read_csv_issue_5150(self, set_async_read_mode):
@@ -1303,7 +1309,7 @@ def test_read_csv_issue_5150(self, set_async_read_mode):
             actual_pandas_df = modin_df._to_pandas()
             if AsyncReadMode.get():
                 # If read operations are asynchronous, then the dataframes
-                # check should be inside `ensure_clean_dir` context
+                # check should be inside `ensure_clean` context
                 # because the file may be deleted before actual reading starts
                 df_equals(expected_pandas_df, actual_pandas_df)
         if not AsyncReadMode.get():
@@ -1341,7 +1347,7 @@ def wrapped_read_table(file, method):
 
             if AsyncReadMode.get():
                 # If read operations are asynchronous, then the dataframes
-                # check should be inside `ensure_clean_dir` context
+                # check should be inside `ensure_clean` context
                 # because the file may be deleted before actual reading starts
                 df_equals(modin_df, pandas_df)
         if not AsyncReadMode.get():
@@ -1398,7 +1404,7 @@ def test_read_parquet_list_of_files_5698(self, engine, make_parquet_file):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_read_parquet_indexing_by_column(self, engine, make_parquet_file):
+    def test_read_parquet_indexing_by_column(self, tmp_path, engine, make_parquet_file):
         # Test indexing into a column of Modin with various parquet file row lengths.
         # Specifically, tests for https://github.com/modin-project/modin/issues/3527
         # which fails when min_partition_size < nrows < min_partition_size * (num_partitions - 1)
@@ -1406,11 +1412,10 @@ def test_read_parquet_indexing_by_column(self, engine, make_parquet_file):
         nrows = (
             MinPartitionSize.get() + 1
         )  # Use the minimal guaranteed failing value for nrows.
-        with ensure_clean_dir() as dirname:
-            unique_filename = get_unique_filename(extension="parquet", data_dir=dirname)
-            make_parquet_file(filename=unique_filename, nrows=nrows)
+        unique_filename = get_unique_filename(extension="parquet", data_dir=tmp_path)
+        make_parquet_file(filename=unique_filename, nrows=nrows)
 
-            parquet_df = pd.read_parquet(unique_filename, engine=engine)
+        parquet_df = pd.read_parquet(unique_filename, engine=engine)
         for col in parquet_df.columns:
             parquet_df[col]
 
@@ -1460,19 +1465,18 @@ def test_read_parquet_directory(
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
     def test_read_parquet_partitioned_directory(
-        self, make_parquet_file, columns, engine
+        self, tmp_path, make_parquet_file, columns, engine
     ):
-        with ensure_clean_dir() as dirname:
-            unique_filename = get_unique_filename(extension=None, data_dir=dirname)
-            make_parquet_file(filename=unique_filename, partitioned_columns=["col1"])
+        unique_filename = get_unique_filename(extension=None, data_dir=tmp_path)
+        make_parquet_file(filename=unique_filename, partitioned_columns=["col1"])
 
-            eval_io(
-                fn_name="read_parquet",
-                # read_parquet kwargs
-                engine=engine,
-                path=unique_filename,
-                columns=columns,
-            )
+        eval_io(
+            fn_name="read_parquet",
+            # read_parquet kwargs
+            engine=engine,
+            path=unique_filename,
+            columns=columns,
+        )
 
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
@@ -1536,7 +1540,7 @@ def test_read_parquet_pandas_index(self, engine):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_read_parquet_pandas_index_partitioned(self, engine):
+    def test_read_parquet_pandas_index_partitioned(self, tmp_path, engine):
         # Ensure modin can read parquet files written by pandas with a non-RangeIndex object
         pandas_df = pandas.DataFrame(
             {
@@ -1546,16 +1550,15 @@ def test_read_parquet_pandas_index_partitioned(self, engine):
                 "C": ["c"] * 2000,
             }
         )
-        with ensure_clean_dir() as dirname:
-            unique_filename = get_unique_filename(extension="parquet", data_dir=dirname)
-            pandas_df.set_index("idx").to_parquet(unique_filename, partition_cols=["A"])
-            # read the same parquet using modin.pandas
-            eval_io(
-                "read_parquet",
-                # read_parquet kwargs
-                path=unique_filename,
-                engine=engine,
-            )
+        unique_filename = get_unique_filename(extension="parquet", data_dir=tmp_path)
+        pandas_df.set_index("idx").to_parquet(unique_filename, partition_cols=["A"])
+        # read the same parquet using modin.pandas
+        eval_io(
+            "read_parquet",
+            # read_parquet kwargs
+            path=unique_filename,
+            engine=engine,
+        )
 
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
@@ -1604,49 +1607,48 @@ def test_read_parquet_s3(self, path_type, engine):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_read_parquet_without_metadata(self, engine):
+    def test_read_parquet_without_metadata(self, tmp_path, engine):
         """Test that Modin can read parquet files not written by pandas."""
         from pyarrow import csv
         from pyarrow import parquet
 
-        with ensure_clean_dir() as dirname:
-            parquet_fname = get_unique_filename(extension="parquet", data_dir=dirname)
-            csv_fname = get_unique_filename(extension="parquet", data_dir=dirname)
-            pandas_df = pandas.DataFrame(
-                {
-                    "idx": np.random.randint(0, 100_000, size=2000),
-                    "A": np.random.randint(0, 10, size=2000),
-                    "B": ["a", "b"] * 1000,
-                    "C": ["c"] * 2000,
-                }
-            )
-            pandas_df.to_csv(csv_fname, index=False)
-            # read into pyarrow table and write it to a parquet file
-            t = csv.read_csv(csv_fname)
-            parquet.write_table(t, parquet_fname)
+        parquet_fname = get_unique_filename(extension="parquet", data_dir=tmp_path)
+        csv_fname = get_unique_filename(extension="parquet", data_dir=tmp_path)
+        pandas_df = pandas.DataFrame(
+            {
+                "idx": np.random.randint(0, 100_000, size=2000),
+                "A": np.random.randint(0, 10, size=2000),
+                "B": ["a", "b"] * 1000,
+                "C": ["c"] * 2000,
+            }
+        )
+        pandas_df.to_csv(csv_fname, index=False)
+        # read into pyarrow table and write it to a parquet file
+        t = csv.read_csv(csv_fname)
+        parquet.write_table(t, parquet_fname)
 
-            eval_io(
-                "read_parquet",
-                # read_parquet kwargs
-                path=parquet_fname,
-                engine=engine,
-            )
+        eval_io(
+            "read_parquet",
+            # read_parquet kwargs
+            path=parquet_fname,
+            engine=engine,
+        )
 
-    def test_read_empty_parquet_file(self, engine):
+    def test_read_empty_parquet_file(self, tmp_path, engine):
         test_df = pandas.DataFrame()
-        with tempfile.TemporaryDirectory() as directory:
-            path = f"{directory}/data"
-            os.makedirs(path)
-            test_df.to_parquet(path + "/part-00000.parquet")
-            eval_io(fn_name="read_parquet", path=path, engine=engine)
+        path = tmp_path / "data"
+        path.mkdir()
+        test_df.to_parquet(path / "part-00000.parquet")
+        eval_io(fn_name="read_parquet", path=path, engine=engine)
 
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_to_parquet(self, engine):
+    def test_to_parquet(self, tmp_path, engine):
         modin_df, pandas_df = create_test_dfs(TEST_DATA)
         parquet_eval_to_file(
+            tmp_path,
             modin_obj=modin_df,
             pandas_obj=pandas_df,
             fn="to_parquet",
@@ -1654,13 +1656,14 @@ def test_to_parquet(self, engine):
             engine=engine,
         )
 
-    def test_to_parquet_keep_index(self, engine):
+    def test_to_parquet_keep_index(self, tmp_path, engine):
         data = {"c0": [0, 1] * 1000, "c1": [2, 3] * 1000}
         modin_df, pandas_df = create_test_dfs(data)
         modin_df.index.name = "foo"
         pandas_df.index.name = "foo"
 
         parquet_eval_to_file(
+            tmp_path,
             modin_obj=modin_df,
             pandas_obj=pandas_df,
             fn="to_parquet",
@@ -1694,43 +1697,39 @@ def test_to_parquet_s3(self, s3_resource, engine, s3_storage_options):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_read_parquet_2462(self, engine):
+    def test_read_parquet_2462(self, tmp_path, engine):
         test_df = pandas.DataFrame({"col1": [["ad_1", "ad_2"], ["ad_3"]]})
+        path = tmp_path / "data"
+        path.mkdir()
+        test_df.to_parquet(path / "part-00000.parquet", engine=engine)
+        read_df = pd.read_parquet(path, engine=engine)
+        df_equals(test_df, read_df)
 
-        with tempfile.TemporaryDirectory() as directory:
-            path = f"{directory}/data"
-            os.makedirs(path)
-            test_df.to_parquet(path + "/part-00000.parquet", engine=engine)
-            read_df = pd.read_parquet(path, engine=engine)
-
-            df_equals(test_df, read_df)
-
-    def test_read_parquet_5767(self, engine):
+    def test_read_parquet_5767(self, tmp_path, engine):
         test_df = pandas.DataFrame({"a": [1, 2, 3, 4], "b": [1, 1, 2, 2]})
+        path = tmp_path / "data"
+        path.mkdir()
         file_name = "modin_issue#0000.parquet"
-        with tempfile.TemporaryDirectory() as directory:
-            path = f"{directory}/data"
-            os.makedirs(path)
-            test_df.to_parquet(path + file_name, engine=engine, partition_cols=["b"])
-            read_df = pd.read_parquet(Path(path + file_name))
+        test_df.to_parquet(path / file_name, engine=engine, partition_cols=["b"])
+        read_df = pd.read_parquet(path / file_name)
         # both Modin and pandas read column "b" as a category
         df_equals(test_df, read_df.astype("int64"))
 
-    def test_read_parquet_5509(self, engine):
+    def test_read_parquet_5509(self, tmp_path, engine):
         test_df = pandas.DataFrame({"col_a": [1, 2, 3], "col_b": ["a", "b", "c"]})
 
-        with tempfile.TemporaryDirectory() as directory:
-            path = f"{directory}/data"
-            os.makedirs(path)
-            test_df.to_parquet(path + "/5509.parquet")
-            with warns_that_defaulting_to_pandas():
-                eval_io(
-                    fn_name="read_parquet",
-                    path=path + "/5509.parquet",
-                    columns=["col_b"],
-                    engine=engine,
-                    filters=[[("col_a", "==", 1)]],
-                )
+        path = tmp_path / "data"
+        path.mkdir()
+        file_name = "5509.parquet"
+        test_df.to_parquet(path / file_name)
+        with warns_that_defaulting_to_pandas():
+            eval_io(
+                fn_name="read_parquet",
+                path=str(path / file_name),
+                columns=["col_b"],
+                engine=engine,
+                filters=[[("col_a", "==", 1)]],
+            )
 
     def test_read_parquet_s3_with_column_partitioning(self, engine):
         # This test case comes from
@@ -1791,10 +1790,14 @@ def test_read_json_string_bytes(self, data):
             data.seek(0)
         df_equals(modin_df, pandas.read_json(data))
 
-    def test_to_json(self):
+    def test_to_json(self, tmp_path):
         modin_df, pandas_df = create_test_dfs(TEST_DATA)
         eval_to_file(
-            modin_obj=modin_df, pandas_obj=pandas_df, fn="to_json", extension="json"
+            tmp_path,
+            modin_obj=modin_df,
+            pandas_obj=pandas_df,
+            fn="to_json",
+            extension="json",
         )
 
     @pytest.mark.parametrize(
@@ -1989,23 +1992,24 @@ def test_ExcelFile(self, make_excel_file):
             pandas_excel_file.close()
 
     @pytest.mark.xfail(strict=False, reason="Flaky test, defaults to pandas")
-    def test_to_excel(self):
+    def test_to_excel(self, tmp_path):
         modin_df, pandas_df = create_test_dfs(TEST_DATA)
 
-        with ensure_clean_dir() as dir:
-            unique_filename_modin = get_unique_filename(extension="xlsx", data_dir=dir)
-            unique_filename_pandas = get_unique_filename(extension="xlsx", data_dir=dir)
+        unique_filename_modin = get_unique_filename(extension="xlsx", data_dir=tmp_path)
+        unique_filename_pandas = get_unique_filename(
+            extension="xlsx", data_dir=tmp_path
+        )
 
-            modin_writer = pandas.ExcelWriter(unique_filename_modin)
-            pandas_writer = pandas.ExcelWriter(unique_filename_pandas)
+        modin_writer = pandas.ExcelWriter(unique_filename_modin)
+        pandas_writer = pandas.ExcelWriter(unique_filename_pandas)
 
-            modin_df.to_excel(modin_writer)
-            pandas_df.to_excel(pandas_writer)
+        modin_df.to_excel(modin_writer)
+        pandas_df.to_excel(pandas_writer)
 
-            modin_writer.save()
-            pandas_writer.save()
+        modin_writer.save()
+        pandas_writer.save()
 
-            assert assert_files_eq(unique_filename_modin, unique_filename_pandas)
+        assert assert_files_eq(unique_filename_modin, unique_filename_pandas)
 
     @check_file_leaks
     @pytest.mark.xfail(
@@ -2041,33 +2045,28 @@ def test_read_hdf(self, make_hdf_file, format):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_HDFStore(self):
-        with ensure_clean_dir() as dirname:
-            unique_filename_modin = get_unique_filename(
-                extension="hdf", data_dir=dirname
-            )
-            unique_filename_pandas = get_unique_filename(
-                extension="hdf", data_dir=dirname
-            )
+    def test_HDFStore(self, tmp_path):
+        unique_filename_modin = get_unique_filename(extension="hdf", data_dir=tmp_path)
+        unique_filename_pandas = get_unique_filename(extension="hdf", data_dir=tmp_path)
 
-            modin_store = pd.HDFStore(unique_filename_modin)
-            pandas_store = pandas.HDFStore(unique_filename_pandas)
+        modin_store = pd.HDFStore(unique_filename_modin)
+        pandas_store = pandas.HDFStore(unique_filename_pandas)
 
-            modin_df, pandas_df = create_test_dfs(TEST_DATA)
+        modin_df, pandas_df = create_test_dfs(TEST_DATA)
 
-            modin_store["foo"] = modin_df
-            pandas_store["foo"] = pandas_df
+        modin_store["foo"] = modin_df
+        pandas_store["foo"] = pandas_df
 
-            modin_df = modin_store.get("foo")
-            pandas_df = pandas_store.get("foo")
-            df_equals(modin_df, pandas_df)
+        modin_df = modin_store.get("foo")
+        pandas_df = pandas_store.get("foo")
+        df_equals(modin_df, pandas_df)
 
-            modin_store.close()
-            pandas_store.close()
-            modin_df = pandas.read_hdf(unique_filename_modin, key="foo", mode="r")
-            pandas_df = pandas.read_hdf(unique_filename_pandas, key="foo", mode="r")
-            df_equals(modin_df, pandas_df)
-            assert isinstance(modin_store, pd.HDFStore)
+        modin_store.close()
+        pandas_store.close()
+        modin_df = pandas.read_hdf(unique_filename_modin, key="foo", mode="r")
+        pandas_df = pandas.read_hdf(unique_filename_pandas, key="foo", mode="r")
+        df_equals(modin_df, pandas_df)
+        assert isinstance(modin_store, pd.HDFStore)
 
         with ensure_clean(".hdf5") as hdf_file:
             with pd.HDFStore(hdf_file, mode="w") as store:
@@ -2100,60 +2099,59 @@ class TestSql:
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
     @pytest.mark.parametrize("read_sql_engine", ["Pandas", "Connectorx"])
-    def test_read_sql(self, make_sql_connection, read_sql_engine):
-        with ensure_clean_dir() as dirname:
-            filename = get_unique_filename(".db")
-            table = "test_read_sql"
-            conn = make_sql_connection(os.path.join(dirname, filename), table)
-            query = f"select * from {table}"
+    def test_read_sql(self, tmp_path, make_sql_connection, read_sql_engine):
+        filename = get_unique_filename(".db")
+        table = "test_read_sql"
+        conn = make_sql_connection(tmp_path / filename, table)
+        query = f"select * from {table}"
 
-            eval_io(
-                fn_name="read_sql",
-                # read_sql kwargs
-                sql=query,
-                con=conn,
-            )
+        eval_io(
+            fn_name="read_sql",
+            # read_sql kwargs
+            sql=query,
+            con=conn,
+        )
 
-            eval_io(
-                fn_name="read_sql",
-                # read_sql kwargs
-                sql=query,
-                con=conn,
-                index_col="index",
-            )
+        eval_io(
+            fn_name="read_sql",
+            # read_sql kwargs
+            sql=query,
+            con=conn,
+            index_col="index",
+        )
 
-            with warns_that_defaulting_to_pandas():
-                pd.read_sql_query(query, conn)
+        with warns_that_defaulting_to_pandas():
+            pd.read_sql_query(query, conn)
 
-            with warns_that_defaulting_to_pandas():
-                pd.read_sql_table(table, conn)
+        with warns_that_defaulting_to_pandas():
+            pd.read_sql_table(table, conn)
 
-            # Test SQLAlchemy engine
-            sqlalchemy_engine = sa.create_engine(conn)
-            eval_io(
-                fn_name="read_sql",
-                # read_sql kwargs
-                sql=query,
-                con=sqlalchemy_engine,
-            )
+        # Test SQLAlchemy engine
+        sqlalchemy_engine = sa.create_engine(conn)
+        eval_io(
+            fn_name="read_sql",
+            # read_sql kwargs
+            sql=query,
+            con=sqlalchemy_engine,
+        )
 
-            # Test SQLAlchemy Connection
-            sqlalchemy_connection = sqlalchemy_engine.connect()
-            eval_io(
-                fn_name="read_sql",
-                # read_sql kwargs
-                sql=query,
-                con=sqlalchemy_connection,
-            )
+        # Test SQLAlchemy Connection
+        sqlalchemy_connection = sqlalchemy_engine.connect()
+        eval_io(
+            fn_name="read_sql",
+            # read_sql kwargs
+            sql=query,
+            con=sqlalchemy_connection,
+        )
 
-            ReadSqlEngine.put(read_sql_engine)
-            if ReadSqlEngine.get() == "Connectorx":
-                modin_df = pd.read_sql(sql=query, con=conn)
-            else:
-                modin_df = pd.read_sql(
-                    sql=query, con=ModinDatabaseConnection("sqlalchemy", conn)
-                )
-            pandas_df = pandas.read_sql(sql=query, con=sqlalchemy_connection)
+        ReadSqlEngine.put(read_sql_engine)
+        if ReadSqlEngine.get() == "Connectorx":
+            modin_df = pd.read_sql(sql=query, con=conn)
+        else:
+            modin_df = pd.read_sql(
+                sql=query, con=ModinDatabaseConnection("sqlalchemy", conn)
+            )
+        pandas_df = pandas.read_sql(sql=query, con=sqlalchemy_connection)
         df_equals(modin_df, pandas_df)
 
     @pytest.mark.skipif(
@@ -2222,26 +2220,25 @@ def test_read_sql_with_chunksize(self, make_sql_connection):
             df_equals(modin_df, pandas_df)
 
     @pytest.mark.parametrize("index", [False, True])
-    def test_to_sql(self, make_sql_connection, index):
+    def test_to_sql(self, tmp_path, make_sql_connection, index):
         table_name = f"test_to_sql_{str(index)}"
         modin_df, pandas_df = create_test_dfs(TEST_DATA)
 
-        with ensure_clean_dir() as dirname:
-            # We do not pass the table name so the fixture won't generate a table
-            conn = make_sql_connection(os.path.join(dirname, f"{table_name}_modin.db"))
-            modin_df.to_sql(table_name, conn, index=index)
-            df_modin_sql = pandas.read_sql(
-                table_name, con=conn, index_col="index" if index else None
-            )
+        # We do not pass the table name so the fixture won't generate a table
+        conn = make_sql_connection(tmp_path / f"{table_name}_modin.db")
+        modin_df.to_sql(table_name, conn, index=index)
+        df_modin_sql = pandas.read_sql(
+            table_name, con=conn, index_col="index" if index else None
+        )
 
-            # We do not pass the table name so the fixture won't generate a table
-            conn = make_sql_connection(os.path.join(dirname, f"{table_name}_pandas.db"))
-            pandas_df.to_sql(table_name, conn, index=index)
-            df_pandas_sql = pandas.read_sql(
-                table_name, con=conn, index_col="index" if index else None
-            )
+        # We do not pass the table name so the fixture won't generate a table
+        conn = make_sql_connection(tmp_path / f"{table_name}_pandas.db")
+        pandas_df.to_sql(table_name, conn, index=index)
+        df_pandas_sql = pandas.read_sql(
+            table_name, con=conn, index_col="index" if index else None
+        )
 
-            assert df_modin_sql.sort_index().equals(df_pandas_sql.sort_index())
+        assert df_modin_sql.sort_index().equals(df_pandas_sql.sort_index())
 
 
 class TestHtml:
@@ -2249,11 +2246,15 @@ class TestHtml:
     def test_read_html(self, make_html_file):
         eval_io(fn_name="read_html", io=make_html_file())
 
-    def test_to_html(self):
+    def test_to_html(self, tmp_path):
         modin_df, pandas_df = create_test_dfs(TEST_DATA)
 
         eval_to_file(
-            modin_obj=modin_df, pandas_obj=pandas_df, fn="to_html", extension="html"
+            tmp_path,
+            modin_obj=modin_df,
+            pandas_obj=pandas_df,
+            fn="to_html",
+            extension="html",
         )
 
 
@@ -2498,10 +2499,14 @@ def test_read_stata(self, make_stata_file):
             filepath_or_buffer=make_stata_file(),
         )
 
-    def test_to_stata(self):
+    def test_to_stata(self, tmp_path):
         modin_df, pandas_df = create_test_dfs(TEST_DATA)
         eval_to_file(
-            modin_obj=modin_df, pandas_obj=pandas_df, fn="to_stata", extension="stata"
+            tmp_path,
+            modin_obj=modin_df,
+            pandas_obj=pandas_df,
+            fn="to_stata",
+            extension="stata",
         )
 
 
@@ -2551,9 +2556,10 @@ def test_read_feather_path_object(self, make_feather_file):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_to_feather(self):
+    def test_to_feather(self, tmp_path):
         modin_df, pandas_df = create_test_dfs(TEST_DATA)
         eval_to_file(
+            tmp_path,
             modin_obj=modin_df,
             pandas_obj=pandas_df,
             fn="to_feather",
@@ -2593,22 +2599,21 @@ def test_read_pickle(self, make_pickle_file):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="There is no point in writing to local files.",
     )
-    def test_to_pickle(self):
+    def test_to_pickle(self, tmp_path):
         modin_df, pandas_df = create_test_dfs(TEST_DATA)
         eval_to_file(
-            modin_obj=modin_df, pandas_obj=pandas_df, fn="to_pickle", extension="pkl"
+            tmp_path,
+            modin_obj=modin_df,
+            pandas_obj=pandas_df,
+            fn="to_pickle",
+            extension="pkl",
         )
-        with ensure_clean_dir() as dirname:
-            unique_filename_modin = get_unique_filename(
-                extension="pkl", data_dir=dirname
-            )
-            unique_filename_pandas = get_unique_filename(
-                extension="pkl", data_dir=dirname
-            )
-            pd.to_pickle(modin_df, unique_filename_modin)
-            pandas.to_pickle(pandas_df, unique_filename_pandas)
+        unique_filename_modin = get_unique_filename(extension="pkl", data_dir=tmp_path)
+        unique_filename_pandas = get_unique_filename(extension="pkl", data_dir=tmp_path)
+        pd.to_pickle(modin_df, unique_filename_modin)
+        pandas.to_pickle(pandas_df, unique_filename_pandas)
 
-            assert assert_files_eq(unique_filename_modin, unique_filename_pandas)
+        assert assert_files_eq(unique_filename_modin, unique_filename_pandas)
 
 
 @pytest.mark.xfail(
diff --git a/modin/pandas/test/utils.py b/modin/pandas/test/utils.py
--- a/modin/pandas/test/utils.py
+++ b/modin/pandas/test/utils.py
@@ -12,6 +12,8 @@
 # governing permissions and limitations under the License.
 
 import re
+from pathlib import Path
+from typing import Union
 import pytest
 import numpy as np
 import math
@@ -1145,7 +1147,7 @@ def get_unique_filename(
     test_name: str = "test",
     kwargs: dict = {},
     extension: str = "csv",
-    data_dir: str = "",
+    data_dir: Union[str, Path] = "",
     suffix: str = "",
     debug_mode=False,
 ):
@@ -1159,7 +1161,7 @@ def get_unique_filename(
         Unique combiantion of test parameters for creation of unique name.
     extension: str
         Extension of unique file.
-    data_dir: str
+    data_dir: Union[str, Path]
         Data directory where test files will be created.
     suffix: str
         String to append to the resulted name.