pytorch

Форк
0
/
test_tryrebase.py 
169 строк · 6.5 Кб
1
from typing import Any
2
from unittest import main, mock, TestCase
3

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
8

9

10
def mocked_rev_parse(branch: str) -> str:
11
    return branch
12

13

14
MAIN_BRANCH = "refs/remotes/origin/main"
15
VIABLE_STRICT_BRANCH = "refs/remotes/origin/viable/strict"
16

17

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")
23
    def test_rebase(
24
        self,
25
        mocked_post_comment: Any,
26
        mocked_rp: Any,
27
        mocked_run_git: Any,
28
        mocked_gql: Any,
29
    ) -> None:
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)
34
        calls = [
35
            mock.call("fetch", "origin", "pull/31093/head:pull/31093/head"),
36
            mock.call("rebase", MAIN_BRANCH, "pull/31093/head"),
37
            mock.call(
38
                "push",
39
                "-f",
40
                "https://github.com/mingxiaoh/pytorch.git",
41
                "pull/31093/head:master",
42
            ),
43
        ]
44
        mocked_run_git.assert_has_calls(calls)
45
        self.assertIn(
46
            f"Successfully rebased `master` onto `{MAIN_BRANCH}`",
47
            mocked_post_comment.call_args[0][3],
48
        )
49

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(
55
        self,
56
        mocked_post_comment: Any,
57
        mocked_rp: Any,
58
        mocked_run_git: Any,
59
        mocked_gql: Any,
60
    ) -> None:
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)
65
        calls = [
66
            mock.call("fetch", "origin", "pull/31093/head:pull/31093/head"),
67
            mock.call("rebase", VIABLE_STRICT_BRANCH, "pull/31093/head"),
68
            mock.call(
69
                "push",
70
                "-f",
71
                "https://github.com/mingxiaoh/pytorch.git",
72
                "pull/31093/head:master",
73
            ),
74
        ]
75
        mocked_run_git.assert_has_calls(calls)
76
        self.assertIn(
77
            f"Successfully rebased `master` onto `{VIABLE_STRICT_BRANCH}`",
78
            mocked_post_comment.call_args[0][3],
79
        )
80

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(
86
        self,
87
        mocked_post_comment: Any,
88
        mocked_rp: Any,
89
        mocked_run_git: Any,
90
        mocked_gql: Any,
91
    ) -> None:
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)
96
        calls = [
97
            mock.call("fetch", "origin", "pull/31093/head:pull/31093/head"),
98
            mock.call("rebase", MAIN_BRANCH, "pull/31093/head"),
99
            mock.call(
100
                "push",
101
                "-f",
102
                "https://github.com/mingxiaoh/pytorch.git",
103
                "pull/31093/head:master",
104
            ),
105
        ]
106
        mocked_run_git.assert_has_calls(calls)
107
        self.assertIn(
108
            "Tried to rebase and push PR #31093, but it was already up to date",
109
            mocked_post_comment.call_args[0][3],
110
        )
111
        self.assertNotIn(
112
            "Try rebasing against [main]",
113
            mocked_post_comment.call_args[0][3],
114
        )
115

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(
121
        self,
122
        mocked_post_comment: Any,
123
        mocked_rp: Any,
124
        mocked_run_git: Any,
125
        mocked_gql: Any,
126
    ) -> None:
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)
131
        self.assertIn(
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],
134
        )
135

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")
140
    def test_same_sha(
141
        self,
142
        mocked_post_comment: Any,
143
        mocked_rp: Any,
144
        mocked_run_git: Any,
145
        mocked_gql: Any,
146
    ) -> None:
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)
154

155
    def test_additional_rebase_failure_info(self) -> None:
156
        error = (
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"
159
            "```\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"
162
            "```"
163
        )
164
        additional_msg = additional_rebase_failure_info(Exception(error))
165
        self.assertTrue("This is likely because" in additional_msg)
166

167

168
if __name__ == "__main__":
169
    main()
170

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.