189 lines
6.4 KiB
Python
189 lines
6.4 KiB
Python
"""Tests for wiki gitignore seeding and matching.
|
|
|
|
Generated by AI/LLM: 2026-09-30 - cover repository ignore patterns.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pathlib
|
|
|
|
import pytest
|
|
|
|
from fiona.wiki.ignore import (
|
|
ensure_wiki_gitignore,
|
|
gitignore_patterns_from_text,
|
|
packaged_gitignore_path,
|
|
path_is_ignored,
|
|
repository_gitignore_path,
|
|
repository_gitignore_text,
|
|
wiki_gitignore_patterns,
|
|
wiki_gitignore_text,
|
|
)
|
|
|
|
|
|
def test_patterns_skip_comments_and_blanks():
|
|
"""Test comment lines and blank lines are not patterns."""
|
|
text = "# comment\n\n__pycache__/\n *.pyc \n"
|
|
|
|
assert gitignore_patterns_from_text(text) == ["__pycache__/", "*.pyc"]
|
|
|
|
|
|
def test_path_matches_segment_and_directory_prefix():
|
|
"""Test unanchored names and trailing-slash directory patterns."""
|
|
patterns = ["*.pyc", "__pycache__/", "*fiona.log", "build/"]
|
|
|
|
assert path_is_ignored("utils/scripts/__pycache__/example.pyc", patterns)
|
|
assert path_is_ignored(
|
|
"utils/scripts/__pycache__/example.cpython-313.pyc", patterns
|
|
)
|
|
assert path_is_ignored("example.pyc", patterns)
|
|
assert path_is_ignored("fiona.log", patterns)
|
|
assert path_is_ignored("nested/myfiona.log", patterns)
|
|
assert path_is_ignored("build/lib/out.py", patterns)
|
|
assert not path_is_ignored("utils/scripts/example.py", patterns)
|
|
assert not path_is_ignored("fiona.log.bak", patterns)
|
|
|
|
|
|
def test_path_matches_pattern_that_contains_a_slash():
|
|
"""Test a pattern with a slash matches that relative prefix."""
|
|
assert path_is_ignored("utils/scripts/example.py", ["utils/scripts/*"])
|
|
assert path_is_ignored("utils/scripts/example.py", ["utils/scripts/"])
|
|
assert path_is_ignored("secret/data.md", ["secret/"])
|
|
assert not path_is_ignored("other/example.py", ["utils/scripts/*"])
|
|
assert not path_is_ignored("file.py", ["/"])
|
|
|
|
|
|
def test_repository_gitignore_is_the_fiona_repo_file():
|
|
"""Test the seed file is the fiona-assistant repository .gitignore."""
|
|
path = repository_gitignore_path()
|
|
|
|
assert path is not None
|
|
assert path.name == ".gitignore"
|
|
assert "__pycache__/" in path.read_text(encoding="utf-8")
|
|
|
|
|
|
def test_packaged_copy_matches_repository_file():
|
|
"""Test the packaged fallback has the same text as the repository file."""
|
|
packaged = packaged_gitignore_path()
|
|
|
|
assert packaged is not None
|
|
packaged_text = packaged.read_text(encoding="utf-8").replace("\r\n", "\n").replace("\r", "\n")
|
|
if packaged_text and not packaged_text.endswith("\n"):
|
|
packaged_text += "\n"
|
|
assert packaged_text == repository_gitignore_text()
|
|
|
|
|
|
def test_wiki_gitignore_adds_personal():
|
|
"""Test the wiki ignore set starts from the repository and adds personal/."""
|
|
patterns = wiki_gitignore_patterns()
|
|
|
|
assert "__pycache__/" in patterns
|
|
assert "*.pyc" in patterns
|
|
assert "*fiona.log" in patterns
|
|
assert "personal/" in patterns
|
|
assert wiki_gitignore_text().endswith("personal/\n")
|
|
|
|
|
|
def test_ensure_writes_a_new_file(tmp_path: pathlib.Path):
|
|
"""Test a missing .gitignore is created from the repository set."""
|
|
ensure_wiki_gitignore(tmp_path)
|
|
|
|
text = (tmp_path / ".gitignore").read_text(encoding="utf-8")
|
|
assert "__pycache__/" in text.splitlines()
|
|
assert "personal/" in text.splitlines()
|
|
|
|
|
|
def test_ensure_appends_missing_patterns(tmp_path: pathlib.Path):
|
|
"""Test an existing .gitignore keeps its lines and gains missing patterns."""
|
|
(tmp_path / ".gitignore").write_text("keep-me\npersonal/\n", encoding="utf-8")
|
|
|
|
ensure_wiki_gitignore(tmp_path)
|
|
|
|
lines = (tmp_path / ".gitignore").read_text(encoding="utf-8").splitlines()
|
|
assert lines[0] == "keep-me"
|
|
assert "personal/" in lines
|
|
assert lines.count("personal/") == 1
|
|
assert "__pycache__/" in lines
|
|
|
|
|
|
def test_ensure_leaves_a_complete_file_unchanged(tmp_path: pathlib.Path):
|
|
"""Test a file that already has every pattern is not rewritten."""
|
|
target = tmp_path / ".gitignore"
|
|
target.write_text(wiki_gitignore_text(), encoding="utf-8")
|
|
before = target.read_bytes()
|
|
|
|
ensure_wiki_gitignore(tmp_path)
|
|
|
|
assert target.read_bytes() == before
|
|
|
|
|
|
def test_repository_text_normalizes_newlines(tmp_path, monkeypatch):
|
|
"""Test CR LF is folded to LF and a missing final newline is added."""
|
|
source = tmp_path / ".gitignore"
|
|
source.write_bytes(b"__pycache__/\r\n*.pyc")
|
|
monkeypatch.setattr(
|
|
"fiona.wiki.ignore.repository_gitignore_path", lambda: source
|
|
)
|
|
|
|
assert repository_gitignore_text() == "__pycache__/\n*.pyc\n"
|
|
|
|
|
|
def test_wiki_text_keeps_personal_when_already_present(monkeypatch):
|
|
"""Test personal/ is not duplicated when the repository file lists it."""
|
|
monkeypatch.setattr(
|
|
"fiona.wiki.ignore.repository_gitignore_text",
|
|
lambda: "__pycache__/\npersonal/\n",
|
|
)
|
|
|
|
assert wiki_gitignore_text() == "__pycache__/\npersonal/\n"
|
|
assert wiki_gitignore_patterns() == ["__pycache__/", "personal/"]
|
|
|
|
|
|
def test_packaged_path_missing(monkeypatch):
|
|
"""Test a missing packaged ignore file returns None."""
|
|
real_is_file = pathlib.Path.is_file
|
|
|
|
def fake_is_file(self):
|
|
if self.name == "default.gitignore":
|
|
return False
|
|
return real_is_file(self)
|
|
|
|
monkeypatch.setattr(pathlib.Path, "is_file", fake_is_file)
|
|
|
|
assert packaged_gitignore_path() is None
|
|
|
|
|
|
def test_ensure_appends_when_file_has_no_trailing_newline(tmp_path):
|
|
"""Test a file without a trailing newline still gains missing patterns."""
|
|
(tmp_path / ".gitignore").write_bytes(b"keep-me")
|
|
|
|
ensure_wiki_gitignore(tmp_path)
|
|
|
|
text = (tmp_path / ".gitignore").read_text(encoding="utf-8")
|
|
assert text.startswith("keep-me\n")
|
|
assert "__pycache__/" in text.splitlines()
|
|
|
|
|
|
def test_repository_text_falls_back_to_package_data(monkeypatch):
|
|
"""Test a checkout without the repo file uses the packaged copy."""
|
|
monkeypatch.setattr(
|
|
"fiona.wiki.ignore.repository_gitignore_path", lambda: None
|
|
)
|
|
|
|
text = repository_gitignore_text()
|
|
|
|
assert "__pycache__/" in text.splitlines()
|
|
|
|
|
|
def test_repository_text_raises_when_no_source_exists(monkeypatch):
|
|
"""Test a missing repository file and packaged copy is an error."""
|
|
monkeypatch.setattr(
|
|
"fiona.wiki.ignore.repository_gitignore_path", lambda: None
|
|
)
|
|
monkeypatch.setattr(
|
|
"fiona.wiki.ignore.packaged_gitignore_path", lambda: None
|
|
)
|
|
|
|
with pytest.raises(FileNotFoundError, match="repository .gitignore"):
|
|
repository_gitignore_text()
|