Branch data Line data Source code
1 : : #!/usr/bin/env python3
2 : :
3 : 1 : """Tests for GitHub Actions cache pruning helpers."""
4 : :
5 : 1 : from __future__ import annotations
6 : :
7 : 1 : import importlib.util
8 : 1 : import unittest
9 : 1 : from pathlib import Path
10 : :
11 : :
12 : 1 : def load_prune_actions_caches():
13 : : """Load the script module from the Bazel runfiles directory."""
14 : 1 : module_path = Path(__file__).with_name("prune_actions_caches.py")
15 : 1 : spec = importlib.util.spec_from_file_location(
16 : : "prune_actions_caches", module_path
17 : : )
18 : 1 : assert spec is not None
19 : 1 : assert spec.loader is not None
20 : 1 : module = importlib.util.module_from_spec(spec)
21 : 1 : spec.loader.exec_module(module)
22 : 1 : return module
23 : :
24 : :
25 : 1 : prune_actions_caches = load_prune_actions_caches()
26 : :
27 : :
28 : 1 : class BranchNameFromCacheKeyTest(unittest.TestCase):
29 : 1 : """Verify branch grouping for cache keys."""
30 : :
31 : 1 : def setUp(self) -> None:
32 : : """Set a stable cache key prefix."""
33 : 1 : self.prefix = "bazel-test-v5-windows-capped-Windows-X64-hash-"
34 : :
35 : 1 : def branch_name(self, suffix: str) -> str | None:
36 : : """Parse a branch name from a cache key suffix."""
37 : 1 : return prune_actions_caches._branch_name_from_cache_key(
38 : : f"{self.prefix}{suffix}", self.prefix
39 : : )
40 : :
41 : 1 : def test_legacy_run_id_suffix(self) -> None:
42 : : """Legacy keys use a numeric run id suffix."""
43 : 1 : self.assertEqual(self.branch_name("main-25654277602"), "main")
44 : 1 : self.assertEqual(
45 : : self.branch_name("jiayi/windows-cache-25654277602"),
46 : : "jiayi/windows-cache",
47 : : )
48 : 1 : self.assertEqual(self.branch_name("release-1-25654277602"), "release-1")
49 : :
50 : 1 : def test_run_attempt_suffix(self) -> None:
51 : : """Rerun-aware keys include a numeric run attempt suffix."""
52 : 1 : self.assertEqual(
53 : : self.branch_name("main-25654277602-attempt2"),
54 : : "main",
55 : : )
56 : 1 : self.assertEqual(
57 : : self.branch_name("jiayi/windows-cache-25654277602-attempt2"),
58 : : "jiayi/windows-cache",
59 : : )
60 : 1 : self.assertEqual(
61 : : self.branch_name("feature-attempt2-25654277602-attempt3"),
62 : : "feature-attempt2",
63 : : )
64 : :
65 : 1 : def test_malformed_keys(self) -> None:
66 : : """Malformed keys are ignored instead of grouped under a branch."""
67 : 1 : self.assertIsNone(
68 : : prune_actions_caches._branch_name_from_cache_key(
69 : : "other-prefix-main-25654277602", self.prefix
70 : : )
71 : : )
72 : 1 : self.assertIsNone(self.branch_name("main"))
73 : 1 : self.assertIsNone(self.branch_name("main-run"))
74 : 1 : self.assertIsNone(self.branch_name("main-25654277602-attempt"))
75 : 1 : self.assertIsNone(self.branch_name("main-25654277602-attemptx"))
76 : :
77 : :
78 : 1 : if __name__ == "__main__":
79 [ + ]: 1 : unittest.main()
|