Skip to content

Commit 292c1fd

Browse files
author
Bastien Abadie
committed
bot: Check path is supported and not 3rd party before sending a warning, fixes #200
1 parent fa1a69a commit 292c1fd

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

bot/code_coverage_bot/phabricator.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
import os
34
import re
45

56
import structlog
@@ -9,6 +10,7 @@
910

1011
from code_coverage_bot import hgmo
1112
from code_coverage_bot.secrets import secrets
13+
from code_coverage_tools import COVERAGE_EXTENSIONS
1214

1315
logger = structlog.get_logger(__name__)
1416

@@ -29,6 +31,16 @@ def __init__(self, repo_dir, revision):
2931
self.repo_dir = repo_dir
3032
self.revision = revision
3133

34+
# Read third party exclusion lists from repo
35+
third_parties = os.path.join(
36+
self.repo_dir, "tools/rewriting/ThirdPartyPaths.txt"
37+
)
38+
if os.path.exists(third_parties):
39+
self.third_parties = [line.rstrip() for line in open(third_parties)]
40+
else:
41+
self.third_parties = []
42+
logger.warn("Missing third party exclusion list", path=third_parties)
43+
3244
def _find_coverage(self, report, path):
3345
"""
3446
Find coverage value in a covdir report
@@ -38,7 +50,13 @@ def _find_coverage(self, report, path):
3850
parts = path.split("/")
3951
for part in filter(None, parts):
4052
if part not in report["children"]:
41-
logger.warn("Path {} not found in report".format(path))
53+
# Only send warning for supported extensions
54+
if self.is_supported_extension(path):
55+
logger.warn("Path {} not found in report".format(path))
56+
else:
57+
logger.info(
58+
"Path not found in report for unsupported extension", path=path
59+
)
4260
return
4361
report = report["children"][part]
4462

@@ -95,6 +113,24 @@ def _apply_coverage_map(self, annotate, coverage_map):
95113

96114
return phab_coverage_data
97115

116+
def is_third_party(self, path):
117+
"""
118+
Check a file against known list of third party paths
119+
"""
120+
for third_party in self.third_parties:
121+
if path.startswith(third_party):
122+
return True
123+
return False
124+
125+
def is_supported_extension(self, path):
126+
"""
127+
Check a file has a supported extension
128+
"""
129+
_, ext = os.path.splitext(path)
130+
if not ext:
131+
return False
132+
return ext[1:] in COVERAGE_EXTENSIONS
133+
98134
def generate(self, report, changesets):
99135
results = {}
100136

@@ -109,6 +145,12 @@ def generate(self, report, changesets):
109145

110146
# For each file...
111147
for path in changeset["files"]:
148+
149+
# Skip third party files
150+
if self.is_third_party(path):
151+
logger.info("Skip third party path", path=path)
152+
continue
153+
112154
# Retrieve the coverage data.
113155
coverage_record = self._find_coverage(report, path)
114156
if coverage_record is None:

bot/tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def copy_pushlog_database(remote, local):
2525

2626
def add_file(hg, repo_dir, name, contents):
2727
path = os.path.join(repo_dir, name)
28+
os.makedirs(os.path.dirname(path), exist_ok=True)
2829

2930
with open(path, "w") as f:
3031
f.write(contents)

bot/tests/test_phabricator.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,61 @@ def test_backout_removed_file(mock_secrets, fake_hg_repo):
317317
1: {"file": {"coverage": "NUCCCCU", "lines_added": 7, "lines_covered": 5}},
318318
2: {},
319319
}
320+
321+
322+
def test_third_party(mock_secrets, fake_hg_repo):
323+
hg, local, remote = fake_hg_repo
324+
325+
add_file(hg, local, "tools/rewriting/ThirdPartyPaths.txt", "third_party\nsome/path")
326+
revision = commit(hg, 1)
327+
328+
phabricator = PhabricatorUploader(local, revision)
329+
330+
assert phabricator.third_parties == ["third_party", "some/path"]
331+
332+
assert phabricator.is_third_party("js/src/xx.cpp") is False
333+
assert phabricator.is_third_party("dom/media/yyy.h") is False
334+
assert phabricator.is_third_party("third_party/test.cpp") is True
335+
assert phabricator.is_third_party("some/test.cpp") is False
336+
assert phabricator.is_third_party("some/path/test.cpp") is True
337+
338+
339+
def test_supported_extensions(mock_secrets, fake_hg_repo):
340+
hg, local, remote = fake_hg_repo
341+
342+
add_file(hg, local, "file", "1\n2\n3\n4\n5\n6\n7\n")
343+
revision = commit(hg, 1)
344+
345+
phabricator = PhabricatorUploader(local, revision)
346+
347+
assert phabricator.is_supported_extension("README") is False
348+
assert phabricator.is_supported_extension("requirements.txt") is False
349+
assert phabricator.is_supported_extension("tools/Cargo.toml") is False
350+
assert phabricator.is_supported_extension("tools/Cargo.lock") is False
351+
assert phabricator.is_supported_extension("rust/code.rs") is False
352+
assert phabricator.is_supported_extension("dom/feature.idl") is False
353+
assert phabricator.is_supported_extension("dom/feature.webidl") is False
354+
assert phabricator.is_supported_extension("xpcom/moz.build") is False
355+
assert phabricator.is_supported_extension("payload.json") is False
356+
assert phabricator.is_supported_extension("inline.patch") is False
357+
assert phabricator.is_supported_extension("README.mozilla") is False
358+
assert phabricator.is_supported_extension("config.yml") is False
359+
assert phabricator.is_supported_extension("config.yaml") is False
360+
assert phabricator.is_supported_extension("config.ini") is False
361+
assert phabricator.is_supported_extension("tooling.py") is False
362+
363+
assert phabricator.is_supported_extension("test.cpp") is True
364+
assert phabricator.is_supported_extension("some/path/to/test.cpp") is True
365+
assert phabricator.is_supported_extension("xxxYYY.h") is True
366+
assert phabricator.is_supported_extension("test.c") is True
367+
assert phabricator.is_supported_extension("test.cc") is True
368+
assert phabricator.is_supported_extension("test.cxx") is True
369+
assert phabricator.is_supported_extension("test.hh") is True
370+
assert phabricator.is_supported_extension("test.hpp") is True
371+
assert phabricator.is_supported_extension("test.hxx") is True
372+
assert phabricator.is_supported_extension("test.js") is True
373+
assert phabricator.is_supported_extension("test.jsm") is True
374+
assert phabricator.is_supported_extension("test.xul") is True
375+
assert phabricator.is_supported_extension("test.xml") is True
376+
assert phabricator.is_supported_extension("test.html") is True
377+
assert phabricator.is_supported_extension("test.xhtml") is True

0 commit comments

Comments
 (0)