diff --git a/scripts/tests/test_spec_validate.py b/scripts/tests/test_spec_validate.py index 6b9d21371..8f7e826c3 100755 --- a/scripts/tests/test_spec_validate.py +++ b/scripts/tests/test_spec_validate.py @@ -258,5 +258,27 @@ def test_over_the_cap_is_rejected(self) -> None: ) +class TreeSourceRootCase(unittest.TestCase): + """A tree source naming the repository root is refused however it is spelled.""" + + def test_rejects_every_spelling_that_reduces_to_the_root(self) -> None: + for value in (".", "./", ".//.", ".///.", "./."): + with self.subTest(value=value): + self.assertTrue(validate.reduces_to_repo_root(value)) + + def test_accepts_a_source_below_the_root(self) -> None: + for value in ( + ".github/skills", + "spec", + "a/b/c", + ".github", + "./docs", + "docs//sub", + "docs/./sub", + ): + with self.subTest(value=value): + self.assertFalse(validate.reduces_to_repo_root(value)) + + if __name__ == "__main__": unittest.main() diff --git a/spec/validate.py b/spec/validate.py index 078ee2b86..0e79d16a0 100755 --- a/spec/validate.py +++ b/spec/validate.py @@ -127,6 +127,14 @@ def escapes_repo_root(value): ) +def reduces_to_repo_root(value): + """Whether `value` reduces to the repository root under POSIX path rules. + + A raw-string compare against "." accepts "./" and ".///.", which the schema also allows and which `scripts/carry.py` then refuses, so a manifest author gets a green validator and a failing carrier. Backslash and drive-letter spellings are `escapes_repo_root`'s subject and are not read here. + """ + return pathlib.PurePosixPath(value) == pathlib.PurePosixPath(".") + + def canonical_file_in_root(rel_path): """Whether `ROOT / rel_path` resolves, symlinks followed, to an existing file under ROOT. @@ -756,9 +764,9 @@ def check_selector(where, applies_to): continue for field, value in (("source", source), ("target", target)): parts = pathlib.PurePosixPath(value).parts - if value == "." or value.startswith("/") or ".." in parts: + if reduces_to_repo_root(value) or value.startswith("/") or ".." in parts: errors.append( - f"files.json: tree {source} {field} '{value}' must be below the repository root (no leading /, . or ..)" + f"files.json: tree {source} {field} '{value}' must be a path below the repository root" ) if tree.get("fidelity") != "verbatim-tree": errors.append(f"files.json: tree {source} fidelity must be 'verbatim-tree'")