Skip to content

Commit de57d9d

Browse files
authored
report: Upload HTML report as artifact, fixes #388 (#395)
1 parent af914c2 commit de57d9d

File tree

7 files changed

+57
-5
lines changed

7 files changed

+57
-5
lines changed

.isort.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[settings]
22
known_first_party = code_coverage_backend,code_coverage_bot,code_coverage_events,code_coverage_tools,conftest,firefox_code_coverage
3-
known_third_party = connexion,datadog,dateutil,fakeredis,flask,flask_cors,flask_talisman,google,hglib,jsone,jsonschema,libmozdata,libmozevent,logbook,pytest,pytz,raven,redis,requests,responses,setuptools,structlog,taskcluster,tenacity,werkzeug,zstandard
3+
known_third_party = connexion,datadog,dateutil,fakeredis,flask,flask_cors,flask_talisman,google,hglib,jsone,jsonschema,libmozdata,libmozevent,logbook,magic,pytest,pytz,raven,redis,requests,responses,setuptools,structlog,taskcluster,tenacity,werkzeug,zstandard
44
force_single_line = True
55
default_section=FIRSTPARTY
66
line_length=159

.taskcluster.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,15 @@ tasks:
143143
created: {$fromNow: ''}
144144
deadline: {$fromNow: '1 hour'}
145145
payload:
146+
features:
147+
taskclusterProxy: true
146148
maxRunTime: 3600
147149
image: python:3.8
148150
command:
149151
- sh
150152
- -lxce
151153
- "git clone --quiet ${repository} /src && cd /src && git checkout ${head_rev} -b checks &&
152-
cd /src/report && pip install -r requirements.txt && ./ci-test.sh"
154+
cd /src/report && ./ci-test.sh"
153155
metadata:
154156
name: "Code Coverage Report checks: unit tests"
155157
description: Check python code with unittest

report/ci-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash -e
22

3-
pip install --disable-pip-version-check --no-cache-dir --quiet -r test-requirements.txt
3+
pip install --disable-pip-version-check --no-cache-dir --quiet -r test-requirements.txt -r requirements.txt .
44

55
python -m unittest discover tests
66
python setup.py sdist bdist_wheel

report/firefox_code_coverage/codecoverage.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import argparse
44
import errno
55
import json
6+
import logging
67
import os
78
import shutil
89
import subprocess
@@ -11,7 +12,10 @@
1112
import tempfile
1213
import time
1314
import warnings
15+
from datetime import timedelta
16+
from pathlib import Path
1417

18+
import magic
1519
import requests
1620
import tenacity
1721

@@ -24,6 +28,8 @@
2428
GRCOV_INDEX = "gecko.cache.level-3.toolchains.v3.linux64-grcov.latest"
2529
GRCOV_ARTIFACT = "public/build/grcov.tar.xz"
2630

31+
logger = logging.getLogger(__name__)
32+
2733

2834
def is_taskcluster_loaner():
2935
return "TASKCLUSTER_INTERACTIVE" in os.environ
@@ -311,6 +317,27 @@ def download_grcov():
311317
return local_path
312318

313319

320+
def upload_html_report(
321+
report_dir, base_artifact="public/report", ttl=timedelta(days=10)
322+
):
323+
assert os.path.isdir(report_dir), "Not a directory {}".format(report_dir)
324+
report_dir = os.path.realpath(report_dir)
325+
assert not base_artifact.endswith("/"), "No trailing / in base_artifact"
326+
327+
# Use Taskcluster proxy when available
328+
taskcluster.auth()
329+
330+
for path in Path(report_dir).rglob("*"):
331+
332+
filename = str(path.relative_to(report_dir))
333+
content_type = magic.from_file(str(path), mime=True)
334+
logger.debug("Uploading {} as {}".format(filename, content_type))
335+
336+
taskcluster.upload_artifact(
337+
"{}/{}".format(base_artifact, filename), path.read_text(), content_type, ttl
338+
)
339+
340+
314341
def main():
315342
parser = argparse.ArgumentParser()
316343

@@ -438,6 +465,9 @@ def main():
438465
else:
439466
generate_report(grcov_path, "html", args.output_dir, artifact_paths)
440467

468+
if is_taskcluster_loaner():
469+
upload_html_report(args.output_dir)
470+
441471

442472
if __name__ == "__main__":
443473
main()

report/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
taskcluster==24.2.0
1+
python-magic==0.4.15
2+
taskcluster==24.3.1
23
tenacity==6.0.0

report/setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ def read_requirements(file_):
1212
for line in f.readlines():
1313
line = line.strip()
1414
if line.startswith("https://"):
15-
line = line.split("#")[1].split("egg=")[1]
15+
params = {
16+
p[: p.index("=")]: p[p.index("=") + 1 :]
17+
for p in line.split("#")[1].split("&")
18+
}
19+
line = params["egg"]
1620
elif line == "" or line.startswith("#") or line.startswith("-"):
1721
continue
1822
line = line.split("#")[0].strip()

report/tests/test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import errno
88
import os
99
import shutil
10+
import tempfile
1011
import unittest
12+
from datetime import timedelta
1113

1214
from firefox_code_coverage import codecoverage
1315

@@ -143,6 +145,19 @@ def test_download_grcov(self):
143145
with open("grcov_ver", "r") as f:
144146
self.assertEqual(ver, f.read())
145147

148+
def test_upload_report(self):
149+
150+
# Can only run on Taskcluster
151+
if "TASK_ID" not in os.environ:
152+
return
153+
154+
_dir = tempfile.mkdtemp()
155+
156+
with open(os.path.join(_dir, "report.html"), "w") as f:
157+
f.write("<strong>This is a test</strong>")
158+
159+
codecoverage.upload_html_report(str(_dir), ttl=timedelta(days=1))
160+
146161

147162
if __name__ == "__main__":
148163
unittest.main()

0 commit comments

Comments
 (0)