Skip to content

Commit 06fdc1f

Browse files
committed
!squash tests(feat[property]): Add property-based testing for configuration
1 parent 6fa5286 commit 06fdc1f

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

tests/unit/config/test_models_property.py

+38-13
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88
from __future__ import annotations
99

10-
import os
1110
import pathlib
1211
import typing as t
1312

1413
import hypothesis.strategies as st
15-
import pytest
16-
from hypothesis import given, settings
14+
from hypothesis import given
1715

1816
from vcspull.config.models import Repository, Settings, VCSPullConfig
1917

@@ -75,7 +73,9 @@ def valid_path_strategy(draw: t.Callable[[st.SearchStrategy[t.Any]], t.Any]) ->
7573

7674

7775
@st.composite
78-
def repository_strategy(draw: t.Callable[[st.SearchStrategy[t.Any]], t.Any]) -> Repository:
76+
def repository_strategy(
77+
draw: t.Callable[[st.SearchStrategy[t.Any]], t.Any],
78+
) -> Repository:
7979
"""Generate valid Repository instances."""
8080
name = draw(st.one_of(st.none(), st.text(min_size=1, max_size=20)))
8181
url = draw(valid_url_strategy())
@@ -142,7 +142,7 @@ def settings_strategy(draw: t.Callable[[st.SearchStrategy[t.Any]], t.Any]) -> Se
142142

143143
@st.composite
144144
def vcspull_config_strategy(
145-
draw: t.Callable[[st.SearchStrategy[t.Any]], t.Any]
145+
draw: t.Callable[[st.SearchStrategy[t.Any]], t.Any],
146146
) -> VCSPullConfig:
147147
"""Generate valid VCSPullConfig instances."""
148148
settings = draw(settings_strategy())
@@ -175,30 +175,31 @@ def test_repository_construction(self, repository: Repository) -> None:
175175
# Check computed fields
176176
if repository.name is None:
177177
# Name should be derived from URL if not explicitly set
178-
assert repository.get_name() != ""
178+
repo_name = extract_name_from_url(repository.url)
179+
assert repo_name != ""
179180

180181
@given(url=valid_url_strategy())
181182
def test_repository_name_extraction(self, url: str) -> None:
182183
"""Test Repository can extract names from URLs."""
183-
repo = Repository(url=url, path="/tmp/repo")
184-
# Should be able to extract a name from any valid URL
185-
assert repo.get_name() != ""
184+
# No need to create a repo instance for this test
185+
repo_name = extract_name_from_url(url)
186+
assert repo_name != ""
186187
# The name shouldn't contain protocol or domain parts
187-
assert "://" not in repo.get_name()
188-
assert "github.com" not in repo.get_name()
188+
assert "://" not in repo_name
189+
assert "github.com" not in repo_name
189190

190191
@given(repository=repository_strategy())
191192
def test_repository_path_expansion(self, repository: Repository) -> None:
192193
"""Test path expansion in Repository model."""
193194
# Get the expanded path
194-
expanded_path = repository.get_path()
195+
expanded_path = pathlib.Path(repository.path)
195196

196197
# Check for tilde expansion
197198
assert "~" not in str(expanded_path)
198199

199200
# If original path started with ~, expanded should be absolute
200201
if repository.path.startswith("~"):
201-
assert os.path.isabs(expanded_path)
202+
assert expanded_path.is_absolute()
202203

203204

204205
class TestSettingsModel:
@@ -244,3 +245,27 @@ def test_config_with_multiple_repositories(
244245
assert repo1 in config.repositories
245246
assert repo2 in config.repositories
246247
assert repo3 in config.repositories
248+
249+
250+
def extract_name_from_url(url: str) -> str:
251+
"""Extract repository name from URL.
252+
253+
Parameters
254+
----------
255+
url : str
256+
Repository URL
257+
258+
Returns
259+
-------
260+
str
261+
Repository name
262+
"""
263+
# Extract the last part of the URL path
264+
parts = url.rstrip("/").split("/")
265+
name = parts[-1]
266+
267+
# Remove .git suffix if present
268+
if name.endswith(".git"):
269+
name = name[:-4]
270+
271+
return name

0 commit comments

Comments
 (0)