1336 lines
44 KiB
Python
1336 lines
44 KiB
Python
"""Tests for Frontmatter Linter (fmlint) module.
|
|
|
|
This module tests the custom linter for validating markdown file frontmatter
|
|
against project requirements, ensuring all md files have the required `name`
|
|
and `version` fields for source control and version tracking.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import pathlib
|
|
import sys
|
|
import tempfile
|
|
|
|
import pytest
|
|
|
|
from fiona.customlinter.base import FrontmatterLintError, LintViolation
|
|
from fiona.customlinter.fmlint import (
|
|
FrontmatterLinter,
|
|
main,
|
|
validate_file_frontmatter,
|
|
)
|
|
from fiona.librarian.md_frontmatter import FrontmatterError
|
|
|
|
# =============================================================================
|
|
# Fixtures
|
|
# =============================================================================
|
|
|
|
@pytest.fixture
|
|
def valid_frontmatter_content():
|
|
"""Sample markdown content with valid frontmatter including name and version."""
|
|
return """---
|
|
name: Test Document
|
|
version: 1.0.0
|
|
title: Test Page
|
|
created: 2025-09-20
|
|
updated: 2025-09-21
|
|
type: concept
|
|
---
|
|
|
|
# Page Content
|
|
This is the page content.
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def minimal_valid_frontmatter_content():
|
|
"""Sample markdown content with minimal required frontmatter (name and version only)."""
|
|
return """---
|
|
name: Minimal Document
|
|
version: 0.1.0
|
|
---
|
|
|
|
# Content here
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def missing_name_content():
|
|
"""Sample markdown content missing name field."""
|
|
return """---
|
|
version: 1.0.0
|
|
title: Nameless Page
|
|
---
|
|
|
|
# Content here
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def missing_version_content():
|
|
"""Sample markdown content missing version field."""
|
|
return """---
|
|
name: Versionless Document
|
|
title: Versionless Page
|
|
---
|
|
|
|
# Content here
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def missing_both_content():
|
|
"""Sample markdown content missing both name and version fields."""
|
|
return """---
|
|
title: Incomplete Page
|
|
type: concept
|
|
---
|
|
|
|
# Content here
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def no_frontmatter_content():
|
|
"""Sample markdown content with no frontmatter at all."""
|
|
return """# Page Without Frontmatter
|
|
|
|
This is content without frontmatter.
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def malformed_yaml_content():
|
|
"""Sample markdown content with malformed YAML frontmatter."""
|
|
return """---
|
|
name: Test Document
|
|
version: 1.0.0
|
|
invalid_yaml: [unclosed
|
|
---
|
|
|
|
# Content here
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def empty_frontmatter_content():
|
|
"""Sample markdown content with empty frontmatter delimiters."""
|
|
return """---
|
|
---
|
|
|
|
# Content here
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_directory_with_files():
|
|
"""Create a temporary directory with test markdown files."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
tmpdir_path = pathlib.Path(tmpdir)
|
|
|
|
# Create valid file
|
|
valid_file = tmpdir_path / "valid.md"
|
|
valid_file.write_text("""---
|
|
name: Valid Document
|
|
version: 1.0.0
|
|
---
|
|
# Valid""")
|
|
|
|
# Create file missing name
|
|
missing_name_file = tmpdir_path / "missing_name.md"
|
|
missing_name_file.write_text("""---
|
|
version: 1.0.0
|
|
---
|
|
# Missing Name""")
|
|
|
|
# Create file missing version
|
|
missing_version_file = tmpdir_path / "missing_version.md"
|
|
missing_version_file.write_text("""---
|
|
name: Missing Version
|
|
---
|
|
# Missing Version""")
|
|
|
|
# Create file with no frontmatter
|
|
no_frontmatter_file = tmpdir_path / "no_frontmatter.md"
|
|
no_frontmatter_file.write_text("# No Frontmatter\nContent")
|
|
|
|
# Create non-md file (should be skipped)
|
|
non_md_file = tmpdir_path / "readme.txt"
|
|
non_md_file.write_text("Not a markdown file")
|
|
|
|
yield tmpdir_path
|
|
|
|
|
|
@pytest.fixture
|
|
def fiona_settings_file():
|
|
"""Sample fiona-settings.json with frontmatter linter configuration."""
|
|
return {
|
|
"markdown": {
|
|
"frontmatter": {
|
|
"enabled": True,
|
|
"required_fields": ["name", "version"],
|
|
"type_requirements": {
|
|
"toc": ["title", "created", "updated", "type"],
|
|
"concept": ["name", "version", "title", "type"]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
# =============================================================================
|
|
# Test: validate_file_frontmatter function
|
|
# =============================================================================
|
|
|
|
class TestValidateFileFrontmatter:
|
|
"""Tests for the validate_file_frontmatter function."""
|
|
|
|
def test_valid_frontmatter_passes(
|
|
self, valid_frontmatter_content, tmp_path
|
|
):
|
|
"""Test that a file with valid frontmatter passes validation."""
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text(valid_frontmatter_content)
|
|
|
|
# Should not raise an exception
|
|
result = validate_file_frontmatter(test_file)
|
|
assert result is True
|
|
|
|
def test_minimal_valid_frontmatter_passes(
|
|
self, minimal_valid_frontmatter_content, tmp_path
|
|
):
|
|
"""Test that a file with minimal required fields passes validation."""
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text(minimal_valid_frontmatter_content)
|
|
|
|
result = validate_file_frontmatter(test_file)
|
|
assert result is True
|
|
|
|
def test_missing_name_raises_error(
|
|
self, missing_name_content, tmp_path
|
|
):
|
|
"""Test that a file missing the name field raises an error."""
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text(missing_name_content)
|
|
|
|
with pytest.raises(FrontmatterLintError) as exc_info:
|
|
validate_file_frontmatter(test_file)
|
|
|
|
assert "name" in str(exc_info.value).lower()
|
|
assert "missing" in str(exc_info.value).lower()
|
|
|
|
def test_missing_version_raises_error(
|
|
self, missing_version_content, tmp_path
|
|
):
|
|
"""Test that a file missing the version field raises an error."""
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text(missing_version_content)
|
|
|
|
with pytest.raises(FrontmatterLintError) as exc_info:
|
|
validate_file_frontmatter(test_file)
|
|
|
|
assert "version" in str(exc_info.value).lower()
|
|
assert "missing" in str(exc_info.value).lower()
|
|
|
|
def test_missing_both_raises_error(
|
|
self, missing_both_content, tmp_path
|
|
):
|
|
"""Test that a file missing both name and version raises an error."""
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text(missing_both_content)
|
|
|
|
with pytest.raises(FrontmatterLintError) as exc_info:
|
|
validate_file_frontmatter(test_file)
|
|
|
|
error_msg = str(exc_info.value).lower()
|
|
assert "name" in error_msg
|
|
assert "version" in error_msg
|
|
assert "missing" in error_msg
|
|
|
|
def test_no_frontmatter_raises_error(
|
|
self, no_frontmatter_content, tmp_path
|
|
):
|
|
"""Test that a file with no frontmatter raises an error."""
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text(no_frontmatter_content)
|
|
|
|
with pytest.raises(FrontmatterLintError) as exc_info:
|
|
validate_file_frontmatter(test_file)
|
|
|
|
error_msg = str(exc_info.value).lower()
|
|
assert "frontmatter" in error_msg or "missing" in error_msg
|
|
|
|
def test_malformed_yaml_raises_error(
|
|
self, malformed_yaml_content, tmp_path
|
|
):
|
|
"""Test that a file with malformed YAML raises an error."""
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text(malformed_yaml_content)
|
|
|
|
with pytest.raises(FrontmatterLintError) as exc_info:
|
|
validate_file_frontmatter(test_file)
|
|
|
|
error_msg = str(exc_info.value).lower()
|
|
assert "yaml" in error_msg or "parse" in error_msg or "malformed" in error_msg
|
|
|
|
def test_empty_frontmatter_raises_error(
|
|
self, empty_frontmatter_content, tmp_path
|
|
):
|
|
"""Test that a file with empty frontmatter raises an error."""
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text(empty_frontmatter_content)
|
|
|
|
with pytest.raises(FrontmatterLintError) as exc_info:
|
|
validate_file_frontmatter(test_file)
|
|
|
|
error_msg = str(exc_info.value).lower()
|
|
assert "missing" in error_msg or "empty" in error_msg
|
|
|
|
|
|
# =============================================================================
|
|
# Test: FrontmatterLinter class
|
|
# =============================================================================
|
|
|
|
class TestFrontmatterLinter:
|
|
"""Tests for the FrontmatterLinter class."""
|
|
|
|
def test_scan_directory_finds_all_md_files(
|
|
self, temp_directory_with_files
|
|
):
|
|
"""Test that scanning a directory finds all markdown files."""
|
|
linter = FrontmatterLinter()
|
|
results = linter.scan_directory(temp_directory_with_files)
|
|
|
|
# Should find all .md files
|
|
assert len(results) == 4 # valid, missing_name, missing_version, no_frontmatter
|
|
|
|
# Check that all files are pathlib.Path objects
|
|
for filepath in results:
|
|
assert isinstance(filepath, pathlib.Path)
|
|
assert filepath.suffix == ".md"
|
|
|
|
def test_scan_directory_skips_non_md_files(
|
|
self, temp_directory_with_files
|
|
):
|
|
"""Test that scanning a directory skips non-markdown files."""
|
|
linter = FrontmatterLinter()
|
|
results = linter.scan_directory(temp_directory_with_files)
|
|
|
|
# Should not include the .txt file
|
|
for filepath in results:
|
|
assert filepath.suffix == ".md"
|
|
|
|
def test_lint_directory_returns_violations(
|
|
self, temp_directory_with_files
|
|
):
|
|
"""Test that linting a directory returns violation information."""
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(temp_directory_with_files)
|
|
|
|
# Should have violations for missing_name, missing_version, no_frontmatter
|
|
assert len(results) > 0
|
|
|
|
# Check structure of results
|
|
assert "files" in results
|
|
assert "meta" in results
|
|
assert "descriptions" in results
|
|
|
|
def test_lint_directory_valid_files_only(
|
|
self, tmp_path
|
|
):
|
|
"""Test that linting a directory with only valid files returns no violations."""
|
|
# Create directory with only valid files
|
|
valid_file1 = tmp_path / "valid1.md"
|
|
valid_file1.write_text("""---
|
|
name: Document 1
|
|
version: 1.0.0
|
|
---
|
|
# Doc 1""")
|
|
|
|
valid_file2 = tmp_path / "valid2.md"
|
|
valid_file2.write_text("""---
|
|
name: Document 2
|
|
version: 2.0.0
|
|
---
|
|
# Doc 2""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have no violations
|
|
assert results["meta"]["total_violations"] == 0
|
|
|
|
def test_custom_required_fields(
|
|
self, tmp_path
|
|
):
|
|
"""Test that custom required fields can be configured."""
|
|
# Create a file that has name and version but not title
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text("""---
|
|
name: Test Document
|
|
version: 1.0.0
|
|
---
|
|
# Content""")
|
|
|
|
# With default fields (name, version), should pass
|
|
linter = FrontmatterLinter()
|
|
result = linter.validate_file(test_file)
|
|
assert result is True
|
|
|
|
# With custom fields including title, should fail
|
|
linter_custom = FrontmatterLinter(required_fields=["name", "version", "title"])
|
|
with pytest.raises(FrontmatterLintError):
|
|
linter_custom.validate_file(test_file)
|
|
|
|
def test_format_violations_ai_friendly(
|
|
self, temp_directory_with_files
|
|
):
|
|
"""Test that violation output is AI-friendly and parseable."""
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(temp_directory_with_files)
|
|
formatted = linter.format_violations(results)
|
|
|
|
# Should be a string
|
|
assert isinstance(formatted, str)
|
|
|
|
# Should contain certain keywords
|
|
assert "Frontmatter" in formatted or "frontmatter" in formatted
|
|
assert "Violation" in formatted or "violation" in formatted
|
|
|
|
# Should have structured sections
|
|
assert "FILE:" in formatted or "file:" in formatted
|
|
|
|
def test_format_violations_uses_shared_base_formatter(
|
|
self, temp_directory_with_files
|
|
):
|
|
"""Test format_violations delegates to BaseLinter shared format."""
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(temp_directory_with_files)
|
|
|
|
formatted = linter.format_violations(results)
|
|
|
|
assert formatted.splitlines()[0] == "Frontmatter Lint Violations"
|
|
|
|
def test_respect_gitignore(
|
|
self, tmp_path
|
|
):
|
|
"""Test that the linter respects .gitignore files."""
|
|
# Create .gitignore
|
|
gitignore_file = tmp_path / ".gitignore"
|
|
gitignore_file.write_text("ignored.md\n")
|
|
|
|
# Create files
|
|
valid_file = tmp_path / "valid.md"
|
|
valid_file.write_text("""---
|
|
name: Valid
|
|
version: 1.0.0
|
|
---
|
|
# Valid""")
|
|
|
|
ignored_file = tmp_path / "ignored.md"
|
|
ignored_file.write_text("# Should be ignored")
|
|
|
|
linter = FrontmatterLinter(respect_gitignore=True)
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# ignored.md should not appear in results
|
|
for filepath in results.get("files", {}):
|
|
assert "ignored.md" not in str(filepath)
|
|
|
|
|
|
# =============================================================================
|
|
# Test: CLI Integration
|
|
# =============================================================================
|
|
|
|
class TestCLIIntegration:
|
|
"""Tests for CLI integration of the frontmatter linter."""
|
|
|
|
def test_chklint_subcommand_exists(self):
|
|
"""Test that the chklint subcommand exists in the CLI."""
|
|
# This will be tested more thoroughly when CLI is implemented
|
|
# For now, just check that the module can be imported
|
|
linter = FrontmatterLinter()
|
|
assert linter is not None
|
|
|
|
|
|
# =============================================================================
|
|
# Test: Settings Integration
|
|
# =============================================================================
|
|
|
|
class TestSettingsIntegration:
|
|
"""Tests for fiona-settings.json integration."""
|
|
|
|
def test_load_settings_from_file(
|
|
self, tmp_path, fiona_settings_file
|
|
):
|
|
"""Test that settings can be loaded from a fiona-settings.json file."""
|
|
settings_file = tmp_path / "fiona-settings.json"
|
|
settings_file.write_text(json.dumps(fiona_settings_file))
|
|
|
|
linter = FrontmatterLinter(settings_file=settings_file)
|
|
|
|
# Settings should be loaded
|
|
assert linter.enabled is True
|
|
assert set(linter.required_fields) == {"name", "version"}
|
|
|
|
def test_settings_disable_linter(
|
|
self, tmp_path
|
|
):
|
|
"""Test that the linter can be disabled via settings."""
|
|
settings = {
|
|
"markdown": {
|
|
"frontmatter": {
|
|
"enabled": False,
|
|
"required_fields": ["name", "version"]
|
|
}
|
|
}
|
|
}
|
|
|
|
settings_file = tmp_path / "fiona-settings.json"
|
|
settings_file.write_text(json.dumps(settings))
|
|
|
|
linter = FrontmatterLinter(settings_file=settings_file)
|
|
|
|
assert linter.enabled is False
|
|
|
|
def test_settings_custom_required_fields(
|
|
self, tmp_path
|
|
):
|
|
"""Test that custom required fields can be set via settings."""
|
|
settings = {
|
|
"markdown": {
|
|
"frontmatter": {
|
|
"enabled": True,
|
|
"required_fields": ["name", "version", "title"]
|
|
}
|
|
}
|
|
}
|
|
|
|
settings_file = tmp_path / "fiona-settings.json"
|
|
settings_file.write_text(json.dumps(settings))
|
|
|
|
linter = FrontmatterLinter(settings_file=settings_file)
|
|
|
|
assert set(linter.required_fields) == {"name", "version", "title"}
|
|
|
|
|
|
# =============================================================================
|
|
# Test: Edge Cases
|
|
# =============================================================================
|
|
|
|
class TestEdgeCases:
|
|
"""Tests for edge cases and error handling."""
|
|
|
|
def test_nonexistent_directory(self, tmp_path):
|
|
"""Test handling of non-existent directory."""
|
|
linter = FrontmatterLinter()
|
|
nonexistent_dir = tmp_path / "does_not_exist"
|
|
|
|
# Should handle gracefully (either return empty or raise clear error)
|
|
results = linter.scan_directory(nonexistent_dir)
|
|
# Implementation can choose to return empty dict or raise error
|
|
# This test just ensures it doesn't crash unexpectedly
|
|
assert isinstance(results, dict)
|
|
|
|
def test_empty_directory(self, tmp_path):
|
|
"""Test handling of empty directory."""
|
|
linter = FrontmatterLinter()
|
|
results = linter.scan_directory(tmp_path)
|
|
|
|
assert results == {} or len(results) == 0
|
|
|
|
def test_file_with_bom(self, tmp_path):
|
|
"""Test handling of file with UTF-8 BOM."""
|
|
test_file = tmp_path / "test.md"
|
|
# Write with BOM
|
|
test_file.write_bytes(b"\xef\xbb\xbf---\nname: Test\nversion: 1.0.0\n---\n# Content")
|
|
|
|
linter = FrontmatterLinter()
|
|
result = linter.validate_file(test_file)
|
|
|
|
# Should handle BOM and still parse frontmatter correctly
|
|
assert result is True
|
|
|
|
def test_nested_directories(self, tmp_path):
|
|
"""Test handling of nested directory structures."""
|
|
# Create nested structure
|
|
nested_dir = tmp_path / "subdir" / "nested"
|
|
nested_dir.mkdir(parents=True)
|
|
|
|
nested_file = nested_dir / "test.md"
|
|
nested_file.write_text("""---
|
|
name: Nested Document
|
|
version: 1.0.0
|
|
---
|
|
# Nested""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.scan_directory(tmp_path)
|
|
|
|
# Should find the nested file
|
|
assert len(results) >= 1
|
|
found_nested = False
|
|
for filepath in results:
|
|
if "nested" in str(filepath) or "test.md" in str(filepath):
|
|
found_nested = True
|
|
break
|
|
assert found_nested is True
|
|
|
|
def test_readonly_zones_skipped(self, tmp_path):
|
|
"""Test that read-only zones are skipped."""
|
|
# Create personal and raw directories
|
|
personal_dir = tmp_path / "personal"
|
|
personal_dir.mkdir()
|
|
raw_dir = tmp_path / "raw"
|
|
raw_dir.mkdir()
|
|
|
|
# Create files in read-only zones
|
|
personal_file = personal_dir / "test.md"
|
|
personal_file.write_text("# Personal")
|
|
|
|
raw_file = raw_dir / "test.md"
|
|
raw_file.write_text("# Raw")
|
|
|
|
# Create valid file in normal zone
|
|
normal_file = tmp_path / "normal.md"
|
|
normal_file.write_text("""---
|
|
name: Normal
|
|
version: 1.0.0
|
|
---
|
|
# Normal""")
|
|
|
|
linter = FrontmatterLinter(respect_readonly_zones=True)
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should not include files from personal/ or raw/
|
|
for filepath in results.get("files", {}):
|
|
assert "personal" not in str(filepath)
|
|
assert "raw" not in str(filepath)
|
|
|
|
|
|
# =============================================================================
|
|
# Test: SKILL.md Validation
|
|
# =============================================================================
|
|
|
|
class TestSkillMdValidation:
|
|
"""Tests for SKILL.md specific validation."""
|
|
|
|
def test_skill_name_valid_lowercase_with_hyphens(
|
|
self, tmp_path
|
|
):
|
|
"""Test that a SKILL.md with valid lowercase name and hyphens passes."""
|
|
skill_dir = tmp_path / "python-tdd"
|
|
skill_dir.mkdir()
|
|
|
|
skill_file = skill_dir / "SKILL.md"
|
|
skill_file.write_text("""---
|
|
name: python-tdd
|
|
description: Python TDD skill
|
|
version: 1.0.0
|
|
---
|
|
# Content""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have no violations
|
|
assert results["meta"]["total_violations"] == 0
|
|
|
|
def test_skill_name_invalid_uppercase(
|
|
self, tmp_path
|
|
):
|
|
"""Test that a SKILL.md with uppercase letters in name is reported."""
|
|
skill_dir = tmp_path / "python-tdd"
|
|
skill_dir.mkdir()
|
|
|
|
skill_file = skill_dir / "SKILL.md"
|
|
skill_file.write_text("""---
|
|
name: Python TDD
|
|
version: 1.0.0
|
|
---
|
|
# Content""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have violations
|
|
assert results["meta"]["total_violations"] > 0
|
|
|
|
# Check for FML005 violation (SKILL_NAME_INVALID -> FML005)
|
|
found_invalid = False
|
|
for file_path, violations in results.get("files", {}).items():
|
|
if "SKILL.md" in str(file_path):
|
|
for violation in violations:
|
|
if "FML005" in violation:
|
|
found_invalid = True
|
|
break
|
|
assert found_invalid is True
|
|
|
|
def test_skill_name_invalid_spaces(
|
|
self, tmp_path
|
|
):
|
|
"""Test that a SKILL.md with spaces in name is reported."""
|
|
skill_dir = tmp_path / "test-skill"
|
|
skill_dir.mkdir()
|
|
|
|
skill_file = skill_dir / "SKILL.md"
|
|
skill_file.write_text("""---
|
|
name: test skill
|
|
version: 1.0.0
|
|
---
|
|
# Content""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have violations
|
|
assert results["meta"]["total_violations"] > 0
|
|
|
|
# Check for FML005 violation (SKILL_NAME_INVALID -> FML005)
|
|
found_invalid = False
|
|
for file_path, violations in results.get("files", {}).items():
|
|
if "SKILL.md" in str(file_path):
|
|
for violation in violations:
|
|
if "FML005" in violation:
|
|
found_invalid = True
|
|
break
|
|
assert found_invalid is True
|
|
|
|
def test_skill_name_invalid_underscores(
|
|
self, tmp_path
|
|
):
|
|
"""Test that a SKILL.md with underscores in name is reported."""
|
|
skill_dir = tmp_path / "test-skill"
|
|
skill_dir.mkdir()
|
|
|
|
skill_file = skill_dir / "SKILL.md"
|
|
skill_file.write_text("""---
|
|
name: test_skill
|
|
version: 1.0.0
|
|
---
|
|
# Content""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have violations
|
|
assert results["meta"]["total_violations"] > 0
|
|
|
|
# Check for FML005 violation (SKILL_NAME_INVALID -> FML005)
|
|
found_invalid = False
|
|
for file_path, violations in results.get("files", {}).items():
|
|
if "SKILL.md" in str(file_path):
|
|
for violation in violations:
|
|
if "FML005" in violation:
|
|
found_invalid = True
|
|
break
|
|
assert found_invalid is True
|
|
|
|
def test_skill_name_mismatch_folder(
|
|
self, tmp_path
|
|
):
|
|
"""Test that a SKILL.md with name not matching folder is reported."""
|
|
skill_dir = tmp_path / "python-tdd"
|
|
skill_dir.mkdir()
|
|
|
|
skill_file = skill_dir / "SKILL.md"
|
|
skill_file.write_text("""---
|
|
name: other-skill
|
|
version: 1.0.0
|
|
---
|
|
# Content""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have violations
|
|
assert results["meta"]["total_violations"] > 0
|
|
|
|
# Check for FML006 violation (SKILL_NAME_MISMATCH -> FML006)
|
|
found_mismatch = False
|
|
for file_path, violations in results.get("files", {}).items():
|
|
if "SKILL.md" in str(file_path):
|
|
for violation in violations:
|
|
if "FML006" in violation:
|
|
found_mismatch = True
|
|
break
|
|
assert found_mismatch is True
|
|
|
|
def test_skill_name_valid_with_numbers(
|
|
self, tmp_path
|
|
):
|
|
"""Test that a SKILL.md with numbers in name passes."""
|
|
skill_dir = tmp_path / "test-123"
|
|
skill_dir.mkdir()
|
|
|
|
skill_file = skill_dir / "SKILL.md"
|
|
skill_file.write_text("""---
|
|
name: test-123
|
|
description: Test skill with numbers
|
|
version: 1.0.0
|
|
---
|
|
# Content""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have no violations
|
|
assert results["meta"]["total_violations"] == 0
|
|
|
|
def test_regular_md_file_not_affected(
|
|
self, tmp_path
|
|
):
|
|
"""Test that regular .md files are not affected by SKILL.md validation."""
|
|
regular_file = tmp_path / "regular.md"
|
|
regular_file.write_text("""---
|
|
name: Regular Document
|
|
version: 1.0.0
|
|
---
|
|
# Content""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have no violations for regular file
|
|
assert results["meta"]["total_violations"] == 0
|
|
|
|
def test_skill_file_case_sensitive(
|
|
self, tmp_path
|
|
):
|
|
"""Test that only SKILL.md (case-sensitive) is validated, not skill.md."""
|
|
skill_dir = tmp_path / "test-skill"
|
|
skill_dir.mkdir()
|
|
|
|
# Create lowercase skill.md (should not be validated as SKILL.md)
|
|
skill_file_lower = skill_dir / "skill.md"
|
|
skill_file_lower.write_text("""---
|
|
name: Test Skill
|
|
version: 1.0.0
|
|
---
|
|
# Content""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have no violations since it's not SKILL.md
|
|
assert results["meta"]["total_violations"] == 0
|
|
|
|
def test_skill_missing_description(
|
|
self, tmp_path
|
|
):
|
|
"""Test that a SKILL.md without description field is reported."""
|
|
skill_dir = tmp_path / "test-skill"
|
|
skill_dir.mkdir()
|
|
|
|
skill_file = skill_dir / "SKILL.md"
|
|
skill_file.write_text("""---
|
|
name: test-skill
|
|
version: 1.0.0
|
|
---
|
|
# Content""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have violations
|
|
assert results["meta"]["total_violations"] > 0
|
|
|
|
# Check for FML004 violation (SKILL_MISSING_DESCRIPTION -> FML004)
|
|
found_missing = False
|
|
for file_path, violations in results.get("files", {}).items():
|
|
if "SKILL.md" in str(file_path):
|
|
for violation in violations:
|
|
if "FML004" in violation:
|
|
found_missing = True
|
|
break
|
|
assert found_missing is True
|
|
|
|
def test_skill_with_description_passes(
|
|
self, tmp_path
|
|
):
|
|
"""Test that a SKILL.md with description field passes validation."""
|
|
skill_dir = tmp_path / "test-skill"
|
|
skill_dir.mkdir()
|
|
|
|
skill_file = skill_dir / "SKILL.md"
|
|
skill_file.write_text("""---
|
|
name: test-skill
|
|
description: A test skill
|
|
version: 1.0.0
|
|
---
|
|
# Content""")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have no violations
|
|
assert results["meta"]["total_violations"] == 0
|
|
|
|
|
|
# =============================================================================
|
|
# Test: Uncovered Lines - Error Handling
|
|
# =============================================================================
|
|
|
|
class TestUncoveredErrorHandling:
|
|
"""Tests for error handling paths that were not covered."""
|
|
|
|
def test_validate_nonexistent_file(self, tmp_path):
|
|
"""Test validate_file_frontmatter with non-existent file (line 126)."""
|
|
nonexistent_file = tmp_path / "does_not_exist.md"
|
|
|
|
with pytest.raises(FrontmatterLintError) as exc_info:
|
|
validate_file_frontmatter(nonexistent_file)
|
|
|
|
assert "File not found" in str(exc_info.value)
|
|
assert exc_info.value.filepath == nonexistent_file
|
|
|
|
def test_validate_file_read_error(self, tmp_path, mocker):
|
|
"""Test validate_file_frontmatter when file read fails (lines 131-132)."""
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text("---\nname: Test\nversion: 1.0.0\n---\n# Content")
|
|
|
|
# Mock read_text to raise OSError
|
|
mocker.patch.object(
|
|
pathlib.Path, 'read_text',
|
|
side_effect=OSError("Permission denied")
|
|
)
|
|
|
|
with pytest.raises(FrontmatterLintError) as exc_info:
|
|
validate_file_frontmatter(test_file)
|
|
|
|
assert "Failed to read file" in str(exc_info.value)
|
|
|
|
def test_load_settings_nonexistent_file(self, tmp_path):
|
|
"""Test _load_settings with non-existent settings file (line 213)."""
|
|
nonexistent_settings = tmp_path / "nonexistent.json"
|
|
|
|
linter = FrontmatterLinter(settings_file=nonexistent_settings)
|
|
|
|
# Should not raise, just use defaults
|
|
assert linter.required_fields == ["name", "version"]
|
|
|
|
def test_load_settings_malformed_json(self, tmp_path):
|
|
"""Test _load_settings with malformed JSON (lines 233-236)."""
|
|
settings_file = tmp_path / "fiona-settings.json"
|
|
settings_file.write_text("{ invalid json")
|
|
|
|
# Should not raise, just use defaults
|
|
linter = FrontmatterLinter(settings_file=settings_file)
|
|
assert linter.required_fields == ["name", "version"]
|
|
|
|
def test_load_settings_oserror(self, tmp_path, mocker):
|
|
"""Test _load_settings with OSError (lines 233-236)."""
|
|
settings_file = tmp_path / "fiona-settings.json"
|
|
|
|
# Mock open to raise OSError
|
|
mocker.patch('builtins.open', side_effect=OSError("Cannot read"))
|
|
|
|
# Should not raise, just use defaults
|
|
linter = FrontmatterLinter(settings_file=settings_file)
|
|
assert linter.required_fields == ["name", "version"]
|
|
|
|
def test_scan_directory_parse_error(self, tmp_path):
|
|
"""Test scan_directory with parse errors (lines 294-295)."""
|
|
# Create file with malformed YAML
|
|
test_file = tmp_path / "malformed.md"
|
|
test_file.write_text("---\n invalid: [unclosed\n---\n# Content")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.scan_directory(tmp_path)
|
|
|
|
# Should have the file with an error
|
|
assert len(results) >= 1
|
|
for filepath, data in results.items():
|
|
if "malformed" in str(filepath):
|
|
assert data.get("error") is not None
|
|
assert "error" in data
|
|
|
|
def test_is_in_readonly_zone_value_error(self, tmp_path):
|
|
"""Test _is_in_readonly_zone with ValueError (lines 316-318)."""
|
|
linter = FrontmatterLinter()
|
|
|
|
# Create a file outside the base directory
|
|
outside_file = tmp_path / ".." / "outside.md"
|
|
base_dir = tmp_path / "subdir"
|
|
base_dir.mkdir()
|
|
|
|
# This should trigger ValueError when trying relative_to
|
|
result = linter._is_in_readonly_zone(outside_file, base_dir)
|
|
|
|
# Should return False when ValueError is raised
|
|
assert result is False
|
|
|
|
def test_is_gitignored_no_gitignore_file(self, tmp_path):
|
|
"""Test _is_gitignored when .gitignore doesn't exist (line 337)."""
|
|
linter = FrontmatterLinter()
|
|
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text("# Test")
|
|
|
|
result = linter._is_gitignored(test_file, tmp_path)
|
|
|
|
assert result is False
|
|
|
|
def test_is_gitignored_empty_line(self, tmp_path):
|
|
"""Test _is_gitignored with empty lines (line 344)."""
|
|
linter = FrontmatterLinter()
|
|
|
|
# Create .gitignore with empty lines
|
|
gitignore_file = tmp_path / ".gitignore"
|
|
gitignore_file.write_text("\n\n*.md\n\n")
|
|
|
|
test_file = tmp_path / "test.txt"
|
|
test_file.write_text("content")
|
|
|
|
result = linter._is_gitignored(test_file, tmp_path)
|
|
|
|
assert result is False
|
|
|
|
def test_is_gitignored_comment_line(self, tmp_path):
|
|
"""Test _is_gitignored with comment lines (line 344)."""
|
|
linter = FrontmatterLinter()
|
|
|
|
# Create .gitignore with comment
|
|
gitignore_file = tmp_path / ".gitignore"
|
|
gitignore_file.write_text("# This is a comment\n*.md\n")
|
|
|
|
test_file = tmp_path / "test.txt"
|
|
test_file.write_text("content")
|
|
|
|
result = linter._is_gitignored(test_file, tmp_path)
|
|
|
|
assert result is False
|
|
|
|
def test_is_gitignored_value_error(self, tmp_path):
|
|
"""Test _is_gitignored with ValueError (lines 351-352)."""
|
|
linter = FrontmatterLinter()
|
|
|
|
# Create a file outside the base directory
|
|
outside_file = tmp_path / ".." / "outside.md"
|
|
base_dir = tmp_path / "subdir"
|
|
base_dir.mkdir()
|
|
|
|
result = linter._is_gitignored(outside_file, base_dir)
|
|
|
|
# Should return False when exception is raised
|
|
assert result is False
|
|
|
|
def test_is_gitignored_oserror(self, tmp_path, mocker):
|
|
"""Test _is_gitignored with OSError (lines 351-352)."""
|
|
linter = FrontmatterLinter()
|
|
|
|
test_file = tmp_path / "test.md"
|
|
|
|
# Mock open to raise OSError
|
|
mocker.patch('builtins.open', side_effect=OSError("Cannot read"))
|
|
|
|
result = linter._is_gitignored(test_file, tmp_path)
|
|
|
|
# Should return False when exception is raised
|
|
assert result is False
|
|
|
|
def test_lint_directory_with_parse_error(self, tmp_path):
|
|
"""Test lint_directory with parse errors (lines 401-408)."""
|
|
# Create file with malformed YAML
|
|
test_file = tmp_path / "malformed.md"
|
|
test_file.write_text("---\n invalid: [unclosed\n---\n# Content")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
# Should have FML001 violation (FRONTMATTER_PARSE_ERROR -> FML001)
|
|
assert results["meta"]["total_violations"] > 0
|
|
|
|
# Check for parse error in descriptions
|
|
assert "FML001" in results["descriptions"]
|
|
|
|
def test_format_violations_no_violations(self, tmp_path):
|
|
"""Test format_violations with no violations (lines 524-525)."""
|
|
# Create valid file
|
|
test_file = tmp_path / "valid.md"
|
|
test_file.write_text("---\nname: Test\nversion: 1.0.0\n---\n# Content")
|
|
|
|
linter = FrontmatterLinter()
|
|
results = linter.lint_directory(tmp_path)
|
|
|
|
formatted = linter.format_violations(results)
|
|
|
|
# Should contain "No violations found"
|
|
assert "No violations found" in formatted
|
|
|
|
|
|
# =============================================================================
|
|
# Test: CLI Main Function
|
|
# =============================================================================
|
|
|
|
class TestCLIMainFunction:
|
|
"""Tests for the main() CLI function (lines 539-579)."""
|
|
|
|
def test_main_chklint_default(self, tmp_path, mocker):
|
|
"""Test main() with default chklint subcommand (lines 561-564)."""
|
|
# Create valid file
|
|
test_file = tmp_path / "valid.md"
|
|
test_file.write_text("---\nname: Test\nversion: 1.0.0\n---\n# Content")
|
|
|
|
# Mock sys.argv to simulate no arguments (should default to chklint)
|
|
original_argv = sys.argv
|
|
|
|
try:
|
|
sys.argv = ["fmlint"]
|
|
|
|
# Mock print to capture output
|
|
captured_output = []
|
|
mocker.patch('builtins.print', side_effect=lambda x: captured_output.append(x))
|
|
|
|
# Mock exit
|
|
mocker.patch('sys.exit')
|
|
|
|
main()
|
|
|
|
# Should have printed something
|
|
assert len(captured_output) > 0
|
|
|
|
finally:
|
|
sys.argv = original_argv
|
|
|
|
def test_main_chklint_explicit(self, tmp_path, mocker):
|
|
"""Test main() with explicit chklint subcommand (lines 566-576)."""
|
|
# Create valid file
|
|
test_file = tmp_path / "valid.md"
|
|
test_file.write_text("---\nname: Test\nversion: 1.0.0\n---\n# Content")
|
|
|
|
# Mock sys.argv
|
|
original_argv = sys.argv
|
|
|
|
try:
|
|
sys.argv = ["fmlint", "chklint", str(tmp_path)]
|
|
|
|
# Mock print to capture output
|
|
captured_output = []
|
|
mocker.patch('builtins.print', side_effect=lambda x: captured_output.append(x))
|
|
|
|
# Mock exit
|
|
mocker.patch('sys.exit')
|
|
|
|
main()
|
|
|
|
# Should have printed violation results
|
|
assert len(captured_output) > 0
|
|
|
|
finally:
|
|
sys.argv = original_argv
|
|
|
|
def test_main_nonexistent_directory(self, tmp_path, mocker):
|
|
"""Test main() with non-existent directory (lines 570-572)."""
|
|
original_argv = sys.argv
|
|
|
|
try:
|
|
sys.argv = ["fmlint", "chklint", "/nonexistent/path"]
|
|
|
|
# Mock print to stderr
|
|
mocker.patch('sys.stderr.write')
|
|
|
|
# Mock exit
|
|
captured_exit = []
|
|
def mock_exit(code):
|
|
captured_exit.append(code)
|
|
mocker.patch('sys.exit', side_effect=mock_exit)
|
|
|
|
main()
|
|
|
|
# Should have exited with code 1
|
|
assert len(captured_exit) > 0
|
|
assert captured_exit[0] == 1
|
|
|
|
finally:
|
|
sys.argv = original_argv
|
|
|
|
def test_main_no_subcommand_help(self, tmp_path, mocker):
|
|
"""Test main() with no subcommand and help displayed (lines 577-579)."""
|
|
original_argv = sys.argv
|
|
|
|
try:
|
|
# Mock sys.argv with invalid subcommand
|
|
sys.argv = ["fmlint", "invalid"]
|
|
|
|
# Mock print_help
|
|
mocker.patch.object(argparse.ArgumentParser, 'print_help')
|
|
|
|
# Mock exit
|
|
captured_exit = []
|
|
def mock_exit(code):
|
|
captured_exit.append(code)
|
|
mocker.patch('sys.exit', side_effect=mock_exit)
|
|
|
|
main()
|
|
|
|
# Should have exited (argparse exits with 2 for invalid choice)
|
|
assert len(captured_exit) > 0
|
|
assert captured_exit[0] in [1, 2] # argparse exits with 2 for invalid subcommand
|
|
|
|
finally:
|
|
sys.argv = original_argv
|
|
|
|
|
|
# =============================================================================
|
|
# Test lint_file method directly
|
|
# =============================================================================
|
|
|
|
|
|
class TestLintFileDirect:
|
|
"""Tests for calling lint_file directly (not through lint_directory)."""
|
|
|
|
def test_lint_file_with_ignore_pattern(self, tmp_path):
|
|
"""Test lint_file respects ignore_file_patterns when called directly."""
|
|
linter = FrontmatterLinter()
|
|
linter.ignore_file_patterns = ["test.md"]
|
|
|
|
# Create a file that matches the ignore pattern
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text("---\nname: Test\nversion: 1.0.0\n---\n# Content")
|
|
|
|
violations = linter.lint_file(test_file)
|
|
# Should return empty because file matches ignore pattern
|
|
assert violations == []
|
|
|
|
def test_lint_file_with_read_error(self, tmp_path, mocker):
|
|
"""Test lint_file handles OSError when reading file."""
|
|
linter = FrontmatterLinter()
|
|
|
|
# Create a file
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text("content")
|
|
|
|
# Mock read_text to raise OSError
|
|
mocker.patch.object(pathlib.Path, "read_text", side_effect=OSError("Cannot read"))
|
|
|
|
violations = linter.lint_file(test_file)
|
|
# Should return a violation for the read error
|
|
assert len(violations) == 1
|
|
assert violations[0].code == "FML001"
|
|
|
|
def test_lint_file_with_parse_error(self, tmp_path, mocker):
|
|
"""Test lint_file handles FrontmatterError when parsing."""
|
|
linter = FrontmatterLinter()
|
|
|
|
# Create a file with invalid frontmatter
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text("---\nname: Test\nversion: 1.0.0\n") # Missing closing ---
|
|
|
|
# Mock parse_frontmatter to raise error; patch the binding that
|
|
# lint_file actually uses (module-level, see python-tdd rule 0)
|
|
mocker.patch(
|
|
"fiona.customlinter.fmlint.parse_frontmatter",
|
|
side_effect=FrontmatterError("Invalid frontmatter")
|
|
)
|
|
|
|
violations = linter.lint_file(test_file)
|
|
# Should return a violation for the parse error
|
|
assert len(violations) == 1
|
|
assert violations[0].code == "FML001"
|
|
|
|
def test_lint_file_with_string_path(self, tmp_path):
|
|
"""Test lint_file handles string path argument."""
|
|
linter = FrontmatterLinter()
|
|
|
|
# Create a valid file
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text("---\nname: Test\nversion: 1.0.0\n---\n# Content")
|
|
|
|
# Call lint_file with string path
|
|
violations = linter.lint_file(str(test_file))
|
|
# Should return empty for valid file
|
|
assert violations == []
|
|
|
|
def test_lint_file_with_should_skip(self, tmp_path):
|
|
"""Test lint_file respects _should_skip when called directly."""
|
|
linter = FrontmatterLinter()
|
|
|
|
# Create a file in personal directory (should be skipped)
|
|
personal_dir = tmp_path / "personal"
|
|
personal_dir.mkdir()
|
|
test_file = personal_dir / "test.md"
|
|
test_file.write_text("---\nname: Test\nversion: 1.0.0\n---\n# Content")
|
|
|
|
violations = linter.lint_file(test_file)
|
|
# Should return empty because file is in personal directory
|
|
assert violations == []
|
|
|
|
def test_lint_file_with_star_pattern(self, tmp_path):
|
|
"""Test lint_file handles */ pattern in ignore_file_patterns."""
|
|
linter = FrontmatterLinter()
|
|
linter.ignore_file_patterns = ["*/test.md"]
|
|
|
|
# Create a file that matches the */ pattern
|
|
test_file = tmp_path / "test.md"
|
|
test_file.write_text("---\nname: Test\nversion: 1.0.0\n---\n# Content")
|
|
|
|
violations = linter.lint_file(test_file)
|
|
# Should return empty because file matches */test.md pattern
|
|
assert violations == []
|
|
|
|
def test_lint_directory_with_star_pattern(self, tmp_path):
|
|
"""Test lint_directory handles */ pattern in ignore_file_patterns."""
|
|
linter = FrontmatterLinter()
|
|
linter.ignore_file_patterns = ["*/test.md"]
|
|
|
|
# Create files where one matches the pattern
|
|
directory = tmp_path
|
|
(directory / "test.md").write_text("---\nname: Test\nversion: 1.0.0\n---\n# Content")
|
|
(directory / "other.md").write_text("---\nname: Other\nversion: 1.0.0\n---\n# Content")
|
|
|
|
# Lint the directory - test.md should be skipped
|
|
violations_dict = linter.lint_directory(directory)
|
|
|
|
# Check that test.md is not in the files with violations
|
|
assert "test.md" not in violations_dict["files"]
|
|
|
|
def test_lint_directory_with_wildcard_pattern(self, tmp_path):
|
|
"""Test lint_directory handles wildcard pattern in ignore_file_patterns."""
|
|
linter = FrontmatterLinter()
|
|
linter.ignore_file_patterns = ["test.*"]
|
|
|
|
# Create files where one matches the wildcard pattern
|
|
directory = tmp_path
|
|
(directory / "test.md").write_text("---\nname: Test\nversion: 1.0.0\n---\n# Content")
|
|
(directory / "other.md").write_text("---\nname: Other\nversion: 1.0.0\n---\n# Content")
|
|
|
|
# Lint the directory - test.md should be skipped
|
|
violations_dict = linter.lint_directory(directory)
|
|
|
|
# Check that test.md is not in the files with violations
|
|
assert "test.md" not in violations_dict["files"]
|
|
|
|
def test_format_violations_report(self, tmp_path):
|
|
"""Test format_violations_report method."""
|
|
linter = FrontmatterLinter()
|
|
|
|
# Create a violation
|
|
test_file = tmp_path / "test.md"
|
|
violation = LintViolation(
|
|
filepath=test_file,
|
|
line=10,
|
|
column=5,
|
|
code="FML001",
|
|
message="Test message",
|
|
rule_id="test-rule"
|
|
)
|
|
|
|
# Format the report
|
|
report = linter.format_violations_report(
|
|
[violation],
|
|
str(tmp_path),
|
|
"Test Linter"
|
|
)
|
|
|
|
# Check that the report contains expected elements
|
|
assert "Test Linter Violations" in report
|
|
assert "TARGET:" in report
|
|
assert "DEFINITIONS:" in report
|
|
assert "FML001" in report
|
|
assert "Test message" in report
|
|
assert "SUMMARY:" in report
|
|
assert "Total violations: 1" in report
|