forked from ipfs-shipyard/py-ipfs-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-tests.py
executable file
·213 lines (173 loc) · 6.06 KB
/
run-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
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
#!/usr/bin/python
import contextlib
import itertools
import locale
import os
import pathlib
import random
import shutil
import subprocess
import sys
import tempfile
import pytest
######################
# Test configuration #
######################
# Environment name as used by `tox`
ENVNAME = "py{}{}".format(sys.version_info.major, sys.version_info.minor)
# Determine project base directory and testing path
BASE_PATH = pathlib.Path(__file__).parent.parent
TEST_PATH = BASE_PATH / "build" / "test-{}".format(ENVNAME)
IPFS_PATH = TEST_PATH / "ipfs-path"
ADDR = "/ip4/127.0.0.1/tcp/{0}".format(random.randrange(40000, 65535))
###########################
# Set up test environment #
###########################
# Add project directory to PYTHONPATH
sys.path.insert(0, str(BASE_PATH))
# Switch working directory to project directory
os.chdir(str(BASE_PATH))
# Export environment variables required for testing
os.environ["IPFS_PATH"] = str(IPFS_PATH)
os.environ["PY_IPFS_HTTP_CLIENT_DEFAULT_ADDR"] = str(ADDR)
# Make sure the IPFS data directory exists and is empty
with contextlib.suppress(FileNotFoundError):
shutil.rmtree(str(IPFS_PATH))
with contextlib.suppress(FileExistsError):
os.makedirs(str(IPFS_PATH))
# Initialize the IPFS data directory
subprocess.call(["ipfs", "init"])
subprocess.call(["ipfs", "config", "Addresses.Gateway", ""])
subprocess.call(["ipfs", "config", "Addresses.API", ADDR])
subprocess.call(["ipfs", "config", "--bool", "Experimental.FilestoreEnabled", "true"])
################
# Start daemon #
################
extra_args = {
"encoding": locale.getpreferredencoding()
}
# Spawn IPFS daemon in data directory
print("Starting IPFS daemon on {0}…".format(ADDR), file=sys.stderr)
DAEMON = subprocess.Popen(
["ipfs", "daemon", "--enable-pubsub-experiment"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
**extra_args
)
class DaemonProcessPlugin:
"""Tiny pytest plugin to inject daemon object reference as test “fixture” value."""
@pytest.hookimpl(hookwrapper=True)
def pytest_pyfunc_call(self, pyfuncitem):
if "daemon" in pyfuncitem.funcargs:
pyfuncitem.funcargs["daemon"] = DAEMON
yield
# Wait for daemon to start up
for line in DAEMON.stdout:
print("\t{0}".format(line), end="", file=sys.stderr)
if line.strip() == "Daemon is ready":
break
#XXX: This design could deadlock the test run if the daemon were to produce more
# output than fits into its output pipe before shutdown
##################
# Run test suite #
##################
PYTEST_CODE = 1
try:
# Make sure all required pytest plugins are loaded up-front
os.environ["PYTEST_PLUGINS"] = ",".join([
"cid",
"dependency",
"localserver",
"pytest_cov",
"pytest_mock",
"pytest_order",
])
with tempfile.NamedTemporaryFile("r+") as coveragerc:
coverage_args = []
if os.name != "nt":
PREFER_HTTPX = (os.environ.get("PY_IPFS_HTTP_CLIENT_PREFER_HTTPX", "no").lower()
not in ("0", "f", "false", "n", "no"))
# Assemble list of files to exclude from coverage analysis
omitted_files = [
"ipfshttpclient/requests_wrapper.py",
]
if PREFER_HTTPX and sys.version_info >= (3, 6):
omitted_files.append("ipfshttpclient/http_requests.py")
else: #PY35: Fallback to old requests-based code instead of HTTPX
omitted_files.append("ipfshttpclient/http_httpx.py")
# Assemble list of coverage data exclusion patterns (also escape the
# hash sign [#] as it has a special meaning [comment] in the generated
# configuration file)
exclusions = [
# Add the standard coverage exclusion statement
r"pragma:\s+no\s+cover",
# Ignore typing-only branches
r"if\s+(?:[A-Za-z]+\s*[.]\s*)?TYPE_CHECKING\s*:",
# Ignore dummy ellipsis expression line
r"^\s*\.\.\.\s*$",
]
if sys.version_info.major == 2:
exclusions.append(r"\#PY3")
else:
# Exclude the past
exclusions.append(r"\#PY2")
# Exclude code only used for compatibility with a previous Python version
exclusions.append(r"\#PY3({0})([^\d+]|$)".format(
"|".join(map(str, range(0, sys.version_info.minor)))
))
# Exclude code only used in future Python versions
exclusions.append(r"\#PY3({0})\+".format(
"|".join(map(str, range(sys.version_info.minor + 1, 20)))
))
if PREFER_HTTPX and sys.version_info >= (3, 6):
exclusions.append(r"\# pragma: http-backend=requests")
else: #PY35: Fallback to old requests-based code instead of HTTPX
exclusions.append(r"\# pragma: http-backend=httpx")
# Create temporary file with extended *coverage.py* configuration data
coveragerc.file.writelines(
map(
lambda s: s + "\n",
itertools.chain(
(
"[run]",
"omit =",
),
map(lambda s: "\t" + s, omitted_files),
(
"[report]",
"# Exclude lines specific to some other Python version from coverage",
"exclude_lines =",
),
map(lambda s: "\t" + s, exclusions))))
coveragerc.file.flush()
coverage_args = [
"--cov=ipfshttpclient",
"--cov-branch",
"--cov-config={0}".format(coveragerc.name),
"--no-cov-on-fail",
"--cov-fail-under=90",
"--cov-report=term",
"--cov-report=html:{}".format(str(TEST_PATH / "cov_html")),
"--cov-report=xml:{}".format(str(TEST_PATH / "cov.xml")),
]
# Launch pytest in-process
PYTEST_CODE = pytest.main([
"--verbose",
] + coverage_args + sys.argv[1:], plugins=[DaemonProcessPlugin()])
finally:
try:
# Move coverage file to test directory (so that the coverage files of different
# versions can be merged later on)
shutil.move(str(BASE_PATH / ".coverage"), str(TEST_PATH / "cov_raw"))
except FileNotFoundError:
pass # Early crash in pytest or Windows – no coverage data generated
# Make sure daemon was terminated during the tests
if DAEMON.poll() is None: # "if DAEMON is running"
DAEMON.kill()
print("IPFS daemon was still running after test!", file=sys.stderr)
output = list(DAEMON.stdout)
if output:
print("IPFS daemon printed extra messages:", file=sys.stderr)
for line in output:
print("\t{0}".format(line), end="", file=sys.stderr)
sys.exit(PYTEST_CODE)