-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_forksync.py
185 lines (132 loc) · 5.88 KB
/
test_forksync.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
import logging
import os
import shutil
import git
import pytest
from forksync import (fix_https_url, fix_ssh_url, get_or_create_remote,
get_or_create_repo, parse_repo, validate_url)
def test_fix_https_url_github_http():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = 'http://github.com/ikaruswill/forksync.git'
assert fix_https_url(url) == target
def test_fix_https_url_github_https():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = 'https://github.com/ikaruswill/forksync.git'
assert fix_https_url(url) == target
def test_fix_https_url_github_no_path():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = 'https://github.com.git'
pytest.raises(ValueError, fix_https_url, url)
def test_fix_https_url_github_one_element_path():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = 'https://github.com/ikaruswill.git'
pytest.raises(ValueError, fix_https_url, url)
def test_fix_ssh_url_github_ssh():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = '[email protected]:ikaruswill/forksync.git'
assert fix_ssh_url(url) == target
def test_fix_ssh_url_github_no_path():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = '[email protected]'
url_with_colon = url + ':'
pytest.raises(ValueError, fix_ssh_url, url)
pytest.raises(ValueError, fix_ssh_url, url_with_colon)
def test_fix_ssh_url_github_one_path_element():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = '[email protected]:ikaruswill'
pytest.raises(ValueError, fix_ssh_url, url)
def test_parse_repo():
target = ('ikaruswill', 'forksync')
url = 'ssh://[email protected]/ikaruswill/forksync.git'
assert parse_repo(url) == target
def test_validate_url_https():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = 'https://github.com/ikaruswill/forksync.git'
assert validate_url(url) == target
def test_validate_url_http():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = 'http://github.com/ikaruswill/forksync.git'
assert validate_url(url) == target
def test_validate_url_https_trailing_slash():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = 'https://github.com/ikaruswill/forksync.git/'
assert validate_url(url) == target
def test_validate_url_https_no_git():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = 'https://github.com/ikaruswill/forksync'
assert validate_url(url) == target
def test_validate_url_https_no_path():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = 'https://github.com'
pytest.raises(ValueError, validate_url, url)
def test_validate_url_https_one_path_element():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = 'https://github.com/ikaruswill'
pytest.raises(ValueError, validate_url, url)
def test_validate_url_ssh():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = '[email protected]:ikaruswill/forksync.git'
assert validate_url(url) == target
def test_validate_url_ssh_trailing_slash():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = '[email protected]:ikaruswill/forksync.git/'
assert validate_url(url) == target
def test_validate_url_ssh_no_git():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = '[email protected]:ikaruswill/forksync'
assert validate_url(url) == target
def test_validate_url_ssh_no_path():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = '[email protected]'
pytest.raises(ValueError, validate_url, url)
pytest.raises(ValueError, validate_url, url + ':')
def test_validate_url_ssh_one_path_element():
target = 'ssh://[email protected]/ikaruswill/forksync.git'
url = '[email protected]:ikaruswill'
pytest.raises(ValueError, validate_url, url)
def test_get_or_create_repo_existing(tmpdir, caplog):
remote_url = tmpdir.join('remote')
remote_repo = git.Repo.init(tmpdir.join('remote'))
cache_path = tmpdir.join('cache')
repo_path = cache_path.join('repo')
target = git.Repo.init(repo_path)
with caplog.at_level(logging.INFO):
assert get_or_create_repo(repo_path, remote_url) == target
assert 'hit' in caplog.text
def test_get_or_create_repo_missing(tmpdir, caplog):
remote_url = tmpdir.join('remote')
remote_repo = git.Repo.init(tmpdir.join('remote'))
cache_path = tmpdir.join('cache')
repo_path = cache_path.join('repo')
target = git.Repo.init(repo_path)
shutil.rmtree(repo_path)
with caplog.at_level(logging.INFO):
assert get_or_create_repo(repo_path, remote_url) == target
assert 'miss' in caplog.text
def test_get_or_create_repo_invalid(tmpdir, caplog):
remote_url = tmpdir.join('remote')
remote_repo = git.Repo.init(tmpdir.join('remote'))
cache_path = tmpdir.join('cache')
repo_path = cache_path.join('repo')
target = git.Repo.init(repo_path)
shutil.rmtree(repo_path)
os.mkdir(repo_path)
with caplog.at_level(logging.INFO):
assert get_or_create_repo(repo_path, remote_url) == target
assert 'invalid' in caplog.text
def test_get_or_create_remote_existing(tmpdir, caplog):
remote_url = tmpdir.join('remote')
remote_repo = git.Repo.init(tmpdir.join('remote'))
cache_path = tmpdir.join('cache')
repo_path = cache_path.join('repo')
repo = git.Repo.clone_from(remote_url, repo_path)
target = repo.remote('origin')
assert get_or_create_remote(repo, 'origin', remote_url) == target
def test_get_or_create_remote_missing(tmpdir, caplog):
remote_url = tmpdir.join('remote')
repo_path = tmpdir.join('repo')
target = git.Repo.init(repo_path).create_remote('upstream', remote_url)
shutil.rmtree(repo_path)
repo = git.Repo.init(repo_path)
with caplog.at_level(logging.INFO):
assert get_or_create_remote(repo, 'upstream', remote_url) == target