-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathtest_registry_utils.py
More file actions
177 lines (136 loc) · 5.75 KB
/
Copy pathtest_registry_utils.py
File metadata and controls
177 lines (136 loc) · 5.75 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""Tests for shared registry utilities."""
import json
import tempfile
from pathlib import Path
from unittest.mock import patch
from registry_utils import (
extract_npm_package_name,
extract_npm_package_version,
extract_pypi_package_name,
load_quarantine,
normalize_version,
should_skip_dir,
validate_distribution_urls,
)
class TestExtractNpmPackageName:
def test_scoped_with_version(self):
assert extract_npm_package_name("@google/gemini-cli@0.30.0") == "@google/gemini-cli"
def test_scoped_without_version(self):
assert extract_npm_package_name("@google/gemini-cli") == "@google/gemini-cli"
def test_unscoped_with_version(self):
assert extract_npm_package_name("some-package@1.2.3") == "some-package"
def test_unscoped_without_version(self):
assert extract_npm_package_name("some-package") == "some-package"
def test_empty_string(self):
assert extract_npm_package_name("") == ""
class TestExtractNpmPackageVersion:
def test_scoped_with_version(self):
assert extract_npm_package_version("@google/gemini-cli@0.30.0") == "0.30.0"
def test_scoped_without_version(self):
assert extract_npm_package_version("@google/gemini-cli") is None
def test_unscoped_with_version(self):
assert extract_npm_package_version("some-package@1.2.3") == "1.2.3"
def test_unscoped_without_version(self):
assert extract_npm_package_version("some-package") is None
class TestExtractPypiPackageName:
def test_with_double_equals(self):
assert extract_pypi_package_name("some-package==1.2.3") == "some-package"
def test_with_at_version(self):
assert extract_pypi_package_name("some-package@1.2.3") == "some-package"
def test_with_gte(self):
assert extract_pypi_package_name("some-package>=1.0") == "some-package"
def test_plain_name(self):
assert extract_pypi_package_name("some-package") == "some-package"
class TestNormalizeVersion:
def test_already_semver(self):
assert normalize_version("1.2.3") == "1.2.3"
def test_two_parts(self):
assert normalize_version("1.2") == "1.2.0"
def test_one_part(self):
assert normalize_version("1") == "1.0.0"
def test_four_parts_truncated(self):
assert normalize_version("1.2.3.4") == "1.2.3"
class TestLoadQuarantine:
def test_missing_file(self):
with tempfile.TemporaryDirectory() as d:
assert load_quarantine(Path(d)) == {}
def test_empty_object(self):
with tempfile.TemporaryDirectory() as d:
p = Path(d) / "quarantine.json"
p.write_text("{}")
assert load_quarantine(Path(d)) == {}
def test_with_entries(self):
with tempfile.TemporaryDirectory() as d:
data = {"bad-agent": "broke auth", "other": "removed"}
p = Path(d) / "quarantine.json"
p.write_text(json.dumps(data))
assert load_quarantine(Path(d)) == data
def test_invalid_json(self):
with tempfile.TemporaryDirectory() as d:
p = Path(d) / "quarantine.json"
p.write_text("not json")
assert load_quarantine(Path(d)) == {}
class TestValidateDistributionUrls:
"""Distribution URL validation (moved from build_registry.py)."""
def test_returns_no_errors_when_all_urls_reachable(self):
distribution = {
"binary": {
"darwin-aarch64": {
"archive": "https://example.com/agent-darwin.tar.gz",
"cmd": "./agent",
},
"linux-x86_64": {
"archive": "https://example.com/agent-linux.tar.gz",
"cmd": "./agent",
},
}
}
with patch("registry_utils.url_exists", return_value=True):
assert validate_distribution_urls(distribution) == []
def test_reports_unreachable_binary_archive(self):
distribution = {
"binary": {
"darwin-aarch64": {
"archive": "https://example.com/ok.tar.gz",
"cmd": "./agent",
},
"windows-x86_64": {
"archive": "https://example.com/missing-windows.zip",
"cmd": "agent.exe",
},
}
}
def fake_url_exists(url, *_args, **_kwargs):
return "missing" not in url
with patch("registry_utils.url_exists", side_effect=fake_url_exists):
errors = validate_distribution_urls(distribution)
assert len(errors) == 1
assert "windows-x86_64" in errors[0]
assert "missing-windows.zip" in errors[0]
def test_skip_url_validation_env_returns_empty(self, monkeypatch):
monkeypatch.setenv("SKIP_URL_VALIDATION", "1")
# Re-import to pick up the patched env var.
import importlib
import registry_utils
importlib.reload(registry_utils)
try:
distribution = {
"binary": {
"darwin-aarch64": {
"archive": "https://example.com/anything.tar.gz",
"cmd": "./agent",
}
}
}
assert registry_utils.validate_distribution_urls(distribution) == []
finally:
monkeypatch.delenv("SKIP_URL_VALIDATION", raising=False)
importlib.reload(registry_utils)
class TestShouldSkipDir:
def test_skips_hidden_runtime_dirs(self):
assert should_skip_dir(".sandbox")
assert should_skip_dir(".matrix-sandbox-debug")
assert should_skip_dir(".protocol-matrix-goose-check")
assert should_skip_dir(".tmp-junie-run")
def test_keeps_agent_dirs(self):
assert not should_skip_dir("codex-acp")