Skip to content

Commit 8046a6b

Browse files
committed
docs: added docs for test files
1 parent 193c3c9 commit 8046a6b

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def sample_query() -> dict[str, Any]:
2525

2626
@pytest.fixture
2727
def temp_directory(tmp_path: Path) -> Path:
28+
"""
2829
# Creates the following structure:
2930
# test_repo/
3031
# ├── file1.txt
@@ -39,6 +40,7 @@ def temp_directory(tmp_path: Path) -> Path:
3940
# | └── file_dir1.txt
4041
# └── dir2/
4142
# └── file_dir2.txt
43+
"""
4244

4345
test_dir = tmp_path / "test_repo"
4446
test_dir.mkdir()

tests/test_clone.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
@pytest.mark.asyncio
1111
async def test_clone_repo_with_commit() -> None:
12+
"""
13+
Test the `clone_repo` function when a specific commit hash is provided.
14+
Verifies that the repository is cloned and checked out to the specified commit.
15+
"""
1216
clone_config = CloneConfig(
1317
url="https://github.com/user/repo",
1418
local_path="/tmp/repo",
@@ -28,6 +32,10 @@ async def test_clone_repo_with_commit() -> None:
2832

2933
@pytest.mark.asyncio
3034
async def test_clone_repo_without_commit() -> None:
35+
"""
36+
Test the `clone_repo` function when no commit hash is provided.
37+
Verifies that only the repository clone operation is performed.
38+
"""
3139
query = CloneConfig(url="https://github.com/user/repo", local_path="/tmp/repo", commit=None, branch="main")
3240

3341
with patch("gitingest.clone._check_repo_exists", return_value=True) as mock_check:
@@ -43,6 +51,10 @@ async def test_clone_repo_without_commit() -> None:
4351

4452
@pytest.mark.asyncio
4553
async def test_clone_repo_nonexistent_repository() -> None:
54+
"""
55+
Test the `clone_repo` function when the repository does not exist.
56+
Verifies that a ValueError is raised with an appropriate error message.
57+
"""
4658
clone_config = CloneConfig(
4759
url="https://github.com/user/nonexistent-repo",
4860
local_path="/tmp/repo",
@@ -57,6 +69,10 @@ async def test_clone_repo_nonexistent_repository() -> None:
5769

5870
@pytest.mark.asyncio
5971
async def test_check_repo_exists() -> None:
72+
"""
73+
Test the `_check_repo_exists` function to verify if a repository exists.
74+
Covers cases for existing repositories, non-existing repositories (404), and failed requests.
75+
"""
6076
url = "https://github.com/user/repo"
6177

6278
with patch("asyncio.create_subprocess_exec", new_callable=AsyncMock) as mock_exec:
@@ -80,6 +96,10 @@ async def test_check_repo_exists() -> None:
8096

8197
@pytest.mark.asyncio
8298
async def test_clone_repo_invalid_url() -> None:
99+
"""
100+
Test the `clone_repo` function when an invalid or empty URL is provided.
101+
Verifies that a ValueError is raised with an appropriate error message.
102+
"""
83103
clone_config = CloneConfig(
84104
url="",
85105
local_path="/tmp/repo",
@@ -90,6 +110,10 @@ async def test_clone_repo_invalid_url() -> None:
90110

91111
@pytest.mark.asyncio
92112
async def test_clone_repo_invalid_local_path() -> None:
113+
"""
114+
Test the `clone_repo` function when an invalid or empty local path is provided.
115+
Verifies that a ValueError is raised with an appropriate error message.
116+
"""
93117
clone_config = CloneConfig(
94118
url="https://github.com/user/repo",
95119
local_path="",
@@ -100,6 +124,10 @@ async def test_clone_repo_invalid_local_path() -> None:
100124

101125
@pytest.mark.asyncio
102126
async def test_clone_repo_with_custom_branch() -> None:
127+
"""
128+
Test the `clone_repo` function when a custom branch is specified.
129+
Verifies that the repository is cloned with the specified branch using a shallow clone.
130+
"""
103131
clone_config = CloneConfig(
104132
url="https://github.com/user/repo",
105133
local_path="/tmp/repo",
@@ -122,6 +150,10 @@ async def test_clone_repo_with_custom_branch() -> None:
122150

123151
@pytest.mark.asyncio
124152
async def test_git_command_failure() -> None:
153+
"""
154+
Test the `clone_repo` function when a Git command fails during execution.
155+
Verifies that a RuntimeError is raised with an appropriate error message.
156+
"""
125157
clone_config = CloneConfig(
126158
url="https://github.com/user/repo",
127159
local_path="/tmp/repo",
@@ -134,6 +166,10 @@ async def test_git_command_failure() -> None:
134166

135167
@pytest.mark.asyncio
136168
async def test_clone_repo_default_shallow_clone() -> None:
169+
"""
170+
Test the `clone_repo` function with default shallow clone behavior.
171+
Verifies that the repository is cloned with `--depth=1` and `--single-branch` options.
172+
"""
137173
clone_config = CloneConfig(
138174
url="https://github.com/user/repo",
139175
local_path="/tmp/repo",
@@ -148,6 +184,10 @@ async def test_clone_repo_default_shallow_clone() -> None:
148184

149185
@pytest.mark.asyncio
150186
async def test_clone_repo_commit_without_branch() -> None:
187+
"""
188+
Test the `clone_repo` function when a commit hash is provided but no branch is specified.
189+
Verifies that the repository is cloned and checked out to the specified commit.
190+
"""
151191
clone_config = CloneConfig(
152192
url="https://github.com/user/repo",
153193
local_path="/tmp/repo",
@@ -163,6 +203,10 @@ async def test_clone_repo_commit_without_branch() -> None:
163203

164204
@pytest.mark.asyncio
165205
async def test_check_repo_exists_with_redirect() -> None:
206+
"""
207+
Test the `_check_repo_exists` function for handling HTTP redirects (302 Found).
208+
Verifies that it correctly identifies the repository's existence.
209+
"""
166210
url = "https://github.com/user/repo"
167211
with patch("asyncio.create_subprocess_exec", new_callable=AsyncMock) as mock_exec:
168212
mock_process = AsyncMock()

tests/test_parse_query.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88

99
def test_parse_url_valid_https() -> None:
10+
"""
11+
Test `_parse_url` with valid HTTPS URLs from supported platforms (GitHub, GitLab, Bitbucket).
12+
Verifies that user and repository names are correctly extracted.
13+
"""
1014
test_cases = [
1115
"https://github.com/user/repo",
1216
"https://gitlab.com/user/repo",
@@ -20,6 +24,10 @@ def test_parse_url_valid_https() -> None:
2024

2125

2226
def test_parse_url_valid_http() -> None:
27+
"""
28+
Test `_parse_url` with valid HTTP URLs from supported platforms.
29+
Verifies that user and repository names, as well as the slug, are correctly extracted.
30+
"""
2331
test_cases = [
2432
"http://github.com/user/repo",
2533
"http://gitlab.com/user/repo",
@@ -33,12 +41,20 @@ def test_parse_url_valid_http() -> None:
3341

3442

3543
def test_parse_url_invalid() -> None:
44+
"""
45+
Test `_parse_url` with an invalid URL that does not include a repository structure.
46+
Verifies that a ValueError is raised with an appropriate error message.
47+
"""
3648
url = "https://only-domain.com"
3749
with pytest.raises(ValueError, match="Invalid repository URL"):
3850
_parse_url(url)
3951

4052

4153
def test_parse_query_basic() -> None:
54+
"""
55+
Test `parse_query` with basic inputs including valid repository URLs.
56+
Verifies that user and repository names, URL, and ignore patterns are correctly parsed.
57+
"""
4258
test_cases = ["https://github.com/user/repo", "https://gitlab.com/user/repo"]
4359
for url in test_cases:
4460
result = parse_query(url, max_file_size=50, from_web=True, ignore_patterns="*.txt")
@@ -49,19 +65,31 @@ def test_parse_query_basic() -> None:
4965

5066

5167
def test_parse_query_include_pattern() -> None:
68+
"""
69+
Test `parse_query` with an include pattern.
70+
Verifies that the include pattern is set correctly and default ignore patterns are applied.
71+
"""
5272
url = "https://github.com/user/repo"
5373
result = parse_query(url, max_file_size=50, from_web=True, include_patterns="*.py")
5474
assert result["include_patterns"] == ["*.py"]
5575
assert set(result["ignore_patterns"]) == set(DEFAULT_IGNORE_PATTERNS)
5676

5777

5878
def test_parse_query_invalid_pattern() -> None:
79+
"""
80+
Test `parse_query` with an invalid pattern containing special characters.
81+
Verifies that a ValueError is raised with an appropriate error message.
82+
"""
5983
url = "https://github.com/user/repo"
6084
with pytest.raises(ValueError, match="Pattern.*contains invalid characters"):
6185
parse_query(url, max_file_size=50, from_web=True, include_patterns="*.py;rm -rf")
6286

6387

6488
def test_parse_url_with_subpaths() -> None:
89+
"""
90+
Test `_parse_url` with a URL containing a branch and subpath.
91+
Verifies that user name, repository name, branch, and subpath are correctly extracted.
92+
"""
6593
url = "https://github.com/user/repo/tree/main/subdir/file"
6694
result = _parse_url(url)
6795
assert result["user_name"] == "user"
@@ -71,38 +99,62 @@ def test_parse_url_with_subpaths() -> None:
7199

72100

73101
def test_parse_url_invalid_repo_structure() -> None:
102+
"""
103+
Test `_parse_url` with an invalid repository structure in the URL.
104+
Verifies that a ValueError is raised with an appropriate error message.
105+
"""
74106
url = "https://github.com/user"
75107
with pytest.raises(ValueError, match="Invalid repository URL"):
76108
_parse_url(url)
77109

78110

79111
def test_parse_patterns_valid() -> None:
112+
"""
113+
Test `_parse_patterns` with valid patterns separated by commas.
114+
Verifies that the patterns are correctly parsed into a list.
115+
"""
80116
patterns = "*.py, *.md, docs/*"
81117
result = _parse_patterns(patterns)
82118
assert result == ["*.py", "*.md", "docs/*"]
83119

84120

85121
def test_parse_patterns_invalid_characters() -> None:
122+
"""
123+
Test `_parse_patterns` with invalid patterns containing special characters.
124+
Verifies that a ValueError is raised with an appropriate error message.
125+
"""
86126
patterns = "*.py;rm -rf"
87127
with pytest.raises(ValueError, match="Pattern.*contains invalid characters"):
88128
_parse_patterns(patterns)
89129

90130

91131
def test_parse_query_with_large_file_size() -> None:
132+
"""
133+
Test `parse_query` with a very large file size limit.
134+
Verifies that the file size limit and default ignore patterns are set correctly.
135+
"""
92136
url = "https://github.com/user/repo"
93137
result = parse_query(url, max_file_size=10**9, from_web=True)
94138
assert result["max_file_size"] == 10**9
95139
assert result["ignore_patterns"] == DEFAULT_IGNORE_PATTERNS
96140

97141

98142
def test_parse_query_empty_patterns() -> None:
143+
"""
144+
Test `parse_query` with empty include and ignore patterns.
145+
Verifies that the include patterns are set to None and default ignore patterns are applied.
146+
"""
99147
url = "https://github.com/user/repo"
100148
result = parse_query(url, max_file_size=50, from_web=True, include_patterns="", ignore_patterns="")
101149
assert result["include_patterns"] is None
102150
assert result["ignore_patterns"] == DEFAULT_IGNORE_PATTERNS
103151

104152

105153
def test_parse_query_include_and_ignore_overlap() -> None:
154+
"""
155+
Test `parse_query` with overlapping include and ignore patterns.
156+
Verifies that overlapping patterns are removed from the ignore patterns.
157+
"""
106158
url = "https://github.com/user/repo"
107159
result = parse_query(
108160
url,
@@ -117,6 +169,10 @@ def test_parse_query_include_and_ignore_overlap() -> None:
117169

118170

119171
def test_parse_query_local_path() -> None:
172+
"""
173+
Test `parse_query` with a local file path.
174+
Verifies that the local path is set, a unique ID is generated, and the slug is correctly created.
175+
"""
120176
path = "/home/user/project"
121177
result = parse_query(path, max_file_size=100, from_web=False)
122178
assert result["local_path"] == "/home/user/project"
@@ -125,18 +181,30 @@ def test_parse_query_local_path() -> None:
125181

126182

127183
def test_parse_query_relative_path() -> None:
184+
"""
185+
Test `parse_query` with a relative file path.
186+
Verifies that the local path and slug are correctly resolved.
187+
"""
128188
path = "./project"
129189
result = parse_query(path, max_file_size=100, from_web=False)
130190
assert result["local_path"].endswith("project")
131191
assert result["slug"].endswith("project")
132192

133193

134194
def test_parse_query_empty_source() -> None:
195+
"""
196+
Test `parse_query` with an empty source input.
197+
Verifies that a ValueError is raised with an appropriate error message.
198+
"""
135199
with pytest.raises(ValueError, match="Invalid repository URL"):
136200
parse_query("", max_file_size=100, from_web=True)
137201

138202

139203
def test_parse_url_branch_and_commit_distinction() -> None:
204+
"""
205+
Test `_parse_url` with URLs containing either a branch name or a commit hash.
206+
Verifies that the branch and commit are correctly distinguished.
207+
"""
140208
url_branch = "https://github.com/user/repo/tree/main"
141209
url_commit = "https://github.com/user/repo/tree/abcd1234abcd1234abcd1234abcd1234abcd1234"
142210

@@ -151,13 +219,20 @@ def test_parse_url_branch_and_commit_distinction() -> None:
151219

152220

153221
def test_parse_query_uuid_uniqueness() -> None:
222+
"""
223+
Test `parse_query` to ensure that each call generates a unique UUID for the query result.
224+
"""
154225
path = "/home/user/project"
155226
result1 = parse_query(path, max_file_size=100, from_web=False)
156227
result2 = parse_query(path, max_file_size=100, from_web=False)
157228
assert result1["id"] != result2["id"]
158229

159230

160231
def test_parse_url_with_query_and_fragment() -> None:
232+
"""
233+
Test `_parse_url` with a URL containing query parameters and a fragment.
234+
Verifies that the URL is cleaned and other fields are correctly extracted.
235+
"""
161236
url = "https://github.com/user/repo?arg=value#fragment"
162237
result = _parse_url(url)
163238
assert result["user_name"] == "user"

0 commit comments

Comments
 (0)