-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_remote_flags.py
More file actions
190 lines (154 loc) · 6.73 KB
/
test_cli_remote_flags.py
File metadata and controls
190 lines (154 loc) · 6.73 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
178
179
180
181
182
183
184
185
186
187
188
189
190
"""Tests for the Session C CLI flags: --remote, --all, --gh-token,
--update-sticky-issue."""
from __future__ import annotations
import json
from pathlib import Path
from unittest.mock import patch
import pytest
from scripts.drift_check import cli
from tests.conftest import FIXTURES
@pytest.fixture
def meta_repo(tmp_path: Path) -> Path:
root = tmp_path / "meta"
root.mkdir()
(root / "VERSION").write_text("1.6.3", encoding="utf-8")
(root / "standards").mkdir()
(root / "standards" / "required-refs.json").write_text(
'{"version": 1, "requirements": {"cursor-plugin": {}, "mcp-server": {}}}',
encoding="utf-8",
)
(root / "registry.json").write_text(
json.dumps([
{"repo": "TMHSDigital/Alpha", "status": "active"},
{"repo": "TMHSDigital/Beta", "status": "active"},
{"repo": "TMHSDigital/Stale", "status": "archived"},
]),
encoding="utf-8",
)
return root
def _base(meta_repo: Path) -> list[str]:
return ["--meta-repo", str(meta_repo), "--config", "nonexistent.json"]
def test_remote_without_token_errors(capsys, meta_repo: Path, monkeypatch):
monkeypatch.delenv("DRIFT_CHECK_TOKEN", raising=False)
monkeypatch.delenv("GITHUB_TOKEN", raising=False)
rc = cli.main(["--remote", "TMHSDigital/Alpha", *_base(meta_repo)])
assert rc == 2
assert "gh-token" in capsys.readouterr().err.lower()
def test_all_without_registry_errors(capsys, tmp_path: Path):
bare = tmp_path / "bare-meta"
bare.mkdir()
(bare / "VERSION").write_text("1.6.3", encoding="utf-8")
rc = cli.main([
"--all",
"--meta-repo", str(bare),
"--config", "nonexistent.json",
"--gh-token", "x",
])
assert rc == 2
assert "registry.json" in capsys.readouterr().err
def test_all_expands_active_repos(capsys, meta_repo: Path):
"""--all without a working network would normally fail at sparse-checkout.
We patch build_remote_snapshot to verify the expansion logic only."""
seen = []
def fake_remote(repo_slug, **kw):
seen.append((repo_slug, kw["owner"]))
from scripts.drift_check.types import RepoConfig, RepoSnapshot
return RepoSnapshot(
slug=repo_slug,
repo_type="cursor-plugin",
files={},
meta_version=kw["meta_version"],
meta_commit=kw["meta_commit"],
config=RepoConfig(slug=repo_slug, repo_type="cursor-plugin"),
)
with patch("scripts.drift_check.cli.build_remote_snapshot", side_effect=fake_remote):
rc = cli.main(["--all", "--gh-token", "x", *_base(meta_repo)])
# 2 active entries; archived is skipped
assert {s for s, _ in seen} == {"Alpha", "Beta"}
assert all(o == "TMHSDigital" for _, o in seen)
# Empty snapshots have no findings -> exit 0
assert rc == 0
def test_remote_slug_without_owner_defaults_to_TMHSDigital(meta_repo: Path):
seen = []
def fake_remote(repo_slug, **kw):
seen.append((repo_slug, kw["owner"]))
from scripts.drift_check.types import RepoConfig, RepoSnapshot
return RepoSnapshot(
slug=repo_slug, repo_type="cursor-plugin", files={},
meta_version=kw["meta_version"], meta_commit=kw["meta_commit"],
config=RepoConfig(slug=repo_slug, repo_type="cursor-plugin"),
)
with patch("scripts.drift_check.cli.build_remote_snapshot", side_effect=fake_remote):
cli.main(["--remote", "Foo-Bar", "--gh-token", "x", *_base(meta_repo)])
assert seen == [("Foo-Bar", "TMHSDigital")]
def test_remote_slug_with_owner_parses(meta_repo: Path):
seen = []
def fake_remote(repo_slug, **kw):
seen.append((repo_slug, kw["owner"]))
from scripts.drift_check.types import RepoConfig, RepoSnapshot
return RepoSnapshot(
slug=repo_slug, repo_type="cursor-plugin", files={},
meta_version=kw["meta_version"], meta_commit=kw["meta_commit"],
config=RepoConfig(slug=repo_slug, repo_type="cursor-plugin"),
)
with patch("scripts.drift_check.cli.build_remote_snapshot", side_effect=fake_remote):
cli.main(["--remote", "OtherOrg/Foo", "--gh-token", "x", *_base(meta_repo)])
assert seen == [("Foo", "OtherOrg")]
def test_token_env_fallback(meta_repo: Path, monkeypatch):
monkeypatch.setenv("DRIFT_CHECK_TOKEN", "from-env")
seen = []
def fake_remote(repo_slug, **kw):
seen.append(kw["gh_token"])
from scripts.drift_check.types import RepoConfig, RepoSnapshot
return RepoSnapshot(
slug=repo_slug, repo_type="cursor-plugin", files={},
meta_version=kw["meta_version"], meta_commit=kw["meta_commit"],
config=RepoConfig(slug=repo_slug, repo_type="cursor-plugin"),
)
with patch("scripts.drift_check.cli.build_remote_snapshot", side_effect=fake_remote):
cli.main(["--remote", "Alpha", *_base(meta_repo)])
assert seen == ["from-env"]
def test_update_sticky_issue_invokes_upsert(capsys, meta_repo: Path):
# Use clean_repo locally so there's nothing to flag => sticky upsert
# is called with no findings (no_op state).
captured = {}
def fake_upsert(snapshots, findings, meta_commit, **kw):
captured["snapshots"] = list(snapshots)
captured["findings"] = list(findings)
captured["meta_commit"] = meta_commit
captured["repo"] = kw.get("repo")
return ("no_op", None)
with patch("scripts.drift_check.cli.issue.upsert_sticky_issue", side_effect=fake_upsert):
rc = cli.main([
"--local", str(FIXTURES / "clean_repo"),
"--update-sticky-issue",
"--gh-token", "x",
"--meta-commit", "deadbeef",
*_base(meta_repo),
])
assert rc == 0
assert captured["meta_commit"] == "deadbeef"
assert captured["repo"] == "TMHSDigital/Developer-Tools-Directory"
out = capsys.readouterr().out
assert "Sticky issue: no_op" in out
def test_update_sticky_issue_without_token_errors(capsys, meta_repo: Path, monkeypatch):
monkeypatch.delenv("DRIFT_CHECK_TOKEN", raising=False)
monkeypatch.delenv("GITHUB_TOKEN", raising=False)
rc = cli.main([
"--local", str(FIXTURES / "clean_repo"),
"--update-sticky-issue",
*_base(meta_repo),
])
assert rc == 2
assert "gh-token" in capsys.readouterr().err.lower()
def test_update_sticky_issue_propagates_runtime_error(meta_repo: Path):
def fake_upsert(*a, **kw):
raise RuntimeError("gh failed")
with patch("scripts.drift_check.cli.issue.upsert_sticky_issue", side_effect=fake_upsert):
rc = cli.main([
"--local", str(FIXTURES / "clean_repo"),
"--update-sticky-issue",
"--gh-token", "x",
*_base(meta_repo),
])
assert rc == 2