-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_push.py
More file actions
238 lines (201 loc) · 9.24 KB
/
Copy pathtest_push.py
File metadata and controls
238 lines (201 loc) · 9.24 KB
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
import re
import subprocess
from uuid import uuid4
def test_push_private_repo(
git2cpp_path, tmp_path, run_in_tmp_path, private_test_repo, commit_env_config
):
# Unique branch name to avoid branch name collisions on remote repo.
branch_name = f"test-{uuid4()}"
# Start of test follows test_clone_private_repo, then creates a new local branch and pushes
# that to the remote.
username = "abc" # Can be any non-empty string.
password = private_test_repo["token"]
input = f"{username}\n{password}"
repo_path = tmp_path / private_test_repo["repo_name"]
url = private_test_repo["https_url"]
clone_cmd = [git2cpp_path, "clone", url]
p_clone = subprocess.run(clone_cmd, capture_output=True, text=True, input=input)
assert p_clone.returncode == 0
assert repo_path.exists()
# Single request for username and password.
assert p_clone.stdout.count("Username:") == 1
assert p_clone.stdout.count("Password:") == 1
status_cmd = [git2cpp_path, "status"]
p_status = subprocess.run(status_cmd, cwd=repo_path, capture_output=True, text=True)
assert p_status.returncode == 0
assert "On branch main" in p_status.stdout
assert "Your branch is up to date with 'origin/main'" in p_status.stdout
checkout_cmd = [git2cpp_path, "checkout", "-b", branch_name]
p_checkout = subprocess.run(checkout_cmd, cwd=repo_path, capture_output=True, text=True)
assert p_checkout.returncode == 0
p_status = subprocess.run(status_cmd, cwd=repo_path, capture_output=True, text=True)
assert p_status.returncode == 0
assert f"On branch {branch_name}" in p_status.stdout
(repo_path / "new_file.txt").write_text("Some text")
add_cmd = [git2cpp_path, "add", "new_file.txt"]
p_add = subprocess.run(add_cmd, cwd=repo_path, capture_output=True, text=True)
assert p_add.returncode == 0
commit_cmd = [git2cpp_path, "commit", "-m", "This is my commit message"]
p_commit = subprocess.run(commit_cmd, cwd=repo_path, capture_output=True, text=True)
assert p_commit.returncode == 0
log_cmd = [git2cpp_path, "log", "-n1"]
p_log = subprocess.run(log_cmd, cwd=repo_path, capture_output=True, text=True)
assert p_log.returncode == 0
assert p_log.stdout.count("Author:") == 1
assert p_log.stdout.count("Date:") == 1
assert p_log.stdout.count("This is my commit message") == 1
# push with incorrect credentials to check it fails, then with correct to check it works.
input = f"${username}\ndef\n{username}\n{password}"
push_cmd = [git2cpp_path, "push", "origin"]
p_push = subprocess.run(push_cmd, cwd=repo_path, capture_output=True, text=True, input=input)
assert p_push.returncode == 0
assert p_push.stdout.count("Username:") == 2
assert p_push.stdout.count("Password:") == 2
assert " * [new branch] test-" in p_push.stdout
def test_push_branch_private_repo(
git2cpp_path, tmp_path, run_in_tmp_path, private_test_repo, commit_env_config
):
"""Test push with an explicit branch name: git2cpp push <remote> <branch>."""
branch_name = f"test-{uuid4()}"
username = "abc"
password = private_test_repo["token"]
input = f"{username}\n{password}"
repo_path = tmp_path / private_test_repo["repo_name"]
url = private_test_repo["https_url"]
# Clone the private repo.
clone_cmd = [git2cpp_path, "clone", url]
p_clone = subprocess.run(clone_cmd, capture_output=True, text=True, input=input)
assert p_clone.returncode == 0
assert repo_path.exists()
# Create a new branch and commit on it.
checkout_cmd = [git2cpp_path, "checkout", "-b", branch_name]
p_checkout = subprocess.run(checkout_cmd, cwd=repo_path, capture_output=True, text=True)
assert p_checkout.returncode == 0
(repo_path / "push_branch_file.txt").write_text("push branch test")
subprocess.run([git2cpp_path, "add", "push_branch_file.txt"], cwd=repo_path, check=True)
subprocess.run([git2cpp_path, "commit", "-m", "branch commit"], cwd=repo_path, check=True)
# Switch back to main so HEAD is NOT on the branch we want to push.
subprocess.run(
[git2cpp_path, "checkout", "main"], capture_output=True, check=True, cwd=repo_path
)
status_cmd = [git2cpp_path, "status"]
p_status = subprocess.run(status_cmd, cwd=repo_path, capture_output=True, text=True)
assert p_status.returncode == 0
assert "On branch main" in p_status.stdout
# Push specifying the branch explicitly (HEAD is on main, not the test branch).
input = f"{username}\n{password}"
push_cmd = [git2cpp_path, "push", "origin", branch_name]
p_push = subprocess.run(push_cmd, cwd=repo_path, capture_output=True, text=True, input=input)
assert p_push.returncode == 0
assert " * [new branch] test-" in p_push.stdout
def test_push_updates_existing_branch_private_repo(
git2cpp_path, tmp_path, run_in_tmp_path, private_test_repo, commit_env_config
):
"""Create a branch on remote, then update it locally and push — expect old..new output."""
branch_name = f"test-update-{uuid4()}"
username = "abc"
password = private_test_repo["token"]
creds = f"{username}\n{password}"
repo_path = tmp_path / private_test_repo["repo_name"]
url = private_test_repo["https_url"]
# Clone the private repo.
p_clone = subprocess.run(
[git2cpp_path, "clone", url], capture_output=True, text=True, input=creds
)
assert p_clone.returncode == 0
assert repo_path.exists()
# Create the branch locally and push it to the remote (first push: new branch)
subprocess.run([git2cpp_path, "checkout", "-b", branch_name], cwd=repo_path, check=True)
(repo_path / "file_for_branch.txt").write_text("initial content\n")
subprocess.run([git2cpp_path, "add", "file_for_branch.txt"], cwd=repo_path, check=True)
subprocess.run(
[git2cpp_path, "commit", "-m", "create branch commit"], cwd=repo_path, check=True
)
# first push creates the branch on remote
p_push1 = subprocess.run(
[git2cpp_path, "push", "origin", branch_name],
cwd=repo_path,
capture_output=True,
text=True,
input=creds,
)
assert p_push1.returncode == 0
assert " * [new branch]" in p_push1.stdout # first push creates branch
# IMPORTANT: fetch to update local remote-tracking refs (refs/remotes/origin/...)
p_fetch = subprocess.run(
[git2cpp_path, "fetch", "origin"],
cwd=repo_path,
capture_output=True,
text=True,
input=creds,
)
assert p_fetch.returncode == 0, (
f"fetch failed: stdout={p_fetch.stdout!r} stderr={p_fetch.stderr!r}"
)
# Now make another commit on the same branch and push again — this should be an update.
(repo_path / "file_for_branch.txt").write_text("modified content\n")
subprocess.run([git2cpp_path, "add", "file_for_branch.txt"], cwd=repo_path, check=True)
subprocess.run(
[git2cpp_path, "commit", "-m", "update branch commit"], cwd=repo_path, check=True
)
p_push2 = subprocess.run(
[git2cpp_path, "push", "origin", branch_name],
cwd=repo_path,
capture_output=True,
text=True,
input=creds,
)
assert p_push2.returncode == 0
# Match old..new short-hex and branch short name
pattern = re.compile(
r"[0-9a-f]{7}\.\.[0-9a-f]{7}\s+"
+ re.escape(branch_name)
+ r"\s+->\s+"
+ re.escape(branch_name),
re.IGNORECASE,
)
assert pattern.search(p_push2.stdout), (
f"expected old..new line for branch in push output, got:\n{p_push2.stdout}"
)
def test_push_branches_flag_private_repo(
git2cpp_path, tmp_path, run_in_tmp_path, private_test_repo, commit_env_config
):
"""Test push --branches pushes all local branches."""
branch_a = f"test-a-{uuid4()}"
branch_b = f"test-b-{uuid4()}"
username = "abc"
password = private_test_repo["token"]
input = f"{username}\n{password}"
repo_path = tmp_path / private_test_repo["repo_name"]
url = private_test_repo["https_url"]
# Clone the private repo.
clone_cmd = [git2cpp_path, "clone", url]
p_clone = subprocess.run(clone_cmd, capture_output=True, text=True, input=input)
assert p_clone.returncode == 0
assert repo_path.exists()
# Create two extra branches with commits.
for branch_name in [branch_a, branch_b]:
subprocess.run(
[git2cpp_path, "checkout", "-b", branch_name],
capture_output=True,
check=True,
cwd=repo_path,
)
(repo_path / f"{branch_name}.txt").write_text(f"content for {branch_name}")
subprocess.run([git2cpp_path, "add", f"{branch_name}.txt"], cwd=repo_path, check=True)
subprocess.run(
[git2cpp_path, "commit", "-m", f"commit on {branch_name}"],
cwd=repo_path,
check=True,
)
# Go back to main.
subprocess.run(
[git2cpp_path, "checkout", "main"], capture_output=True, check=True, cwd=repo_path
)
# Push all branches at once.
input = f"{username}\n{password}"
push_cmd = [git2cpp_path, "push", "origin", "--branches"]
p_push = subprocess.run(push_cmd, cwd=repo_path, capture_output=True, text=True, input=input)
assert p_push.returncode == 0
assert p_push.stdout.count(" * [new branch] test-") == 2
assert "main" not in p_push.stdout