1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
|
# SPDX-FileCopyrightText: 2022-2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
import unittest
from pathlib import Path
from unittest.mock import MagicMock, patch
from pontos.git import (
DEFAULT_TAG_SORT_SUFFIX,
ConfigScope,
Git,
GitError,
MergeStrategy,
ResetMode,
Status,
TagSort,
)
from pontos.testing import temp_directory, temp_git_repository
class GitTestCase(unittest.TestCase):
@patch("pontos.git._git.exec_git")
def test_exec(self, exec_git_mock):
git = Git()
git.exec("foo", "bar", "baz")
exec_git_mock.assert_called_once_with("foo", "bar", "baz", cwd=None)
@patch("pontos.git._git.exec_git")
def test_clone(self, exec_git_mock):
git = Git()
git.clone("http://foo/foo.git", Path("/bar"))
exec_git_mock.assert_called_once_with(
"clone", "http://foo/foo.git", "/bar", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_clone_with_remote(self, exec_git_mock):
git = Git()
git.clone("http://foo/foo.git", Path("/bar"), remote="foo")
exec_git_mock.assert_called_once_with(
"clone", "-o", "foo", "http://foo/foo.git", "/bar", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_clone_with_branch(self, exec_git_mock):
git = Git()
git.clone("http://foo/foo.git", Path("/bar"), branch="foo")
exec_git_mock.assert_called_once_with(
"clone", "-b", "foo", "http://foo/foo.git", "/bar", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_clone_with_depth(self, exec_git_mock):
git = Git()
git.clone("http://foo/foo.git", Path("/bar"), depth=1)
exec_git_mock.assert_called_once_with(
"clone", "--depth", "1", "http://foo/foo.git", "/bar", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_init(self, exec_git_mock):
git = Git()
git.init()
exec_git_mock.assert_called_once_with("init", cwd=None)
@patch("pontos.git._git.exec_git")
def test_init_bare(self, exec_git_mock):
git = Git()
git.init(bare=True)
exec_git_mock.assert_called_once_with("init", "--bare", cwd=None)
def test_cwd(self):
git = Git()
self.assertIsNone(git.cwd)
new_cwd = Path("foo")
git.cwd = new_cwd
self.assertEqual(git.cwd, new_cwd.absolute())
@patch("pontos.git._git.exec_git")
def test_create_branch(self, exec_git_mock):
git = Git()
git.create_branch("foo")
exec_git_mock.assert_called_once_with("checkout", "-b", "foo", cwd=None)
@patch("pontos.git._git.exec_git")
def test_create_branch_with_starting_point(self, exec_git_mock):
git = Git()
git.create_branch("foo", start_point="bar")
exec_git_mock.assert_called_once_with(
"checkout", "-b", "foo", "bar", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_rebase(self, exec_git_mock):
git = Git()
git.rebase("foo")
exec_git_mock.assert_called_once_with("rebase", "foo", cwd=None)
@patch("pontos.git._git.exec_git")
def test_rebase_with_head(self, exec_git_mock):
git = Git()
git.rebase("foo", head="bar")
exec_git_mock.assert_called_once_with("rebase", "foo", "bar", cwd=None)
@patch("pontos.git._git.exec_git")
def test_rebase_with_head_and_onto(self, exec_git_mock):
git = Git()
git.rebase("foo", head="bar", onto="staging")
exec_git_mock.assert_called_once_with(
"rebase", "--onto", "staging", "foo", "bar", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_rebase_with_octopus_strategy(self, exec_git_mock):
git = Git()
git.rebase("foo", strategy=MergeStrategy.OCTOPUS)
exec_git_mock.assert_called_once_with(
"rebase", "--strategy", "octopus", "foo", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_rebase_with_ort_ours_strategy(self, exec_git_mock):
git = Git()
git.rebase("foo", strategy=MergeStrategy.ORT_OURS)
exec_git_mock.assert_called_once_with(
"rebase", "--strategy", "ort", "-X", "ours", "foo", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_push(self, exec_git_mock):
git = Git()
git.push()
exec_git_mock.assert_called_once_with("push", cwd=None)
@patch("pontos.git._git.exec_git")
def test_push_with_remote(self, exec_git_mock):
git = Git()
git.push(remote="foo")
exec_git_mock.assert_called_once_with("push", "foo", cwd=None)
@patch("pontos.git._git.exec_git")
def test_push_with_remote_and_branch(self, exec_git_mock):
git = Git()
git.push(remote="foo", branch="bar")
exec_git_mock.assert_called_once_with("push", "foo", "bar", cwd=None)
@patch("pontos.git._git.exec_git")
def test_push_with_follow_tags(self, exec_git_mock):
git = Git()
git.push(follow_tags=True)
exec_git_mock.assert_called_once_with("push", "--follow-tags", cwd=None)
@patch("pontos.git._git.exec_git")
def test_push_with_follow_tags_false(self, exec_git_mock):
git = Git()
git.push(follow_tags=False)
exec_git_mock.assert_called_once_with("push", cwd=None)
@patch("pontos.git._git.exec_git")
def test_push_with_force(self, exec_git_mock):
git = Git()
git.push(force=True)
exec_git_mock.assert_called_once_with("push", "--force", cwd=None)
@patch("pontos.git._git.exec_git")
def test_push_with_force_false(self, exec_git_mock):
git = Git()
git.push(force=False)
exec_git_mock.assert_called_once_with("push", cwd=None)
@patch("pontos.git._git.exec_git")
def test_push_branch_with_delete(self, exec_git_mock):
git = Git()
git.push(delete=True, branch="v1.2.3", remote="origin")
exec_git_mock.assert_called_once_with(
"push", "--delete", "origin", "v1.2.3", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_push_refspec_with_delete(self, exec_git_mock):
git = Git()
git.push("v1.2.3", delete=True)
exec_git_mock.assert_called_once_with(
"push", "--delete", "v1.2.3", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_push_refspec(self, exec_git_mock):
git = Git()
git.push("v1.2.3")
exec_git_mock.assert_called_once_with("push", "v1.2.3", cwd=None)
@patch("pontos.git._git.exec_git")
def test_push_refspecs(self, exec_git_mock):
git = Git()
git.push(["v1.2.3", "main"])
exec_git_mock.assert_called_once_with(
"push", "v1.2.3", "main", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_config_get(self, exec_git_mock):
git = Git()
git.config("foo")
exec_git_mock.assert_called_once_with("config", "foo", cwd=None)
@patch("pontos.git._git.exec_git")
def test_config_set(self, exec_git_mock):
git = Git()
git.config("foo", "bar")
exec_git_mock.assert_called_once_with("config", "foo", "bar", cwd=None)
@patch("pontos.git._git.exec_git")
def test_config_get_local_scope(self, exec_git_mock):
git = Git()
git.config("foo", scope=ConfigScope.LOCAL)
exec_git_mock.assert_called_once_with(
"config", "--local", "foo", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_config_get_system_scope(self, exec_git_mock):
git = Git()
git.config("foo", scope=ConfigScope.SYSTEM)
exec_git_mock.assert_called_once_with(
"config", "--system", "foo", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_config_get_global_scope(self, exec_git_mock):
git = Git()
git.config("foo", scope=ConfigScope.GLOBAL)
exec_git_mock.assert_called_once_with(
"config", "--global", "foo", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_config_get_worktree_scope(self, exec_git_mock):
git = Git()
git.config("foo", scope=ConfigScope.WORKTREE)
exec_git_mock.assert_called_once_with(
"config", "--worktree", "foo", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_cherry_pick(self, exec_git_mock):
git = Git()
git.cherry_pick(["foo", "bar"])
exec_git_mock.assert_called_once_with(
"cherry-pick", "foo", "bar", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_cherry_pick_single_commit(self, exec_git_mock):
git = Git()
git.cherry_pick("foo")
exec_git_mock.assert_called_once_with("cherry-pick", "foo", cwd=None)
@patch("pontos.git._git.exec_git")
def test_list_tags(self, exec_git_mock):
exec_git_mock.return_value = "v1.0\nv2.0\nv2.1\n"
git = Git()
tags = git.list_tags()
exec_git_mock.assert_called_once_with("tag", "-l", cwd=None)
self.assertEqual(len(tags), 3)
self.assertEqual(tags[0], "v1.0")
self.assertEqual(tags[1], "v2.0")
self.assertEqual(tags[2], "v2.1")
@patch("pontos.git._git.exec_git")
def test_list_tags_with_version_sort(self, exec_git_mock):
exec_git_mock.return_value = "v1.0\nv2.0\nv2.1\n"
git = Git()
tags = git.list_tags(sort=TagSort.VERSION)
exec_git_mock.assert_called_once_with(
"tag",
"-l",
"--sort=version:refname",
cwd=None,
)
self.assertEqual(len(tags), 3)
self.assertEqual(tags[0], "v1.0")
self.assertEqual(tags[1], "v2.0")
self.assertEqual(tags[2], "v2.1")
@patch("pontos.git._git.exec_git")
def test_list_tags_with_version_suffix_sort(self, exec_git_mock):
exec_git_mock.return_value = "v1.0\nv2.0\nv2.1\n"
git = Git()
tags = git.list_tags(
sort=TagSort.VERSION, sort_suffix=DEFAULT_TAG_SORT_SUFFIX
)
exec_git_mock.assert_called_once_with(
"-c",
"versionsort.suffix=-alpha",
"-c",
"versionsort.suffix=a",
"-c",
"versionsort.suffix=-beta",
"-c",
"versionsort.suffix=b",
"-c",
"versionsort.suffix=-rc",
"-c",
"versionsort.suffix=rc",
"tag",
"-l",
"--sort=version:refname",
cwd=None,
)
self.assertEqual(len(tags), 3)
self.assertEqual(tags[0], "v1.0")
self.assertEqual(tags[1], "v2.0")
self.assertEqual(tags[2], "v2.1")
@patch("pontos.git._git.exec_git")
def test_list_tags_with_tag_name(self, exec_git_mock):
exec_git_mock.return_value = "v2.0\nv2.1\n"
git = Git()
tags = git.list_tags(tag_name="v2.*")
exec_git_mock.assert_called_once_with("tag", "-l", "v2.*", cwd=None)
self.assertEqual(len(tags), 2)
self.assertEqual(tags[0], "v2.0")
self.assertEqual(tags[1], "v2.1")
@patch("pontos.git._git.exec_git")
def test_add_single_file(self, exec_git_mock):
git = Git()
git.add("foo")
exec_git_mock.assert_called_once_with("add", "foo", cwd=None)
@patch("pontos.git._git.exec_git")
def test_add(self, exec_git_mock):
git = Git()
git.add(["foo", "bar"])
exec_git_mock.assert_called_once_with("add", "foo", "bar", cwd=None)
@patch("pontos.git._git.exec_git")
def test_commit(self, exec_git_mock):
git = Git()
git.commit("Add foo")
exec_git_mock.assert_called_once_with(
"commit", "-m", "Add foo", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_commit_with_signing_key(self, exec_git_mock):
git = Git()
git.commit(
"Add foo",
gpg_signing_key="8AE4BE429B60A59B311C2E739823FAA60ED1E580",
)
exec_git_mock.assert_called_once_with(
"commit",
"-S8AE4BE429B60A59B311C2E739823FAA60ED1E580",
"-m",
"Add foo",
cwd=None,
)
@patch("pontos.git._git.exec_git")
def test_commit_without_verify(self, exec_git_mock):
git = Git()
git.commit("Add foo", verify=False)
exec_git_mock.assert_called_once_with(
"commit", "--no-verify", "-m", "Add foo", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_commit_without_gpg_sign(self, exec_git_mock):
git = Git()
git.commit("Add foo", gpg_sign=False)
exec_git_mock.assert_called_once_with(
"commit", "--no-gpg-sign", "-m", "Add foo", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_tag(self, exec_git_mock):
git = Git()
git.tag(tag="test")
exec_git_mock.assert_called_once_with("tag", "test", cwd=None)
@patch("pontos.git._git.exec_git")
def test_tag_with_gpg_key(self, exec_git_mock):
git = Git()
git.tag("test", gpg_key_id="0x123")
exec_git_mock.assert_called_once_with(
"tag", "-u", "0x123", "test", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_tag_with_message(self, exec_git_mock):
git = Git()
git.tag("test", message="Tag for 123 release")
exec_git_mock.assert_called_once_with(
"tag", "-m", "Tag for 123 release", "test", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_tag_with_force(self, exec_git_mock):
git = Git()
git.tag("test", force=True)
exec_git_mock.assert_called_once_with(
"tag", "--force", "test", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_tag_without_sign(self, exec_git_mock):
git = Git()
git.tag("test", sign=False)
exec_git_mock.assert_called_once_with(
"tag", "--no-sign", "test", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_fetch(self, exec_git_mock):
git = Git()
git.fetch()
exec_git_mock.assert_called_once_with("fetch", cwd=None)
@patch("pontos.git._git.exec_git")
def test_fetch_with_remote(self, exec_git_mock):
git = Git()
git.fetch("foo")
exec_git_mock.assert_called_once_with("fetch", "foo", cwd=None)
@patch("pontos.git._git.exec_git")
def test_fetch_with_remote_and_refspec(self, exec_git_mock):
git = Git()
git.fetch("foo", "my-branch")
exec_git_mock.assert_called_once_with(
"fetch", "foo", "my-branch", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_fetch_with_verbose(self, exec_git_mock):
git = Git()
git.fetch(verbose=True)
exec_git_mock.assert_called_once_with("fetch", "-v", cwd=None)
@patch("pontos.git._git.exec_git")
def test_add_remote(self, exec_git_mock):
git = Git()
git.add_remote("foo", "https://foo.bar/foo.git")
exec_git_mock.assert_called_once_with(
"remote", "add", "foo", "https://foo.bar/foo.git", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_checkout(self, exec_git_mock):
git = Git()
git.checkout("foo")
exec_git_mock.assert_called_once_with("checkout", "foo", cwd=None)
@patch("pontos.git._git.exec_git")
def test_checkout_with_start_point(self, exec_git_mock):
git = Git()
git.checkout("foo", start_point="bar")
exec_git_mock.assert_called_once_with(
"checkout", "-b", "foo", "bar", cwd=None
)
@patch("pontos.git._git.exec_git")
def test_remote_url(self, exec_git_mock):
url = "git@github.com:foo/foo.git"
exec_git_mock.return_value = url
git = Git()
remote = git.remote_url("foo")
exec_git_mock.assert_called_once_with(
"remote", "get-url", "foo", cwd=None
)
self.assertEqual(remote, url)
@patch("pontos.git._git.exec_git")
def test_remote_url_with_default(self, exec_git_mock):
url = "git@github.com:foo/foo.git"
exec_git_mock.return_value = url
git = Git()
remote = git.remote_url()
exec_git_mock.assert_called_once_with(
"remote", "get-url", "origin", cwd=None
)
self.assertEqual(remote, url)
@patch("pontos.git._git.exec_git")
def test_log(self, exec_git_mock):
# pylint: disable=line-too-long
exec_git_mock.return_value = """commit 68c6c3785bbb049df63dc51f8b5b709eb19f8517
Author: Björn Ricks <bjoern.ricks@greenbone.net>
Date: Wed Apr 8 15:16:05 2020 +0200
Add a draft for a README.md document
commit 464f24d43d7293091b168c6b37ee37978a650958
Author: Björn Ricks <bjoern.ricks@greenbone.net>
Date: Wed Apr 8 14:28:53 2020 +0200
Initial commit
""" # noqa: E501
git = Git()
logs = git.log()
exec_git_mock.assert_called_once_with("log", cwd=None)
self.assertEqual(
logs[0], "commit 68c6c3785bbb049df63dc51f8b5b709eb19f8517"
)
self.assertEqual(
logs[6], "commit 464f24d43d7293091b168c6b37ee37978a650958"
)
@patch("pontos.git._git.exec_git")
def test_log_with_oneline(self, exec_git_mock):
exec_git_mock.return_value = """50f9963 Add CircleCI config for pontos
9a8feaa Rename to pontos only
047cfae Update README for installation and development
e6ea80d Update README
68c6c37 Add a draft for a README.md document
464f24d Initial commit"""
git = Git()
logs = git.log(oneline=True)
exec_git_mock.assert_called_once_with("log", "--oneline", cwd=None)
self.assertEqual(logs[0], "50f9963 Add CircleCI config for pontos")
self.assertEqual(logs[5], "464f24d Initial commit")
@patch("pontos.git._git.exec_git")
def test_log_with_format(self, exec_git_mock):
exec_git_mock.return_value = """Add CircleCI config for pontos
Rename to pontos only
Update README for installation and development
Update README
Add a draft for a README.md document
Initial commit"""
git = Git()
logs = git.log(format="format:%s")
exec_git_mock.assert_called_once_with(
"log", "--format=format:%s", cwd=None
)
self.assertEqual(logs[0], "Add CircleCI config for pontos")
self.assertEqual(logs[5], "Initial commit")
@patch("pontos.git._git.exec_git")
def test_rev_list(self, exec_git_mock):
git = Git()
git.rev_list("foo", "bar", "baz", max_parents=123, abbrev_commit=True)
exec_git_mock.assert_called_once_with(
"rev-list",
"--max-parents=123",
"--abbrev-commit",
"foo",
"bar",
"baz",
cwd=None,
)
@patch("pontos.git._git.exec_git")
def test_move(self, exec_git_mock):
git = Git()
git.move("foo", "bar")
exec_git_mock.assert_called_once_with(
"mv",
"foo",
"bar",
cwd=None,
)
@patch("pontos.git._git.exec_git")
def test_remove(self, exec_git_mock):
git = Git()
git.remove("foo")
exec_git_mock.assert_called_once_with(
"rm",
"foo",
cwd=None,
)
@patch("pontos.git._git.exec_git")
def test_status(self, exec_git_mock):
git = Git()
git.status()
exec_git_mock.assert_called_once_with(
"status",
"-z",
"--ignore-submodules",
"--untracked-files=no",
cwd=None,
)
@patch("pontos.git._git.exec_git")
def test_status_with_files(self, exec_git_mock):
git = Git()
git.status(["foo", "bar", "baz"])
exec_git_mock.assert_called_once_with(
"status",
"-z",
"--ignore-submodules",
"--untracked-files=no",
"--",
"foo",
"bar",
"baz",
cwd=None,
)
@patch("pontos.git._git.exec_git")
def test_version(self, exec_git_mock: MagicMock):
exec_git_mock.return_value = "git version 1.2.3"
git = Git()
self.assertEqual(git.version, "1.2.3")
def test_version_runs(self):
"""Getting the git version should not raise an error"""
git = Git()
git.version
@patch("pontos.git._git.exec_git")
def test_show_with_online_and_objects(self, exec_git_mock: MagicMock):
exec_git_mock.return_value = """9a8feaa Rename to pontos only
047cfae Update README for installation and development
"""
git = Git()
show = git.show(oneline=True, objects=["9a8feaa", "047cfae"])
exec_git_mock.assert_called_once_with(
"show", "--oneline", "9a8feaa", "047cfae", cwd=None
)
self.assertEqual(show[0], "9a8feaa Rename to pontos only")
self.assertEqual(
show[1], "047cfae Update README for installation and development"
)
@patch("pontos.git._git.exec_git")
def test_show_with_format(self, exec_git_mock: MagicMock):
exec_git_mock.return_value = """Rename to pontos only
"""
git = Git()
show = git.show(format="format:%s")
exec_git_mock.assert_called_once_with(
"show", "--format=format:%s", cwd=None
)
self.assertEqual(show, "Rename to pontos only")
@patch("pontos.git._git.exec_git")
def test_show_with_single_object(self, exec_git_mock: MagicMock):
content = """commit a6956fb1398cae9426e7ced0396248a90dc1ff64
Author: Björn Ricks <bjoern.ricks@greenbone.net>
Date: Wed Jul 19 15:07:03 2023 +0200
Add: Allow to get the git version string
diff --git a/pontos.git._git.py b/pontos.git._git.py
index a83eed8..09aed3d 100644
--- a/pontos.git._git.py
+++ b/pontos.git._git.py
@@ -168,6 +168,14 @@ class Git:
...
"""
exec_git_mock.return_value = content
git = Git()
show = git.show(objects="9a8feaa")
exec_git_mock.assert_called_once_with("show", "9a8feaa", cwd=None)
self.assertEqual(show, content.strip())
@patch("pontos.git._git.exec_git")
def test_show_with_patch(self, exec_git_mock: MagicMock):
content = """commit a6956fb1398cae9426e7ced0396248a90dc1ff64
Author: Björn Ricks <bjoern.ricks@greenbone.net>
Date: Wed Jul 19 15:07:03 2023 +0200
Add: Allow to get the git version string
diff --git a/pontos.git._git.py b/pontos.git._git.py
index a83eed8..09aed3d 100644
--- a/pontos.git._git.py
+++ b/pontos.git._git.py
@@ -168,6 +168,14 @@ class Git:
...
"""
exec_git_mock.return_value = content
git = Git()
show = git.show(patch=True)
exec_git_mock.assert_called_once_with("show", "--patch", cwd=None)
self.assertEqual(show, content.strip())
@patch("pontos.git._git.exec_git")
def test_show_with_no_patch(self, exec_git_mock: MagicMock):
content = """commit a6956fb1398cae9426e7ced0396248a90dc1ff64
Author: Björn Ricks <bjoern.ricks@greenbone.net>
Date: Wed Jul 19 15:07:03 2023 +0200
Add: Allow to get the git version string
"""
exec_git_mock.return_value = content
git = Git()
show = git.show(patch=False)
exec_git_mock.assert_called_once_with("show", "--no-patch", cwd=None)
self.assertEqual(show, content.strip())
@patch("pontos.git._git.exec_git")
def test_delete_tag(self, exec_git_mock):
git = Git()
git.delete_tag("v1.2.3")
exec_git_mock.assert_called_once_with("tag", "-d", "v1.2.3", cwd=None)
@patch("pontos.git._git.exec_git")
def test_reset_mixed(self, exec_git_mock):
git = Git()
git.reset("c1234", mode=ResetMode.MIXED)
exec_git_mock.assert_called_once_with(
"reset", "--mixed", "c1234", cwd=None
)
class GitExtendedTestCase(unittest.TestCase):
def test_semantic_list_tags(self):
with temp_git_repository() as tmp_git:
tags = [
"v0.6.5-alpha3",
"v0.6.5-beta1",
"v0.6.5-alpha2",
"v0.6.5-rc1",
"v0.6.5-alpha1",
"v0.6.5",
"v0.6.4",
"v0.6.3-alpha1",
"v1.0.0",
]
git = Git(tmp_git)
git.config("commit.gpgSign", "false", scope=ConfigScope.LOCAL)
git.config("tag.gpgSign", "false", scope=ConfigScope.LOCAL)
git.config("tag.sort", "refname", scope=ConfigScope.LOCAL)
tmp_file = tmp_git / "foo.txt"
tmp_file.touch()
git.add(tmp_file)
git.commit("some commit")
for tag in tags:
git.tag(tag)
tags = git.list_tags()
self.assertEqual(
tags,
[
"v0.6.3-alpha1",
"v0.6.4",
"v0.6.5",
"v0.6.5-alpha1",
"v0.6.5-alpha2",
"v0.6.5-alpha3",
"v0.6.5-beta1",
"v0.6.5-rc1",
"v1.0.0",
],
)
tags = git.list_tags(sort=TagSort.VERSION)
self.assertEqual(
tags,
[
"v0.6.3-alpha1",
"v0.6.4",
"v0.6.5",
"v0.6.5-alpha1",
"v0.6.5-alpha2",
"v0.6.5-alpha3",
"v0.6.5-beta1",
"v0.6.5-rc1",
"v1.0.0",
],
)
tags = git.list_tags(sort=TagSort.VERSION, tag_name="v0.6*")
self.assertEqual(
tags,
[
"v0.6.3-alpha1",
"v0.6.4",
"v0.6.5",
"v0.6.5-alpha1",
"v0.6.5-alpha2",
"v0.6.5-alpha3",
"v0.6.5-beta1",
"v0.6.5-rc1",
],
)
tags = git.list_tags(sort=TagSort.VERSION, tag_name="v0.6.5*")
self.assertEqual(
tags,
[
"v0.6.5",
"v0.6.5-alpha1",
"v0.6.5-alpha2",
"v0.6.5-alpha3",
"v0.6.5-beta1",
"v0.6.5-rc1",
],
)
tags = git.list_tags(tag_name="v0.6.5*")
self.assertEqual(
tags,
[
"v0.6.5",
"v0.6.5-alpha1",
"v0.6.5-alpha2",
"v0.6.5-alpha3",
"v0.6.5-beta1",
"v0.6.5-rc1",
],
)
tags = git.list_tags(
sort=TagSort.VERSION, sort_suffix=DEFAULT_TAG_SORT_SUFFIX
)
self.assertEqual(
tags,
[
"v0.6.3-alpha1",
"v0.6.4",
"v0.6.5-alpha1",
"v0.6.5-alpha2",
"v0.6.5-alpha3",
"v0.6.5-beta1",
"v0.6.5-rc1",
"v0.6.5",
"v1.0.0",
],
)
def test_pep440_list_tags(self):
with temp_git_repository() as tmp_git:
tags = [
"v0.6.5a3",
"v0.6.5b1",
"v0.6.5a2",
"v0.6.5rc1",
"v0.6.5a1",
"v0.6.5",
"v0.6.4",
"v0.6.3a1",
"v1.0.0",
]
git = Git(tmp_git)
git.config("commit.gpgSign", "false", scope=ConfigScope.LOCAL)
git.config("tag.gpgSign", "false", scope=ConfigScope.LOCAL)
git.config("tag.sort", "refname", scope=ConfigScope.LOCAL)
tmp_file = tmp_git / "foo.txt"
tmp_file.touch()
git.add(tmp_file)
git.commit("some commit")
for tag in tags:
git.tag(tag)
tags = git.list_tags()
self.assertEqual(
tags,
[
"v0.6.3a1",
"v0.6.4",
"v0.6.5",
"v0.6.5a1",
"v0.6.5a2",
"v0.6.5a3",
"v0.6.5b1",
"v0.6.5rc1",
"v1.0.0",
],
)
tags = git.list_tags(sort=TagSort.VERSION)
self.assertEqual(
tags,
[
"v0.6.3a1",
"v0.6.4",
"v0.6.5",
"v0.6.5a1",
"v0.6.5a2",
"v0.6.5a3",
"v0.6.5b1",
"v0.6.5rc1",
"v1.0.0",
],
)
tags = git.list_tags(
sort=TagSort.VERSION, sort_suffix=DEFAULT_TAG_SORT_SUFFIX
)
self.assertEqual(
tags,
[
"v0.6.3a1",
"v0.6.4",
"v0.6.5a1",
"v0.6.5a2",
"v0.6.5a3",
"v0.6.5b1",
"v0.6.5rc1",
"v0.6.5",
"v1.0.0",
],
)
def test_git_status(self):
with temp_git_repository() as tmp_git:
tracked_file = tmp_git / "foo.json"
tracked_file.write_text("sed diam nonumy eirmod", encoding="utf8")
changed_file = tmp_git / "bar.json"
changed_file.touch()
staged_changed_file = tmp_git / "ipsum.json"
staged_changed_file.write_text("tempor invidunt ut labore")
removed_file = tmp_git / "lorem.json"
removed_file.write_text(
"consetetur sadipscing elitr", encoding="utf8"
)
renamed_file = tmp_git / "foo.md"
renamed_file.write_text(
"et dolore magna aliquyam erat", encoding="utf8"
)
git = Git(tmp_git)
git.config("commit.gpgSign", "false", scope=ConfigScope.LOCAL)
git.add(
[
tracked_file,
changed_file,
staged_changed_file,
removed_file,
renamed_file,
]
)
git.commit("Some commit")
changed_file.write_text("Lorem Ipsum", encoding="utf8")
staged_changed_file.write_text("Lorem Ipsum", encoding="utf8")
added_file = tmp_git / "foo.txt"
added_file.touch()
added_modified_file = tmp_git / "ipsum.txt"
added_modified_file.touch()
git.add([added_file, staged_changed_file, added_modified_file])
staged_changed_file.write_text("Dolor sit", encoding="utf8")
added_modified_file.write_text("Lorem Ipsum", encoding="utf8")
git.move(renamed_file, "foo.rst")
git.remove(removed_file)
untracked_file = tmp_git / "bar.txt"
untracked_file.touch()
it = git.status()
status = next(it)
self.assertEqual(status.index, Status.UNMODIFIED)
self.assertEqual(status.working_tree, Status.MODIFIED)
self.assertEqual(status.path, Path("bar.json"))
status = next(it)
self.assertEqual(status.index, Status.RENAMED)
self.assertEqual(status.working_tree, Status.UNMODIFIED)
self.assertEqual(status.path, Path("foo.rst"))
self.assertEqual(status.old_path, Path("foo.md"))
status = next(it)
self.assertEqual(status.index, Status.ADDED)
self.assertEqual(status.working_tree, Status.UNMODIFIED)
self.assertEqual(status.path, Path("foo.txt"))
status = next(it)
self.assertEqual(status.index, Status.MODIFIED)
self.assertEqual(status.working_tree, Status.MODIFIED)
self.assertEqual(status.path, Path("ipsum.json"))
status = next(it)
self.assertEqual(status.index, Status.ADDED)
self.assertEqual(status.working_tree, Status.MODIFIED)
self.assertEqual(status.path, Path("ipsum.txt"))
status = next(it)
self.assertEqual(status.index, Status.DELETED)
self.assertEqual(status.working_tree, Status.UNMODIFIED)
self.assertEqual(status.path, Path("lorem.json"))
with self.assertRaises(StopIteration):
next(it)
def test_git_error(self):
with (
temp_directory(change_into=True),
self.assertRaises(GitError) as cm,
):
Git().log()
self.assertEqual(cm.exception.returncode, 128)
self.assertEqual(
cm.exception.stderr,
"fatal: not a git repository (or any of the parent directories): "
".git\n",
)
self.assertEqual(cm.exception.stdout, "")
self.assertEqual(cm.exception.cmd, ["git", "log"])
|