1- """pytest fixtures. Live inside libvcs for doctest ."""
1+ """pytest plugin for VCS Repository testing and management ."""
22import functools
33import getpass
44import pathlib
2020
2121
2222class MaxUniqueRepoAttemptsExceeded (exc .LibVCSException ):
23+ """Raised when exceeded threshold of attempts to find a unique repo destination."""
24+
2325 def __init__ (self , attempts : int , * args : object ):
26+ """Raise LibVCSException exception with message including attempts tried."""
2427 return super ().__init__ (
2528 f"Could not find unused repo destination (attempts: { attempts } )"
2629 )
@@ -38,22 +41,27 @@ def __init__(self, attempts: int, *args: object):
3841
3942
4043class RandomStrSequence :
44+ """Create a random string sequence."""
45+
4146 def __init__ (
4247 self , characters : str = "abcdefghijklmnopqrstuvwxyz0123456789_"
4348 ) -> None :
4449 self .characters : str = characters
4550
4651 def __iter__ (self ) -> "RandomStrSequence" :
52+ """Iterate across generated strings."""
4753 return self
4854
4955 def __next__ (self ) -> str :
56+ """Iterate to next string possibility."""
5057 return "" .join (random .sample (self .characters , k = 8 ))
5158
5259
5360namer = RandomStrSequence ()
5461
5562
5663def pytest_ignore_collect (collection_path : pathlib .Path , config : pytest .Config ) -> bool :
64+ """Skip tests if VCS binaries are missing."""
5765 if not shutil .which ("svn" ) and any (
5866 needle in str (collection_path ) for needle in ["svn" , "subversion" ]
5967 ):
@@ -70,17 +78,19 @@ def pytest_ignore_collect(collection_path: pathlib.Path, config: pytest.Config)
7078
7179@pytest .fixture (scope = "session" )
7280def home_path (tmp_path_factory : pytest .TempPathFactory ) -> pathlib .Path :
81+ """Return temporary directory to use as user's home path, pytest fixture."""
7382 return tmp_path_factory .mktemp ("home" )
7483
7584
7685@pytest .fixture (scope = "session" )
7786def home_user_name () -> str :
78- """Default username to set for :func:`user_path` fixture."""
87+ """Return default username to set for :func:`user_path` fixture."""
7988 return getpass .getuser ()
8089
8190
8291@pytest .fixture (scope = "session" )
8392def user_path (home_path : pathlib .Path , home_user_name : str ) -> pathlib .Path :
93+ """Return user's home directory, pytest fixture."""
8494 p = home_path / home_user_name
8595 p .mkdir ()
8696 return p
@@ -91,12 +101,14 @@ def set_home(
91101 monkeypatch : pytest .MonkeyPatch ,
92102 user_path : pathlib .Path ,
93103) -> None :
104+ """Set home directory, pytest fixture."""
94105 monkeypatch .setenv ("HOME" , str (user_path ))
95106
96107
97108@pytest .fixture
98109@skip_if_git_missing
99110def gitconfig (user_path : pathlib .Path , set_home : pathlib .Path ) -> pathlib .Path :
111+ """Return git configuration, pytest fixture."""
100112 gitconfig = user_path / ".gitconfig"
101113 user_email = "libvcs@git-pull.com"
102114 gitconfig .write_text (
@@ -131,6 +143,7 @@ def gitconfig(user_path: pathlib.Path, set_home: pathlib.Path) -> pathlib.Path:
131143@pytest .fixture
132144@skip_if_hg_missing
133145def hgconfig (user_path : pathlib .Path , set_home : pathlib .Path ) -> pathlib .Path :
146+ """Return Mercurial configuration, pytest fixture."""
134147 hgrc = user_path / ".hgrc"
135148 hgrc .write_text (
136149 textwrap .dedent (
@@ -179,6 +192,7 @@ def clean() -> None:
179192
180193
181194def unique_repo_name (remote_repos_path : pathlib .Path , max_retries : int = 15 ) -> str :
195+ """Attempt to find and return a unique repo named based on path."""
182196 attempts = 1
183197 while True :
184198 if attempts > max_retries :
@@ -195,18 +209,24 @@ def unique_repo_name(remote_repos_path: pathlib.Path, max_retries: int = 15) ->
195209
196210
197211class CreateRepoPostInitFn (Protocol ):
212+ """Typing for VCS repo creation callback."""
213+
198214 def __call__ (self , remote_repo_path : pathlib .Path ) -> None :
215+ """Ran after creating a repo from pytest fixture."""
199216 ...
200217
201218
202219class CreateRepoPytestFixtureFn (Protocol ):
220+ """Typing for VCS pytest fixture callback."""
221+
203222 def __call__ (
204223 self ,
205224 remote_repos_path : pathlib .Path = ...,
206225 remote_repo_name : Optional [str ] = ...,
207226 remote_repo_post_init : Optional [CreateRepoPostInitFn ] = ...,
208227 init_cmd_args : InitCmdArgs = ...,
209228 ) -> pathlib .Path :
229+ """py.test fixture function to create a project in a remote repo."""
210230 ...
211231
212232
@@ -235,7 +255,7 @@ def _create_git_remote_repo(
235255def create_git_remote_repo (
236256 remote_repos_path : pathlib .Path ,
237257) -> CreateRepoPytestFixtureFn :
238- """Factory. Create git remote repo to for clone / push purposes."""
258+ """Return factory to create git remote repo to for clone / push purposes."""
239259
240260 def fn (
241261 remote_repos_path : pathlib .Path = remote_repos_path ,
@@ -256,6 +276,7 @@ def fn(
256276
257277
258278def git_remote_repo_single_commit_post_init (remote_repo_path : pathlib .Path ) -> None :
279+ """Post-initialization: Create a test git repo with a single commit."""
259280 testfile_filename = "testfile.test"
260281 run (["touch" , testfile_filename ], cwd = remote_repo_path )
261282 run (["git" , "add" , testfile_filename ], cwd = remote_repo_path )
@@ -298,6 +319,7 @@ def _create_svn_remote_repo(
298319
299320
300321def svn_remote_repo_single_commit_post_init (remote_repo_path : pathlib .Path ) -> None :
322+ """Post-initialization: Create a test SVN repo with a single commit."""
301323 assert remote_repo_path .exists ()
302324 repo_dumpfile = pathlib .Path (__file__ ).parent / "data" / "repotest.dump"
303325 run (
@@ -372,6 +394,7 @@ def _create_hg_remote_repo(
372394
373395
374396def hg_remote_repo_single_commit_post_init (remote_repo_path : pathlib .Path ) -> None :
397+ """Post-initialization: Create a test mercurial repo with a single commit."""
375398 testfile_filename = "testfile.test"
376399 run (["touch" , testfile_filename ], cwd = remote_repo_path )
377400 run (["hg" , "add" , testfile_filename ], cwd = remote_repo_path )
@@ -470,6 +493,7 @@ def add_doctest_fixtures(
470493 create_hg_remote_repo : CreateRepoPytestFixtureFn ,
471494 git_repo : pathlib .Path ,
472495) -> None :
496+ """Harness pytest fixtures to pytest's doctest namespace."""
473497 from _pytest .doctest import DoctestItem
474498
475499 if not isinstance (request ._pyfuncitem , DoctestItem ): # Only run on doctest items
0 commit comments