Skip to content

Commit

Permalink
Fix: support artifactory.exception.ArtifactoryException (#73)
Browse files Browse the repository at this point in the history
* Fix new artifactory.exception.ArtifactoryException

* Fix error

* Require audfactory>=1.0.12

* Add failing test

* Update code

* Cleanup comments

* Fix no-access ficture
  • Loading branch information
hagenw authored Feb 17, 2023
1 parent 0f398ee commit bdd2e8c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
30 changes: 16 additions & 14 deletions audbackend/core/artifactory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dohq_artifactory
import requests
import typing

Expand Down Expand Up @@ -54,12 +55,8 @@ def _exists(
) -> bool:
r"""Check if file exists on backend."""
try:
# Can lead to
# RuntimeError: 404 page not found
# or
# requests.exceptions.HTTPError: 403 Client Error
return audfactory.path(path).exists()
except self._non_existing_path_error: # pragma: nocover
except self._non_existing_path_error:
return False

def _get_file(
Expand Down Expand Up @@ -89,7 +86,7 @@ def _glob(
path = audfactory.path(url)
try:
result = [str(x) for x in path.glob(pattern)]
except self._non_existing_path_error: # pragma: nocover
except self._non_existing_path_error:
result = []
return result

Expand Down Expand Up @@ -130,17 +127,22 @@ def _versions(
group_id = audfactory.path_to_group_id(folder)
return audfactory.versions(self.host, self.repository, group_id, name)

_non_existing_path_error = (RuntimeError, requests.exceptions.HTTPError)
_non_existing_path_error = (
RuntimeError,
requests.exceptions.HTTPError,
dohq_artifactory.exception.ArtifactoryException,
)
r"""Error expected for non-existing paths.
If a user has no permission to a given path
or the path does not exists :func:`audfactory.path`
might return a
``RuntimeError: 404 page not found``
or the path does not exists
:func:`audfactory.path` might return a
``RuntimeError``,
``requests.exceptions.HTTPError``
for ``dohq_artifactory<0.8``
or
``requests.exceptions.HTTPError: 403 Client Error``
error,
which might depend on the instaleld ``dohq-artifactory``
version. So we better catch both of them.
``artifactory.exception.ArtifactoryException``
for ``dohq_artifactory>=0.8``.
So we better catch all of them.
"""
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ classifiers =
packages = find:
install_requires =
audeer >=1.17.1
audfactory >=1.0.10
audfactory >=1.0.12
setup_requires =
setuptools_scm

Expand Down
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ def cleanup_session():
)
if url.exists():
url.unlink()


@pytest.fixture(scope='function', autouse=False)
def no_artifactory_access_rights():
current_username = os.environ.get('ARTIFACTORY_USERNAME', False)
current_api_key = os.environ.get('ARTIFACTORY_API_KEY', False)
os.environ['ARTIFACTORY_USERNAME'] = 'non-existing-user'
os.environ['ARTIFACTORY_API_KEY'] = 'non-existing-password'
yield
if current_username:
os.environ["ARTIFACTORY_USERNAME"] = current_username
else:
del os.environ['ARTIFACTORY_USERNAME']
if current_api_key:
os.environ['ARTIFACTORY_API_KEY'] = current_api_key
else:
del os.environ['ARTIFACTORY_API_KEY']
22 changes: 22 additions & 0 deletions tests/test_artifactory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest

import audbackend


BACKEND = audbackend.Artifactory(
pytest.ARTIFACTORY_HOST,
pytest.REPOSITORY_NAME,
)


def test_exists(no_artifactory_access_rights):
remote_file = BACKEND.join(
pytest.ID,
'file.txt',
)
version = '1.0.0'
assert not BACKEND.exists(remote_file, version)


def test_glob(no_artifactory_access_rights):
assert BACKEND.glob('file*') == []

0 comments on commit bdd2e8c

Please sign in to comment.