Files
fiona-assistant/tests/test_git_diff_report.py
T

592 lines
20 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Tests for fiona.gittool.diff_report."""
from __future__ import annotations
import json
import pathlib
import subprocess
import git
import pytest
from fiona.gittool import diff_report
from fiona.wiki.ignore import wiki_gitignore_patterns
def _configure_test_repo(repo: git.Repo) -> None:
"""Set local git identity for commits in tests.
Generated by CURSOR: 2026-10-19 - isolated git user for pytest.
"""
with repo.config_writer() as config:
config.set_value("user", "email", "fiona-test@example.com")
config.set_value("user", "name", "Fiona Test")
@pytest.fixture
def wiki_git_repo(tmp_path: pathlib.Path) -> pathlib.Path:
"""Git repo with fiona-settings.json and main + HEAD commits."""
repo = git.Repo.init(tmp_path)
_configure_test_repo(repo)
settings = {
"fiona": {
"wiki": {
"graph": {
"theme": "night-sky",
}
}
}
}
(tmp_path / "fiona-settings.json").write_text(
json.dumps(settings),
encoding="utf-8",
)
(tmp_path / "README.md").write_text("base\n", encoding="utf-8")
repo.index.add(["fiona-settings.json", "README.md"])
repo.index.commit("base on main")
# Keep ``main`` at the first commit so HEAD can advance for a non-empty diff.
repo.create_head("main")
(tmp_path / "README.md").write_text("base\nchanged\n", encoding="utf-8")
repo.index.add(["README.md"])
repo.index.commit("feature commit")
return tmp_path
def test_wiki_gitignore_includes_tmp() -> None:
"""SPEC 7.12.5: packaged ignore lists wiki tmp/."""
patterns = wiki_gitignore_patterns()
assert "tmp/" in patterns
def test_resolve_default_base_uses_main(wiki_git_repo: pathlib.Path) -> None:
"""Default base is main when it exists."""
assert diff_report.resolve_default_base(wiki_git_repo) == "main"
def test_theme_css_uses_constellation(wiki_git_repo: pathlib.Path) -> None:
"""HTML theme CSS reflects night-sky background."""
css = diff_report.theme_css_for_wiki(wiki_git_repo)
assert "--bg: #070b14" in css
def test_write_report_under_tmp(wiki_git_repo: pathlib.Path) -> None:
"""Default output path is wiki tmp/ and HTML includes diff content."""
out = diff_report.default_output_path(wiki_git_repo, wiki_git_repo, "HEAD")
assert out.parent == wiki_git_repo / "tmp"
path = diff_report.write_report(
wiki_git_repo,
out,
base="main",
head="HEAD",
title=None,
subtitle=None,
open_details=True,
)
text = path.read_text(encoding="utf-8")
assert "changed" in text
assert "night-sky" not in text # theme name is not embedded; colors are
assert "--bg: #070b14" in text
def test_main_prints_path(wiki_git_repo: pathlib.Path, monkeypatch) -> None:
"""CLI exits 0 and prints the report path."""
monkeypatch.chdir(wiki_git_repo)
code = diff_report.main(["--base", "main"])
assert code == 0
def test_git_check_ignore_tmp(wiki_git_repo: pathlib.Path) -> None:
"""tmp/ is ignored after ensure_wiki_gitignore."""
from fiona.wiki.ignore import ensure_wiki_gitignore
ensure_wiki_gitignore(wiki_git_repo)
(wiki_git_repo / "tmp").mkdir()
report = wiki_git_repo / "tmp" / "sample.html"
report.write_text("<html></html>", encoding="utf-8")
result = subprocess.run(
["git", "check-ignore", "-q", "tmp/sample.html"],
cwd=wiki_git_repo,
check=False,
)
assert result.returncode == 0
def test_find_git_root_from_subdirectory(tmp_path: pathlib.Path) -> None:
"""SPEC 7.12.0: find_git_root discovers root from subdirectory."""
# Create a git repo at the root
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
(repo_path / "file.txt").write_text("root file", encoding="utf-8")
repo.index.add(["file.txt"])
repo.index.commit("initial commit")
# Create a subdirectory
subdir = repo_path / "subdir"
subdir.mkdir()
# The function should find the root even when called from subdirectory
result = diff_report.find_git_root(subdir)
assert result == repo_path
def test_main_from_subdirectory(tmp_path: pathlib.Path, monkeypatch) -> None:
"""SPEC 7.12.0: main() uses git root when run from subdirectory."""
# Create a git repo at the root
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
settings = {
"fiona": {
"wiki": {
"graph": {
"theme": "colorful",
}
}
}
}
(repo_path / "fiona-settings.json").write_text(
json.dumps(settings),
encoding="utf-8",
)
(repo_path / "file.txt").write_text("original\n", encoding="utf-8")
repo.index.add(["fiona-settings.json", "file.txt"])
repo.index.commit("initial commit")
repo.create_head("main")
# Create a subdirectory and change to it
subdir = repo_path / "subdir"
subdir.mkdir()
monkeypatch.chdir(subdir)
# Modify a file in the root
(repo_path / "file.txt").write_text("original\nmodified\n", encoding="utf-8")
repo.index.add(["file.txt"])
repo.index.commit("second commit")
# Run diff from subdirectory - should work and find the root repo
code = diff_report.main(["--base", "main"])
assert code == 0
def test_theme_light_for_non_wiki(tmp_path: pathlib.Path) -> None:
"""SPEC 7.12.5: non-wiki repos use default light theme with dark mode toggle."""
# Create a git repo WITHOUT fiona-settings.json
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
(repo_path / "file.txt").write_text("content", encoding="utf-8")
repo.index.add(["file.txt"])
repo.index.commit("initial")
# Generate report
out = tmp_path / "report.html"
path = diff_report.write_report(
repo_path,
out,
base="HEAD",
head="HEAD",
title="Test",
subtitle=None,
open_details=True,
)
text = path.read_text(encoding="utf-8")
# Check for dark theme colors (default)
assert "--bg: #1a1a1a;" in text
assert "--card: #2d2d2d;" in text
# Check for light mode toggle
assert "Toggle Light Mode" in text
# Check for theme CSS
assert '[data-theme="dark"]' in text
assert '[data-theme="light"]' in text
def test_theme_wiki_uses_wiki_theme(wiki_git_repo: pathlib.Path) -> None:
"""SPEC 7.12.5: wiki repos use their own constellation theme."""
out = diff_report.default_output_path(wiki_git_repo, wiki_git_repo, "HEAD")
path = diff_report.write_report(
wiki_git_repo,
out,
base="main",
head="HEAD",
title=None,
subtitle=None,
open_details=True,
)
text = path.read_text(encoding="utf-8")
# Night-sky theme has dark background
assert "--bg: #070b14" in text
# No toggle button for wikis
assert "Toggle Light Mode" not in text
def test_collapsed_file_diffs_except_first(tmp_path: pathlib.Path) -> None:
"""SPEC 7.12.6: All file diffs except the first are collapsed by default."""
# Create a git repo with multiple changed files
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
(repo_path / "file1.txt").write_text("file1\n", encoding="utf-8")
(repo_path / "file2.txt").write_text("file2\n", encoding="utf-8")
(repo_path / "file3.txt").write_text("file3\n", encoding="utf-8")
repo.index.add(["file1.txt", "file2.txt", "file3.txt"])
repo.index.commit("initial")
repo.create_head("main")
# Modify multiple files
(repo_path / "file1.txt").write_text("file1\nmodified1\n", encoding="utf-8")
(repo_path / "file2.txt").write_text("file2\nmodified2\n", encoding="utf-8")
(repo_path / "file3.txt").write_text("file3\nmodified3\n", encoding="utf-8")
repo.index.add(["file1.txt", "file2.txt", "file3.txt"])
repo.index.commit("changes")
# Generate report with open_details=True
out = tmp_path / "report.html"
path = diff_report.write_report(
repo_path,
out,
base="main",
head="HEAD",
title=None,
subtitle=None,
open_details=True, # Even with open_details=True, only first should be open
)
text = path.read_text(encoding="utf-8")
from bs4 import BeautifulSoup
soup = BeautifulSoup(text, 'html.parser')
details = soup.find_all('details', class_='file')
# Should have 3 file sections
assert len(details) == 3
# Only the first one should be open
assert 'open' in details[0].attrs
assert 'open' not in details[1].attrs
assert 'open' not in details[2].attrs
def test_untracked_files_included_in_diff(tmp_path: pathlib.Path) -> None:
"""SPEC 7.12.6: Untracked files are included in the diff report."""
# Create a git repo
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
(repo_path / "tracked.txt").write_text("tracked\n", encoding="utf-8")
repo.index.add(["tracked.txt"])
repo.index.commit("initial")
repo.create_head("main")
# Add an untracked file
(repo_path / "untracked.txt").write_text("untracked content\n", encoding="utf-8")
# Generate report
out = tmp_path / "report.html"
path = diff_report.write_report(
repo_path,
out,
base="main",
head="HEAD",
title=None,
subtitle=None,
open_details=True,
)
text = path.read_text(encoding="utf-8")
# Untracked file should be in the report
assert "untracked.txt" in text
# Should have a diff section for it
assert "file-untracked-txt" in text or "untracked.txt" in text
def test_deleted_files_included_in_diff(tmp_path: pathlib.Path) -> None:
"""SPEC 7.12.6: Deleted files are included in the diff report."""
# Create a git repo with a file
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
(repo_path / "to_delete.txt").write_text("will be deleted\n", encoding="utf-8")
repo.index.add(["to_delete.txt"])
repo.index.commit("initial")
repo.create_head("main")
# Delete the file
(repo_path / "to_delete.txt").unlink()
repo.index.remove(["to_delete.txt"])
repo.index.commit("deleted file")
# Generate report
out = tmp_path / "report.html"
path = diff_report.write_report(
repo_path,
out,
base="main",
head="HEAD",
title=None,
subtitle=None,
open_details=True,
)
text = path.read_text(encoding="utf-8")
# Deleted file should be in the report
assert "to_delete.txt" in text
# Should have a diff section for it
assert "file-to-delete-txt" in text or "to_delete.txt" in text
def test_diff_unstaged_flag(tmp_path: pathlib.Path, monkeypatch) -> None:
"""SPEC 7.12.7: --unstaged flag shows only unstaged changes."""
# Create a git repo with a file
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
(repo_path / "file1.txt").write_text("original\n", encoding="utf-8")
(repo_path / "file2.txt").write_text("original\n", encoding="utf-8")
repo.index.add(["file1.txt", "file2.txt"])
repo.index.commit("initial")
repo.create_head("main")
# Stage file1 and modify file2 (unstaged)
(repo_path / "file1.txt").write_text("staged change\n", encoding="utf-8")
(repo_path / "file2.txt").write_text("unstaged change\n", encoding="utf-8")
repo.index.add(["file1.txt"])
monkeypatch.chdir(repo_path)
# Run diff with --unstaged flag
code = diff_report.main(["--unstaged"])
assert code == 0
# Check the output file
out = diff_report.default_output_path(repo_path, repo_path, "HEAD")
text = out.read_text(encoding="utf-8")
# Should include unstaged file2 changes
assert "unstaged change" in text
# Should NOT include staged file1 changes (check for the full diff line to avoid substring matches)
assert "+staged change" not in text and "-staged change" not in text
def test_diff_unstaged_includes_untracked_files(tmp_path: pathlib.Path, monkeypatch) -> None:
"""SPEC 7.12.7: --unstaged flag includes untracked files."""
# Create a git repo with a tracked file
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
settings = {
"fiona": {
"wiki": {
"graph": {
"theme": "night-sky",
}
}
}
}
(repo_path / "fiona-settings.json").write_text(
json.dumps(settings),
encoding="utf-8",
)
(repo_path / "tracked.txt").write_text("original\n", encoding="utf-8")
repo.index.add(["fiona-settings.json", "tracked.txt"])
repo.index.commit("initial")
repo.create_head("main")
# Modify tracked file (unstaged) and add untracked file
(repo_path / "tracked.txt").write_text("modified\n", encoding="utf-8")
(repo_path / "untracked.txt").write_text("new content\n", encoding="utf-8")
monkeypatch.chdir(repo_path)
# Run diff with --unstaged flag
code = diff_report.main(["--unstaged"])
assert code == 0
# Check the output file
out = diff_report.default_output_path(repo_path, repo_path, "HEAD")
text = out.read_text(encoding="utf-8")
# Should include both modified tracked file and untracked file
assert "modified" in text
assert "new content" in text
assert "untracked.txt" in text
# Check for file status icons
assert "📄" in text # untracked file icon
assert "📝" in text # modified file icon
def test_file_status_icons_in_diff(tmp_path: pathlib.Path, monkeypatch) -> None:
"""Test that file status icons (🗑️, 📄, 📝, ➕) appear correctly in diff report."""
# Create a git repo
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
settings = {
"fiona": {
"wiki": {
"graph": {
"theme": "night-sky",
}
}
}
}
(repo_path / "fiona-settings.json").write_text(
json.dumps(settings),
encoding="utf-8",
)
# Create initial files
(repo_path / "modified.txt").write_text("original\n", encoding="utf-8")
(repo_path / "deleted.txt").write_text("to delete\n", encoding="utf-8")
repo.index.add(["fiona-settings.json", "modified.txt", "deleted.txt"])
repo.index.commit("initial")
repo.create_head("main")
# Modify one file, delete another, and add an untracked file
(repo_path / "modified.txt").write_text("modified content\n", encoding="utf-8")
(repo_path / "deleted.txt").unlink()
(repo_path / "untracked.txt").write_text("new file\n", encoding="utf-8")
monkeypatch.chdir(repo_path)
# Run diff in default mode
code = diff_report.main([])
assert code == 0
# Check the output file
out = diff_report.default_output_path(repo_path, repo_path, "HEAD")
text = out.read_text(encoding="utf-8")
# Check for file status icons
assert "🗑️" in text, "Deleted file should have trash icon"
assert "📄" in text, "Untracked file should have file icon"
assert "📝" in text, "Modified file should have pencil icon"
def test_file_status_icons_in_staged_mode(tmp_path: pathlib.Path, monkeypatch) -> None:
"""Test that added files show plus icon in staged mode."""
# Create a git repo
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
settings = {
"fiona": {
"wiki": {
"graph": {
"theme": "night-sky",
}
}
}
}
(repo_path / "fiona-settings.json").write_text(
json.dumps(settings),
encoding="utf-8",
)
(repo_path / "existing.txt").write_text("original\n", encoding="utf-8")
repo.index.add(["fiona-settings.json", "existing.txt"])
repo.index.commit("initial")
repo.create_head("main")
# Add a new file and stage it
(repo_path / "new.txt").write_text("new file\n", encoding="utf-8")
repo.index.add(["new.txt"])
monkeypatch.chdir(repo_path)
# Run diff with --staged flag
code = diff_report.main(["--staged"])
assert code == 0
# Check the output file
out = diff_report.default_output_path(repo_path, repo_path, "HEAD")
text = out.read_text(encoding="utf-8")
# Check for plus icon for added file
assert "➕" in text, "Added file should have plus icon"
assert "new.txt" in text
def test_diff_staged_flag(tmp_path: pathlib.Path, monkeypatch) -> None:
"""SPEC 7.12.8: --staged flag shows only staged changes."""
# Create a git repo with a file
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
(repo_path / "file1.txt").write_text("original\n", encoding="utf-8")
(repo_path / "file2.txt").write_text("original\n", encoding="utf-8")
repo.index.add(["file1.txt", "file2.txt"])
repo.index.commit("initial")
repo.create_head("main")
# Stage file1 and modify file2 (unstaged)
(repo_path / "file1.txt").write_text("staged change\n", encoding="utf-8")
(repo_path / "file2.txt").write_text("unstaged change\n", encoding="utf-8")
repo.index.add(["file1.txt"])
monkeypatch.chdir(repo_path)
# Run diff with --staged flag
code = diff_report.main(["--staged"])
assert code == 0
# Check the output file
out = diff_report.default_output_path(repo_path, repo_path, "HEAD")
text = out.read_text(encoding="utf-8")
# Should include staged file1 changes
assert "+staged change" in text or "-staged change" in text
# Should NOT include unstaged file2 changes (check for the full diff line to avoid substring matches)
assert "+unstaged change" not in text and "-unstaged change" not in text
def test_diff_default_behavior(tmp_path: pathlib.Path, monkeypatch) -> None:
"""SPEC 7.12.5: Default behavior (no flag) shows all changes."""
# Create a git repo with files
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
(repo_path / "file1.txt").write_text("original\n", encoding="utf-8")
(repo_path / "file2.txt").write_text("original\n", encoding="utf-8")
repo.index.add(["file1.txt", "file2.txt"])
repo.index.commit("initial")
repo.create_head("main")
# Stage file1 and modify file2 (unstaged)
(repo_path / "file1.txt").write_text("staged change\n", encoding="utf-8")
(repo_path / "file2.txt").write_text("unstaged change\n", encoding="utf-8")
repo.index.add(["file1.txt"])
monkeypatch.chdir(repo_path)
# Run diff without flags
code = diff_report.main([])
assert code == 0
# Check the output file
out = diff_report.default_output_path(repo_path, repo_path, "HEAD")
text = out.read_text(encoding="utf-8")
# Should include both staged and unstaged changes (check for full diff lines to avoid substring matches)
assert "+staged change" in text or "-staged change" in text
assert "+unstaged change" in text or "-unstaged change" in text
def test_diff_staged_and_unstaged_mutually_exclusive(tmp_path: pathlib.Path, monkeypatch) -> None:
"""SPEC 7.12.7/7.12.8: --staged and --unstaged cannot be used together."""
repo_path = tmp_path / "myrepo"
repo = git.Repo.init(repo_path)
_configure_test_repo(repo)
(repo_path / "file.txt").write_text("content\n", encoding="utf-8")
repo.index.add(["file.txt"])
repo.index.commit("initial")
monkeypatch.chdir(repo_path)
# Run diff with both flags - should fail
code = diff_report.main(["--staged", "--unstaged"])
assert code == 1