codex-python / tests /conftest.py
tailot's picture
Upload folder using huggingface_hub
48f2b75 verified
Raw History Blame Contribute Delete
2.05 kB
"""Shared pytest fixtures for the external datasets/codex-python package."""
from __future__ import annotations
import os
import shutil
import stat
import subprocess
import sys
from pathlib import Path
import pytest
sys.dont_write_bytecode = True
PYTHON_ROOT = Path(__file__).resolve().parent.parent
if str(PYTHON_ROOT) not in sys.path:
sys.path.insert(0, str(PYTHON_ROOT))
from lingologos_python._codex_bridge import ensure_codex_on_path # noqa: E402
ensure_codex_on_path(PYTHON_ROOT)
def make_dir_link(link: Path, target: Path) -> bool:
"""Create a directory link, junction on Windows, and report whether it exists."""
try:
os.symlink(str(target), str(link), target_is_directory=True)
return link.is_symlink()
except (OSError, NotImplementedError):
pass
if os.name == "nt":
done = subprocess.run(
["cmd", "/c", "mklink", "/J", str(link), str(target)],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if done.returncode != 0:
return False
# Python 3.12 no longer calls a junction a symlink, so check the reparse bit.
attributes = getattr(link.lstat(), "st_file_attributes", 0)
return bool(attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT)
return False
@pytest.fixture
def dir_link() -> "object":
"""Fixture form of make_dir_link, for tests that must skip when the host refuses."""
return make_dir_link
@pytest.fixture
def sample_pkg(tmp_path: Path) -> Path:
"""Copy fixtures/sample_pkg into a clean temporary directory per test."""
src = PYTHON_ROOT / "fixtures" / "sample_pkg"
dst = tmp_path / "sample_pkg"
shutil.copytree(src, dst)
return dst
@pytest.fixture
def pattern_lab(tmp_path: Path) -> Path:
"""A private copy of the pattern fixture, for tests that write into it."""
src = PYTHON_ROOT / "fixtures" / "pattern_lab"
dst = tmp_path / "pattern_lab"
shutil.copytree(src, dst)
return dst