Option C Tools
Loading...
Searching...
No Matches
conftest.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_core/conftest.py
4
5"""
6Purpose
7-------
8Provide shared fixtures for ``oct.core`` tests, in particular the git
9integration tests in ``test_git.py`` (Phase 4A / G-A6).
10
11Responsibilities
12----------------
13- ``git_repo``: initialise a real git repository in a pytest tmp_path
14 and return its resolved path. Skips the test if the ``git`` binary is
15 not available on the host.
16- ``can_symlink``: cached session-scoped probe that determines whether
17 the platform allows creating symlinks in a temp directory (Linux/
18 macOS always, Windows only with Developer Mode or admin).
19
20Diagnostics
21-----------
22Domain: CORE-TESTS
23Levels:
24 L2 — fixture lifecycle
25 L3 — fixture setup details
26 L4 — deep tracing
27
28Contracts
29---------
30- ``git_repo`` always returns a directory that is a real git working
31 tree with ``user.name`` / ``user.email`` pre-configured so that
32 commit operations in tests do not fail on unconfigured identity.
33- ``can_symlink`` must never raise; it returns ``True`` or ``False``.
34"""
35
36import os
37import shutil
38import subprocess
39from pathlib import Path
40
41import pytest
42
43
44@pytest.fixture(scope="session")
45def can_symlink(tmp_path_factory) -> bool:
46 """Session-cached probe: True if symlink creation works on this host."""
47 probe_dir = tmp_path_factory.mktemp("symlink_probe")
48 target = probe_dir / "target.txt"
49 link = probe_dir / "link.txt"
50 target.write_text("x", encoding="utf-8")
51 try:
52 os.symlink(target, link)
53 except (OSError, NotImplementedError):
54 return False
55 return link.exists()
56
57
58def _git_available() -> bool:
59 return shutil.which("git") is not None
60
61
62@pytest.fixture
63def git_repo(tmp_path: Path) -> Path:
64 """Initialise a real git repo in tmp_path; skip if git is missing."""
65 if not _git_available():
66 pytest.skip("git binary not available on PATH")
67 repo = tmp_path / "repo"
68 repo.mkdir()
69 subprocess.run(
70 ["git", "init", "--quiet", "-b", "main"],
71 cwd=repo, check=True, capture_output=True, text=True,
72 )
73 subprocess.run(
74 ["git", "config", "user.email", "test@example.com"],
75 cwd=repo, check=True, capture_output=True, text=True,
76 )
77 subprocess.run(
78 ["git", "config", "user.name", "Test"],
79 cwd=repo, check=True, capture_output=True, text=True,
80 )
81 subprocess.run(
82 ["git", "config", "commit.gpgsign", "false"],
83 cwd=repo, check=True, capture_output=True, text=True,
84 )
85 return repo.resolve()
86
87
88def commit_file(repo: Path, name: str, content: str, message: str) -> None:
89 """Helper: write a file, stage it, commit it. Used by several tests."""
90 (repo / name).write_text(content, encoding="utf-8")
91 subprocess.run(
92 ["git", "add", name],
93 cwd=repo, check=True, capture_output=True, text=True,
94 )
95 subprocess.run(
96 ["git", "commit", "-m", message, "--quiet"],
97 cwd=repo, check=True, capture_output=True, text=True,
98 )
bool can_symlink(tmp_path_factory)
Definition conftest.py:45
None commit_file(Path repo, str name, str content, str message)
Definition conftest.py:88
bool _git_available()
Definition conftest.py:58
Path git_repo(Path tmp_path)
Definition conftest.py:63