|
| 1 | +from annotator.core import ( |
| 2 | + get_extension, |
| 3 | + should_skip_extensionless_file, |
| 4 | + is_too_deep, |
| 5 | + is_too_large, |
| 6 | + is_in_excluded_directory, |
| 7 | + get_prefix, |
| 8 | + get_annotation_text, |
| 9 | + apply_annotation, |
| 10 | + remove_annotation, |
| 11 | + SIGNATURE, |
| 12 | +) |
| 13 | + |
| 14 | +# --- Pure Function Tests --- |
| 15 | + |
| 16 | + |
| 17 | +def test_get_extension(): |
| 18 | + assert get_extension("script.py") == ".py" |
| 19 | + assert get_extension("archive.tar.gz") == ".gz" |
| 20 | + assert get_extension("Makefile") == "" |
| 21 | + assert ( |
| 22 | + get_extension(".gitignore") == "" |
| 23 | + ) # os.path.splitext treats this as root, empty ext |
| 24 | + |
| 25 | + |
| 26 | +def test_is_too_deep(): |
| 27 | + root = "/app" |
| 28 | + assert is_too_deep("/app/src/main", root, max_depth=1) is True |
| 29 | + assert is_too_deep("/app/src/main", root, max_depth=2) is False |
| 30 | + assert is_too_deep("/app", root, max_depth=0) is False |
| 31 | + |
| 32 | + |
| 33 | +def test_is_in_excluded_directory(): |
| 34 | + exclude_dirs = {"node_modules", "build", ".git"} |
| 35 | + |
| 36 | + # Direct match |
| 37 | + assert is_in_excluded_directory("node_modules/react/index.js", exclude_dirs) is True |
| 38 | + assert is_in_excluded_directory("build/output.css", exclude_dirs) is True |
| 39 | + |
| 40 | + # Nested match |
| 41 | + assert ( |
| 42 | + is_in_excluded_directory("src/components/node_modules/test.js", exclude_dirs) |
| 43 | + is False |
| 44 | + ) # Only matches prefix or exact folder |
| 45 | + exclude_nested = {"src/generated"} |
| 46 | + assert is_in_excluded_directory("src/generated/api.ts", exclude_nested) is True |
| 47 | + |
| 48 | + # Safe path |
| 49 | + assert is_in_excluded_directory("src/main.py", exclude_dirs) is False |
| 50 | + |
| 51 | + |
| 52 | +def test_get_prefix(): |
| 53 | + config = { |
| 54 | + "comment_styles": {".py": "#", ".js": "//", "Dockerfile": "#", ".html": "<!--"} |
| 55 | + } |
| 56 | + assert get_prefix("script.py", config) == "#" |
| 57 | + assert get_prefix("app.js", config) == "//" |
| 58 | + assert get_prefix("Dockerfile", config) == "#" # Matches filename |
| 59 | + assert get_prefix("unknown.xyz", config) == "#" # Fallback |
| 60 | + |
| 61 | + |
| 62 | +def test_get_annotation_text(): |
| 63 | + config = {"comment_styles": {".py": "#", ".html": "<!--"}} |
| 64 | + |
| 65 | + text_py = get_annotation_text("src/main.py", "main.py", config) |
| 66 | + assert text_py == f"# src/main.py {SIGNATURE}\n" |
| 67 | + |
| 68 | + text_html = get_annotation_text("public/index.html", "index.html", config) |
| 69 | + assert text_html == f"<!-- public/index.html {SIGNATURE} -->\n" |
| 70 | + |
| 71 | + |
| 72 | +# --- File I/O Tests (Using pytest tmp_path fixture) --- |
| 73 | + |
| 74 | + |
| 75 | +def test_should_skip_extensionless_file(tmp_path): |
| 76 | + # Test Shebang |
| 77 | + bash_file = tmp_path / "script" |
| 78 | + bash_file.write_text("#!/bin/bash\necho 'hello'") |
| 79 | + assert should_skip_extensionless_file(str(bash_file)) is True |
| 80 | + |
| 81 | + # Test XML |
| 82 | + xml_file = tmp_path / "config" |
| 83 | + xml_file.write_text('<?xml version="1.0"?>\n<root></root>') |
| 84 | + assert should_skip_extensionless_file(str(xml_file)) is True |
| 85 | + |
| 86 | + # Normal text |
| 87 | + txt_file = tmp_path / "notes" |
| 88 | + txt_file.write_text("Just some regular text\nNothing special.") |
| 89 | + assert should_skip_extensionless_file(str(txt_file)) is False |
| 90 | + |
| 91 | + |
| 92 | +def test_is_too_large(tmp_path): |
| 93 | + big_file = tmp_path / "large.bin" |
| 94 | + # Create a 2KB file (2048 bytes) |
| 95 | + big_file.write_bytes(b"0" * 2048) |
| 96 | + |
| 97 | + assert is_too_large(str(big_file), max_kb=1) is True |
| 98 | + assert is_too_large(str(big_file), max_kb=3) is False |
| 99 | + |
| 100 | + |
| 101 | +def test_apply_and_remove_annotation(tmp_path): |
| 102 | + target_file = tmp_path / "test_script.py" |
| 103 | + target_file.write_text("print('hello world')\n") |
| 104 | + |
| 105 | + filepath = str(target_file) |
| 106 | + rel_path = "test_script.py" |
| 107 | + config = {"comment_styles": {".py": "#"}} |
| 108 | + |
| 109 | + # 1. Apply annotation |
| 110 | + success = apply_annotation(filepath, rel_path, config) |
| 111 | + assert success is True |
| 112 | + content = target_file.read_text() |
| 113 | + assert content.startswith(f"# {rel_path} {SIGNATURE}\n") |
| 114 | + assert "print('hello world')" in content |
| 115 | + |
| 116 | + # 2. Prevent double annotation |
| 117 | + success_duplicate = apply_annotation(filepath, rel_path, config) |
| 118 | + assert success_duplicate is False # Should return False because signature exists |
| 119 | + |
| 120 | + # 3. Remove annotation |
| 121 | + success_remove = remove_annotation(filepath, rel_path) |
| 122 | + assert success_remove is True |
| 123 | + clean_content = target_file.read_text() |
| 124 | + assert clean_content == "print('hello world')\n" # Back to original |
| 125 | + |
| 126 | + # 4. Remove when no annotation exists |
| 127 | + success_remove_again = remove_annotation(filepath, rel_path) |
| 128 | + assert success_remove_again is False |
| 129 | + |
| 130 | + |
| 131 | +class MockGitIgnore: |
| 132 | + def match_file(self, path): |
| 133 | + return path == "ignored_file.txt" |
| 134 | + |
| 135 | + |
| 136 | +def test_is_git_ignored(): |
| 137 | + spec = MockGitIgnore() |
| 138 | + |
| 139 | + from annotator.core import is_git_ignored |
| 140 | + |
| 141 | + assert is_git_ignored("ignored_file.txt", spec) is True |
| 142 | + assert is_git_ignored("tracked_file.txt", spec) is False |
| 143 | + assert is_git_ignored("file.txt", None) is False |
0 commit comments