-
Notifications
You must be signed in to change notification settings - Fork 317
/
test_remote.py
227 lines (197 loc) · 5.38 KB
/
test_remote.py
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
import json
from unittest.mock import patch
import os
import pytest
import tornado
from jupyterlab_git.git import Git
from jupyterlab_git.handlers import NAMESPACE
from .testutils import assert_http_error, maybe_future
@patch("jupyterlab_git.git.execute")
async def test_git_add_remote_success_no_name(mock_execute, jp_fetch, jp_root_dir):
# Given
local_path = jp_root_dir / "test_path"
url = "http://github.com/myid/myrepository.git"
mock_execute.return_value = maybe_future((0, "", ""))
# When
body = {
"url": url,
}
response = await jp_fetch(
NAMESPACE,
local_path.name,
"remote",
"add",
body=json.dumps(body),
method="POST",
)
# Then
command = ["git", "remote", "add", "origin", url]
mock_execute.assert_called_once_with(
command,
cwd=str(local_path),
timeout=20,
env=None,
username=None,
password=None,
is_binary=False,
)
assert response.code == 201
payload = json.loads(response.body)
assert payload == {
"code": 0,
"command": " ".join(command),
}
@patch("jupyterlab_git.git.execute")
async def test_git_add_remote_success(mock_execute, jp_fetch, jp_root_dir):
# Given
local_path = jp_root_dir / "test_path"
url = "http://github.com/myid/myrepository.git"
name = "distant"
mock_execute.return_value = maybe_future((0, "", ""))
# When
body = {"url": url, "name": name}
response = await jp_fetch(
NAMESPACE,
local_path.name,
"remote",
"add",
body=json.dumps(body),
method="POST",
)
# Then
command = ["git", "remote", "add", name, url]
mock_execute.assert_called_once_with(
command,
cwd=str(local_path),
timeout=20,
env=None,
username=None,
password=None,
is_binary=False,
)
assert response.code == 201
payload = json.loads(response.body)
assert payload == {
"code": 0,
"command": " ".join(command),
}
@patch("jupyterlab_git.git.execute")
async def test_git_add_remote_failure(mock_execute, jp_fetch, jp_root_dir):
# Given
local_path = jp_root_dir / "test_path"
url = "http://github.com/myid/myrepository.git"
error_msg = "Fake failure"
error_code = 128
mock_execute.return_value = maybe_future((error_code, "", error_msg))
# When
body = {
"url": url,
}
with pytest.raises(tornado.httpclient.HTTPClientError) as e:
await jp_fetch(
NAMESPACE,
local_path.name,
"remote",
"add",
body=json.dumps(body),
method="POST",
)
assert_http_error(e, 500)
# Then
mock_execute.assert_called_once_with(
["git", "remote", "add", "origin", url],
cwd=str(local_path),
timeout=20,
env=None,
username=None,
password=None,
is_binary=False,
)
@patch("jupyterlab_git.git.execute")
async def test_git_remote_show(mock_execute, jp_root_dir):
# Given
local_path = jp_root_dir / "test_path"
mock_execute.return_value = maybe_future(
(0, os.linesep.join(["origin", "test"]), "")
)
# When
output = await Git().remote_show(str(local_path), False)
# Then
command = ["git", "remote", "show"]
mock_execute.assert_called_once_with(
command,
cwd=str(local_path),
timeout=20,
env=None,
username=None,
password=None,
is_binary=False,
)
assert output == {
"code": 0,
"command": " ".join(command),
"remotes": ["origin", "test"],
}
@patch("jupyterlab_git.git.execute")
async def test_git_remote_show_verbose(mock_execute, jp_fetch, jp_root_dir):
# Given
local_path = jp_root_dir / "test_path"
url = "http://github.com/myid/myrepository.git"
process_output = os.linesep.join(
[f"origin\t{url} (fetch)", f"origin\t{url} (push)"]
)
mock_execute.return_value = maybe_future((0, process_output, ""))
# When
response = await jp_fetch(
NAMESPACE,
local_path.name,
"remote",
"show",
method="GET",
)
# Then
command = ["git", "remote", "-v", "show"]
mock_execute.assert_called_once_with(
command,
cwd=str(local_path),
timeout=20,
env=None,
username=None,
password=None,
is_binary=False,
)
assert response.code == 200
payload = json.loads(response.body)
assert payload == {
"code": 0,
"command": " ".join(command),
"remotes": [
{"name": "origin", "url": "http://github.com/myid/myrepository.git"}
],
}
@patch("jupyterlab_git.git.execute")
async def test_git_remote_remove(mock_execute, jp_fetch, jp_root_dir):
# Given
local_path = jp_root_dir / "test_path"
mock_execute.return_value = maybe_future((0, "", ""))
# When
name = "origin"
response = await jp_fetch(
NAMESPACE,
local_path.name,
"remote",
name,
method="DELETE",
)
# Then
command = ["git", "remote", "remove", name]
mock_execute.assert_called_once_with(
command,
cwd=str(local_path),
timeout=20,
env=None,
username=None,
password=None,
is_binary=False,
)
assert response.code == 204