diff --git a/openstack_requirements/cmds/edit_constraint.py b/openstack_requirements/cmds/edit_constraint.py index 1fea91aee0..e1bb602449 100644 --- a/openstack_requirements/cmds/edit_constraint.py +++ b/openstack_requirements/cmds/edit_constraint.py @@ -21,10 +21,11 @@ from openstack_requirements import requirement def edit(reqs, name, replacement): + key = requirement.canonical_name(name) if not replacement: - reqs.pop(name, None) + reqs.pop(key, None) else: - reqs[name] = [ + reqs[key] = [ (requirement.Requirement('', '', '', '', replacement), '')] result = [] for entries in reqs.values(): diff --git a/openstack_requirements/requirement.py b/openstack_requirements/requirement.py index bf568804bc..e8b3b688fb 100644 --- a/openstack_requirements/requirement.py +++ b/openstack_requirements/requirement.py @@ -41,6 +41,11 @@ url_re = re.compile( '#egg=(?P[-\.\w]+)') +def canonical_name(req_name): + """Return the canonical form of req_name.""" + return pkg_resources.safe_name(req_name).lower() + + def parse(content, permit_urls=False): return to_dict(to_reqs(content, permit_urls=permit_urls)) @@ -134,7 +139,8 @@ def to_dict(req_sequence): reqs = dict() for req, req_line in req_sequence: if req is not None: - reqs.setdefault(req.package.lower(), []).append((req, req_line)) + key = canonical_name(req.package) + reqs.setdefault(key, []).append((req, req_line)) return reqs diff --git a/openstack_requirements/tests/test_edit_constraint.py b/openstack_requirements/tests/test_edit_constraint.py index 9bad8e6db8..0c86ddb76f 100644 --- a/openstack_requirements/tests/test_edit_constraint.py +++ b/openstack_requirements/tests/test_edit_constraint.py @@ -83,3 +83,10 @@ class TestEdit(testtools.TestCase): res = edit.edit(reqs, 'foo', 'foo==1.3') self.assertEqual(requirement.Requirements( [requirement.Requirement('', '', '', '', 'foo==1.3')]), res) + + def test_replace_non_canonical(self): + new_req = '-e file:///path#egg=foo_baz' + reqs = requirement.parse("foo-baz===1.0.2\n") + res = edit.edit(reqs, 'foo_baz', new_req) + self.assertEqual(res, requirement.Requirements( + [requirement.Requirement('', '', '', '', new_req)])) diff --git a/openstack_requirements/tests/test_requirement.py b/openstack_requirements/tests/test_requirement.py index 23441023b9..defdce65d6 100644 --- a/openstack_requirements/tests/test_requirement.py +++ b/openstack_requirements/tests/test_requirement.py @@ -148,3 +148,17 @@ class TestToReqs(testtools.TestCase): set(['oslo.config', 'oslo.concurrency', 'oslo.context']), set(reqs.keys()), ) + + +class TestCanonicalName(testtools.TestCase): + + def test_underscores(self): + self.assertEqual('foo-bar', requirement.canonical_name('Foo_bar')) + + +class TestToDict(testtools.TestCase): + + def test_canonicalises(self): + req = requirement.Requirement('Foo_bar', '', '', '', '') + self.assertEqual( + {'foo-bar': [(req, '')]}, requirement.to_dict([(req, '')]))