Skip to content

Commit a3354ee

Browse files
committed
Improve test filtering logic
1 parent 56a83a9 commit a3354ee

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

buildkite/test-template-ci.j2

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,100 @@ COVERAGE_FILE={{ coverage_file }} {{ cmd | replace("pytest ", "pytest --cov=vllm
7373
{%- endif %}
7474
{%- set matched_targets = match_ns.targets %}
7575

76+
{# Filter matched targets to only include those actually covered by step commands #}
77+
{# This ensures intelligent filtering mirrors normal behavior - if a test isn't run normally, don't run it in intelligent mode #}
78+
{%- set filter_ns = namespace(filtered_targets=[], covered_paths=[]) %}
79+
{%- if matched_targets | length > 0 %}
80+
{%- set all_commands = step.commands if step.commands else ([step.command] if step.command else []) %}
81+
{# Extract test paths from pytest commands (e.g., "v1/core", "v1/executor") #}
82+
{%- for cmd in all_commands %}
83+
{%- if "pytest " in cmd %}
84+
{# Split command to find pytest arguments #}
85+
{%- set cmd_parts = cmd | split(" ") | list %}
86+
{%- set in_pytest = false %}
87+
{%- for part in cmd_parts %}
88+
{%- if part == "pytest" %}
89+
{%- set in_pytest = true %}
90+
{%- elif in_pytest and part[:1] != "-" and "/" in part %}
91+
{# This looks like a test path (contains / and doesn't start with -) #}
92+
{# Skip specific tests (contain ::) - we only want directories or files #}
93+
{%- if "::" not in part %}
94+
{%- if part[-3:] == ".py" %}
95+
{# It's a file - add both the file itself and its directory (if deep enough) #}
96+
{# Add the file as-is for exact matching #}
97+
{%- set filter_ns.covered_paths = filter_ns.covered_paths + [part] %}
98+
{# Also add directory if it has at least 2 levels (e.g., v1/core but not just v1) #}
99+
{%- set path_parts = part | split("/") | list %}
100+
{%- if path_parts | length > 2 %}
101+
{%- set dir_parts = path_parts[:-1] %}
102+
{%- set dir_path = dir_parts | join("/") %}
103+
{%- set filter_ns.covered_paths = filter_ns.covered_paths + [dir_path] %}
104+
{%- endif %}
105+
{%- else %}
106+
{# It's a directory path #}
107+
{%- set filter_ns.covered_paths = filter_ns.covered_paths + [part] %}
108+
{%- endif %}
109+
{%- endif %}
110+
{%- endif %}
111+
{%- endfor %}
112+
{%- endif %}
113+
{%- endfor %}
114+
{# Now check each matched target against covered paths #}
115+
{%- for target in matched_targets %}
116+
{%- set target_ns = namespace(is_covered=false) %}
117+
{%- for covered_path in filter_ns.covered_paths %}
118+
{# Check if target starts with this covered path (ensure proper directory matching) #}
119+
{%- set covered_len = covered_path | length %}
120+
{%- if target[:covered_len] == covered_path %}
121+
{# Ensure it's a proper directory match (next char is / or end of string) #}
122+
{%- if target | length == covered_len or target[covered_len:covered_len+1] == "/" %}
123+
{%- set target_ns.is_covered = true %}
124+
{%- endif %}
125+
{%- endif %}
126+
{%- endfor %}
127+
{%- if target_ns.is_covered %}
128+
{%- set filter_ns.filtered_targets = filter_ns.filtered_targets + [target] %}
129+
{%- endif %}
130+
{%- endfor %}
131+
{%- endif %}
132+
{%- set matched_targets = filter_ns.filtered_targets %}
133+
134+
{# Extract pytest markers from original commands to preserve them in intelligent filtering #}
135+
{%- set marker_ns = namespace(markers='') %}
136+
{%- if matched_targets | length > 0 %}
137+
{%- set all_commands = step.commands if step.commands else ([step.command] if step.command else []) %}
138+
{%- for cmd in all_commands %}
139+
{%- if "pytest " in cmd and " -m " in cmd %}
140+
{# Extract the -m marker argument using split filter and convert to list #}
141+
{%- set parts = cmd | split(" -m ") | list %}
142+
{%- if parts | length > 1 %}
143+
{%- set after_m = parts[1] %}
144+
{# Find the marker value (first token after -m, handling quotes) #}
145+
{%- if after_m[0:1] == "'" %}
146+
{%- set marker_parts = after_m[1:] | split("'") | list %}
147+
{%- if marker_parts | length > 0 %}
148+
{%- set marker_ns.markers = " -m '" ~ marker_parts[0] ~ "'" %}
149+
{%- endif %}
150+
{%- elif after_m[0:1] == '"' %}
151+
{%- set marker_parts = after_m[1:] | split('"') | list %}
152+
{%- if marker_parts | length > 0 %}
153+
{%- set marker_ns.markers = ' -m "' ~ marker_parts[0] ~ '"' %}
154+
{%- endif %}
155+
{%- else %}
156+
{# No quotes, take first word #}
157+
{%- set marker_parts = after_m | split(" ") | list %}
158+
{%- if marker_parts | length > 0 %}
159+
{%- set marker_ns.markers = " -m " ~ marker_parts[0] %}
160+
{%- endif %}
161+
{%- endif %}
162+
{%- endif %}
163+
{%- endif %}
164+
{%- endfor %}
165+
{%- endif %}
166+
76167
{# If we have matched targets, run only those specific tests #}
77168
{% if matched_targets | length > 0 %}
78-
pytest -v -s {{ matched_targets | join(' ') }}
169+
pytest -v -s{{ marker_ns.markers }} {{ matched_targets | join(' ') }}
79170
{% else %}
80171
{# Default behavior: preserve original commands with optional coverage injection #}
81172
{% if cov_enabled %}

0 commit comments

Comments
 (0)