-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
139 lines (93 loc) · 3.86 KB
/
tests.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
import json
import threading
from contextlib import contextmanager
from http import HTTPStatus
from http.server import HTTPServer, BaseHTTPRequestHandler
from uuid import uuid4
import pytest
from cv import check_unique, VersionExists, main, PypiError, InvalidVersionFormat, check_version_format, \
InvalidRequirements, VersionTypeMismatch
def test_non_existing():
with pytest.raises(PypiError):
assert check_unique(f'cv-{uuid4()}', '1.0.0') is None
def test_unique():
assert check_unique('cv', '99.0.0') is None
def test_not_unique():
with pytest.raises(VersionExists):
check_unique('cv', '1.0.0.dev8')
def test_invalid_version_format():
with pytest.raises(InvalidVersionFormat):
check_version_format('cv', '1.0.0.beta1')
def test_valid_version_format():
check_version_format('cv', '1.0.0b1')
def test_valid_main():
assert main(['cv']) is None
def test_invalid_format_main():
with pytest.raises(InvalidVersionFormat):
main(['test_modules.invalid_format'])
def test_valid_alpha_main():
assert main(['test_modules.valid_alpha', '--alpha', '--dry']) is None
def test_valid_beta_main():
assert main(['test_modules.valid_beta', '--beta', '--dry']) is None
def test_valid_rc_main():
assert main(['test_modules.valid_rc', '--rc', '--dry']) is None
def test_valid_dev_main():
assert main(['test_modules.valid_dev', '--dev', '--dry']) is None
def test_valid_release_main():
assert main(['test_modules.valid_release', '--release', '--dry']) is None
def test_restrict_invalid_combinations():
with pytest.raises(InvalidRequirements):
main(['test_modules.valid_release', '--release', '--dev'])
with pytest.raises(InvalidRequirements):
main(['test_modules.valid_release', '--release', '--alpha'])
with pytest.raises(InvalidRequirements):
main(['test_modules.valid_release', '--release', '--beta'])
with pytest.raises(InvalidRequirements):
main(['test_modules.valid_release', '--alpha', '--beta'])
with pytest.raises(InvalidRequirements):
main(['test_modules.valid_release', '--beta', '--rc'])
with pytest.raises(InvalidRequirements):
main(['test_modules.valid_release', '--rc', '--alpha'])
def test_invalid_version_type():
with pytest.raises(VersionTypeMismatch):
main(['test_modules.valid_dev', '--release', '--dry'])
with pytest.raises(VersionTypeMismatch):
main(['test_modules.valid_beta', '--alpha', '--dry'])
with pytest.raises(VersionTypeMismatch):
main(['test_modules.valid_release', '--dev', '--dry'])
class WarehousePass(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(HTTPStatus.OK)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps({"releases": {}}).encode(encoding='utf8'))
def log_message(self, format, *args):
pass
class WarehouseFail(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(HTTPStatus.NOT_FOUND)
self.end_headers()
def log_message(self, format, *args):
pass
class ServerThread(threading.Thread):
def __init__(self, server: HTTPServer):
super().__init__()
self.server = server
def run(self):
self.server.serve_forever()
def stop(self):
self.server.shutdown()
@contextmanager
def background_server(port, request_handler):
test_server = HTTPServer(('localhost', port), request_handler)
thread = ServerThread(test_server)
thread.start()
yield
thread.stop()
def test_warehouse():
with background_server(1337, WarehousePass):
assert check_unique('cv', '1.0.0', 'http://localhost:1337') is None
def test_invalid_warehouse():
with background_server(1337, WarehouseFail):
with pytest.raises(PypiError):
check_unique('cv', '1.0.0', 'http://localhost:1337')