File size: 2,045 Bytes
339ddd8
 
 
 
48f2b75
339ddd8
48f2b75
 
339ddd8
 
 
 
 
 
 
 
 
 
 
2a9dc4c
339ddd8
 
 
 
48f2b75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
339ddd8
 
 
 
 
 
 
1600cd6
 
 
 
 
 
 
 
 
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
"""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