-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_clone.py
More file actions
146 lines (111 loc) · 5.79 KB
/
test_clone.py
File metadata and controls
146 lines (111 loc) · 5.79 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
import pytest
import subprocess
xtl_url = "https://github.com/xtensor-stack/xtl.git"
def test_clone(git2cpp_path, tmp_path, run_in_tmp_path):
clone_cmd = [git2cpp_path, "clone", xtl_url]
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_clone.returncode == 0
assert (tmp_path / "xtl").exists()
assert (tmp_path / "xtl/include").exists()
def test_clone_is_bare(git2cpp_path, tmp_path, run_in_tmp_path):
clone_cmd = [git2cpp_path, "clone", "--bare", xtl_url]
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_clone.returncode == 0
assert (tmp_path / "xtl").is_dir()
status_cmd = [git2cpp_path, "status"]
p_status = subprocess.run(status_cmd, capture_output=True, cwd=tmp_path / "xtl", text=True)
assert p_status.returncode != 0
assert "This operation is not allowed against bare repositories" in p_status.stderr
branch_cmd = [git2cpp_path, "branch"]
p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=tmp_path / "xtl", text=True)
assert p_branch.returncode == 0
assert p_branch.stdout.strip() == "* master"
def test_clone_shallow(git2cpp_path, tmp_path, run_in_tmp_path):
clone_cmd = [git2cpp_path, "clone", "--depth", "1", xtl_url]
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_clone.returncode == 0
assert (tmp_path / "xtl").exists()
xtl_path = tmp_path / "xtl"
cmd_log = [git2cpp_path, "log"]
p_log = subprocess.run(cmd_log, capture_output=True, cwd=xtl_path, text=True)
assert p_log.returncode == 0
assert p_log.stdout.count("Author") == 1
@pytest.mark.parametrize("protocol", ["http", "https"])
def test_clone_private_repo(git2cpp_path, tmp_path, run_in_tmp_path, private_test_repo, protocol):
# Succeeds with correct credentials.
# Note that http succeeds by redirecting to https.
username = "abc" # Can be any non-empty string.
password = private_test_repo["token"]
input = f"{username}\n{password}\n"
repo_path = tmp_path / private_test_repo["repo_name"]
url = private_test_repo["https_url" if protocol == "https" else "http_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
def test_clone_private_repo_fails_then_succeeds(
git2cpp_path, tmp_path, run_in_tmp_path, private_test_repo
):
# Fails with wrong credentials, then succeeds with correct ones.
username = "xyz" # Can be any non-empty string.
password = private_test_repo["token"]
input = "\n".join(["wrong1", "wrong2", username, password]) + "\n"
repo_path = tmp_path / private_test_repo["repo_name"]
clone_cmd = [git2cpp_path, "clone", private_test_repo["https_url"]]
p_clone = subprocess.run(clone_cmd, capture_output=True, text=True, input=input)
assert p_clone.returncode == 0
assert repo_path.exists()
# Two requests for username and password.
assert p_clone.stdout.count("Username:") == 2
assert p_clone.stdout.count("Password:") == 2
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
def test_clone_private_repo_fails_on_no_username(
git2cpp_path, tmp_path, run_in_tmp_path, private_test_repo
):
input = "\n"
repo_path = tmp_path / private_test_repo["repo_name"]
clone_cmd = [git2cpp_path, "clone", private_test_repo["https_url"]]
p_clone = subprocess.run(clone_cmd, capture_output=True, text=True, input=input)
assert p_clone.returncode != 0
assert "No username specified" in p_clone.stderr
assert not repo_path.exists()
assert p_clone.stdout.count("Username:") == 1
assert p_clone.stdout.count("Password:") == 0
def test_clone_private_repo_fails_on_no_password(
git2cpp_path, tmp_path, run_in_tmp_path, private_test_repo
):
input = "username\n\n" # Note no password between the \n
repo_path = tmp_path / private_test_repo["repo_name"]
clone_cmd = [git2cpp_path, "clone", private_test_repo["https_url"]]
p_clone = subprocess.run(clone_cmd, capture_output=True, text=True, input=input)
assert p_clone.returncode != 0
assert "No password specified" in p_clone.stderr
assert not repo_path.exists()
assert p_clone.stdout.count("Username:") == 1
assert p_clone.stdout.count("Password:") == 1
@pytest.mark.parametrize("protocol", ["http", "https"])
def test_clone_gitlab(git2cpp_path, tmp_path, run_in_tmp_path, protocol):
repo_url = f"{protocol}://gitlab.quantstack.net/ianthomas23_group/cockle-playground"
clone_cmd = [git2cpp_path, "clone", repo_url]
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_clone.returncode == 0
repo_path = tmp_path / "cockle-playground"
assert repo_path.is_dir()
assert (repo_path / "src").is_dir()
status_cmd = [git2cpp_path, "status"]
p_status = subprocess.run(status_cmd, capture_output=True, cwd=repo_path, 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