Warning, file /acts/CI/test_prune_ccache.py was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 """Test cache selection and deletion without accessing GitHub."""
0002
0003 from unittest.mock import Mock, call
0004
0005 import pytest
0006
0007 import prune_ccache
0008
0009
0010 def cache(i, variant="Linux-linux_ubuntu-r2", **changes):
0011 result = {
0012 "id": i,
0013 "key": f"ccache-{variant}-{i:040x}",
0014 "ref": "refs/heads/main",
0015 "version": "format-a",
0016 "size_in_bytes": 100,
0017 "created_at": f"2026-09-{i:02d}T00:00:00Z",
0018 }
0019 return result | changes
0020
0021
0022 def test_only_superseded_main_archives_of_same_variant_and_version():
0023 old, new = cache(1), cache(2)
0024 protected = [
0025 cache(3, ref="refs/pull/123/merge"),
0026 cache(4, ref="refs/heads/feature"),
0027 cache(5, version="format-b"),
0028 cache(6, variant="Linux-linux_ubuntu_extra-r2-clang22-23"),
0029 cache(7, variant="macOS-macos-r2"),
0030 cache(8, key="spack-r5-Linux"),
0031 cache(9, key="ccache-Linux-linux_ubuntu-r2-not-a-sha"),
0032 cache(10, version=""),
0033 cache(11, variant="Linux-linux_ubuntu-r3"),
0034 cache(12, size_in_bytes=0),
0035 ]
0036 assert prune_ccache.superseded_caches([new, *protected, old]) == [(new, [old])]
0037 assert prune_ccache.superseded_caches([old, cache(2, size_in_bytes=0)]) == []
0038
0039
0040 @pytest.mark.parametrize(
0041 "apply, replacement_present, deleted_ids",
0042 [(False, True, []), (True, True, [1]), (True, False, [])],
0043 ids=["dry-run", "replacement-present", "replacement-disappeared"],
0044 )
0045 def test_cache_deletion(monkeypatch, apply, replacement_present, deleted_ids):
0046
0047 listing = Mock(return_value=[cache(2), cache(3)] if replacement_present else [])
0048 delete = Mock()
0049 monkeypatch.setattr(prune_ccache, "list_caches", listing)
0050 monkeypatch.setattr(prune_ccache.subprocess, "run", delete)
0051
0052 prune_ccache.prune("owner/repo", [cache(1), cache(2)], apply=apply)
0053
0054 assert delete.call_args_list == [
0055 call(
0056 ["gh", "api", "--method", "DELETE", f"repos/owner/repo/actions/caches/{i}"],
0057 check=True,
0058 )
0059 for i in deleted_ids
0060 ]
0061 if not apply:
0062 listing.assert_not_called()