Skip to content

Commit 4b32f1b

Browse files
committed
Let tox only test on Python files that are in the PR.
1 parent 4c78f08 commit 4b32f1b

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

.github/workflows/lint_pr.yml

+54-16
Original file line numberDiff line numberDiff line change
@@ -180,51 +180,89 @@ jobs:
180180
echo "allowlist_externals =" >> tox_pr.ini
181181
echo " pytest" >> tox_pr.ini
182182
echo " coverage" >> tox_pr.ini
183+
echo " python" >> tox_pr.ini
183184
echo "commands =" >> tox_pr.ini
184185
185-
# For tox, let's focus on integration tests and coverage only
186-
# to avoid duplicating what individual matrix jobs are doing
186+
# Check if we have any implementation files that changed
187+
pattern_files=0
188+
test_files=0
187189
188-
# Always run a baseline test with coverage
189-
echo " pytest -xvs --cov=patterns tests/ --cov-report=term-missing" >> tox_pr.ini
190+
for file in $changed_files; do
191+
if [[ $file == patterns/* ]]; then
192+
pattern_files=1
193+
elif [[ $file == tests/* ]]; then
194+
test_files=1
195+
fi
196+
done
190197
191-
# Add coverage-focused test commands
198+
# Only run targeted tests, no baseline
192199
echo " # Run specific tests for changed files" >> tox_pr.ini
200+
201+
has_tests=false
202+
203+
# Add coverage-focused test commands
193204
for file in $changed_files; do
194205
if [[ $file == *.py ]]; then
195206
# Run coverage tests for implementation files
196207
if [[ $file == patterns/* ]]; then
197208
module_name=$(basename $file .py)
198-
pattern_dir=$(dirname $file | cut -d'/' -f2)
209+
210+
# Get the pattern type (behavioral, structural, etc.)
211+
if [[ $file == patterns/behavioral/* ]]; then
212+
pattern_dir="behavioral"
213+
elif [[ $file == patterns/creational/* ]]; then
214+
pattern_dir="creational"
215+
elif [[ $file == patterns/structural/* ]]; then
216+
pattern_dir="structural"
217+
elif [[ $file == patterns/fundamental/* ]]; then
218+
pattern_dir="fundamental"
219+
elif [[ $file == patterns/other/* ]]; then
220+
pattern_dir="other"
221+
else
222+
pattern_dir=""
223+
fi
224+
199225
echo " # Testing $file" >> tox_pr.ini
200226
201-
# Run appropriate tests for this module
202-
echo " python -c \"import os.path; test_path='tests/${pattern_dir}/test_${module_name}.py'; print(f'Test file {test_path} exists: {os.path.exists(test_path)}')\"" >> tox_pr.ini
203-
echo " pytest -xvs --cov=patterns --cov-append tests/${pattern_dir}/ -k \"${module_name}\"" >> tox_pr.ini
227+
# Check if specific test exists
228+
if [ -n "$pattern_dir" ]; then
229+
test_path="tests/${pattern_dir}/test_${module_name}.py"
230+
echo " python -c \"import os.path; test_path='${test_path}'; exists=os.path.exists(test_path); print(f'Test file {test_path} exists: {exists}'); exit(0 if exists else 1)\" && pytest -xvs --cov=patterns --cov-append ${test_path}" >> tox_pr.ini || true
231+
232+
# Also try to find any test that might include this module
233+
echo " pytest -xvs --cov=patterns --cov-append tests/${pattern_dir}/ -k \"${module_name}\" --no-header" >> tox_pr.ini
234+
fi
235+
236+
# Run doctests for the file
237+
echo " pytest --doctest-modules -v --cov=patterns --cov-append $file" >> tox_pr.ini
238+
239+
has_tests=true
204240
fi
205241
206242
# Run test files directly if modified
207243
if [[ $file == tests/* ]]; then
208244
echo " pytest -xvs --cov=patterns --cov-append $file" >> tox_pr.ini
245+
has_tests=true
209246
fi
210247
fi
211248
done
212249
213-
# Run doctests on pattern files
214-
echo " # Run doctests" >> tox_pr.ini
215-
for file in $changed_files; do
216-
if [[ $file == patterns/*.py ]]; then
217-
echo " pytest --doctest-modules -v --cov=patterns --cov-append $file" >> tox_pr.ini
218-
fi
219-
done
250+
# If we didn't find any specific tests to run, mention it
251+
if [ "$has_tests" = false ]; then
252+
echo " python -c \"print('No specific tests found for changed files. Consider adding tests.')\"" >> tox_pr.ini
253+
# Add a minimal test to avoid failure
254+
echo " pytest -xk \"not integration\" --no-header" >> tox_pr.ini
255+
fi
220256
221257
# Add coverage report command
222258
echo " coverage combine" >> tox_pr.ini
223259
echo " coverage report" >> tox_pr.ini
224260
225261
# Run tox with the custom configuration
226262
echo "Running tox with custom PR configuration..."
263+
echo "======================== TOX CONFIG ========================"
227264
cat tox_pr.ini
265+
echo "==========================================================="
228266
tox -c tox_pr.ini
229267
230268
summary:

0 commit comments

Comments
 (0)