Skip to content

Commit

Permalink
Merge pull request #34 from bcongdon/feature/invalid_version_exception
Browse files Browse the repository at this point in the history
Validate version specifier for package installs
  • Loading branch information
toumorokoshi committed Jul 25, 2017
2 parents 19c03a6 + 6888ceb commit 729f807
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
13 changes: 13 additions & 0 deletions tests/packages/test_install_command.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest
from uranium.exceptions import PackageException
from uranium.packages.install_command import _create_args


Expand Down Expand Up @@ -26,3 +28,14 @@ def test_create_args_includes_install_options():
"--install-option", "--install-lib=/opt/srv/lib",
"pytest"
]


def test_create_args_raises_on_invalid_version():
with pytest.raises(PackageException) as exc:
_create_args("pytest", version="3.1.3")
assert "Invalid version specifier" in str(exc)


def test_create_args_accepts_valid_version():
args = _create_args("pytest", version=">=3.1.3")
assert args == ["pytest>=3.1.3"]
3 changes: 3 additions & 0 deletions uranium/packages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def install(self, name, version=None, develop=False, upgrade=False, install_opti
install is used when installing a python package into the environment.
if version is set, the specified version of the package will be installed.
The specified version should be a full `PEP 440`_ version specifier (i.e. "==1.2.0")
.. _`PEP 440`: https://www.python.org/dev/peps/pep-0440/
if develop is set to True, the package will be installed as editable: the source
in the directory passed will be used when using that package.
Expand Down
10 changes: 9 additions & 1 deletion uranium/packages/install_command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pkg_resources
from pip.commands import InstallCommand, UninstallCommand
from pip.req.req_file import process_line
from ..exceptions import PackageException
from ..lib.compat import urlparse


Expand Down Expand Up @@ -100,6 +102,12 @@ def _create_args(package_name, upgrade=False, develop=False,

requirement = package_name
if version:
requirement += version
versioned_requirement = requirement + version
if not pkg_resources.Requirement.parse(versioned_requirement).specs:
raise PackageException(
"Cannot parse requirement for package %s. " % requirement +
"Invalid version specifier: %s." % version
)
requirement = versioned_requirement
args.append(requirement)
return args

0 comments on commit 729f807

Please sign in to comment.