Skip to content

Commit b955e77

Browse files
bdracoedenhaus
andauthored
Cache split_tests pytest --collect-only results between CI runs (home-assistant#171780)
Co-authored-by: Robert Resch <robert@resch.dev>
1 parent 7b9c969 commit b955e77

3 files changed

Lines changed: 1094 additions & 63 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,12 +734,49 @@ jobs:
734734
python-cache-key: ${{ needs.info.outputs.python_cache_key }}
735735
uv-cache-dir: ${{ env.UV_CACHE_DIR }}
736736
apt-cache-version: ${{ env.APT_CACHE_VERSION }}
737+
- name: Restore pytest test counts cache
738+
id: cache-pytest-counts
739+
uses: actions/cache/restore@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
740+
with:
741+
path: pytest_test_counts.json
742+
# Primary key is a sentinel; restore-keys pick the most recent
743+
# prefix match since the real (content-addressed) key isn't
744+
# known until split_tests.py runs below.
745+
key: >-
746+
pytest-counts-${{ runner.os }}-${{ runner.arch }}-${{
747+
steps.python.outputs.python-version }}-${{
748+
needs.info.outputs.python_cache_key }}-restore-sentinel
749+
restore-keys: |
750+
pytest-counts-${{ runner.os }}-${{ runner.arch }}-${{ steps.python.outputs.python-version }}-${{ needs.info.outputs.python_cache_key }}-
737751
- name: Run split_tests.py
738752
env:
739753
TEST_GROUP_COUNT: ${{ needs.info.outputs.test_group_count }}
740754
run: |
741755
. venv/bin/activate
742-
python -m script.split_tests ${TEST_GROUP_COUNT} tests
756+
python -m script.split_tests \
757+
--cache pytest_test_counts.json \
758+
${TEST_GROUP_COUNT} tests
759+
- name: Hash pytest test counts cache
760+
id: cache-pytest-counts-hash
761+
run: |
762+
echo "hash=$(sha256sum pytest_test_counts.json | cut -d' ' -f1)" \
763+
>> "$GITHUB_OUTPUT"
764+
- name: Save pytest test counts cache
765+
# Content-addressed key: identical content reuses the same entry.
766+
# Skip the save when the restore already matched that hash.
767+
if: >-
768+
!endsWith(
769+
steps.cache-pytest-counts.outputs.cache-matched-key,
770+
steps.cache-pytest-counts-hash.outputs.hash
771+
)
772+
uses: actions/cache/save@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
773+
with:
774+
path: pytest_test_counts.json
775+
key: >-
776+
pytest-counts-${{ runner.os }}-${{ runner.arch }}-${{
777+
steps.python.outputs.python-version }}-${{
778+
needs.info.outputs.python_cache_key }}-${{
779+
steps.cache-pytest-counts-hash.outputs.hash }}
743780
- name: Upload pytest_buckets
744781
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
745782
with:

0 commit comments

Comments
 (0)