forked from volcengine/OpenViking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_code_hosting_utils.py
More file actions
190 lines (112 loc) · 5.69 KB
/
test_code_hosting_utils.py
File metadata and controls
190 lines (112 loc) · 5.69 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
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
"""Tests for code_hosting_utils git SSH URL support (Issue #317)."""
import importlib
import importlib.util
import sys
from pathlib import Path
from types import ModuleType, SimpleNamespace
from unittest.mock import patch
import pytest
def _mock_config():
return SimpleNamespace(
code=SimpleNamespace(
github_domains=["github.com", "www.github.com"],
gitlab_domains=["gitlab.com", "www.gitlab.com"],
code_hosting_domains=["github.com", "gitlab.com"],
)
)
# Ensure openviking_cli.utils.config is importable (stub if needed)
_config_mod_name = "openviking_cli.utils.config"
try:
importlib.import_module(_config_mod_name)
except Exception:
for mod_name in ("openviking_cli", "openviking_cli.utils", _config_mod_name):
if mod_name not in sys.modules:
m = ModuleType(mod_name)
sys.modules[mod_name] = m
sys.modules[_config_mod_name].get_openviking_config = _mock_config # type: ignore[attr-defined]
# Load code_hosting_utils directly from file to avoid the heavy openviking/__init__.py chain
_module_path = (
Path(__file__).resolve().parents[1] / "openviking" / "utils" / "code_hosting_utils.py"
)
_spec = importlib.util.spec_from_file_location("openviking.utils.code_hosting_utils", _module_path)
_module = importlib.util.module_from_spec(_spec)
sys.modules["openviking.utils.code_hosting_utils"] = _module
_spec.loader.exec_module(_module)
parse_code_hosting_url = _module.parse_code_hosting_url
is_github_url = _module.is_github_url
is_gitlab_url = _module.is_gitlab_url
is_code_hosting_url = _module.is_code_hosting_url
is_git_repo_url = _module.is_git_repo_url
validate_git_ssh_uri = _module.validate_git_ssh_uri
@pytest.fixture(autouse=True)
def _patch_config():
with patch.object(_module, "get_openviking_config", side_effect=_mock_config):
yield
# --- parse_code_hosting_url ---
def test_parse_code_hosting_url_git_ssh():
assert parse_code_hosting_url("git@github.com:org/repo.git") == "org/repo"
def test_parse_code_hosting_url_git_ssh_no_dotgit():
assert parse_code_hosting_url("git@github.com:org/repo") == "org/repo"
def test_parse_code_hosting_url_git_ssh_unknown_host():
assert parse_code_hosting_url("git@unknown.com:org/repo.git") is None
def test_parse_code_hosting_url_git_ssh_single_segment():
assert parse_code_hosting_url("git@github.com:repo") is None
def test_parse_code_hosting_url_https():
assert parse_code_hosting_url("https://github.com/org/repo") == "org/repo"
def test_parse_code_hosting_url_https_dotgit():
assert parse_code_hosting_url("https://github.com/org/repo.git") == "org/repo"
def test_parse_code_hosting_url_ssh_url_with_userinfo():
assert parse_code_hosting_url("ssh://git@github.com/org/repo.git") == "org/repo"
def test_parse_code_hosting_url_gitlab_ssh_url_with_userinfo():
assert parse_code_hosting_url("ssh://git@gitlab.com/group/repo.git") == "group/repo"
def test_parse_code_hosting_url_https_with_port():
assert parse_code_hosting_url("https://github.com:443/org/repo") == "org/repo"
# --- validate_git_ssh_uri ---
def test_validate_git_ssh_uri_valid():
validate_git_ssh_uri("git@github.com:org/repo.git") # should not raise
def test_validate_git_ssh_uri_not_git():
with pytest.raises(ValueError, match="Not a git@ SSH URI"):
validate_git_ssh_uri("https://github.com/org/repo")
def test_validate_git_ssh_uri_no_colon():
with pytest.raises(ValueError, match="missing colon or empty path"):
validate_git_ssh_uri("git@github.com")
def test_validate_git_ssh_uri_empty_path():
with pytest.raises(ValueError, match="missing colon or empty path"):
validate_git_ssh_uri("git@github.com:")
# --- is_code_hosting_url ---
def test_is_code_hosting_url_git_ssh():
assert is_code_hosting_url("git@github.com:org/repo.git") is True
def test_is_code_hosting_url_git_ssh_no_colon():
assert is_code_hosting_url("git@github.com") is False
def test_is_code_hosting_url_https():
assert is_code_hosting_url("https://github.com/org/repo") is True
def test_is_code_hosting_url_ssh_url_with_userinfo():
assert is_code_hosting_url("ssh://git@github.com/org/repo.git") is True
def test_is_code_hosting_url_https_with_port():
assert is_code_hosting_url("https://github.com:443/org/repo") is True
# --- is_github_url / is_gitlab_url ---
def test_is_github_url_ssh_url_with_userinfo():
assert is_github_url("ssh://git@github.com/org/repo.git") is True
def test_is_gitlab_url_ssh_url_with_userinfo():
assert is_gitlab_url("ssh://git@gitlab.com/group/repo.git") is True
# --- is_git_repo_url ---
def test_is_git_repo_url_git_ssh():
assert is_git_repo_url("git@github.com:org/repo.git") is True
def test_is_git_repo_url_https_repo():
assert is_git_repo_url("https://github.com/org/repo") is True
def test_is_git_repo_url_ssh_url_with_userinfo():
assert is_git_repo_url("ssh://git@github.com/org/repo.git") is True
def test_is_git_repo_url_https_with_port():
assert is_git_repo_url("https://github.com:443/org/repo") is True
def test_is_git_repo_url_https_issues():
assert is_git_repo_url("https://github.com/org/repo/issues/123") is False
def test_is_git_repo_url_https_pull():
assert is_git_repo_url("https://github.com/org/repo/pull/456") is False
def test_is_git_repo_url_https_blob():
assert is_git_repo_url("https://github.com/org/repo/blob/main/file.py") is False
def test_is_git_repo_url_unknown_domain():
assert is_git_repo_url("https://example.com/org/repo") is False
def test_is_git_repo_url_single_segment():
assert is_git_repo_url("https://github.com/org") is False