Repository navigation
Expand file tree
/
Copy pathtest_cache_cli.py
More file actions
146 lines (132 loc) · 4.96 KB
/
Copy pathtest_cache_cli.py
File metadata and controls
146 lines (132 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from pathlib import Path
import json
import os
import time
import unittest
from click.testing import CliRunner
from codeql_bundle.cache import CatalogLoader
from codeql_bundle.cache_cli import main
class CacheCliTests(unittest.TestCase):
def test_prune_removes_only_expired_cache_entries(self) -> None:
runner = CliRunner()
with runner.isolated_filesystem():
cache_dir = Path("cache")
old_entry = cache_dir / "sources" / "old"
recent_entry = cache_dir / "sources" / "recent"
old_entry.mkdir(parents=True)
recent_entry.mkdir(parents=True)
(old_entry / "bundle").write_bytes(b"old")
(recent_entry / "bundle").write_bytes(b"recent")
old_time = time.time() - 40 * 24 * 60 * 60
os.utime(old_entry / "bundle", (old_time, old_time))
os.utime(old_entry, (old_time, old_time))
result = runner.invoke(
main,
[
"prune",
"--cache-dir",
str(cache_dir),
"--max-age-days",
"30",
],
)
self.assertEqual(0, result.exit_code, result.output)
self.assertFalse(old_entry.exists())
self.assertTrue(recent_entry.exists())
def test_build_rejects_invalid_release_plan(self) -> None:
runner = CliRunner()
with runner.isolated_filesystem():
plan_path = Path("plan.json")
plan_path.write_text("{}")
result = runner.invoke(
main,
[
"build",
"--plan",
str(plan_path),
"--target",
"codeql/cpp-all",
"--output-dir",
"dist",
],
)
self.assertNotEqual(0, result.exit_code)
self.assertIn("Invalid release plan", result.output)
def test_catalog_entry_can_be_added_to_catalog(self) -> None:
runner = CliRunner()
with runner.isolated_filesystem():
root = Path.cwd()
assets = root / "assets"
assets.mkdir()
cache_asset = assets / "codeql-compilation-cache-cpp.tar.gz"
cache_asset.write_bytes(b"cache")
plan = {
"cache_format": 1,
"cache_release": "codeql-compilation-cache-v1.2.3",
"cli_version": "1.2.3",
"pack_fingerprint": "0" * 64,
"release": "codeql-bundle-v1.2.3",
"source_assets": [
{
"name": name,
"platform": platform,
"sha256": str(index) * 64,
"size": 100,
"url": f"https://example.test/{name}",
}
for index, (platform, name) in enumerate(
[
("all", "codeql-bundle.tar.gz"),
("linux64", "codeql-bundle-linux64.tar.gz"),
("osx64", "codeql-bundle-osx64.tar.gz"),
("win64", "codeql-bundle-win64.tar.gz"),
],
start=1,
)
],
"source_repository": "github/codeql-action",
"targets": [
{
"language": "cpp",
"query_packs": ["codeql/cpp-queries@1.2.3"],
"target": "codeql/cpp-all",
}
],
}
plan_path = root / "plan.json"
plan_path.write_text(json.dumps(plan))
catalog_path = root / "catalog.json"
catalog_path.write_text(
json.dumps({"schema_version": 1, "bundles": []})
)
entry_path = root / "entry.json"
result = runner.invoke(
main,
[
"catalog-entry",
"--plan",
str(plan_path),
"--assets-dir",
str(assets),
"--validated-platform",
"linux64",
"--output",
str(entry_path),
],
)
self.assertEqual(0, result.exit_code, result.output)
result = runner.invoke(
main,
[
"update-catalog",
"--catalog",
str(catalog_path),
"--entry",
str(entry_path),
],
)
self.assertEqual(0, result.exit_code, result.output)
catalog = CatalogLoader().load(str(catalog_path))
self.assertIsNotNone(catalog.find_release("codeql-bundle-v1.2.3"))
if __name__ == "__main__":
unittest.main()