Browse Source

Merge "refactor: Exchange NotADirectoryError for better exception"

Zuul 5 months ago
parent
commit
25ec5f76be

+ 5
- 0
doc/source/exceptions.rst View File

@@ -56,3 +56,8 @@ Git Exceptions
56 56
    :members:
57 57
    :show-inheritance:
58 58
    :undoc-members:
59
+
60
+.. autoexception:: pegleg.engine.exceptions.GitInvalidRepoException
61
+   :members:
62
+   :show-inheritance:
63
+   :undoc-members:

+ 6
- 1
pegleg/engine/exceptions.py View File

@@ -81,4 +81,9 @@ class GitSSHException(BaseGitException):
81 81
 
82 82
 class GitConfigException(BaseGitException):
83 83
     """Exception that occurs when reading Git repo config fails."""
84
-    message = ("Failed to read Git config file for repo_url=%(repo_url)s")
84
+    message = ("Failed to read Git config file for repo path: %(repo_path)s")
85
+
86
+
87
+class GitInvalidRepoException(BaseGitException):
88
+    """Exception raised when an invalid repository is detected."""
89
+    message = ("The repository path or URL is invalid: %(repo_path)s")

+ 8
- 13
pegleg/engine/util/git.py View File

@@ -17,7 +17,6 @@ import os
17 17
 import tempfile
18 18
 from urllib.parse import urlparse
19 19
 
20
-import click
21 20
 from git import exc as git_exc
22 21
 from git import Git
23 22
 from git import Repo
@@ -61,7 +60,6 @@ def git_handler(repo_url,
61 60
         path to ``repo_url``.
62 61
     :raises ValueError: If ``repo_url`` isn't a valid URL or doesn't begin
63 62
         with a valid protocol (http, https or ssh) for cloning.
64
-    :raises NotADirectoryError: If ``repo_url`` isn't a valid directory path.
65 63
 
66 64
     """
67 65
 
@@ -91,13 +89,9 @@ def git_handler(repo_url,
91 89
     else:
92 90
         LOG.debug('Treating repo_url=%s as an already-cloned repository. '
93 91
                   'Attempting to checkout ref=%s', repo_url, ref)
94
-        try:
95
-            # get absolute path of what is probably a directory
96
-            repo_url, _ = normalize_repo_path(repo_url)
97
-        except Exception:
98
-            msg = "The repo_url=%s is not a valid Git repo" % repo_url
99
-            LOG.error(msg)
100
-            raise NotADirectoryError(msg)
92
+
93
+        # Normalize the repo path.
94
+        repo_url, _ = normalize_repo_path(repo_url)
101 95
 
102 96
         repo = Repo(repo_url, search_parent_directories=True)
103 97
         if repo.is_dirty(untracked_files=True):
@@ -434,7 +428,8 @@ def normalize_repo_path(repo_url_or_path):
434 428
     :param repo_url_or_path: URL of remote Git repo or path to local Git repo.
435 429
     :returns: Tuple of root Git path or URL, additional subpath included (e.g.
436 430
         "deployment_files")
437
-    :rtype: tuple
431
+    :rtype: tuple[str, str]
432
+    :raises GitInvalidRepoException: If the repo is invalid.
438 433
 
439 434
     """
440 435
 
@@ -459,8 +454,8 @@ def normalize_repo_path(repo_url_or_path):
459 454
             repo_url_or_path = os.path.abspath(repo_url_or_path)
460 455
 
461 456
     if not repo_url_or_path or not is_repository(repo_url_or_path):
462
-        raise click.ClickException(
463
-            "Specified site repo path=%s exists but is not a valid Git "
464
-            "repository" % orig_repo_path)
457
+        msg = "The repo_path=%s is not a valid Git repo" % (orig_repo_path)
458
+        LOG.error(msg)
459
+        raise exceptions.GitInvalidRepoException(repo_path=repo_url_or_path)
465 460
 
466 461
     return repo_url_or_path, sub_path

+ 3
- 2
tests/unit/engine/test_site_repository.py View File

@@ -21,6 +21,7 @@ import click
21 21
 import pytest
22 22
 
23 23
 from pegleg import config
24
+from pegleg.engine import exceptions
24 25
 from pegleg.engine import repository
25 26
 from pegleg.engine import util
26 27
 
@@ -238,10 +239,10 @@ def test_process_repositories_with_local_site_path_exists_not_repo(*_):
238 239
     """Validate that when the site repo already exists but isn't a repository
239 240
     that an error is raised.
240 241
     """
241
-    with pytest.raises(click.ClickException) as exc:
242
+    with pytest.raises(exceptions.GitInvalidRepoException) as exc:
242 243
         _test_process_repositories_inner(
243 244
             expected_extra_repos=TEST_REPOSITORIES)
244
-    assert "is not a valid Git repository" in str(exc.value)
245
+    assert "The repository path or URL is invalid" in str(exc.value)
245 246
 
246 247
 
247 248
 def test_process_repositories_with_repo_username():

+ 5
- 3
tests/unit/engine/util/test_git.py View File

@@ -400,9 +400,11 @@ def test_git_clone_invalid_url_type_raises_value_error():
400 400
 @pytest.mark.skipif(
401 401
     not test_utils.is_connected(),
402 402
     reason='git clone requires network connectivity.')
403
-def test_git_clone_invalid_local_repo_url_raises_notadirectory_error():
404
-    url = False
405
-    with pytest.raises(NotADirectoryError):
403
+@mock.patch.object(git, 'is_repository', autospec=True, return_value=False)
404
+@mock.patch('os.path.exists', return_value=True, autospec=True)
405
+def test_git_clone_invalid_local_repo_url_raises_invalid_repo_exc(*args):
406
+    url = 'blah'
407
+    with pytest.raises(exceptions.GitInvalidRepoException):
406 408
         git.git_handler(url, ref='master')
407 409
 
408 410
 

Loading…
Cancel
Save