2
from unittest import main, mock, TestCase
4
from gitutils import get_git_remote_name, get_git_repo_dir, GitRepo
5
from test_trymerge import mocked_gh_graphql
6
from trymerge import GitHubPR
7
from tryrebase import additional_rebase_failure_info, rebase_ghstack_onto, rebase_onto
10
def mocked_rev_parse(branch: str) -> str:
14
MAIN_BRANCH = "refs/remotes/origin/main"
15
VIABLE_STRICT_BRANCH = "refs/remotes/origin/viable/strict"
18
class TestRebase(TestCase):
19
@mock.patch("trymerge.gh_graphql", side_effect=mocked_gh_graphql)
20
@mock.patch("gitutils.GitRepo._run_git")
21
@mock.patch("gitutils.GitRepo.rev_parse", side_effect=mocked_rev_parse)
22
@mock.patch("tryrebase.gh_post_comment")
25
mocked_post_comment: Any,
30
"Tests rebase successfully"
31
pr = GitHubPR("pytorch", "pytorch", 31093)
32
repo = GitRepo(get_git_repo_dir(), get_git_remote_name())
33
rebase_onto(pr, repo, MAIN_BRANCH)
35
mock.call("fetch", "origin", "pull/31093/head:pull/31093/head"),
36
mock.call("rebase", MAIN_BRANCH, "pull/31093/head"),
40
"https://github.com/mingxiaoh/pytorch.git",
41
"pull/31093/head:master",
44
mocked_run_git.assert_has_calls(calls)
46
f"Successfully rebased `master` onto `{MAIN_BRANCH}`",
47
mocked_post_comment.call_args[0][3],
50
@mock.patch("trymerge.gh_graphql", side_effect=mocked_gh_graphql)
51
@mock.patch("gitutils.GitRepo._run_git")
52
@mock.patch("gitutils.GitRepo.rev_parse", side_effect=mocked_rev_parse)
53
@mock.patch("tryrebase.gh_post_comment")
54
def test_rebase_to_stable(
56
mocked_post_comment: Any,
61
"Tests rebase to viable/strict successfully"
62
pr = GitHubPR("pytorch", "pytorch", 31093)
63
repo = GitRepo(get_git_repo_dir(), get_git_remote_name())
64
rebase_onto(pr, repo, VIABLE_STRICT_BRANCH, False)
66
mock.call("fetch", "origin", "pull/31093/head:pull/31093/head"),
67
mock.call("rebase", VIABLE_STRICT_BRANCH, "pull/31093/head"),
71
"https://github.com/mingxiaoh/pytorch.git",
72
"pull/31093/head:master",
75
mocked_run_git.assert_has_calls(calls)
77
f"Successfully rebased `master` onto `{VIABLE_STRICT_BRANCH}`",
78
mocked_post_comment.call_args[0][3],
81
@mock.patch("trymerge.gh_graphql", side_effect=mocked_gh_graphql)
82
@mock.patch("gitutils.GitRepo._run_git", return_value="Everything up-to-date")
83
@mock.patch("gitutils.GitRepo.rev_parse", side_effect=mocked_rev_parse)
84
@mock.patch("tryrebase.gh_post_comment")
85
def test_no_need_to_rebase(
87
mocked_post_comment: Any,
92
"Tests branch already up to date"
93
pr = GitHubPR("pytorch", "pytorch", 31093)
94
repo = GitRepo(get_git_repo_dir(), get_git_remote_name())
95
rebase_onto(pr, repo, MAIN_BRANCH)
97
mock.call("fetch", "origin", "pull/31093/head:pull/31093/head"),
98
mock.call("rebase", MAIN_BRANCH, "pull/31093/head"),
102
"https://github.com/mingxiaoh/pytorch.git",
103
"pull/31093/head:master",
106
mocked_run_git.assert_has_calls(calls)
108
"Tried to rebase and push PR #31093, but it was already up to date",
109
mocked_post_comment.call_args[0][3],
112
"Try rebasing against [main]",
113
mocked_post_comment.call_args[0][3],
116
@mock.patch("trymerge.gh_graphql", side_effect=mocked_gh_graphql)
117
@mock.patch("gitutils.GitRepo._run_git", return_value="Everything up-to-date")
118
@mock.patch("gitutils.GitRepo.rev_parse", side_effect=mocked_rev_parse)
119
@mock.patch("tryrebase.gh_post_comment")
120
def test_no_need_to_rebase_try_main(
122
mocked_post_comment: Any,
127
"Tests branch already up to date again viable/strict"
128
pr = GitHubPR("pytorch", "pytorch", 31093)
129
repo = GitRepo(get_git_repo_dir(), get_git_remote_name())
130
rebase_onto(pr, repo, VIABLE_STRICT_BRANCH)
132
"Tried to rebase and push PR #31093, but it was already up to date. Try rebasing against [main]",
133
mocked_post_comment.call_args[0][3],
136
@mock.patch("trymerge.gh_graphql", side_effect=mocked_gh_graphql)
137
@mock.patch("gitutils.GitRepo._run_git")
138
@mock.patch("gitutils.GitRepo.rev_parse", side_effect=lambda branch: "same sha")
139
@mock.patch("tryrebase.gh_post_comment")
142
mocked_post_comment: Any,
147
"Tests rebase results in same sha"
148
pr = GitHubPR("pytorch", "pytorch", 31093)
149
repo = GitRepo(get_git_repo_dir(), get_git_remote_name())
150
with self.assertRaisesRegex(Exception, "same sha as the target branch"):
151
rebase_onto(pr, repo, MAIN_BRANCH)
152
with self.assertRaisesRegex(Exception, "same sha as the target branch"):
153
rebase_ghstack_onto(pr, repo, MAIN_BRANCH)
155
def test_additional_rebase_failure_info(self) -> None:
157
"Command `git -C /Users/csl/zzzzzzzz/pytorch push --dry-run -f "
158
"https://github.com/Lightning-Sandbox/pytorch.git pull/106089/head:fix/spaces` returned non-zero exit code 128\n"
160
"remote: Permission to Lightning-Sandbox/pytorch.git denied to clee2000.\n"
161
"fatal: unable to access 'https://github.com/Lightning-Sandbox/pytorch.git/': The requested URL returned error: 403\n"
164
additional_msg = additional_rebase_failure_info(Exception(error))
165
self.assertTrue("This is likely because" in additional_msg)
168
if __name__ == "__main__":