-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathpytest_azurepipelines.py
224 lines (187 loc) · 7.87 KB
/
pytest_azurepipelines.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
# -*- coding: utf-8 -*-
import io
import os.path
import sys
import importlib_resources
import pytest
from packaging.version import parse as parse_version
__version__ = "1.0.4"
DEFAULT_PATH = "test-output.xml"
DEFAULT_COVERAGE_PATH = "coverage.xml"
def pytest_addoption(parser):
group = parser.getgroup("pytest_azurepipelines")
group.addoption(
"--test-run-title",
action="store",
dest="azure_run_title",
default="Pytest results",
help="Set the Azure test run title.",
)
group.addoption(
"--napoleon-docstrings",
action="store_true",
dest="napoleon",
default=False,
help="If using Google, NumPy, or PEP 257 multi-line docstrings.",
)
group.addoption(
"--no-coverage-upload",
action="store_true",
dest="no_coverage_upload",
default=False,
help="Skip uploading coverage results to Azure Pipelines.",
)
group.addoption(
"--no-docker-discovery",
action="store_true",
dest="no_docker_discovery",
default=False,
help="Skip detecting running inside a Docker container.",
)
group.addoption(
"--report-dir",
action="store",
dest="report_dir",
default="htmlcov",
help="The directory where the html reports are.",
)
@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
xmlpath = config.getoption("--nunitxml")
if not xmlpath:
config.option.nunit_xmlpath = DEFAULT_PATH
# ensure coverage creates xml format
if config.pluginmanager.has_plugin("pytest_cov"):
if config.option.cov_report.get("xml") is None:
config.option.cov_report["xml"] = os.path.normpath(
os.path.abspath(os.path.expanduser(os.path.expandvars(DEFAULT_COVERAGE_PATH)))
)
if "html" not in config.option.cov_report:
config.option.cov_report["html"] = None
def get_resource_folder_path():
resources_folder_name = "resources"
ancestor = importlib_resources.files(__name__)
# traverse to parent folder until a child folder with name "resources"
# is found, or the root is reached
while not ancestor.joinpath(resources_folder_name).is_dir():
ancestor = ancestor.parent
if ancestor == ancestor.parent: # Effectively at the root
if not (ancestor / resources_folder_name).exists():
raise RuntimeError("Could not find the path to resources folder.")
return os.path.join(ancestor, resources_folder_name)
def get_resource_file_content(file_name):
with open(os.path.join(get_resource_folder_path(), file_name), mode='rt') as source:
return source.read()
def inline_css_into_each_html_report_file(reportdir):
"""
Since <link> does not work inside the iframe used by Azure DevOps,
inline the CSS styles into each HTML file generated by pytest report.
This enables a good UX when reading reports in the portal.
"""
style_fragment = "\n<style>\n" + get_resource_file_content("style.css") + "\n</style>\n"
# since pytest-cov generates a flat folder, we don't need recursion here
for file in os.listdir(reportdir):
if file.endswith(".html"):
full_path = os.path.join(reportdir, file)
with open(full_path, mode="rt", encoding="utf8") as f:
new_text = f.read().replace("</head>", style_fragment + "</head>")
with open(full_path, mode="wt", encoding="utf8") as f:
f.write(new_text)
def try_to_inline_css_into_each_html_report_file(reportdir):
try:
inline_css_into_each_html_report_file(reportdir)
except Exception as ex:
print(
"##vso[task.logissue type=warning;]{0}{1}".format(
"Failed to inline CSS styles in coverage reports. Error: ",
str(ex)
)
)
@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session, exitstatus):
xmlpath = session.config.option.nunit_xmlpath
mode = "NUnit"
# This mirrors https://github.com/pytest-dev/pytest/blob/38adb23bd245329d26b36fd85a43aa9b3dd0406c/src/_pytest/junitxml.py#L368-L369
xmlabspath = os.path.normpath(
os.path.abspath(os.path.expanduser(os.path.expandvars(xmlpath)))
)
mountinfo = None
if not session.config.getoption("no_docker_discovery") and os.path.isfile('/.dockerenv'):
with io.open(
'/proc/1/mountinfo', 'rb',
) as fobj:
mountinfo = fobj.read()
mountinfo = mountinfo.decode(sys.getfilesystemencoding())
if mountinfo:
xmlabspath = apply_docker_mappings(mountinfo, xmlabspath)
# Set the run title in the UI to a configurable setting
description = session.config.option.azure_run_title.replace("'", "")
if not session.config.getoption("no_docker_discovery"):
print(
"##vso[results.publish type={2};runTitle='{1}';publishRunAttachments=true;]{0}".format(
xmlabspath, description, mode
)
)
else:
print("Skipping uploading of test results because --no-docker-discovery set.")
if exitstatus != 0 and session.testsfailed > 0 and not session.shouldfail:
print(
"##vso[task.logissue type=error;]{0} test(s) failed, {1} test(s) collected.".format(
session.testsfailed, session.testscollected
)
)
if not session.config.getoption("no_coverage_upload") and not session.config.getoption("no_docker_discovery") and session.config.pluginmanager.has_plugin("pytest_cov"):
covpath = os.path.normpath(
os.path.abspath(os.path.expanduser(os.path.expandvars(session.config.option.cov_report["xml"])))
)
reportdir = os.path.normpath(os.path.abspath(session.config.getoption("report_dir")))
if os.path.exists(covpath):
if mountinfo:
covpath = apply_docker_mappings(mountinfo, covpath)
reportdir = apply_docker_mappings(mountinfo, reportdir)
try_to_inline_css_into_each_html_report_file(reportdir)
print(
"##vso[codecoverage.publish codecoveragetool=Cobertura;summaryfile={0};reportdirectory={1};]".format(
covpath, reportdir
)
)
else:
print(
"##vso[task.logissue type=warning;]{0}".format(
"Coverage XML was not created, skipping upload."
)
)
else:
print("Skipping uploading of coverage data.")
def apply_docker_mappings(mountinfo, dockerpath):
"""
Parse the /proc/1/mountinfo file and apply the mappings so that docker
paths are transformed into the host path equivalent so the Azure Pipelines
finds the file assuming the path has been bind mounted from the host.
"""
for line in mountinfo.splitlines():
words = line.split(' ')
if len(words) < 5:
continue
docker_mnt_path = words[4]
host_mnt_path = words[3]
if dockerpath.startswith(docker_mnt_path):
dockerpath = ''.join([
host_mnt_path,
dockerpath[len(docker_mnt_path):],
])
return dockerpath
if parse_version(pytest.__version__) >= parse_version("7.0.0"):
def pytest_warning_recorded(warning_message, *args, **kwargs):
print("##vso[task.logissue type=warning;]{0}".format(str(warning_message.message)))
else:
def pytest_warning_captured(warning_message, *args, **kwargs):
print("##vso[task.logissue type=warning;]{0}".format(str(warning_message.message)))
@pytest.fixture
def record_pipelines_property(record_nunit_property):
# Proxy for Nunit fixture, just in case we later change the API
return record_nunit_property
@pytest.fixture
def add_pipelines_attachment(add_nunit_attachment):
# Proxy for Nunit fixture, just in case we later change the API
return add_nunit_attachment