Skip to content

Commit 1b1090f

Browse files
committed
Add gitlab tests
1 parent af2871f commit 1b1090f

File tree

3 files changed

+78
-40
lines changed

3 files changed

+78
-40
lines changed

tests/test_github.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import itertools
2-
import os
32
from contextlib import contextmanager
43
from textwrap import dedent
54
from typing import Iterator
@@ -381,31 +380,3 @@ def test_github_missing_direction(self) -> None:
381380
"Possible values for subcommand are: anno-prs, checkout-prs, create-pr, restack-pr, retarget-pr, update-pr-descriptions, sync\n"
382381
assert type(e) is SystemExit
383382
assert e.code == ExitCode.ARGUMENT_ERROR
384-
385-
def test_github_create_pr_with_missing_head_branch_on_remote(self, mocker: MockerFixture) -> None:
386-
"""Test that creating a PR fails gracefully when head branch doesn't exist in remote"""
387-
# No need to mock GitHub API or input since we catch the error before making API calls
388-
self.patch_symbol(mocker, 'git_machete.code_hosting.OrganizationAndRepository.from_url', mock_from_url)
389-
390-
(local_path, remote_path) = create_repo_with_remote()
391-
new_branch("develop")
392-
commit("develop commit")
393-
push()
394-
395-
new_branch("feature")
396-
commit("feature commit")
397-
push()
398-
399-
# Delete the branch from the actual remote repository to simulate the scenario
400-
os.chdir(remote_path)
401-
delete_branch("feature")
402-
os.chdir(local_path)
403-
404-
rewrite_branch_layout_file("develop\n\tfeature")
405-
406-
# This should now fail with a clear error message before attempting to call the GitHub API
407-
expected_error_message = (
408-
"Head branch feature has been removed from origin remote since the last fetch/push.\n"
409-
"Do you really want to create a PR for this branch?"
410-
)
411-
assert_failure(['github', 'create-pr'], expected_error_message)

tests/test_github_create_pr.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,3 +1102,31 @@ def test_github_create_pr_with_base_flag(self, mocker: MockerFixture) -> None:
11021102
o-feature * PR #1 (some_other_user)
11031103
""",
11041104
)
1105+
1106+
def test_github_create_pr_with_missing_head_branch_on_remote(self, mocker: MockerFixture) -> None:
1107+
"""Test that creating a PR fails gracefully when head branch doesn't exist in remote"""
1108+
# No need to mock GitHub API or input since we catch the error before making API calls
1109+
self.patch_symbol(mocker, 'git_machete.code_hosting.OrganizationAndRepository.from_url', mock_from_url)
1110+
1111+
(local_path, remote_path) = create_repo_with_remote()
1112+
new_branch("develop")
1113+
commit("develop commit")
1114+
push()
1115+
1116+
new_branch("feature")
1117+
commit("feature commit")
1118+
push()
1119+
1120+
# Delete the branch from the actual remote repository to simulate the scenario
1121+
os.chdir(remote_path)
1122+
delete_branch("feature")
1123+
os.chdir(local_path)
1124+
1125+
rewrite_branch_layout_file("develop\n\tfeature")
1126+
1127+
# This should now fail with a clear error message before attempting to call the GitHub API
1128+
expected_error_message = (
1129+
"Head branch feature has been removed from origin remote since the last fetch/push.\n"
1130+
"Do you really want to create a PR for this branch?"
1131+
)
1132+
assert_failure(['github', 'create-pr'], expected_error_message)

