Commit 0adbac0f by Sébastien Eustace

Fix Dependency.to_pep_508() method

parent b6115051
......@@ -107,11 +107,11 @@ class Dependency:
requirement = f'{self.pretty_name}'
if isinstance(self.constraint, MultiConstraint):
requirement += ','.join(
requirement += ' ({})'.format(','.join(
[str(c).replace(' ', '') for c in self.constraint.constraints]
)
))
else:
requirement += str(self.constraint).replace(' ', '')
requirement += ' ({})'.format(str(self.constraint).replace(' ', ''))
# Markers
markers = []
......@@ -119,21 +119,31 @@ class Dependency:
# Python marker
if self.python_versions != '*':
python_constraint = self.python_constraint
marker = 'python_version'
if isinstance(python_constraint, MultiConstraint):
marker += ','.join(
[str(c).replace(' ', '') for c in python_constraint.constraints]
)
else:
marker += str(python_constraint).replace(' ', '')
markers.append(marker)
markers.append(self._create_nested_marker('python_version', python_constraint))
if markers:
requirement += f'; {" and ".join(markers)}'
return requirement
def _create_nested_marker(self, name, constraint):
if isinstance(constraint, MultiConstraint):
parts = []
for c in constraint.constraints:
parts.append(self._create_nested_marker(name, c))
glue = ' and '
if constraint.is_disjunctive():
parts = [f'({part})' for part in parts]
glue = ' or '
marker = glue.join(parts)
else:
marker = f'{name}{constraint.string_operator}"{constraint.version}"'
return marker
def activate(self):
"""
Set the dependency as mandatory.
......
......@@ -42,6 +42,7 @@ class Constraint(BaseConstraint):
)
self._operator = self._trans_op_str[operator]
self._string_operator = operator
self._version = version
@property
......@@ -53,6 +54,10 @@ class Constraint(BaseConstraint):
return self._operator
@property
def string_operator(self):
return self._string_operator
@property
def version(self) -> str:
return self._version
......
......@@ -39,9 +39,9 @@ def test_convert_dependencies():
get_dependency('C', '1.2.3'),
])
main = [
'A>=1.0.0.0,<2.0.0.0',
'B>=1.0.0.0,<1.1.0.0',
'C==1.2.3.0',
'A (>=1.0.0.0,<2.0.0.0)',
'B (>=1.0.0.0,<1.1.0.0)',
'C (==1.2.3.0)',
]
extras = []
......@@ -56,11 +56,11 @@ def test_convert_dependencies():
get_dependency('C', '1.2.3'),
])
main = [
'B>=1.0.0.0,<1.1.0.0',
'C==1.2.3.0',
'B (>=1.0.0.0,<1.1.0.0)',
'C (==1.2.3.0)',
]
extras = [
'A>=1.0.0.0,<2.0.0.0; python_version>=3.4.0.0,<4.0.0.0',
'A (>=1.0.0.0,<2.0.0.0); python_version>="3.4.0.0" and python_version<"4.0.0.0"',
]
assert result == (main, extras)
......@@ -82,7 +82,7 @@ def test_make_setup():
'my_package.sub_pkg2'
]
assert ns['install_requires'] == [
'cleo>=0.6.0.0,<0.7.0.0'
'cleo (>=0.6.0.0,<0.7.0.0)'
]
assert ns['entry_points'] == {
'console_scripts': ['my-script = my_package:main']
......
......@@ -53,3 +53,18 @@ def test_accepts_fails_with_python_versions_mismatch():
package.python_versions = '~3.5'
assert not dependency.accepts(package)
def test_to_pep_508():
dependency = Dependency('Django', '^1.23')
result = dependency.to_pep_508()
assert result == 'Django (>=1.23.0.0,<2.0.0.0)'
dependency = Dependency('Django', '^1.23')
dependency.python_versions = '~2.7 || ^3.6'
result = dependency.to_pep_508()
assert result == 'Django (>=1.23.0.0,<2.0.0.0); ' \
'(python_version>="2.7.0.0" and python_version<"2.8.0.0") ' \
'or (python_version>="3.6.0.0" and python_version<"4.0.0.0")'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment