From bbaa56d0eca207352b77151247c25100da9a934a Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Fri, 27 Nov 2026 15:51:44 +0100 Subject: [PATCH] =?UTF-8?q?Answer=20stability=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=203=20=F0=9F=9F=A1=20(0=20misread);=20skip=20vend?= =?UTF-8?q?ored=20settings=20files,=20test=20the=20worktree=20read,=20use?= =?UTF-8?q?=20assert=5Fnot=5Froot,=20read=20a=20file=20name=20that=20is=20?= =?UTF-8?q?not=20UTF-8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/chunk_preflight/indentation.py | 8 +++++--- bin/chunk_preflight/test_chunk_preflight.py | 9 +++++++++ bin/chunk_preflight/test_indentation.py | 18 ++++++++++++++---- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/bin/chunk_preflight/indentation.py b/bin/chunk_preflight/indentation.py index 4a73e4c0..86ac098b 100644 --- a/bin/chunk_preflight/indentation.py +++ b/bin/chunk_preflight/indentation.py @@ -34,12 +34,14 @@ class SpaceIndented: def space_indented(base: pathlib.Path, timeout: float | None = None) -> SpaceIndented | Failure | None: """The line counts of the tracked code under base where it has more space-indented lines than tab-indented ones and no tracked file sets the indent; None where it doesn't, or has no code; a Failure where a file cannot be read.""" - tracked = git("ls-files", "-z", cwd=str(base), timeout=timeout) + tracked = git("ls-files", "-z", cwd=str(base), timeout=timeout, errors="replace") if isinstance(tracked, Failure): return tracked spaced = tabbed = 0 for name in filter(None, tracked.split("\0")): - code = CODE.fullmatch(name) and not VENDORED.search(name) + if VENDORED.search(name): + continue + code = CODE.fullmatch(name) key = next((key for pattern, key in SETTINGS if pattern.fullmatch(name.rsplit("/", 1)[-1])), None) path = base / name if not (code or key) or not os.path.isfile(path): @@ -47,7 +49,7 @@ def space_indented(base: pathlib.Path, timeout: float | None = None) -> SpaceInd try: text = path.read_text(encoding="utf-8", errors="replace") except OSError as failure: - return Failure(f"cannot read {name}: {failure}") + return Failure(f"{name}: {failure}") if key and key in text: return None if code: diff --git a/bin/chunk_preflight/test_chunk_preflight.py b/bin/chunk_preflight/test_chunk_preflight.py index f23f23ac..2afb7443 100644 --- a/bin/chunk_preflight/test_chunk_preflight.py +++ b/bin/chunk_preflight/test_chunk_preflight.py @@ -830,6 +830,15 @@ class IndentationFact(unittest.TestCase): spaced.commit(spaced.root, "setting") self.assertNotIn("space-indented", spaced.preflight().stdout) + def test_reads_the_item_worktree_made_off_origins_default_branch_not_the_checkout(self): + repo = Repo(self) + repo.write(repo.root, "src/app.py", "def run():\n if True:\n return 1\n") + repo.commit(repo.root, "spaces, not pushed") + self.assertIn("space-indented", repo.preflight().stdout) + done = repo.preflight("--worktree") + self.assertEqual(done.returncode, 0, done.stderr) + self.assertNotIn("space-indented", done.stdout, "the worktree holds origin/main, whose code is indented with tabs") + class BesideTest(unittest.TestCase): """The fact that names the items that may start chunks beside the next one.""" diff --git a/bin/chunk_preflight/test_indentation.py b/bin/chunk_preflight/test_indentation.py index e6902346..9b8e1015 100644 --- a/bin/chunk_preflight/test_indentation.py +++ b/bin/chunk_preflight/test_indentation.py @@ -8,7 +8,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from failure import Failure -from test_env import isolated_env +from test_env import assert_not_root, isolated_env from chunk_preflight.indentation import space_indented, SpaceIndented TABBED = "def run():\n\tif True:\n\t\treturn 1\n" @@ -59,8 +59,16 @@ class SpaceIndentation(unittest.TestCase): 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): + 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(**{"a.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}))) @@ -78,12 +86,14 @@ class SpaceIndentation(unittest.TestCase): (root / "a.py").write_bytes(b"\xff\xfe\n x\n y\n") self.assertEqual(space_indented(root), SpaceIndented(spaced=2, tabbed=0)) - @unittest.skipIf(os.geteuid() == 0, "root reads any file") 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) - self.assertEqual(space_indented(root), Failure(f"cannot read a.py: [Errno 13] Permission denied: '{root}/a.py'")) + 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)