8000 refactor(pytest_plugin): Remove faker dependency · vcs-python/libvcs@a3d2bba · GitHub
[go: up one dir, main page]

Skip to content

Commit a3d2bba

Browse files
committed
refactor(pytest_plugin): Remove faker dependency
1 parent 21b5fd8 commit a3d2bba

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/libvcs/pytest_plugin.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import functools
33
import getpass
44
import pathlib
5+
import random
56
import shutil
67
import textwrap
78
from typing import Any, Optional, Protocol
@@ -10,7 +11,6 @@
1011

1112
from _pytest.doctest import DoctestItem
1213
from _pytest.fixtures import SubRequest
13-
from faker import Faker
1414

1515
from libvcs._internal.run import run
1616
from libvcs.sync.git import GitRemote, GitSync
@@ -28,6 +28,22 @@
2828
)
2929

3030

31+
class RandomStrSequence:
32+
def __init__(
33+
self, characters: str = "abcdefghijklmnopqrstuvwxyz0123456789_"
34+
) -> None:
35+
self.characters: str = characters
36+
37+
def __iter__(self) -> "RandomStrSequence":
38+
return self
39+
40+
def __next__(self) -> str:
41+
return "".join(random.sample(self.characters, k=8))
42+
43+
44+
namer = RandomStrSequence()
45+
46+
3147
def pytest_ignore_collect(collection_path: pathlib.Path, config: pytest.Config) -> bool:
3248
if not shutil.which("svn") and any(
3349
needle in str(collection_path) for needle in ["svn", "subversion"]
@@ -153,16 +169,14 @@ def clean() -> None:
153169
return dir
154170

155171

156-
def unique_repo_name(
157-
faker: Faker, remote_repos_path: pathlib.Path, max_retries: int = 15
158-
) -> str:
172+
def unique_repo_name(remote_repos_path: pathlib.Path, max_retries: int = 15) -> str:
159173
attempts = 1
160174
while True:
161175
if attempts > max_retries:
162176
raise Exception(
163177
f"Could not find unused repo destination (attempts: {attempts})"
164178
)
165-
remote_repo_name: str = faker.slug()
179+
remote_repo_name: str = next(namer)
166180
suggestion = remote_repos_path / remote_repo_name
167181
if suggestion.exists():
168182
attempts += 1
@@ -202,7 +216,7 @@ def _create_git_remote_repo(
202216
@pytest.fixture
203217
@skip_if_git_missing
204218
def create_git_remote_repo(
205-
remote_repos_path: pathlib.Path, faker: Faker
219+
remote_repos_path: pathlib.Path,
206220
) -> CreateProjectCallbackFixtureProtocol:
207221
"""Factory. Create git remote repo to for clone / push purposes"""
208222

@@ -215,7 +229,7 @@ def fn(
215229
remote_repos_path=remote_repos_path,
216230
remote_repo_name=remote_repo_name
217231
if remote_repo_name is not None
218-
else unique_repo_n 8000 ame(faker=faker, remote_repos_path=remote_repos_path),
232+
else unique_repo_name(remote_repos_path=remote_repos_path),
219233
remote_repo_post_init=remote_repo_post_init,
220234
)
221235

@@ -260,7 +274,7 @@ def _create_svn_remote_repo(
260274
@pytest.fixture
261275
@skip_if_svn_missing
262276
def create_svn_remote_repo(
263-
remote_repos_path: pathlib.Path, faker: Faker
277+
remote_repos_path: pathlib.Path,
264278
) -> CreateProjectCallbackFixtureProtocol:
265279
"""Pre-made svn repo, bare, used as a file:// remote to checkout and commit to."""
266280

@@ -273,7 +287,7 @@ def fn(
273287
remote_repos_path=remote_repos_path,
274288
remote_repo_name=remote_repo_name
275289
if remote_repo_name is not None
276-
else unique_repo_name(faker=faker, remote_repos_path=remote_repos_path),
290+
else unique_repo_name(remote_repos_path=remote_repos_path),
277291
remote_repo_post_init=remote_repo_post_init,
278292
)
279293

@@ -320,7 +334,6 @@ def hg_remote_repo_single_commit_post_init(remote_repo_path: pathlib.Path) -> No
320334
@skip_if_hg_missing
321335
def create_hg_remote_repo(
322336
remote_repos_path: pathlib.Path,
323-
faker: Faker,
324337
hgconfig: pathlib.Path,
325338
set_home: pathlib.Path,
326339
) -> CreateProjectCallbackFixtureProtocol:
@@ -335,7 +348,7 @@ def fn(
335348
remote_repos_path=remote_repos_path,
336349
remote_repo_name=remote_repo_name
337350
if remote_repo_name is not None
338-
else unique_repo_name(faker=faker, remote_repos_path=remote_repos_path),
351+
else unique_repo_name(remote_repos_path=remote_repos_path),
339352
remote_repo_post_init=remote_repo_post_init,
340353
)
341354

0 commit comments

Comments
 (0)
0