import os import pathlib import subprocess import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from test_env import assert_not_root, isolated_env from failure import Failure 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"), "an editorconfig two folders down": ("src__web__.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_and_the_settings_files_it_holds(self): self.assertIsNone(space_indented(self.checkout(**{"vendor__lib.py": SPACED, "web__node_modules__x__index.js": SPACED}))) vendored = self.checkout(**{"z.py": SPACED, "vendor__lib__.editorconfig": "indent_style = space\n", "third_party__x__package.json": '{"useTabs": false}'}) self.assertEqual(space_indented(vendored), SpaceIndented(spaced=2, tabbed=0)) def test_reads_a_checkout_with_a_file_name_that_is_not_utf8(self): root = self.checkout(**{"a.py": SPACED}) (root / os.fsdecode(b"caf\xe9.py")).write_text(SPACED) subprocess.run(["git", "add", "-A"], cwd=root, check=True, env=isolated_env()) self.assertEqual(space_indented(root), SpaceIndented(spaced=2, tabbed=0)) 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_sums_the_tabbed_lines_of_every_file(self): self.assertIsNone(space_indented(self.checkout(**{"a.py": SPACED, "b.py": SPACED, "c.py": TABBED, "d.py": TABBED, "e.py": TABBED}))) def test_skips_a_tracked_file_that_is_gone_from_the_disk(self): root = self.checkout(**{"a.py": SPACED, "gone.py": SPACED}) (root / "gone.py").unlink() self.assertEqual(space_indented(root), SpaceIndented(spaced=2, tabbed=0)) def test_reads_a_file_that_is_not_utf8(self): root = self.checkout(**{"a.py": ""}) (root / "a.py").write_bytes(b"\xff\xfe\n x\n y\n") self.assertEqual(space_indented(root), SpaceIndented(spaced=2, tabbed=0)) def test_names_a_file_it_cannot_read(self): assert_not_root(self) root = self.checkout(**{"a.py": SPACED}) (root / "a.py").chmod(0) self.addCleanup((root / "a.py").chmod, 0o644) failed = space_indented(root) self.assertIsInstance(failed, Failure) self.assertTrue(failed.reason.startswith("a.py: "), failed.reason) def test_says_why_it_cannot_read_a_checkout(self): self.assertIsInstance(space_indented(pathlib.Path("/nonexistent-checkout")), Failure) if __name__ == "__main__": unittest.main()