diff options
| author | Romain PINSOLLE <romain.pins@gmail.com> | 2026-11-24 10:53:11 +0100 |
|---|---|---|
| committer | Romain PINSOLLE <romain.pins@gmail.com> | 2026-11-24 10:53:11 +0100 |
| commit | 5abe553f5fcc1e373ebf4f9e640cf2aec79b69e7 (patch) | |
| tree | 50b367219ddf4bdccc8db354ce7fbc6c25a17fd7 | |
| parent | 21625bca8c675c62f9dbffc318ce6a59d57faf31 (diff) | |
merge_check: exit 2 when git cannot be run (MERGE-CHECK-GIT-MISSING)
The docstring promises exit 2 on a git error. When git is not on PATH,
subprocess.run raised FileNotFoundError, which escaped main() as a
traceback with exit 1. The git() helper now turns the OSError into a
GitError, so the message reads "merge_check: cannot run git: ..." and
the exit code is 2.
Tests cover a patched FileNotFoundError, an empty PATH, and a folder
that is not a repository (git exits nonzero; still exit 2).
| -rw-r--r-- | scripts/merge_check.py | 5 | ||||
| -rw-r--r-- | tests/test_merge_check.py | 27 |
2 files changed, 31 insertions, 1 deletions
diff --git a/scripts/merge_check.py b/scripts/merge_check.py index c4cbdceca..a5a098964 100644 --- a/scripts/merge_check.py +++ b/scripts/merge_check.py | |||
| @@ -42,7 +42,10 @@ class GitError(Exception): | |||
| 42 | 42 | ||
| 43 | 43 | ||
| 44 | def git(repo, *args, check=True): | 44 | def git(repo, *args, check=True): |
| 45 | r = subprocess.run(["git", "-C", repo, *args], capture_output=True) | 45 | try: |
| 46 | r = subprocess.run(["git", "-C", repo, *args], capture_output=True) | ||
| 47 | except OSError as e: # git is not on PATH, or cannot be executed: a usage error (exit 2), not a traceback | ||
| 48 | raise GitError(f"cannot run git: {e.strerror or e}") from None | ||
| 46 | # Decode without universal-newline translation: a bare CR inside a Git line is content. | 49 | # Decode without universal-newline translation: a bare CR inside a Git line is content. |
| 47 | r.stdout = r.stdout.decode("utf-8", "replace") | 50 | r.stdout = r.stdout.decode("utf-8", "replace") |
| 48 | r.stderr = r.stderr.decode("utf-8", "replace") | 51 | r.stderr = r.stderr.decode("utf-8", "replace") |
diff --git a/tests/test_merge_check.py b/tests/test_merge_check.py index 0b3588ce1..4a12bc7fd 100644 --- a/tests/test_merge_check.py +++ b/tests/test_merge_check.py | |||
| @@ -404,6 +404,33 @@ class MergeCheckTest(unittest.TestCase): | |||
| 404 | self.assertEqual(mc.main(["--repo", self.repo, "--main", "main", "1=no-such-ref"]), 2) | 404 | self.assertEqual(mc.main(["--repo", self.repo, "--main", "main", "1=no-such-ref"]), 2) |
| 405 | self.assertIn("merge_check:", err.getvalue()) | 405 | self.assertIn("merge_check:", err.getvalue()) |
| 406 | 406 | ||
| 407 | def run_without_git(self, patch): | ||
| 408 | out, err = io.StringIO(), io.StringIO() | ||
| 409 | with patch, redirect_stdout(out), redirect_stderr(err): | ||
| 410 | code = mc.main(["--repo", self.repo, "--main", "main", "--json", "20=whatever"]) | ||
| 411 | self.assertEqual((code, out.getvalue()), (2, "")) | ||
| 412 | self.assertNotIn("Traceback", err.getvalue()) | ||
| 413 | self.assertTrue(err.getvalue().startswith("merge_check: cannot run git: "), err.getvalue()) | ||
| 414 | return err.getvalue() | ||
| 415 | |||
| 416 | def test_git_that_cannot_be_run_is_a_git_error_not_a_traceback(self): | ||
| 417 | missing = FileNotFoundError(2, "No such file or directory") | ||
| 418 | message = self.run_without_git(mock.patch.object(subprocess, "run", side_effect=missing)) | ||
| 419 | self.assertEqual(message, "merge_check: cannot run git: No such file or directory\n") | ||
| 420 | |||
| 421 | def test_git_missing_from_an_empty_path_is_a_git_error(self): | ||
| 422 | self.run_without_git(mock.patch.dict(os.environ, {"PATH": os.devnull})) | ||
| 423 | |||
| 424 | def test_a_folder_that_is_not_a_repository_is_a_git_error(self): | ||
| 425 | plain = tempfile.TemporaryDirectory(prefix="leonida not a repo ") | ||
| 426 | self.addCleanup(plain.cleanup) | ||
| 427 | err = io.StringIO() | ||
| 428 | with redirect_stdout(io.StringIO()), redirect_stderr(err): | ||
| 429 | code = mc.main(["--repo", plain.name, "--main", "main", "1=HEAD"]) | ||
| 430 | self.assertEqual(code, 2) | ||
| 431 | self.assertTrue(err.getvalue().startswith("merge_check: git "), err.getvalue()) | ||
| 432 | self.assertNotIn("Traceback", err.getvalue()) | ||
| 433 | |||
| 407 | 434 | ||
| 408 | if __name__ == "__main__": | 435 | if __name__ == "__main__": |
| 409 | unittest.main() | 436 | unittest.main() |