Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions magicli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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✨
Expand Down Expand Up @@ -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)):
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
28 changes: 26 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -92,14 +100,30 @@ 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",
email="patrick@elmer.ws",
description="docstring",
homepage="https://github.com/PatrickElmer/magicli",
)
out, _ = capsys.readouterr()
assert "Unknown license" in out
assert (
Path("pyproject.toml").read_text()
== """\
Expand Down
Loading