tests/test_gitlab_create_mr.py

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def test_gitlab_create_mr(self, mocker: MockerFixture) -> None:
150150
|
151151
x-drop-constraint (untracked)
152152
153+
Checking if source branch chore/fields exists in origin remote... YES
153154
Checking if target branch ignore-trailing exists in origin remote... YES
154155
Creating a draft MR from chore/fields to ignore-trailing... OK, see www.gitlab.com
155156
Checking for open GitLab MRs... OK
@@ -237,6 +238,7 @@ def test_gitlab_create_mr(self, mocker: MockerFixture) -> None:
237238
|
238239
x-drop-constraint (untracked)
239240
241+
Checking if source branch hotfix/add-trigger exists in origin remote... YES
240242
Checking if target branch master exists in origin remote... YES
241243
Creating an MR from hotfix/add-trigger to master... OK, see www.gitlab.com
242244
Updating description of MR !6 to include the chain of MRs... OK
@@ -333,6 +335,7 @@ def test_gitlab_create_mr(self, mocker: MockerFixture) -> None:
333335
|
334336
o-testing/endpoints
335337
338+
Checking if source branch allow-ownership-link exists in origin remote... YES
336339
Checking if target branch develop exists in origin remote... YES
337340
Creating an MR from allow-ownership-link to develop... OK, see www.gitlab.com
338341
Setting milestone of MR !7 to 42... OK
@@ -468,7 +471,8 @@ def test_gitlab_create_mr_missing_base_branch_on_remote(self, mocker: MockerFixt
468471
"""
469472
rewrite_branch_layout_file(body)
470473

471-
expected_msg = ("Checking if target branch feature/api_handling exists in origin remote... NO\n"
474+
expected_msg = ("Checking if source branch feature/api_exception_handling exists in origin remote... YES\n"
475+
"Checking if target branch feature/api_handling exists in origin remote... NO\n"
472476
"Pushing untracked branch feature/api_handling to origin...\n"
473477
"Creating an MR from feature/api_exception_handling to feature/api_handling... OK, see www.gitlab.com\n")
474478

@@ -609,6 +613,7 @@ def test_gitlab_create_mr_with_multiple_non_origin_remotes(self, mocker: MockerF
609613
could not be created in example-org/example-repo-2, since its source branch feature lives in example-org/example-repo-1.
610614
Generally, MRs need to be created in whatever project the target branch lives.
611615
616+
Checking if source branch feature exists in origin_1 remote... YES
612617
Checking if target branch branch-1 exists in origin_2 remote... YES
613618
Creating an MR from feature to branch-1... OK, see www.gitlab.com
614619
Checking for open GitLab MRs... OK
@@ -670,6 +675,7 @@ def test_gitlab_create_mr_with_multiple_non_origin_remotes(self, mocker: MockerF
670675
could not be created in example-org/example-repo-1, since its source branch feature_1 lives in example-org/example-repo-2.
671676
Generally, MRs need to be created in whatever project the target branch lives.
672677
678+
Checking if source branch feature_1 exists in origin_2 remote... YES
673679
Checking if target branch feature exists in origin_1 remote... YES
674680
Creating an MR from feature_1 to feature... OK, see www.gitlab.com
675681
Checking for open GitLab MRs... OK
@@ -706,6 +712,7 @@ def test_gitlab_create_mr_with_multiple_non_origin_remotes(self, mocker: MockerF
706712
|
707713
o-feature_2 *
708714
715+
Checking if source branch feature_2 exists in origin_1 remote... YES
709716
Checking if target branch feature exists in origin_1 remote... YES
710717
Creating an MR from feature_2 to feature... OK, see www.gitlab.com
711718
Checking for open GitLab MRs... OK
@@ -726,6 +733,7 @@ def test_gitlab_create_mr_with_multiple_non_origin_remotes(self, mocker: MockerF
726733
expected_result = """
727734
Add feature_3 onto the inferred upstream (parent) branch feature_2? (y, N)
728735
Added branch feature_3 onto feature_2
736+
Checking if source branch feature_3 exists in origin_1 remote... YES
729737
Checking if target branch feature_2 exists in origin_1 remote... YES
730738
Creating an MR from feature_3 to feature_2... OK, see www.gitlab.com
731739
Checking for open GitLab MRs... OK
@@ -755,6 +763,7 @@ def test_gitlab_create_mr_with_multiple_non_origin_remotes(self, mocker: MockerF
755763
could not be created in example-org/example-repo-1, since its source branch feature_4 lives in example-org/example-repo-2.
756764
Generally, MRs need to be created in whatever project the target branch lives.
757765
766+
Checking if source branch feature_4 exists in origin_2 remote... YES
758767
Checking if target branch feature_3 exists in origin_1 remote... YES
759768
Creating an MR from feature_4 to feature_3... OK, see www.gitlab.com
760769
Checking for open GitLab MRs... OK
@@ -785,6 +794,7 @@ def test_gitlab_create_mr_with_multiple_non_origin_remotes(self, mocker: MockerF
785794
could not be created in example-org/example-repo-1, since its source branch feature_5 lives in example-org/example-repo-2.
786795
Generally, MRs need to be created in whatever project the target branch lives.
787796
797+
Checking if source branch feature_5 exists in origin_2 remote... YES
788798
Checking if target branch feature_3 exists in origin_1 remote... YES
789799
Creating an MR from feature_5 to feature_3... OK, see www.gitlab.com
790800
Checking for open GitLab MRs... OK
@@ -808,12 +818,14 @@ def test_gitlab_create_mr_for_no_push_qualifier(self, mocker: MockerFixture) ->
808818

809819
new_branch("develop")
810820
commit()
821+
push()
811822

812823
rewrite_branch_layout_file("master\n\tdevelop push=no")
813824

814825
assert_success(
815826
['gitlab', 'create-mr'],
816827
"""
828+
Checking if source branch develop exists in origin remote... YES
817829
Checking if target branch master exists in origin remote... YES
818830
Creating an MR from develop to master... OK, see www.gitlab.com
819831
"""
@@ -864,6 +876,7 @@ def test_gitlab_create_mr_for_branch_behind_remote(self, mocker: MockerFixture)
864876
"""
865877
Warn: branch develop is behind its remote counterpart. Consider using git pull.
866878
Proceed with creating merge request? (y, Q)
879+
Checking if source branch develop exists in origin remote... YES
867880
Checking if target branch master exists in origin remote... YES
868881
Creating an MR from develop to master... OK, see www.gitlab.com
869882
"""
@@ -936,15 +949,15 @@ def test_gitlab_create_mr_for_branch_diverged_from_and_older_than_remote(self, m
936949
"""
937950
Warn: branch develop is diverged from and older than its remote counterpart. Consider using git reset --keep.
938951
Proceed with creating merge request? (y, Q)
952+
Checking if source branch develop exists in origin remote... YES
939953
Checking if target branch master exists in origin remote... YES
940954
Creating an MR from develop to master... OK, see www.gitlab.com
941955
"""
942956
)
943957

944958
def test_gitlab_create_mr_when_target_branch_disappeared_from_remote(self, mocker: MockerFixture) -> None:
959+
# No need to mock GitLab API since we catch the error before making API calls
945960
self.patch_symbol(mocker, 'git_machete.code_hosting.OrganizationAndRepository.from_url', mock_from_url)
946-
self.patch_symbol(mocker, 'git_machete.gitlab.GitLabToken.for_domain', mock_gitlab_token_for_domain_none)
947-
self.patch_symbol(mocker, 'urllib.request.urlopen', mock_urlopen(MockGitLabAPIState.with_mrs()))
948961

949962
(local_path, remote_path) = create_repo_with_remote()
950963

@@ -961,15 +974,40 @@ def test_gitlab_create_mr_when_target_branch_disappeared_from_remote(self, mocke
961974
os.chdir(local_path)
962975

963976
rewrite_branch_layout_file("develop\n\tfeature")
964-
self.patch_symbol(mocker, 'builtins.input', mock_input_returning('y'))
965-
assert_success(
966-
['gitlab', 'create-mr'],
967-
"""
968-
Checking if target branch develop exists in origin remote... NO
969-
Push untracked branch develop to origin? (y, Q)
970-
Creating an MR from feature to develop... OK, see www.gitlab.com
971-
"""
977+
978+
expected_error_message = (
979+
"Target branch develop has been removed from origin remote since the last fetch/push.\n"
980+
"Do you really want to create an MR to this branch?"
981+
)
982+
assert_failure(['gitlab', 'create-mr'], expected_error_message)
983+
984+
def test_gitlab_create_mr_with_missing_source_branch_on_remote(self, mocker: MockerFixture) -> None:
985+
"""Test that creating an MR fails gracefully when source branch doesn't exist in remote"""
986+
# No need to mock GitLab API since we catch the error before making API calls
987+
self.patch_symbol(mocker, 'git_machete.code_hosting.OrganizationAndRepository.from_url', mock_from_url)
988+
989+
(local_path, remote_path) = create_repo_with_remote()
990+
new_branch("develop")
991+
commit()
992+
push()
993+
994+
new_branch("feature")
995+
commit()
996+
push()
997+
998+
# Delete the branch from the actual remote repository to simulate the scenario
999+
os.chdir(remote_path)
1000+
delete_branch("feature")
1001+
os.chdir(local_path)
1002+
1003+
rewrite_branch_layout_file("develop\n\tfeature")
1004+
1005+
# This should now fail with a clear error message before attempting to call the GitLab API
1006+
expected_error_message = (
1007+
"Source branch feature has been removed from origin remote since the last fetch/push.\n"
1008+
"Do you really want to create an MR for this branch?"
9721009
)
1010+
assert_failure(['gitlab', 'create-mr'], expected_error_message)
9731011

9741012
def test_gitlab_create_mr_when_target_branch_appeared_on_remote(self, mocker: MockerFixture) -> None:
9751013
self.patch_symbol(mocker, 'git_machete.code_hosting.OrganizationAndRepository.from_url', mock_from_url)
@@ -992,6 +1030,7 @@ def test_gitlab_create_mr_when_target_branch_appeared_on_remote(self, mocker: Mo
9921030
assert_success(
9931031
['gitlab', 'create-mr'],
9941032
"""
1033+
Checking if source branch feature exists in origin remote... YES
9951034
Checking if target branch develop exists in origin remote... YES
9961035
Creating an MR from feature to develop... OK, see www.gitlab.com
9971036
"""

0 commit comments

Comments
 (0)