|
1 | 1 | """Unit tests for main.py.""" |
2 | 2 |
|
| 3 | +import functools |
3 | 4 | import importlib.metadata |
4 | 5 | import io |
5 | 6 | import json |
6 | 7 | import os |
7 | 8 | import re |
8 | 9 | import runpy |
9 | 10 | import shutil |
| 11 | +import subprocess |
10 | 12 | import sys |
11 | 13 | import tempfile |
12 | 14 | import unittest |
@@ -390,32 +392,55 @@ def test_invalid_json_falls_back_to_raw_text(self): |
390 | 392 |
|
391 | 393 |
|
392 | 394 | class TestRunPrMessageChecks(unittest.TestCase): |
393 | | - """Commits the clone lacks are piped in; the rest are checked by hash.""" |
| 395 | + """Each commit's raw message is piped in, with that commit's own author.""" |
394 | 396 |
|
395 | 397 | def setUp(self): |
396 | | - resolves = patch("main._rev_resolves", return_value=False) |
397 | | - resolves.start() |
398 | | - self.addCleanup(resolves.stop) |
| 398 | + local = patch("main._local_author", return_value=None) |
| 399 | + self.local_author = local.start() |
| 400 | + self.addCleanup(local.stop) |
399 | 401 | main.API_AUTHORS.clear() |
400 | 402 | self.addCleanup(main.API_AUTHORS.clear) |
401 | 403 |
|
402 | | - def test_a_commit_in_the_clone_is_checked_by_its_hash(self): |
403 | | - """The CLI then reads that commit's own author (CC017, ignore_authors).""" |
| 404 | + def test_a_commit_in_the_clone_carries_its_own_author(self): |
| 405 | + """The raw message is still piped, so it reads as before (no --rev).""" |
| 406 | + self.local_author.return_value = ("Renovate Bot", "bot@renovateapp.com") |
404 | 407 | mock_result = MagicMock(returncode=0, stdout=json_output(make_check("message"))) |
405 | | - with ( |
406 | | - patch("main._rev_resolves", return_value=True), |
407 | | - patch("main.subprocess.run", return_value=mock_result) as mock_run, |
408 | | - ): |
| 408 | + with patch("main.subprocess.run", return_value=mock_result) as mock_run: |
409 | 409 | scopes = main.run_pr_message_checks([(SHA_A, "fix: something")]) |
410 | 410 | self.assertEqual(scopes[0].status, "pass") |
411 | 411 | self.assertEqual(scopes[0].sha, SHA_A) |
| 412 | + self.local_author.assert_called_once_with(SHA_A) |
412 | 413 | self.assertEqual( |
413 | 414 | mock_run.call_args[0][0], |
414 | | - ["commit-check", "--format", "json", "--message", "--rev", SHA_A], |
| 415 | + ["commit-check", "--format", "json", "--message"], |
415 | 416 | ) |
416 | | - self.assertIsNone(mock_run.call_args[1]["input"]) |
| 417 | + self.assertEqual(mock_run.call_args[1]["input"], "fix: something") |
| 418 | + env = mock_run.call_args[1]["env"] |
| 419 | + self.assertEqual(env["GIT_AUTHOR_NAME"], "Renovate Bot") |
| 420 | + self.assertEqual(env["GIT_AUTHOR_EMAIL"], "bot@renovateapp.com") |
| 421 | + |
| 422 | + def test_a_blank_author_field_is_left_to_git(self): |
| 423 | + self.local_author.return_value = ("Renovate Bot", "") |
| 424 | + mock_result = MagicMock(returncode=0, stdout=json_output(make_check("message"))) |
| 425 | + with patch("main.subprocess.run", return_value=mock_result) as mock_run: |
| 426 | + main.run_pr_message_checks([(SHA_A, "fix: something")]) |
| 427 | + env = mock_run.call_args[1]["env"] |
| 428 | + self.assertEqual(env["GIT_AUTHOR_NAME"], "Renovate Bot") |
| 429 | + self.assertNotEqual(env.get("GIT_AUTHOR_EMAIL"), "") |
| 430 | + |
| 431 | + def test_no_author_at_all_passes_no_environment(self): |
| 432 | + self.local_author.return_value = ("", "") |
| 433 | + mock_result = MagicMock(returncode=0, stdout=json_output(make_check("message"))) |
| 434 | + with patch("main.subprocess.run", return_value=mock_result) as mock_run: |
| 435 | + main.run_pr_message_checks([(SHA_A, "fix: something")]) |
417 | 436 | self.assertIsNone(mock_run.call_args[1]["env"]) |
418 | 437 |
|
| 438 | + def test_a_commit_without_a_hash_is_not_looked_up(self): |
| 439 | + mock_result = MagicMock(returncode=0, stdout=json_output(make_check("message"))) |
| 440 | + with patch("main.subprocess.run", return_value=mock_result): |
| 441 | + main.run_pr_message_checks([("", "fix: something")]) |
| 442 | + self.local_author.assert_not_called() |
| 443 | + |
419 | 444 | def test_an_api_listed_commit_carries_its_author(self): |
420 | 445 | """No commit to read, so the API's author goes in git's variables.""" |
421 | 446 | main.API_AUTHORS[SHA_A] = ("google-labs-jules[bot]", "jules@example.com") |
@@ -490,6 +515,69 @@ def test_empty_list(self): |
490 | 515 | mock_run.assert_not_called() |
491 | 516 |
|
492 | 517 |
|
| 518 | +class TestLocalAuthor(unittest.TestCase): |
| 519 | + """The author of a commit the clone holds, read from git.""" |
| 520 | + |
| 521 | + def test_reads_name_and_email(self): |
| 522 | + result = MagicMock(returncode=0, stdout="Jane Dev\x1fjane@example.com\n") |
| 523 | + with patch("main.subprocess.run", return_value=result) as mock_run: |
| 524 | + self.assertEqual( |
| 525 | + main._local_author(SHA_A), ("Jane Dev", "jane@example.com") |
| 526 | + ) |
| 527 | + self.assertEqual( |
| 528 | + mock_run.call_args[0][0], |
| 529 | + ["git", "show", "-s", "--format=%an%x1f%ae", SHA_A], |
| 530 | + ) |
| 531 | + |
| 532 | + def test_a_commit_git_cannot_show_has_no_author(self): |
| 533 | + result = MagicMock(returncode=128, stdout="") |
| 534 | + with patch("main.subprocess.run", return_value=result): |
| 535 | + self.assertIsNone(main._local_author(SHA_A)) |
| 536 | + |
| 537 | + def test_output_without_the_separator_has_no_author(self): |
| 538 | + result = MagicMock(returncode=0, stdout="unexpected\n") |
| 539 | + with patch("main.subprocess.run", return_value=result): |
| 540 | + self.assertIsNone(main._local_author(SHA_A)) |
| 541 | + |
| 542 | + def test_missing_git_has_no_author(self): |
| 543 | + with patch("main.subprocess.run", side_effect=OSError("no git")): |
| 544 | + self.assertIsNone(main._local_author(SHA_A)) |
| 545 | + |
| 546 | + def test_reads_a_real_commit(self): |
| 547 | + with tempfile.TemporaryDirectory() as repo: |
| 548 | + env = { |
| 549 | + **os.environ, |
| 550 | + "GIT_AUTHOR_NAME": "Renovate Bot", |
| 551 | + "GIT_AUTHOR_EMAIL": "bot@renovateapp.com", |
| 552 | + "GIT_COMMITTER_NAME": "Renovate Bot", |
| 553 | + "GIT_COMMITTER_EMAIL": "bot@renovateapp.com", |
| 554 | + } |
| 555 | + run = functools.partial( |
| 556 | + subprocess.run, cwd=repo, env=env, check=True, capture_output=True |
| 557 | + ) |
| 558 | + run(["git", "init", "-q"]) |
| 559 | + run( |
| 560 | + [ |
| 561 | + "git", |
| 562 | + "-c", |
| 563 | + "commit.gpgsign=false", |
| 564 | + "commit", |
| 565 | + "-q", |
| 566 | + "--allow-empty", |
| 567 | + "-m", |
| 568 | + "chore: bump", |
| 569 | + ] |
| 570 | + ) |
| 571 | + sha = run(["git", "rev-parse", "HEAD"], text=True).stdout.strip() |
| 572 | + cwd = os.getcwd() |
| 573 | + os.chdir(repo) |
| 574 | + try: |
| 575 | + author = main._local_author(sha) |
| 576 | + finally: |
| 577 | + os.chdir(cwd) |
| 578 | + self.assertEqual(author, ("Renovate Bot", "bot@renovateapp.com")) |
| 579 | + |
| 580 | + |
493 | 581 | class TestRunOtherChecks(unittest.TestCase): |
494 | 582 | def test_empty_args_returns_no_scopes(self): |
495 | 583 | with patch("main.subprocess.run") as mock_run: |
|
0 commit comments