File size: 2,246 Bytes
53b2ac9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # SPDX-License-Identifier: MIT
"""Tests for the code_stats plugin."""
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "plugins"))
from code_stats import _is_binary, run
def test_is_binary_detects_text_binary_and_missing_files(tmp_path):
text_file = tmp_path / "plain.py"
text_file.write_text("print('hello')\n")
binary_file = tmp_path / "compiled.py"
binary_file.write_bytes(b"print\x00hidden")
assert _is_binary(str(text_file)) is False
assert _is_binary(str(binary_file)) is True
assert _is_binary(str(tmp_path / "missing.py")) is True
def test_run_counts_languages_markers_and_skips_ignored_paths(tmp_path):
(tmp_path / "app.py").write_text(
"print('hello')\n"
"# TODO: test marker\n"
"# FIXME: fix marker\n"
"# HACK: hack marker\n"
)
(tmp_path / "web.js").write_text("console.log('hi')\n")
(tmp_path / "README.md").write_text("# Project\n")
(tmp_path / "bundle.min.js").write_text("minified should be skipped\n")
(tmp_path / "binary.py").write_bytes(b"ignored\x00binary\n")
hidden = tmp_path / ".hidden"
hidden.mkdir()
(hidden / "secret.py").write_text("print('skip hidden')\n")
vendor = tmp_path / "vendor"
vendor.mkdir()
(vendor / "vendored.py").write_text("print('skip vendor')\n")
result = run(path=str(tmp_path), top_n=5)
assert "Total: 3 files, 6 lines" in result
assert "Python" in result
assert "JavaScript" in result
assert "Markdown" in result
assert "TODO: 1" in result
assert "FIXME: 1" in result
assert "HACK: 1" in result
assert "app.py" in result
assert "web.js" in result
assert "README.md" in result
assert "bundle.min.js" not in result
assert "binary.py" not in result
assert "secret.py" not in result
assert "vendored.py" not in result
def test_run_reports_missing_directory_and_empty_source_tree(tmp_path):
missing = tmp_path / "missing"
empty = tmp_path / "empty"
empty.mkdir()
(empty / "notes.txt").write_text("not a recognized source extension\n")
assert "Error: Not a directory" in run(path=str(missing))
assert "No recognized source files found" in run(path=str(empty))
|