diff --git a/tools/check_conventions.py b/tools/check_conventions.py index b559a7be9..ed761959e 100644 --- a/tools/check_conventions.py +++ b/tools/check_conventions.py @@ -66,7 +66,7 @@ class Check: path = None for line in git("diff", "-U0", "--no-color", "--no-renames", "--no-prefix", self.base, head).splitlines(): if line.startswith("+++ "): - path = None if line == "+++ /dev/null" else line[4:] + path = None if line == "+++ /dev/null" else line[4:].rstrip("\t") elif line.startswith("@@"): number = int(re.search(r"\+(\d+)", line).group(1)) elif line.startswith("+") and path: diff --git a/tools/tests/test_check_conventions_comments.py b/tools/tests/test_check_conventions_comments.py index 33fe1f9c6..a629714cc 100644 --- a/tools/tests/test_check_conventions_comments.py +++ b/tools/tests/test_check_conventions_comments.py @@ -226,5 +226,37 @@ class PullRequestTemplateTests(unittest.TestCase): self.assertEqual(self.findings("- [x] all"), []) +class SpacedPathTests(unittest.TestCase): + def test_comment_in_path_with_space_is_reported(self): + checker = Path(__file__).resolve().parents[1] / "check_conventions.py" + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + + def git(*args): + return subprocess.run( + ["git", "-c", "user.name=Conventions Test", "-c", "user.email=test@example.com", + "-c", "commit.gpgsign=false", *args], + cwd=root, capture_output=True, text=True, check=True, + ) + + git("init", "-q") + (root / "readme.txt").write_text("x\n", encoding="utf-8") + git("add", ".") + git("commit", "-qm", "docs: add readme") + source = root / "core/a b.cpp" + source.parent.mkdir(parents=True) + source.write_text("int x; // note\n", encoding="utf-8") + git("add", ".") + git("commit", "-qm", "feat: add spaced file") + result = subprocess.run( + [sys.executable, "-I", str(checker), "--base", "HEAD~1"], + cwd=root, capture_output=True, text=True, + env={key: value for key, value in os.environ.items() + if key not in ("GITHUB_EVENT_PATH", "GITHUB_STEP_SUMMARY", "GITHUB_ACTIONS")}, + ) + self.assertEqual(result.returncode, 1, result.stdout + result.stderr) + self.assertIn("core/a b.cpp:1: error: [comment]", result.stdout) + + if __name__ == "__main__": unittest.main()