Files
fiona-assistant/tests/conftest.py
T
2026-09-29 16:01:16 +02:00

200 lines
6.0 KiB
Python

"""Pytest fixtures for librarian, mdoctor, and wiki sanity tests."""
import os
import shutil
import tempfile
import pytest
from fiona.librarian.librarian import Librarian
from fiona.mdoctor.mdoctor import MDoctor
from fiona.python_ruff.python_ruff import PythonRuff
from fiona.wiki.sanity_manager import run_wiki_checks
from fiona.wiki.wiki import get_llm_wiki_source_path, wiki_init
@pytest.fixture
def librarian():
"""Fixture that provides a Librarian instance."""
return Librarian()
@pytest.fixture
def mdoctor():
"""Fixture that provides an MDoctor instance."""
return MDoctor()
@pytest.fixture
def pyruff():
"""Fixture that provides an MDoctor instance."""
return PythonRuff()
@pytest.fixture
def temp_dir_with_files():
"""Fixture that creates a temporary directory with a known file structure."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create test files
test_files = [
os.path.join(temp_dir, "file1.txt"),
os.path.join(temp_dir, "file2.txt"),
os.path.join(temp_dir, "subdir", "file3.txt")
]
# Create the files and subdirectory
os.makedirs(os.path.dirname(test_files[2]), exist_ok=True)
for file_path in test_files:
with open(file_path, 'w') as f:
f.write("test content")
yield temp_dir, test_files
@pytest.fixture
def temp_dir_with_mdfiles():
"""Fixture that creates a temporary directory with a known file structure."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create test files
test_files = [
os.path.join(temp_dir, "file1.txt"),
os.path.join(temp_dir, "file2.txt"),
os.path.join(temp_dir, "subdir", "file3.txt")
]
md_files = [
os.path.join(temp_dir, "mdfile1.md"),
os.path.join(temp_dir, "mdfile2.md"),
os.path.join(temp_dir, "subdir", "mdfile3.md")
]
# Create the files and subdirectory
os.makedirs(os.path.dirname(test_files[2]), exist_ok=True)
for file_path in [*test_files, *md_files]:
with open(file_path, 'w') as f:
f.write("test content")
yield temp_dir, test_files, md_files
@pytest.fixture
def markdownlint_simulated_violations():
return [
{
'scan_file': 'file1.md',
'line': 1,
'column': 1,
'rule_id': 'MD100',
'rule_name': 'rule_name_01',
'description': 'description_01'
},
{
'scan_file': 'file1.md',
'line': 2,
'column': 1,
'rule_id': 'MD200',
'rule_name': 'rule_name_02',
'description': 'description_02'
},
{
'scan_file': 'file2.md',
'line': 2,
'column': 1,
'rule_id': 'MD400',
'rule_name': 'rule_name_04',
'description': 'description_04'
},
{
'scan_file': 'file2.md',
'line': 3,
'column': 1,
'rule_id': 'MD300',
'rule_name': 'rule_name_03',
'description': 'description_03'
},
]
@pytest.fixture
def markdownlint_actual_violations(mdoctor, tmp_path):
"""Scan a purpose-built violated sample.
The sample is generated in a temp directory instead of living under
src/ so the repo-wide markdownlint check never sees an intentionally
violating fixture file.
"""
violated_md_sample = tmp_path / "violated.md"
violated_md_sample.write_text(
"\n"
"# violations\n"
"\n"
"## violated_md_sample_01 blanks-around-headings,blanks-around-headers\n"
"```text\n"
"Headings should be surrounded by blank lines\n"
"````\n",
encoding="utf-8",
)
return mdoctor.scan_file_programmatically(str(violated_md_sample)), violated_md_sample
@pytest.fixture
def compressed_violations_no_extra(mdoctor, markdownlint_simulated_violations):
return mdoctor.compress_violations(
markdownlint_simulated_violations,
put_extra=False
)
@pytest.fixture
def ruff_sample_output():
return [
{
"code": "F401",
"filename": "/path/to/file.py",
"location": { "row": 3, "column": 8 },
"end_location": { "row": 3, "column": 14 },
"message": "`os` imported but unused",
"fix": {
"applicability": "safe",
"message": "Remove unused import: `os`"
}
}
]
@pytest.fixture(scope="session")
def pristine_wiki(tmp_path_factory):
"""Session-scoped llm-wiki initialized from the packaged template.
Tests receive it read-only. The full template makes every sanity
check scan ~20 markdown files, so the wiki is initialized once
per session instead of once per test.
"""
wiki_root = tmp_path_factory.mktemp("pristine") / "wiki"
wiki_init(str(wiki_root))
return wiki_root
@pytest.fixture(scope="session")
def pristine_check_results(pristine_wiki):
"""One shared run_wiki_checks pass over the pristine wiki.
Read-only assertions reuse the results instead of each test
re-running the whole check suite.
"""
return run_wiki_checks(str(pristine_wiki))
@pytest.fixture
def minimal_wiki(tmp_path):
"""Minimal llm-wiki for check-specific sanity tests.
Holds the template's fiona-settings.json plus one clean seeded
page: pymarkdown refuses to scan a wiki without markdown files,
and the seeded page keeps every other check at zero violations.
Tests append just the files their check needs so mdoctor scans
two markdown files instead of the whole template.
"""
wiki_root = tmp_path / "wiki"
concepts = wiki_root / "wiki" / "concepts"
concepts.mkdir(parents=True)
settings_source = get_llm_wiki_source_path() / "fiona-settings.json"
shutil.copy2(settings_source, wiki_root / "fiona-settings.json")
(concepts / "index.md").write_text(
"---\nname: index\nversion: 1.0.0\n---\n\n# Index\n",
encoding="utf-8",
)
return wiki_root