1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
# Copyright 2021-2024 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
#
import functools
import logging
import os
import stat
import tempfile
import textwrap
from typing import Optional
from typing import Tuple
import pytest
try:
from pytest import FixtureRequest
except ImportError:
from _pytest.fixtures import FixtureRequest
try: # pragma: no cover
import importlib.metadata
pkg_version = importlib.metadata.version
except ImportError: # pragma: no cover
try:
import importlib_metadata
pkg_version = importlib_metadata.version
except ImportError: # pragma: no cover
import pkg_resources
def pkg_version(package): # type: ignore[no-untyped-def]
return pkg_resources.get_distribution(package).version
log = logging.getLogger(__name__)
def pkg_version_info(package: str) -> Tuple[int, ...]:
"""
Return a version info tuple for the given package.
"""
return tuple(int(part) for part in pkg_version(package).split(".") if part.isdigit())
if pkg_version_info("pytest") >= (6, 2):
pytest_plugins = ["pytester"]
else: # pragma: no cover
@pytest.fixture
def pytester() -> None:
pytest.skip("The pytester fixture is not available in Pytest < 6.2.0")
class Tempfiles:
"""
Class which generates temporary files and cleans them when done.
"""
def __init__(self, request: FixtureRequest):
self.request = request
def makepyfile(
self, contents: str, prefix: Optional[str] = None, executable: bool = False
) -> str:
"""
Creates a python file and returns it's path.
"""
tfile = tempfile.NamedTemporaryFile("w", prefix=prefix or "tmp", suffix=".py", delete=False)
contents = textwrap.dedent(contents.lstrip("\n")).strip()
tfile.write(contents)
tfile.close()
if executable is True:
st = os.stat(tfile.name)
os.chmod(tfile.name, st.st_mode | stat.S_IEXEC)
self.request.addfinalizer(functools.partial(self._delete_temp_file, tfile.name))
with open(tfile.name, encoding="utf-8") as rfh:
log.debug(
"Created python file with contents:\n>>>>> %s >>>>>\n%s\n<<<<< %s <<<<<\n",
tfile.name,
rfh.read(),
tfile.name,
)
return tfile.name
def _delete_temp_file(self, fpath: str) -> None:
"""
Cleanup the temporary path.
"""
if os.path.exists(fpath): # pragma: no branch
os.unlink(fpath)
@pytest.fixture
def tempfiles(request: FixtureRequest) -> Tempfiles:
"""
Temporary files fixture.
"""
return Tempfiles(request)
|