Skip to content

TST: Use uuid instead of random chars for temp files #45996

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 2 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions pandas/_testing/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
from contextlib import contextmanager
import os
from pathlib import Path
import random
from shutil import rmtree
import string
import tempfile
from typing import (
IO,
Any,
)
import uuid

import numpy as np

Expand Down Expand Up @@ -107,9 +106,7 @@ def ensure_clean(filename=None, return_filelike: bool = False, **kwargs: Any):

if filename is None:
filename = ""
filename = (
"".join(random.choices(string.ascii_letters + string.digits, k=30)) + filename
)
filename = str(uuid.uuid4()) + filename
path = folder / filename

path.touch()
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/io/parser/common/test_file_buffer_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import platform
from urllib.error import URLError
import uuid

import pytest

Expand Down Expand Up @@ -87,7 +88,7 @@ def test_nonexistent_path(all_parsers):
# gh-14086: raise more helpful FileNotFoundError
# GH#29233 "File foo" instead of "File b'foo'"
parser = all_parsers
path = f"{tm.rands(10)}.csv"
path = f"{uuid.uuid4()}.csv"

msg = r"\[Errno 2\]"
with pytest.raises(FileNotFoundError, match=msg) as e:
Expand Down Expand Up @@ -255,7 +256,7 @@ def test_internal_eof_byte_to_file(all_parsers):
parser = all_parsers
data = b'c1,c2\r\n"test \x1a test", test\r\n'
expected = DataFrame([["test \x1a test", " test"]], columns=["c1", "c2"])
path = f"__{tm.rands(10)}__.csv"
path = f"__{uuid.uuid4()}__.csv"

with tm.ensure_clean(path) as path:
with open(path, "wb") as f:
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/parser/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Tests encoding functionality during parsing
for all of the parsers defined in parsers.py
"""

from io import BytesIO
import os
import tempfile
import uuid

import numpy as np
import pytest
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_utf16_bom_skiprows(all_parsers, sep, encoding):
4,5,6""".replace(
",", sep
)
path = f"__{tm.rands(10)}__.csv"
path = f"__{uuid.uuid4()}__.csv"
kwargs = {"sep": sep, "skiprows": 2}
utf8 = "utf-8"

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/io/pytables/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import uuid

import pytest

import pandas._testing as tm
Expand All @@ -6,7 +8,7 @@
@pytest.fixture
def setup_path():
"""Fixture for setup path"""
return f"tmp.__{tm.rands(10)}__.h5"
return f"tmp.__{uuid.uuid4()}__.h5"


@pytest.fixture(scope="module", autouse=True)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/io/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pathlib import Path
import pickle
import shutil
import uuid
from warnings import (
catch_warnings,
filterwarnings,
Expand Down Expand Up @@ -248,7 +249,7 @@ def test_legacy_sparse_warning(datapath, typ):

@pytest.fixture
def get_random_path():
return f"__{tm.rands(10)}__.pickle"
return f"__{uuid.uuid4()}__.pickle"


class TestCompression:
Expand Down