SHA256
forked from course-team/tjhlp-gitea
- 两个 config.yaml 的 contact_links 从 example.com 换成真实入口:
官网《学会提问》/ 课程官网 / 平台提问速查, 让答疑平台与官网互相导流
- 模板支持 {{BASE_URL}}/{{ORG}}/{{REPO}} 占位符, 推送时替换,
换学期建新组织不用改模板; 未配置时保留占位符并告警而不是推坏链接
- scripts: 新增 gitea.public_url(对外地址, 与 CLI 用的 url 区分),
utils.web_base_url()/render_template_placeholders() 及单元测试
154 lines
5.1 KiB
Python
154 lines
5.1 KiB
Python
"""utils.py 单元测试: parse_repo, compute_issue_stats, render_template_placeholders"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from pydantic import SecretStr
|
|
|
|
from tjhlp.config import GiteaConfig
|
|
from tjhlp.utils import (
|
|
compute_issue_stats,
|
|
parse_repo,
|
|
render_template_placeholders,
|
|
web_base_url,
|
|
)
|
|
|
|
# typer.Exit 在非 CLI 上下文中抛出 click.exceptions.Exit
|
|
_EXIT_EXCEPTIONS = (SystemExit, Exception)
|
|
|
|
|
|
class TestParseRepo:
|
|
def test_valid(self):
|
|
assert parse_repo("org/repo") == ("org", "repo")
|
|
|
|
def test_valid_with_hyphens(self):
|
|
assert parse_repo("tjhlp-2025f/hw01") == ("tjhlp-2025f", "hw01")
|
|
|
|
def test_no_slash(self):
|
|
with pytest.raises(_EXIT_EXCEPTIONS):
|
|
parse_repo("noslash")
|
|
|
|
def test_multiple_slashes(self):
|
|
with pytest.raises(_EXIT_EXCEPTIONS):
|
|
parse_repo("a/b/c")
|
|
|
|
def test_empty_org(self):
|
|
with pytest.raises(_EXIT_EXCEPTIONS):
|
|
parse_repo("/repo")
|
|
|
|
def test_empty_repo(self):
|
|
with pytest.raises(_EXIT_EXCEPTIONS):
|
|
parse_repo("org/")
|
|
|
|
def test_empty_string(self):
|
|
with pytest.raises(_EXIT_EXCEPTIONS):
|
|
parse_repo("")
|
|
|
|
|
|
class TestComputeIssueStats:
|
|
def test_empty_list(self):
|
|
stats = compute_issue_stats([])
|
|
assert stats["total"] == 0
|
|
assert stats["open"] == 0
|
|
assert stats["closed"] == 0
|
|
assert stats["no_reply"] == 0
|
|
|
|
def test_basic_counts(self):
|
|
issues = [
|
|
{"state": "open", "comments": 0},
|
|
{"state": "open", "comments": 2},
|
|
{"state": "closed", "comments": 1},
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert stats["total"] == 3
|
|
assert stats["open"] == 2
|
|
assert stats["closed"] == 1
|
|
assert stats["with_comments"] == 2
|
|
assert stats["no_reply"] == 1
|
|
|
|
def test_label_counts(self):
|
|
issues = [
|
|
{"state": "open", "comments": 0, "labels": [{"name": "bug"}, {"name": "紧急"}]},
|
|
{"state": "open", "comments": 0, "labels": [{"name": "bug"}]},
|
|
{"state": "closed", "comments": 1, "labels": []},
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert stats["label_counts"]["bug"] == 2
|
|
assert stats["label_counts"]["紧急"] == 1
|
|
|
|
def test_user_counts(self):
|
|
issues = [
|
|
{"state": "open", "comments": 0, "user": {"login": "student1"}},
|
|
{"state": "open", "comments": 0, "user": {"login": "student1"}},
|
|
{"state": "open", "comments": 0, "user": {"login": "student2"}},
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert stats["user_counts"]["student1"] == 2
|
|
assert stats["user_counts"]["student2"] == 1
|
|
|
|
def test_missing_user(self):
|
|
issues = [
|
|
{"state": "open", "comments": 0, "user": None},
|
|
{"state": "open", "comments": 0}, # no user key at all
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert stats["total"] == 2
|
|
assert len(stats["user_counts"]) == 0
|
|
|
|
def test_missing_labels(self):
|
|
issues = [
|
|
{"state": "open", "comments": 0}, # no labels key
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert len(stats["label_counts"]) == 0
|
|
|
|
def test_no_reply_only_counts_open(self):
|
|
"""closed issues with 0 comments should NOT count as no_reply"""
|
|
issues = [
|
|
{"state": "closed", "comments": 0},
|
|
{"state": "open", "comments": 0},
|
|
]
|
|
stats = compute_issue_stats(issues)
|
|
assert stats["no_reply"] == 1
|
|
|
|
|
|
class TestRenderTemplatePlaceholders:
|
|
def test_replaces_all_placeholders(self):
|
|
out = render_template_placeholders(
|
|
'url: "{{BASE_URL}}/{{ORG}}/{{REPO}}/issues"',
|
|
org="tjhlp-2025f",
|
|
repo="hw01",
|
|
web_base="https://cpp.tongji.sh/QA/",
|
|
)
|
|
assert out == 'url: "https://cpp.tongji.sh/QA/tjhlp-2025f/hw01/issues"'
|
|
|
|
def test_strips_trailing_slash_of_web_base(self):
|
|
out = render_template_placeholders(
|
|
"{{BASE_URL}}/assets/x.html", "org", "repo", "http://h/QA//"
|
|
)
|
|
assert out == "http://h/QA/assets/x.html"
|
|
|
|
def test_leaves_base_url_when_not_configured(self):
|
|
# public_url 没配时应保留占位符(由 push_templates 打警告), 而不是生成坏链接
|
|
out = render_template_placeholders("{{BASE_URL}}/x", "org", "repo")
|
|
assert "{{BASE_URL}}" in out
|
|
|
|
def test_text_without_placeholders_is_unchanged(self):
|
|
text = "blank_issues_enabled: false\n"
|
|
assert render_template_placeholders(text, "org", "repo", "http://h") == text
|
|
|
|
|
|
class TestWebBaseUrl:
|
|
def _cfg(self, **kw):
|
|
base = {"url": "https://cpp.tongji.sh:3000", "token": SecretStr("x")}
|
|
return GiteaConfig(**{**base, **kw})
|
|
|
|
def test_prefers_public_url(self):
|
|
assert (
|
|
web_base_url(self._cfg(public_url="https://cpp.tongji.sh/QA/"))
|
|
== "https://cpp.tongji.sh/QA"
|
|
)
|
|
|
|
def test_falls_back_to_url(self):
|
|
assert web_base_url(self._cfg()) == "https://cpp.tongji.sh:3000"
|