summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-10-29 16:44:40 +0000
committerGerrit Code Review <review@openstack.org>2018-10-29 16:44:40 +0000
commit25ec5f76be341da13692a0d06efb58191430d7f9 (patch)
treee38094375590c1a94a4fca1a04e9417a2ab236b6
parent77201c4f337688d2a40a608de4e7356b2891f4f2 (diff)
parent2e51779d57f5def8c4ba79522e08a84e9eeb2d3a (diff)
Merge "refactor: Exchange NotADirectoryError for better exception"
-rw-r--r--doc/source/exceptions.rst5
-rw-r--r--pegleg/engine/exceptions.py7
-rw-r--r--pegleg/engine/util/git.py21
-rw-r--r--tests/unit/engine/test_site_repository.py5
-rw-r--r--tests/unit/engine/util/test_git.py8
5 files changed, 27 insertions, 19 deletions
diff --git a/doc/source/exceptions.rst b/doc/source/exceptions.rst
index 75d3bcd..f23ef1d 100644
--- a/doc/source/exceptions.rst
+++ b/doc/source/exceptions.rst
@@ -56,3 +56,8 @@ Git Exceptions
56 :members: 56 :members:
57 :show-inheritance: 57 :show-inheritance:
58 :undoc-members: 58 :undoc-members:
59
60.. autoexception:: pegleg.engine.exceptions.GitInvalidRepoException
61 :members:
62 :show-inheritance:
63 :undoc-members:
diff --git a/pegleg/engine/exceptions.py b/pegleg/engine/exceptions.py
index 808aa4e..ae5adaf 100644
--- a/pegleg/engine/exceptions.py
+++ b/pegleg/engine/exceptions.py
@@ -81,4 +81,9 @@ class GitSSHException(BaseGitException):
81 81
82class GitConfigException(BaseGitException): 82class GitConfigException(BaseGitException):
83 """Exception that occurs when reading Git repo config fails.""" 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
87class GitInvalidRepoException(BaseGitException):
88 """Exception raised when an invalid repository is detected."""
89 message = ("The repository path or URL is invalid: %(repo_path)s")
diff --git a/pegleg/engine/util/git.py b/pegleg/engine/util/git.py
index f6b0d26..d2e1feb 100644
--- a/pegleg/engine/util/git.py
+++ b/pegleg/engine/util/git.py
@@ -17,7 +17,6 @@ import os
17import tempfile 17import tempfile
18from urllib.parse import urlparse 18from urllib.parse import urlparse
19 19
20import click
21from git import exc as git_exc 20from git import exc as git_exc
22from git import Git 21from git import Git
23from git import Repo 22from git import Repo
@@ -61,7 +60,6 @@ def git_handler(repo_url,
61 path to ``repo_url``. 60 path to ``repo_url``.
62 :raises ValueError: If ``repo_url`` isn't a valid URL or doesn't begin 61 :raises ValueError: If ``repo_url`` isn't a valid URL or doesn't begin
63 with a valid protocol (http, https or ssh) for cloning. 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 else: 89 else:
92 LOG.debug('Treating repo_url=%s as an already-cloned repository. ' 90 LOG.debug('Treating repo_url=%s as an already-cloned repository. '
93 'Attempting to checkout ref=%s', repo_url, ref) 91 'Attempting to checkout ref=%s', repo_url, ref)
94 try: 92
95 # get absolute path of what is probably a directory 93 # Normalize the repo path.
96 repo_url, _ = normalize_repo_path(repo_url) 94 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)
101 95
102 repo = Repo(repo_url, search_parent_directories=True) 96 repo = Repo(repo_url, search_parent_directories=True)
103 if repo.is_dirty(untracked_files=True): 97 if repo.is_dirty(untracked_files=True):
@@ -434,7 +428,8 @@ def normalize_repo_path(repo_url_or_path):
434 :param repo_url_or_path: URL of remote Git repo or path to local Git repo. 428 :param repo_url_or_path: URL of remote Git repo or path to local Git repo.
435 :returns: Tuple of root Git path or URL, additional subpath included (e.g. 429 :returns: Tuple of root Git path or URL, additional subpath included (e.g.
436 "deployment_files") 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 repo_url_or_path = os.path.abspath(repo_url_or_path) 454 repo_url_or_path = os.path.abspath(repo_url_or_path)
460 455
461 if not repo_url_or_path or not is_repository(repo_url_or_path): 456 if not repo_url_or_path or not is_repository(repo_url_or_path):
462 raise click.ClickException( 457 msg = "The repo_path=%s is not a valid Git repo" % (orig_repo_path)
463 "Specified site repo path=%s exists but is not a valid Git " 458 LOG.error(msg)
464 "repository" % orig_repo_path) 459 raise exceptions.GitInvalidRepoException(repo_path=repo_url_or_path)
465 460
466 return repo_url_or_path, sub_path 461 return repo_url_or_path, sub_path
diff --git a/tests/unit/engine/test_site_repository.py b/tests/unit/engine/test_site_repository.py
index 7fb48ec..605f3a8 100644
--- a/tests/unit/engine/test_site_repository.py
+++ b/tests/unit/engine/test_site_repository.py
@@ -21,6 +21,7 @@ import click
21import pytest 21import pytest
22 22
23from pegleg import config 23from pegleg import config
24from pegleg.engine import exceptions
24from pegleg.engine import repository 25from pegleg.engine import repository
25from pegleg.engine import util 26from pegleg.engine import util
26 27
@@ -238,10 +239,10 @@ def test_process_repositories_with_local_site_path_exists_not_repo(*_):
238 """Validate that when the site repo already exists but isn't a repository 239 """Validate that when the site repo already exists but isn't a repository
239 that an error is raised. 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 _test_process_repositories_inner( 243 _test_process_repositories_inner(
243 expected_extra_repos=TEST_REPOSITORIES) 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
247def test_process_repositories_with_repo_username(): 248def test_process_repositories_with_repo_username():
diff --git a/tests/unit/engine/util/test_git.py b/tests/unit/engine/util/test_git.py
index e732814..e718faf 100644
--- a/tests/unit/engine/util/test_git.py
+++ b/tests/unit/engine/util/test_git.py
@@ -400,9 +400,11 @@ def test_git_clone_invalid_url_type_raises_value_error():
400@pytest.mark.skipif( 400@pytest.mark.skipif(
401 not test_utils.is_connected(), 401 not test_utils.is_connected(),
402 reason='git clone requires network connectivity.') 402 reason='git clone requires network connectivity.')
403def test_git_clone_invalid_local_repo_url_raises_notadirectory_error(): 403@mock.patch.object(git, 'is_repository', autospec=True, return_value=False)
404 url = False 404@mock.patch('os.path.exists', return_value=True, autospec=True)
405 with pytest.raises(NotADirectoryError): 405def test_git_clone_invalid_local_repo_url_raises_invalid_repo_exc(*args):
406 url = 'blah'
407 with pytest.raises(exceptions.GitInvalidRepoException):
406 git.git_handler(url, ref='master') 408 git.git_handler(url, ref='master')
407 409
408 410