Answer stability round 1: 0 🔴, 3 🟡 (0 misread); skip vendored settings files, test the worktree read, use assert_not_root, read a file name that is not UTF-8
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user