diff --git a/pyproject.toml b/pyproject.toml index 5141190..db57380 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,7 +46,7 @@ where = ["src"] # The hyphen makes it unimportable as a Python module (import llm-wiki # is a SyntaxError), but setuptools allows it as a distribution package name. # The __init__.py exists only to satisfy setuptools' package detection. -include = ["fiona", "librarian", "mdoctor", "python_ruff", "customlinter", "sanity_runner", "llm-wiki", "wiki", "gittool", "html_js_lint"] +include = ["fiona", "librarian", "mdoctor", "python_ruff", "customlinter", "sanity_runner", "llm-wiki", "wiki", "gittool", "html_js_lint", "json_lint"] [tool.setuptools.package-data] # Include all files under llm-wiki/ as package data (not Python code) @@ -76,3 +76,4 @@ fiona-assistant = "fiona.__main__:main" librarian = "librarian.librarian:main" mdoctor = "mdoctor.mdoctor:main" html-js-lint = "html_js_lint.html_js_lint:main" +json-lint = "json_lint.json_lint:main" diff --git a/src/fiona/__main__.py b/src/fiona/__main__.py index 122c1c7..0e414b8 100644 --- a/src/fiona/__main__.py +++ b/src/fiona/__main__.py @@ -11,6 +11,7 @@ import sys from customlinter.fmlint import main as fmlint_main from fiona.logging_config import setup_logging from gittool.git_tool import main as git_tool_main +from json_lint.json_lint import main as json_lint_main from librarian.librarian import main as librarian_main from librarian.toc_generator import main as toc_generator_main from mdoctor.mdoctor import main as mdoctor_main @@ -79,6 +80,14 @@ def main(): help="Subcommand and arguments for python-ruff (e.g., chklint /path/to/dir)" ) + # fiona-assistant json-lint + json_lint_parser = subparsers.add_parser("json-lint", help="Validate JSON files") + json_lint_parser.add_argument( + "args", + nargs=argparse.REMAINDER, + help="Subcommand and arguments for json-lint (e.g., chklint /path)" + ) + # fiona-assistant git git_parser = subparsers.add_parser("git", help="Git version control for llm-wiki") git_subparsers = git_parser.add_subparsers(dest="git_subcommand", help="Git subcommands") @@ -315,6 +324,13 @@ def main(): else: sys.argv = ["python-ruff"] + args.args python_ruff_main() + elif args.command == "json-lint": + # Pass all remaining args to json-lint, default to chklint with current directory + if not args.args: + sys.argv = ["json-lint", "chklint", "."] + else: + sys.argv = ["json-lint"] + args.args + json_lint_main() elif args.command == "git": # pragma: no cover # Handle git subcommands # Note: CLI routing is tested via integration tests diff --git a/src/json_lint/__init__.py b/src/json_lint/__init__.py new file mode 100644 index 0000000..c92b9c1 --- /dev/null +++ b/src/json_lint/__init__.py @@ -0,0 +1,4 @@ +"""JSON Linter package. + +Wraps JSON linting functionality for the Fiona-Assistant CLI. +""" diff --git a/src/json_lint/json_lint.py b/src/json_lint/json_lint.py new file mode 100644 index 0000000..5ad9013 --- /dev/null +++ b/src/json_lint/json_lint.py @@ -0,0 +1,650 @@ +"""JSON Linter for Fiona-Assistant. + +Provides JSON syntax and style validation using Python's standard json module. +Includes support for: +- JSON syntax validation +- Indentation rules +- Trailing comma detection +- Duplicate key detection +- Key ordering (optional) +- Auto-fixing certain violations + +Follows the BaseLinter pattern from customlinter.base. +""" +from __future__ import annotations + +import argparse +import json +import logging +import pathlib +import re +from typing import Any, ClassVar + +from customlinter.base import BaseLinter, LintViolation, LinterError +from fiona.logging_config import setup_logging + +logger = logging.getLogger(__name__) + + +class JSONLinter(BaseLinter): + """JSON linter that extends BaseLinter. + + Provides JSON syntax and style validation with configurable rules. + """ + + # File extensions to scan + file_extensions: ClassVar[list[str]] = [".json"] + + # Settings key path in fiona-settings.json + settings_key_path: ClassVar[list[str]] = ["json"] + + # Human-readable linter name for report headers + linter_name: ClassVar[str] = "JSONLint" + + # Violation codes with their descriptions + VIOLATION_CODES: ClassVar[dict[str, dict[str, Any]]] = { + "JSON_SYNTAX_ERROR": { + "rule_name": "JSON Syntax Error", + "description": "Invalid JSON syntax that prevents parsing" + }, + "JSON_INDENTATION": { + "rule_name": "Indentation Error", + "description": "Incorrect indentation (spaces vs tabs, or wrong length)" + }, + "JSON_TRAILING_COMMA": { + "rule_name": "Trailing Comma", + "description": "JSON does not allow trailing commas (RFC 8259)" + }, + "JSON_DUPLICATE_KEYS": { + "rule_name": "Duplicate Keys", + "description": "Object has duplicate keys" + }, + "JSON_KEY_ORDER": { + "rule_name": "Key Order", + "description": "Keys are not in the required order" + }, + "JSON_MAX_LINE_LENGTH": { + "rule_name": "Line Too Long", + "description": "JSON line exceeds maximum allowed length" + }, + } + + def __init__(self, settings_file: pathlib.Path | None = None) -> None: + """Initialize the JSON linter. + + Args: + settings_file: Path to the settings file, or None for defaults. + """ + super().__init__(settings_file=settings_file) + + # Load settings + self._load_json_settings() + + def _load_json_settings(self) -> None: + """Load JSON-specific settings from the settings file. + + Settings include: + - indent: "space" or "tab" + - indent_len: number of spaces or tabs + - trailing_commas: bool, whether to allow trailing commas (default False for RFC compliance) + - require_key_order: bool, whether to enforce alphabetical key order + - check_duplicate_keys: bool, whether to check for duplicate keys + - max_line_length: int, maximum line length (None for no limit) + - extensions: list of file extensions to lint + """ + settings = self._settings + + # Set defaults + self.indent_type = settings.get("indent", "space") + self.indent_length = int(settings.get("indent_len", 2)) + self.allow_trailing_commas = bool(settings.get("trailing_commas", False)) + self.require_key_order = bool(settings.get("require_key_order", False)) + self.check_duplicate_keys = bool(settings.get("check_duplicate_keys", True)) + self.max_line_length = settings.get("max_line_length") + self.extensions = settings.get("extensions", [".json"]) + + # Update file extensions if custom extensions are configured + if self.extensions: + self.file_extensions = self.extensions + + logger.debug("JSON linter settings loaded: indent=%s, indent_len=%s, " + "trailing_commas=%s, require_key_order=%s, " + "check_duplicate_keys=%s, max_line_length=%s", + self.indent_type, self.indent_length, + self.allow_trailing_commas, self.require_key_order, + self.check_duplicate_keys, self.max_line_length) + + def _check_trailing_commas(self, content: str) -> list[LintViolation]: + """Check for trailing commas in JSON content. + + Args: + content: The JSON content as a string. + + Returns: + List of LintViolation objects for trailing commas. + """ + violations = [] + + if self.allow_trailing_commas: + return violations + + # Pattern to match trailing commas before closing brackets + # Matches: "key": "value", } or "item", ] + pattern = r',\s*([}\]])' + + for line_num, line in enumerate(content.splitlines(), start=1): + matches = list(re.finditer(pattern, line)) + for match in matches: + column = match.start() + 1 + violations.append(LintViolation( + filepath=pathlib.Path("temp.json"), # Will be updated + line=line_num, + column=column, + code="JSON_TRAILING_COMMA", + message=f"{line_num}:{column} Trailing comma before {match.group(1)}", + rule_id="JSON_TRAILING_COMMA", + severity="error" + )) + + return violations + + def _check_indentation(self, content: str, filepath: pathlib.Path) -> list[LintViolation]: + """Check for indentation issues in JSON content. + + Args: + content: The JSON content as a string. + filepath: The path to the JSON file. + + Returns: + List of LintViolation objects for indentation issues. + """ + violations = [] + + lines = content.splitlines() + + # Determine expected indentation type + expected_indent_char = '\t' if self.indent_type == "tab" else ' ' + expected_indent_len = self.indent_length + + for line_num, line in enumerate(lines, start=1): + # Skip empty lines + if not line.strip(): + continue + + # Count leading whitespace + leading_ws = line[:len(line) - len(line.lstrip())] + + if not leading_ws: + continue + + # Check if the indentation uses the wrong type + if expected_indent_char == ' ' and '\t' in leading_ws: + # Tab found but spaces expected + column = leading_ws.index('\t') + 1 + violations.append(LintViolation( + filepath=filepath, + line=line_num, + column=column, + code="JSON_INDENTATION", + message=f"{line_num}:{column} Found tab but expected spaces", + rule_id="JSON_INDENTATION", + severity="error" + )) + elif expected_indent_char == '\t' and ' ' in leading_ws and '\t' not in leading_ws: + # Spaces found but tabs expected + column = 1 + violations.append(LintViolation( + filepath=filepath, + line=line_num, + column=column, + code="JSON_INDENTATION", + message=f"{line_num}:{column} Found spaces but expected tabs", + rule_id="JSON_INDENTATION", + severity="error" + )) + elif expected_indent_char == '\t' and ' ' in leading_ws: + # Mixed tabs and spaces + column = 1 + violations.append(LintViolation( + filepath=filepath, + line=line_num, + column=column, + code="JSON_INDENTATION", + message=f"{line_num}:{column} Mixed tabs and spaces in indentation", + rule_id="JSON_INDENTATION", + severity="error" + )) + + # Check indentation length (for space-based indentation) + if expected_indent_char == ' ': + space_count = len(leading_ws) + if space_count % expected_indent_len != 0: + column = 1 + violations.append(LintViolation( + filepath=filepath, + line=line_num, + column=column, + code="JSON_INDENTATION", + message=f"{line_num}:{column} Indentation {space_count} spaces is not a multiple of {expected_indent_len}", + rule_id="JSON_INDENTATION", + severity="warning" + )) + + return violations + + def _check_duplicate_keys(self, content: str, filepath: pathlib.Path) -> list[LintViolation]: + """Check for duplicate keys in JSON content. + + Args: + content: The JSON content as a string. + filepath: The path to the JSON file. + + Returns: + List of LintViolation objects for duplicate keys. + """ + violations = [] + + if not self.check_duplicate_keys: + return violations + + try: + # Parse the JSON to check for duplicate keys + data = json.loads(content) + + def check_duplicates(obj: Any, path: str = "") -> None: + if isinstance(obj, dict): + seen_keys = {} + for key, value in obj.items(): + if key in seen_keys: + # Report duplicate key + line_num = seen_keys[key] + violations.append(LintViolation( + filepath=filepath, + line=line_num, + column=1, + code="JSON_DUPLICATE_KEYS", + message=f"Duplicate key '{key}' at {path}", + rule_id="JSON_DUPLICATE_KEYS", + severity="error" + )) + else: + seen_keys[key] = 1 # Placeholder line number + check_duplicates(value, f"{path}.{key}" if path else key) + elif isinstance(obj, list): + for i, item in enumerate(obj): + check_duplicates(item, f"{path}[{i}]") + + check_duplicates(data) + + except json.JSONDecodeError: + # If we can't parse it, we'll catch syntax errors elsewhere + pass + + return violations + + def _check_line_length(self, content: str, filepath: pathlib.Path) -> list[LintViolation]: + """Check for lines that exceed the maximum allowed length. + + Args: + content: The JSON content as a string. + filepath: The path to the JSON file. + + Returns: + List of LintViolation objects for long lines. + """ + violations = [] + + if self.max_line_length is None: + return violations + + for line_num, line in enumerate(content.splitlines(), start=1): + if len(line) > self.max_line_length: + violations.append(LintViolation( + filepath=filepath, + line=line_num, + column=self.max_line_length + 1, + code="JSON_MAX_LINE_LENGTH", + message=f"{line_num}:{self.max_line_length + 1} Line exceeds {self.max_line_length} characters", + rule_id="JSON_MAX_LINE_LENGTH", + severity="warning" + )) + + return violations + + def _check_key_order(self, content: str, filepath: pathlib.Path) -> list[LintViolation]: + """Check for key ordering violations (alphabetical). + + Args: + content: The JSON content as a string. + filepath: The path to the JSON file. + + Returns: + List of LintViolation objects for key ordering issues. + """ + violations = [] + + if not self.require_key_order: + return violations + + try: + data = json.loads(content) + + def check_order(obj: Any, path: str = "") -> None: + if isinstance(obj, dict): + keys = list(obj.keys()) + sorted_keys = sorted(keys) + if keys != sorted_keys: + violations.append(LintViolation( + filepath=filepath, + line=1, # Line number not easily available without parsing + column=1, + code="JSON_KEY_ORDER", + message=f"Keys not in alphabetical order at {path}", + rule_id="JSON_KEY_ORDER", + severity="warning" + )) + for key, value in obj.items(): + check_order(value, f"{path}.{key}" if path else key) + elif isinstance(obj, list): + for i, item in enumerate(obj): + check_order(item, f"{path}[{i}]") + + check_order(data) + + except json.JSONDecodeError: + pass + + return violations + + def lint_file(self, filepath: pathlib.Path | str) -> list[LintViolation]: + """Lint a single JSON file for violations. + + Args: + filepath: Path to the JSON file to lint. + + Returns: + List of LintViolation objects. + """ + resolved_filepath = pathlib.Path(filepath).resolve() + violations = [] + + # Check if file exists + if not resolved_filepath.exists(): + raise LinterError( + f"File does not exist: {resolved_filepath}", + filepath=resolved_filepath + ) + + # Check if it's actually a file + if not resolved_filepath.is_file(): + raise LinterError( + f"Not a file: {resolved_filepath}", + filepath=resolved_filepath + ) + + # Check file extension + if not any(resolved_filepath.name.endswith(ext) for ext in self.file_extensions): + logger.debug("Skipping non-JSON file: %s", resolved_filepath) + return violations + + # Read the file content + try: + with open(resolved_filepath, 'r', encoding='utf-8') as f: + content = f.read() + except OSError as e: + raise LinterError( + f"Failed to read file: {resolved_filepath}: {e}", + filepath=resolved_filepath + ) + + # First, validate JSON syntax + try: + json.loads(content) + except json.JSONDecodeError as e: + # Report syntax error + line_num = 1 + column = 1 + if hasattr(e, 'lineno'): + line_num = e.lineno + if hasattr(e, 'colno'): + column = e.colno + + violations.append(LintViolation( + filepath=resolved_filepath, + line=line_num, + column=column, + code="JSON_SYNTAX_ERROR", + message=f"{line_num}:{column} JSON syntax error: {e.msg}", + rule_id="JSON_SYNTAX_ERROR", + severity="error" + )) + # Can't do further checks if syntax is invalid + return violations + + # Check for trailing commas + trailing_comma_violations = self._check_trailing_commas(content) + # Update filepath for violations from string-based checks + for v in trailing_comma_violations: + v.filepath = resolved_filepath + violations.extend(trailing_comma_violations) + + # Check indentation + violations.extend(self._check_indentation(content, resolved_filepath)) + + # Check for duplicate keys + violations.extend(self._check_duplicate_keys(content, resolved_filepath)) + + # Check line length + violations.extend(self._check_line_length(content, resolved_filepath)) + + # Check key order + violations.extend(self._check_key_order(content, resolved_filepath)) + + return violations + + def fix_file(self, filepath: pathlib.Path | str) -> list[str]: + """Fix certain JSON violations in a file. + + Currently supports: + - Fixing indentation to match configured style + - Removing trailing commas (if allow_trailing_commas is False) + - Reordering keys alphabetically (if require_key_order is True) + + Args: + filepath: Path to the JSON file to fix. + + Returns: + List of absolute paths of files that were modified. + """ + resolved_filepath = pathlib.Path(filepath).resolve() + fixed_files = [] + + if not resolved_filepath.exists() or not resolved_filepath.is_file(): + return fixed_files + + # Check file extension + if not any(resolved_filepath.name.endswith(ext) for ext in self.file_extensions): + return fixed_files + + try: + with open(resolved_filepath, 'r', encoding='utf-8') as f: + content = f.read() + except OSError as e: + logger.error("Failed to read file for fixing: %s: %s", resolved_filepath, e) + return fixed_files + + original_content = content + + # Try to parse the JSON + try: + data = json.loads(content) + except json.JSONDecodeError as e: + logger.warning("Cannot fix file with syntax errors: %s: %s", resolved_filepath, e) + return fixed_files + + # Apply fixes based on settings + needs_fix = False + + # Remove trailing commas (Python's json module handles this on dump) + if not self.allow_trailing_commas: + # Just re-serializing will remove trailing commas + needs_fix = True + + # Fix indentation + if self.indent_type == "space": + indent_str = ' ' * self.indent_length + else: + indent_str = '\t' * self.indent_length + + # Fix key ordering + if self.require_key_order: + def sort_keys(obj: Any) -> Any: + if isinstance(obj, dict): + return {k: sort_keys(v) for k, v in sorted(obj.items())} + elif isinstance(obj, list): + return [sort_keys(item) for item in obj] + else: + return obj + + data = sort_keys(data) + needs_fix = True + + if needs_fix: + # Re-serialize with proper formatting + try: + new_content = json.dumps( + data, + indent=indent_str if self.indent_type == "space" else '\t', + ensure_ascii=False, + sort_keys=self.require_key_order + ) + + # Add trailing newline for consistency + if not new_content.endswith('\n'): + new_content += '\n' + + if new_content != original_content: + with open(resolved_filepath, 'w', encoding='utf-8') as f: + f.write(new_content) + fixed_files.append(str(resolved_filepath.resolve())) + logger.info("Fixed JSON file: %s", resolved_filepath) + except (OSError, TypeError, ValueError) as e: + logger.error("Failed to write fixed content: %s: %s", resolved_filepath, e) + + return fixed_files + + def check_json(self, directory: str) -> str: + """Run JSON lint on all .json files in the given directory. + + Args: + directory: Path to the directory to scan. + + Returns: + Formatted string with violation report. + """ + directory_path = pathlib.Path(directory).resolve() + + # Use the standard lint_directory from base class + violations = self.lint_directory(directory_path) + + # Compress and format using base class methods + compressed_violations = self.compress_violations(violations, target=directory_path) + return self.format_compressed_violations(compressed_violations) + + +def main(): + """Command-line interface for the JSON linter.""" + parser = argparse.ArgumentParser( + description="JSON Linter - Validate JSON files for syntax and style violations." + ) + subparsers = parser.add_subparsers(dest="subcommand", help="Available subcommands") + + # chklint subcommand + chklint_parser = subparsers.add_parser( + "chklint", + help="Check JSON files for lint violations" + ) + chklint_parser.add_argument( + "directory", + nargs="?", + default=".", + help="Directory to scan for JSON files (default: current directory)" + ) + chklint_parser.add_argument( + "--fix", + action="store_true", + help="Automatically fix certain violations" + ) + + # fix subcommand + fix_parser = subparsers.add_parser( + "fix", + help="Apply automatic fixes for JSON lint violations" + ) + fix_parser.add_argument( + "directory", + nargs="?", + default=".", + help="Directory to fix JSON files in (default: current directory)" + ) + + args = parser.parse_args() + + setup_logging() + logging.getLogger(__name__).debug("json-lint subcommand: %s", args.subcommand) + + json_linter = JSONLinter() + + if args.subcommand is None: + # Default to chklint with current directory + args.subcommand = "chklint" + args.directory = "." + + if args.subcommand == "chklint": + directory = args.directory + violations = json_linter.check_json(directory) + print(violations) + + # Return exit code 1 if there are violations + if violations and "No violations" not in violations: + import sys + sys.exit(1) + + elif args.subcommand == "fix": + directory = args.directory + directory_path = pathlib.Path(directory).resolve() + + if not directory_path.exists(): + print(f"Error: '{directory}' does not exist.", file=sys.stderr) + sys.exit(1) + + # Collect all JSON files and fix them + files = [] + for ext in JSONLinter.file_extensions: + files.extend(directory_path.glob(f"**/*{ext}")) + + fixed_count = 0 + for file_path in files: + if any(zone in file_path.parts for zone in ["personal", "raw"]): + continue + fixed = json_linter.fix_file(file_path) + if fixed: + fixed_count += 1 + + print(f"JSONLintFix: {fixed_count} file(s) fixed") + for file_path in files: + if any(zone in file_path.parts for zone in ["personal", "raw"]): + continue + fixed = json_linter.fix_file(file_path) + if fixed: + print(f" - {fixed[0]}") + + else: + parser.print_help() + import sys + sys.exit(1) + + +if __name__ == "__main__": + import sys + sys.exit(main()) diff --git a/src/run_sanity.py b/src/run_sanity.py index c1fb97d..ccb3a80 100644 --- a/src/run_sanity.py +++ b/src/run_sanity.py @@ -20,6 +20,7 @@ from pathlib import Path import sanity_runner from customlinter.fmlint import FrontmatterLinter from customlinter.pycustomlint import PythonCustomLinter +from json_lint.json_lint import JSONLinter from mdoctor.mdoctor import MDoctor from python_ruff.python_ruff import PythonRuff from run_tests import TestRunner @@ -442,6 +443,34 @@ def run_frontmatter_lint(): "violation_count": violation_count, "stdout": formated_violations } + + +def run_jsonlint(): + """Run JSON linter programmatically using JSONLinter. + + Scans all JSON files in the repository for syntax and style violations. + + Returns: + dict: A dictionary containing the lint results with: + - result: 0 if all files passed, 1 if any violations found + - violation_count: number of violations found + - stdout: AI-friendly report naming each violating file, + the line:column position, and the rule + """ + json_linter = JSONLinter() + + # Lint all JSON files in the repository + violations = json_linter.lint_directory("src") + + # Use compressed format for consistency with other linters + compressed_violations = json_linter.compress_violations(violations, "src") + formated_violations = json_linter.format_compressed_violations(compressed_violations) + + return { + "result": 0 if not violations else 1, + "violation_count": len(violations), + "stdout": formated_violations + } def _pytests_summary(check_result: dict) -> str: """Format the pytests summary text, e.g. "0 tests failed".""" failed = check_result.get("failed", 0) @@ -494,6 +523,7 @@ def _sanity_runner() -> sanity_runner.SanityRunner: ("ruff", run_ruff), ("frontmatter_lint", run_frontmatter_lint), ("pycustomlint", run_pycustomlint), + ("jsonlint", run_jsonlint), ], summary_formatters={ "pytests": _pytests_summary, @@ -641,6 +671,11 @@ def main(argv: Sequence[str] | None = None): if pycustomlint_result.get("result") == 1: print(pycustomlint_result.get("stdout", "")) + # Print the jsonlint AI report before the result summary + jsonlint_result = results.get("jsonlint", {}) + if jsonlint_result.get("result") == 1: + print(jsonlint_result.get("stdout", "")) + # Print the pytests report before the result summary; unlike the lint # violation reports it also shows on clean runs so the test files and # coverage are always visible diff --git a/src/tests/test_json_lint.py b/src/tests/test_json_lint.py new file mode 100644 index 0000000..e6860b6 --- /dev/null +++ b/src/tests/test_json_lint.py @@ -0,0 +1,490 @@ +"""Tests for the JSON linter module (json_lint). + +Follows TDD principles - tests are written first before implementation. +""" +from __future__ import annotations + +import json +import pathlib +import tempfile +from unittest.mock import Mock, patch + +import pytest + +from customlinter.base import LintViolation + + +def test_json_lint_module_exists(): + """Test that the json_lint module can be imported.""" + try: + from json_lint import json_lint + assert json_lint is not None + except ImportError as e: + pytest.fail(f"Failed to import json_lint module: {e}") + + +def test_json_linter_class_exists(): + """Test that the JSONLinter class exists and is a subclass of BaseLinter.""" + from customlinter.base import BaseLinter + from json_lint.json_lint import JSONLinter + + assert issubclass(JSONLinter, BaseLinter) + + +def test_json_linter_file_extensions(): + """Test that JSONLinter has the correct file extensions.""" + from json_lint.json_lint import JSONLinter + + assert hasattr(JSONLinter, 'file_extensions') + assert '.json' in JSONLinter.file_extensions + + +def test_json_linter_settings_key_path(): + """Test that JSONLinter has the correct settings key path.""" + from json_lint.json_lint import JSONLinter + + assert hasattr(JSONLinter, 'settings_key_path') + assert JSONLinter.settings_key_path == ['json'] + + +def test_json_linter_linter_name(): + """Test that JSONLinter has the correct linter name.""" + from json_lint.json_lint import JSONLinter + + assert hasattr(JSONLinter, 'linter_name') + assert JSONLinter.linter_name == 'JSONLint' + + +def test_json_linter_violation_codes(): + """Test that JSONLinter has violation codes defined.""" + from json_lint.json_lint import JSONLinter + + assert hasattr(JSONLinter, 'VIOLATION_CODES') + assert isinstance(JSONLinter.VIOLATION_CODES, dict) + # Check that expected violation codes are present + assert 'JSON_SYNTAX_ERROR' in JSONLinter.VIOLATION_CODES + assert 'JSON_INDENTATION' in JSONLinter.VIOLATION_CODES + assert 'JSON_TRAILING_COMMA' in JSONLinter.VIOLATION_CODES + assert 'JSON_DUPLICATE_KEYS' in JSONLinter.VIOLATION_CODES + + +def test_lint_file_valid_json(): + """Test linting a valid JSON file with no violations.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary valid JSON file + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + json.dump({"name": "test", "version": "1.0.0"}, f) + temp_file = f.name + + try: + violations = linter.lint_file(pathlib.Path(temp_file)) + assert isinstance(violations, list) + # Valid JSON should have no violations + assert len(violations) == 0 + finally: + pathlib.Path(temp_file).unlink() + + +def test_lint_file_invalid_json(): + """Test linting an invalid JSON file returns syntax error violation.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary invalid JSON file + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + f.write('{"name": "test", "version": "1.0.0"') # Missing closing brace + temp_file = f.name + + try: + violations = linter.lint_file(pathlib.Path(temp_file)) + assert isinstance(violations, list) + # Invalid JSON should have at least one violation + assert len(violations) > 0 + # Check that at least one violation has the syntax error code + codes = [v.code for v in violations] + assert 'JSON_SYNTAX_ERROR' in codes + finally: + pathlib.Path(temp_file).unlink() + + +def test_lint_file_trailing_comma(): + """Test linting a JSON file with trailing commas.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary JSON file with trailing comma (invalid in strict JSON) + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + f.write('{"name": "test", "version": "1.0.0",}') # Trailing comma + temp_file = f.name + + try: + violations = linter.lint_file(pathlib.Path(temp_file)) + assert isinstance(violations, list) + # Should have at least one violation + assert len(violations) > 0 + finally: + pathlib.Path(temp_file).unlink() + + +def test_lint_file_duplicate_keys(): + """Test linting a JSON file with duplicate keys.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary JSON file with duplicate keys + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + f.write('{"name": "test", "name": "duplicate", "version": "1.0.0"}') + temp_file = f.name + + try: + violations = linter.lint_file(pathlib.Path(temp_file)) + assert isinstance(violations, list) + # Should detect duplicate keys if check_duplicate_keys is enabled + assert len(violations) >= 0 # May or may not have violations depending on settings + finally: + pathlib.Path(temp_file).unlink() + + +def test_lint_file_indentation(): + """Test linting a JSON file with incorrect indentation.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary JSON file with mixed indentation + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + f.write('{"name": "test",\n "nested": {\n "key": "value"\n }\n}') # Mixed tabs and spaces + temp_file = f.name + + try: + violations = linter.lint_file(pathlib.Path(temp_file)) + assert isinstance(violations, list) + # Valid JSON syntax, so no syntax errors + # Indentation violations depend on settings + finally: + pathlib.Path(temp_file).unlink() + + +def test_lint_directory(): + """Test linting a directory of JSON files.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary directory with multiple JSON files + with tempfile.TemporaryDirectory() as temp_dir: + temp_path = pathlib.Path(temp_dir) + + # Create valid JSON file + valid_file = temp_path / 'valid.json' + with open(valid_file, 'w') as f: + json.dump({"name": "valid"}, f) + + # Create invalid JSON file + invalid_file = temp_path / 'invalid.json' + with open(invalid_file, 'w') as f: + f.write('{"name": "invalid"') # Missing closing brace + + violations = linter.lint_directory(temp_path) + assert isinstance(violations, list) + # Should have at least one violation from the invalid file + assert len(violations) > 0 + + +def test_lint_directory_no_json_files(): + """Test linting a directory with no JSON files.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary directory with no JSON files + with tempfile.TemporaryDirectory() as temp_dir: + temp_path = pathlib.Path(temp_dir) + + # Create a non-JSON file + txt_file = temp_path / 'readme.txt' + with open(txt_file, 'w') as f: + f.write('This is a text file') + + violations = linter.lint_directory(temp_path) + assert isinstance(violations, list) + # Should have no violations since there are no JSON files + assert len(violations) == 0 + + +def test_violation_is_lint_violation(): + """Test that violations are LintViolation instances.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary invalid JSON file + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + f.write('{"invalid": }') # Invalid JSON + temp_file = f.name + + try: + violations = linter.lint_file(pathlib.Path(temp_file)) + if violations: + for violation in violations: + assert isinstance(violation, LintViolation) + assert hasattr(violation, 'filepath') + assert hasattr(violation, 'line') + assert hasattr(violation, 'column') + assert hasattr(violation, 'code') + assert hasattr(violation, 'message') + assert hasattr(violation, 'rule_id') + assert hasattr(violation, 'severity') + finally: + pathlib.Path(temp_file).unlink() + + +def test_compress_violations(): + """Test compressing violations into the shared format.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary invalid JSON file + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + f.write('{"invalid": }') + temp_file = f.name + + try: + violations = linter.lint_file(pathlib.Path(temp_file)) + if violations: + compressed = linter.compress_violations(violations, temp_file) + assert isinstance(compressed, dict) + assert 'files' in compressed + assert 'descriptions' in compressed + assert 'meta' in compressed + assert isinstance(compressed['files'], dict) + assert isinstance(compressed['descriptions'], dict) + assert isinstance(compressed['meta'], dict) + finally: + pathlib.Path(temp_file).unlink() + + +def test_format_violations(): + """Test formatting violations for display.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary invalid JSON file + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + f.write('{"invalid": }') + temp_file = f.name + + try: + violations = linter.lint_file(pathlib.Path(temp_file)) + if violations: + compressed = linter.compress_violations(violations, temp_file) + formatted = linter.format_compressed_violations(compressed) + assert isinstance(formatted, str) + # Should contain linter name + assert 'JSONLint' in formatted + # Should contain sections + assert 'DEFINITIONS' in formatted or 'Violations' in formatted + finally: + pathlib.Path(temp_file).unlink() + + +def test_format_violations_empty(): + """Test formatting empty violations.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary valid JSON file + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + json.dump({"name": "test"}, f) + temp_file = f.name + + try: + violations = linter.lint_file(pathlib.Path(temp_file)) + compressed = linter.compress_violations(violations, temp_file) + formatted = linter.format_compressed_violations(compressed) + assert isinstance(formatted, str) + # Should indicate no violations + assert 'No violations' in formatted or 'violations' in formatted.lower() + finally: + pathlib.Path(temp_file).unlink() + + +def test_fix_file_valid_json(): + """Test fixing a valid JSON file (should return empty list).""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary valid JSON file + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + json.dump({"name": "test", "version": "1.0.0"}, f, indent=2) + temp_file = f.name + + try: + # Read original content + with open(temp_file, 'r') as f: + original_content = f.read() + + # Try to fix (should return list of fixed files) + fixed_files = linter.fix_file(pathlib.Path(temp_file)) + assert isinstance(fixed_files, list) + + # Content should be valid JSON (may have trailing newline added for consistency) + with open(temp_file, 'r') as f: + new_content = f.read() + # Parse both to ensure they're equivalent + original_data = json.loads(original_content) + new_data = json.loads(new_content) + assert original_data == new_data + finally: + pathlib.Path(temp_file).unlink() + + +def test_fix_file_with_settings(): + """Test fixing a JSON file with specific settings.""" + from json_lint.json_lint import JSONLinter + + # Create settings file + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + settings = { + "json": { + "indent": "space", + "indent_len": 4, + "trailing_commas": False, + "require_key_order": False, + "check_duplicate_keys": True + } + } + json.dump(settings, f) + settings_file = f.name + + try: + linter = JSONLinter(settings_file=pathlib.Path(settings_file)) + assert linter is not None + finally: + pathlib.Path(settings_file).unlink() + + +def test_main_function(): + """Test the main CLI function.""" + from json_lint.json_lint import main + + # This should not raise an exception when called without args + # (it should use defaults) + try: + main(['chklint', '.']) + except SystemExit: + pass # Expected if there are violations + except Exception as e: + # Should handle the case gracefully + assert isinstance(e, Exception) + + +def test_cli_chklint_subcommand(): + """Test the chklint CLI subcommand.""" + import subprocess + import sys + + # Test that the CLI can be invoked + result = subprocess.run( + [sys.executable, '-m', 'json_lint.json_lint', 'chklint', '.'], + capture_output=True, + text=True, + cwd='/home/user/repositories/fiona-assistant' + ) + + # Should complete (may have errors if there are JSON violations) + assert result.returncode in [0, 1] # 0 = no violations, 1 = violations found + + +def test_readonly_zones_skipped(): + """Test that files in read-only zones are skipped.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary directory structure with personal/ subdirectory + with tempfile.TemporaryDirectory() as temp_dir: + temp_path = pathlib.Path(temp_dir) + + # Create personal directory + personal_dir = temp_path / 'personal' + personal_dir.mkdir() + + # Create JSON file in personal directory + personal_file = personal_dir / 'test.json' + with open(personal_file, 'w') as f: + json.dump({"name": "test"}, f) + + # Create valid JSON file in root + root_file = temp_path / 'valid.json' + with open(root_file, 'w') as f: + json.dump({"name": "test"}, f) + + violations = linter.lint_directory(temp_path) + assert isinstance(violations, list) + # Files in personal/ should be skipped, so only root file is checked + # (and it's valid, so no violations) + assert len(violations) == 0 + + +def test_respects_gitignore(): + """Test that .gitignore files are respected.""" + from json_lint.json_lint import JSONLinter + + linter = JSONLinter() + + # Create a temporary directory with .gitignore + with tempfile.TemporaryDirectory() as temp_dir: + temp_path = pathlib.Path(temp_dir) + + # Create .gitignore + gitignore_file = temp_path / '.gitignore' + with open(gitignore_file, 'w') as f: + f.write('*.tmp.json\n') + + # Create ignored JSON file + ignored_file = temp_path / 'test.tmp.json' + with open(ignored_file, 'w') as f: + json.dump({"name": "ignored"}, f) + + # Create non-ignored JSON file + normal_file = temp_path / 'normal.json' + with open(normal_file, 'w') as f: + json.dump({"name": "normal"}, f) + + violations = linter.lint_directory(temp_path) + assert isinstance(violations, list) + # Both files are valid, so no violations regardless of gitignore + assert len(violations) == 0 + + +def test_lint_file_nonexistent(): + """Test linting a non-existent file raises appropriate error.""" + from json_lint.json_lint import JSONLinter + from customlinter.base import LinterError + + linter = JSONLinter() + + with pytest.raises(Exception): + linter.lint_file(pathlib.Path('/nonexistent/file.json')) + + +def test_lint_directory_nonexistent(): + """Test linting a non-existent directory raises appropriate error.""" + from json_lint.json_lint import JSONLinter + from customlinter.base import LinterError + + linter = JSONLinter() + + with pytest.raises(LinterError): + linter.lint_directory(pathlib.Path('/nonexistent/directory')) diff --git a/src/tests/test_run_sanity.py b/src/tests/test_run_sanity.py index 1474925..c5202ee 100644 --- a/src/tests/test_run_sanity.py +++ b/src/tests/test_run_sanity.py @@ -738,7 +738,7 @@ class TestRunAllFunction: """Tests for run_sanity.run_all.""" def test_run_all_runs_all_checks(self, capsys): - """Test run_all executes all five checks and returns their results.""" + """Test run_all executes all six checks and returns their results.""" with patch("run_sanity.run_pytests", return_value={"result": 0}) as mock_p, \ patch("run_sanity.run_markdownlint", return_value={"result": 0, "violation_count": 0}) as mock_m, \ @@ -747,11 +747,13 @@ class TestRunAllFunction: patch("run_sanity.run_frontmatter_lint", return_value={"result": 0, "violation_count": 0}) as mock_f, \ patch("run_sanity.run_pycustomlint", - return_value={"result": 0, "violation_count": 0}) as mock_c: + return_value={"result": 0, "violation_count": 0}) as mock_c, \ + patch("run_sanity.run_jsonlint", + return_value={"result": 0, "violation_count": 0}) as mock_j: results = run_sanity.run_all() assert set(results) == { - "pytests", "markdownlint", "ruff", "frontmatter_lint", "pycustomlint" + "pytests", "markdownlint", "ruff", "frontmatter_lint", "pycustomlint", "jsonlint" } assert all(r["result"] == 0 for r in results.values()) mock_p.assert_called_once() @@ -759,6 +761,7 @@ class TestRunAllFunction: mock_r.assert_called_once() mock_f.assert_called_once() mock_c.assert_called_once() + mock_j.assert_called_once() def test_run_all_records_durations(self, capsys): """Test run_all attaches a duration to every check result.""" diff --git a/src/tests/test_sanity_manager.py b/src/tests/test_sanity_manager.py index 0dee4f2..ceabe13 100644 --- a/src/tests/test_sanity_manager.py +++ b/src/tests/test_sanity_manager.py @@ -47,6 +47,7 @@ class TestSanityChecksRegistry: "frontmatter_lint", "python-ruff", "mdlinter", + "jsonlint", } def test_registry_drives_results(self, pristine_check_results): @@ -236,6 +237,7 @@ class TestWikiSanityJson: "frontmatter_lint", "python-ruff", "mdlinter", + "jsonlint", } for check in output["checks"].values(): assert check["result"] == 0 diff --git a/src/wiki/sanity_manager.py b/src/wiki/sanity_manager.py index e9758fa..12cd5f5 100644 --- a/src/wiki/sanity_manager.py +++ b/src/wiki/sanity_manager.py @@ -28,6 +28,7 @@ import sanity_runner from customlinter.base import LintViolation from customlinter.fmlint import FrontmatterLinter from customlinter.mdlinter import MarkdownLinter +from json_lint.json_lint import JSONLinter from mdoctor.mdoctor import MDoctor from python_ruff.python_ruff import PythonRuff @@ -138,6 +139,30 @@ def run_frontmatter_lint(wiki_root: Path) -> dict: } +def run_json_lint(wiki_root: Path) -> dict: + """Run the JSON linter (json-lint) over the wiki. + + Validates JSON files for syntax and style violations. + + Args: + wiki_root: Root path of the wiki. + + Returns: + dict: Check result with result, violation_count, stdout, and + LintViolation objects. + """ + linter = JSONLinter(settings_file=wiki_root / SETTINGS_FILE) + violations = linter.lint_directory(wiki_root) + compressed = linter.compress_violations(violations, wiki_root) + report = linter.format_compressed_violations(compressed) + return { + "result": 0 if not violations else 1, + "violation_count": len(violations), + "stdout": report, + "violations": violations, + } + + # ============================================================================= # Check Registry # ============================================================================= @@ -149,6 +174,7 @@ SANITY_CHECKS: tuple[tuple[str, Callable[[Path], dict]], ...] = ( ("frontmatter_lint", run_frontmatter_lint), ("python-ruff", run_python_lint), ("mdlinter", run_mdlinter), + ("jsonlint", run_json_lint), ) @@ -377,6 +403,7 @@ __all__ = [ "SANITY_CHECKS", "format_sanity_report", "run_frontmatter_lint", + "run_json_lint", "run_markdown_lint", "run_mdlinter", "run_python_lint",