Commit 65e042ca by Jamie McClymont

Provide hints when an invalid license id is input

parent c9be792e
...@@ -17,7 +17,36 @@ def license_by_id(identifier): ...@@ -17,7 +17,36 @@ def license_by_id(identifier):
id = identifier.lower() id = identifier.lower()
if id not in _licenses: if id not in _licenses:
raise ValueError("Invalid license id: {}".format(identifier)) err = "Invalid license id: {}\nPoetry uses SPDX license identifiers: https://spdx.org/licenses/".format(
identifier
)
# Covers the licenses listed as common for python packages in https://snyk.io/blog/over-10-of-python-packages-on-pypi-are-distributed-without-any-license/
# MIT/WTFPL/Unlicense are excluded as their ids are simply their name - if someone types "mit", they've already found the license they were looking for
common_strings = ["agpl", "lgpl", "gpl", "bsd", "apache", "mpl", "cc0"]
for string in common_strings:
if string in id:
err += "\n"
err += "Did you mean one of the following?"
matches = sorted(
{
license.id
for license in _licenses.values()
if license.id.lower().startswith(string)
and not license.is_deprecated
}
)
for license in matches:
err += "\n * {}".format(license)
# Don't match agpl for "gpl"
break
raise ValueError(err)
return _licenses[id] return _licenses[id]
......
...@@ -41,3 +41,38 @@ def test_license_by_id_with_full_name(): ...@@ -41,3 +41,38 @@ def test_license_by_id_with_full_name():
def test_license_by_id_invalid(): def test_license_by_id_invalid():
with pytest.raises(ValueError): with pytest.raises(ValueError):
license_by_id("invalid") license_by_id("invalid")
def test_license_by_id_invalid_gpl():
with pytest.raises(ValueError) as exc_info:
license_by_id("gpl")
assert "Did you mean" in str(exc_info.value)
assert " GPL-3.0-only" in str(exc_info.value)
assert " AGPL-3.0-only" not in str(exc_info.value)
def test_license_by_id_invalid_agpl():
with pytest.raises(ValueError) as exc_info:
license_by_id("agpl")
assert "Did you mean" in str(exc_info.value)
assert " GPL-3.0-only" not in str(exc_info.value)
assert " AGPL-3.0-only" in str(exc_info.value)
def test_license_by_id_invalid_agpl_versioned():
with pytest.raises(ValueError) as exc_info:
license_by_id("gnu agpl v3+")
assert "Did you mean" in str(exc_info.value)
assert " GPL-3.0-only" not in str(exc_info.value)
assert " AGPL-3.0-only" in str(exc_info.value)
def test_license_by_id_invalid_unpopular():
with pytest.raises(ValueError) as exc_info:
license_by_id("not-a-well-known-license")
assert "spdx.org" in str(exc_info.value)
assert "Did you mean" not in str(exc_info.value)
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