Files
claude-config/bin/chunk_preflight/test_indentation.py
T
lilleman e785ce8f6b
CI / tests (pull_request) Successful in 5m8s
Sort the new imports by first binding
2026-11-27 15:16:10 +01:00

73 lines
3.0 KiB
Python

import os
import pathlib
import subprocess
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from failure import Failure
from test_env import isolated_env
from chunk_preflight.indentation import space_indented, SpaceIndented
TABBED = "def run():\n\tif True:\n\t\treturn 1\n"
SPACED = "def run():\n if True:\n return 1\n"
class SpaceIndentation(unittest.TestCase):
"""Each case is a fake checkout whose tracked files are those given."""
def checkout(self, **files):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
root = pathlib.Path(os.path.realpath(temp.name))
for name, text in files.items():
path = root / name.replace("__", "/")
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text)
subprocess.run(["git", "init", "--quiet"], cwd=root, check=True, env=isolated_env())
subprocess.run(["git", "add", "-A"], cwd=root, check=True, env=isolated_env())
return root
def test_stays_silent_for_code_indented_with_tabs(self):
self.assertIsNone(space_indented(self.checkout(**{"app.py": TABBED})))
def test_counts_the_lines_of_code_indented_with_spaces_where_no_tool_setting_says_so(self):
root = self.checkout(**{"app.py": SPACED, "lib__more.rs": SPACED, "web__page.ts": TABBED, "README.md": SPACED})
self.assertEqual(space_indented(root), SpaceIndented(spaced=4, tabbed=2))
def test_stays_silent_where_a_tool_reads_a_setting_that_says_so(self):
settings = {
"editorconfig": (".editorconfig", "[*]\nindent_style = space\n"),
"rustfmt": ("rustfmt.toml", "hard_tabs = false\n"),
"prettier": (".prettierrc.json", '{"useTabs": false}\n'),
"prettier in package.json": ("package.json", '{"prettier": {"useTabs": false}}\n'),
"biome": ("biome.jsonc", '{"formatter": {"indentStyle": "space"}}\n'),
"a nested editorconfig": ("src__.editorconfig", "indent_style = space\n"),
}
for label, (name, text) in settings.items():
with self.subTest(label):
self.assertIsNone(space_indented(self.checkout(**{"app.py": SPACED, name: text})))
def test_reports_a_file_that_names_no_such_setting(self):
for name, text in ((".editorconfig", "[*]\nend_of_line = lf\n"), ("package.json", '{"name": "x"}\n'), ("notes.txt", "indent_style = space\n")):
with self.subTest(name):
self.assertIsInstance(space_indented(self.checkout(**{"app.py": SPACED, name: text})), SpaceIndented)
def test_stays_silent_for_a_repo_without_code(self):
self.assertIsNone(space_indented(self.checkout(**{"README.md": SPACED, "data.json": SPACED, "ci.yml": SPACED})))
def test_leaves_out_vendored_code(self):
self.assertIsNone(space_indented(self.checkout(**{"vendor__lib.py": SPACED, "web__node_modules__x__index.js": SPACED})))
def test_stays_silent_where_tabs_are_as_many_as_spaces(self):
self.assertIsNone(space_indented(self.checkout(**{"a.py": SPACED, "b.py": TABBED})))
def test_says_why_it_cannot_read_a_checkout(self):
self.assertIsInstance(space_indented(pathlib.Path("/nonexistent-checkout")), Failure)
if __name__ == "__main__":
unittest.main()