diff --git a/magicli.py b/magicli.py index 20d2e36..4653222 100644 --- a/magicli.py +++ b/magicli.py @@ -287,6 +287,21 @@ def get_description(name): return " ".join(stripped for line in doc.splitlines() if (stripped := line.strip())) +def get_license_expression(content): + """Returns the license expression used in pyproject.toml.""" + return { + "Apache License": "Apache-2.0", + "BSD 2-Clause License": "BSD-2-Clause", + "BSD 3-Clause License": "BSD-3-Clause", + "GNU AFFERO GENERAL PUBLIC LICENSE": "AGPL-3.0-or-later", + "GNU GENERAL PUBLIC LICENSE": "GPL-3.0-or-later", + "GNU LESSER GENERAL PUBLIC LICENSE": "LGPL-3.0-or-later", + "MIT License": "MIT", + "Mozilla Public License Version 2.0": "MPL-2.0", + "Public domain statement": "Unlicense", + }.get(content.split("\n")[0].strip()) + + def cli(name="", author="", email="", description="", homepage=""): """ magiCLI✨ @@ -331,6 +346,11 @@ def cli(name="", author="", email="", description="", homepage=""): project.append('readme = "README.md"') if Path("LICENSE").exists(): + license_content = Path("LICENSE").read_text(encoding="utf-8") + if license_expression := get_license_expression(license_content): + project.append(f'license = "{license_expression}"') + else: + print("Unknown license: Failed to add SPDX identifier to project.license") project.append('license-files = ["LICENSE"]') if description or (description := get_description(name)): diff --git a/tests/fixtures.py b/tests/fixtures.py index b526fe3..3ef34bf 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -30,6 +30,13 @@ def with_tempdir(): _teardown(directory, cwd) +@pytest.fixture +def with_license(): + directory, cwd = _setup(["LICENSE"]) + yield directory.name + _teardown(directory, cwd) + + @pytest.fixture def with_readme_and_license(): directory, cwd = _setup(["README.md", "LICENSE"]) diff --git a/tests/test_cli.py b/tests/test_cli.py index 6e82c3d..89619e4 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -6,12 +6,20 @@ empty_directory, pyproject, with_git, + with_license, with_readme_and_license, with_tempdir, with_two_files, ) -from magicli import cli, get_description, get_homepage, get_output, get_project_name +from magicli import ( + cli, + get_description, + get_homepage, + get_license_expression, + get_output, + get_project_name, +) def module(name): @@ -92,7 +100,21 @@ def test_get_description(): assert get_description("magicli") is not None -def test_cli_with_kwargs(with_readme_and_license): +def test_get_license_expression(): + assert get_license_expression("Apache License") == "Apache-2.0" + assert get_license_expression(" GNU GENERAL PUBLIC LICENSE ") == "GPL-3.0-or-later" + assert get_license_expression("") == None + + +def test_cli_with_license(with_license): + Path(with_license, "LICENSE").write_text("MIT License") + cli(name="name", author="Patrick Elmer", email="patrick@elmer.ws") + pyproject = Path("pyproject.toml").read_text() + assert 'license = "MIT"' in pyproject + assert 'license-files = ["LICENSE"]' in pyproject + + +def test_cli_with_kwargs(capsys, with_readme_and_license): cli( name="name", author="Patrick Elmer", @@ -100,6 +122,8 @@ def test_cli_with_kwargs(with_readme_and_license): description="docstring", homepage="https://github.com/PatrickElmer/magicli", ) + out, _ = capsys.readouterr() + assert "Unknown license" in out assert ( Path("pyproject.toml").read_text() == """\