Skip to content

Commit 98c5e95

Browse files
committed
replace many LIKE queries with one IN
1 parent 32c468b commit 98c5e95

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

.github/workflows/scripts/select-tests.py

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
MAX_SHARDS = 22
1212
DEFAULT_SHARDS = 22
1313

14-
PYTEST_IGNORED_FILES = (
14+
IGNORED_FILES = {
1515
# the pytest code itself is not part of the test suite but will be referenced by most tests
1616
"sentry/testutils/pytest/sentry.py",
17-
)
17+
}
1818

1919
IGNORED_NODEIDS = ("tests/sentry/test_wsgi.py::test_wsgi_init",)
2020

@@ -32,48 +32,46 @@ def executed_lines(bitblob: bytes) -> set[int]:
3232
return lines
3333

3434

35-
def select_tests(coverage_db_path: str, changed_files: list[str]):
35+
def select_tests(coverage_db_path: str, changed_files: set[str]):
3636
test_nodeids = set()
3737

3838
with sqlite3.connect(coverage_db_path) as conn:
3939
cursor = conn.cursor()
4040

41-
for file_path in changed_files:
42-
if any(file_path.endswith(ignored_file) for ignored_file in PYTEST_IGNORED_FILES):
43-
continue
44-
45-
cleaned_file_path = file_path
46-
if cleaned_file_path.startswith("/src"):
47-
cleaned_file_path = cleaned_file_path[len("/src") :]
48-
49-
# TODO: change to IN query; much faster
50-
cursor.execute(
51-
"""
52-
SELECT c.context, lb.numbits
53-
FROM line_bits lb
54-
JOIN file f ON lb.file_id = f.id
55-
JOIN context c ON lb.context_id = c.id
56-
WHERE f.path LIKE '%' || ?
57-
AND c.context != ''
41+
file_paths = [
42+
f"/home/runner/work/sentry/sentry/{file_path}"
43+
for file_path in changed_files - IGNORED_FILES
44+
]
45+
46+
placeholders = ",".join("?" for _ in file_paths)
47+
48+
cursor.execute(
49+
f"""
50+
SELECT c.context, lb.numbits
51+
FROM line_bits lb
52+
JOIN file f ON lb.file_id = f.id
53+
JOIN context c ON lb.context_id = c.id
54+
WHERE f.path IN ({placeholders})
55+
AND c.context != ''
5856
""",
59-
(f"%{cleaned_file_path}",),
60-
)
57+
file_paths,
58+
)
6159

62-
for test_context, bitblob in cursor.fetchall():
63-
if not test_context.endswith("|run"):
64-
# for now we're ignoring |setup and |teardown
65-
continue
60+
for test_context, bitblob in cursor.fetchall():
61+
if not test_context.endswith("|run"):
62+
# for now we're ignoring |setup and |teardown
63+
continue
6664

67-
lines = executed_lines(bitblob)
68-
if not lines:
69-
# test wasn't executed
70-
continue
65+
lines = executed_lines(bitblob)
66+
if not lines:
67+
# test wasn't executed
68+
continue
7169

72-
test_nodeid = test_context.partition("|")[0]
73-
if test_nodeid in IGNORED_NODEIDS:
74-
continue
70+
test_nodeid = test_context.partition("|")[0]
71+
if test_nodeid in IGNORED_NODEIDS:
72+
continue
7573

76-
test_nodeids.add(test_nodeid)
74+
test_nodeids.add(test_nodeid)
7775

7876
return test_nodeids
7977

@@ -122,7 +120,7 @@ def main():
122120

123121
print(f"changed files:\n{'\n'.join(changed_files)}\n")
124122

125-
selected_tests = select_tests(COVERAGE_DB_PATH, changed_files)
123+
selected_tests = select_tests(COVERAGE_DB_PATH, set(changed_files))
126124
selected_tests_str = "\n".join(selected_tests)
127125

128126
with open("selected-tests", "w") as f:

0 commit comments

Comments
 (0)